2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2001 Billy Biggs <vektor@dumbterm.net>.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
33 #include "dvdreadsrc.h"
35 /* #include <gst/gst-i18n-plugin.h> */
36 /* FIXME: remove once GETTEXT_PACKAGE etc. is set */
39 GST_DEBUG_CATEGORY_STATIC (gstgst_dvd_read_src_debug);
40 #define GST_CAT_DEFAULT (gstgst_dvd_read_src_debug)
42 static void gst_dvd_read_src_do_init (GType dvdreadsrc_type);
53 static GstElementDetails gst_dvd_read_src_details = {
56 "Access a DVD title/chapter/angle using libdvdread",
57 "Erik Walthinsen <omega@cse.ogi.edu>",
60 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
63 GST_STATIC_CAPS ("video/mpeg, mpegversion=2, systemstream=(boolean)true"));
65 static GstFormat title_format;
66 static GstFormat angle_format;
67 static GstFormat sector_format;
68 static GstFormat chapter_format;
70 static gboolean gst_dvd_read_src_start (GstBaseSrc * basesrc);
71 static gboolean gst_dvd_read_src_stop (GstBaseSrc * basesrc);
72 static GstFlowReturn gst_dvd_read_src_create (GstPushSrc * pushsrc,
74 static gboolean gst_dvd_read_src_src_query (GstBaseSrc * basesrc,
76 static gboolean gst_dvd_read_src_src_event (GstBaseSrc * basesrc,
78 static gboolean gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title,
80 static gboolean gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src,
82 static gboolean gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, gint angle);
83 static void gst_dvd_read_src_set_property (GObject * object, guint prop_id,
84 const GValue * value, GParamSpec * pspec);
85 static void gst_dvd_read_src_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
87 static GstEvent *gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
89 static gboolean gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size);
91 GST_BOILERPLATE_FULL (GstDvdReadSrc, gst_dvd_read_src, GstPushSrc,
92 GST_TYPE_PUSH_SRC, gst_dvd_read_src_do_init)
94 static void gst_dvd_read_src_base_init (gpointer g_class)
96 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
98 gst_element_class_add_pad_template (element_class,
99 gst_static_pad_template_get (&srctemplate));
101 gst_element_class_set_details (element_class, &gst_dvd_read_src_details);
105 gst_dvd_read_src_finalize (GObject * object)
107 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
109 g_free (src->location);
110 g_free (src->last_uri);
112 G_OBJECT_CLASS (parent_class)->finalize (object);
116 gst_dvd_read_src_init (GstDvdReadSrc * src, GstDvdReadSrcClass * klass)
119 src->vts_file = NULL;
120 src->vmg_file = NULL;
121 src->dvd_title = NULL;
123 src->location = g_strdup ("/dev/dvd");
124 src->last_uri = NULL;
125 src->new_seek = TRUE;
126 src->new_cell = TRUE;
127 src->change_cell = FALSE;
129 src->uri_chapter = 1;
132 src->seek_pend = FALSE;
133 src->flush_pend = FALSE;
134 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
135 src->title_lang_event_pending = NULL;
136 src->pending_clut_event = NULL;
138 gst_pad_use_fixed_caps (GST_BASE_SRC_PAD (src));
139 gst_pad_set_caps (GST_BASE_SRC_PAD (src),
140 gst_static_pad_template_get_caps (&srctemplate));
144 gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
146 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
147 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
148 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
150 gobject_class->finalize = gst_dvd_read_src_finalize;
151 gobject_class->set_property = gst_dvd_read_src_set_property;
152 gobject_class->get_property = gst_dvd_read_src_get_property;
154 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
155 g_param_spec_string ("device", "Device",
156 "DVD device location", NULL, G_PARAM_READWRITE));
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));
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));
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));
167 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
168 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
169 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_query);
170 gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_event);
172 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dvd_read_src_create);
176 gst_dvd_read_src_start (GstBaseSrc * basesrc)
178 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
180 g_return_val_if_fail (src->location != NULL, FALSE);
182 GST_DEBUG_OBJECT (src, "Opening DVD '%s'", src->location);
184 if ((src->dvd = DVDOpen (src->location)) == NULL)
187 /* Load the video manager to find out the information about the titles */
188 GST_DEBUG_OBJECT (src, "Loading VMG info");
190 if (!(src->vmg_file = ifoOpen (src->dvd, 0)))
191 goto ifo_open_failed;
193 src->tt_srpt = src->vmg_file->tt_srpt;
195 src->title = src->uri_title - 1;
196 src->chapter = src->uri_chapter - 1;
197 src->angle = src->uri_angle - 1;
199 if (!gst_dvd_read_src_goto_title (src, src->title, src->angle))
200 goto title_open_failed;
202 if (!gst_dvd_read_src_goto_chapter (src, src->chapter))
203 goto chapter_open_failed;
205 src->new_seek = FALSE;
206 src->change_cell = TRUE;
213 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
214 (_("Could not open DVD")),
215 ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
220 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
221 (_("Could not open DVD")),
222 ("ifoOpen() failed: %s", g_strerror (errno)));
227 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
228 (_("Could not open DVD title %d"), src->uri_title), (NULL));
233 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
234 (_("Failed to go to chapter %d of DVD title %d"),
235 src->uri_chapter, src->uri_title), (NULL));
241 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
243 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
246 ifoClose (src->vts_file);
247 src->vts_file = NULL;
250 ifoClose (src->vmg_file);
251 src->vmg_file = NULL;
253 if (src->dvd_title) {
254 DVDCloseFile (src->dvd_title);
255 src->dvd_title = NULL;
261 src->new_cell = TRUE;
262 src->new_seek = TRUE;
263 src->change_cell = FALSE;
266 src->flush_pend = FALSE;
267 src->seek_pend = FALSE;
268 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
269 if (src->title_lang_event_pending) {
270 gst_event_unref (src->title_lang_event_pending);
271 src->title_lang_event_pending = NULL;
273 if (src->pending_clut_event) {
274 gst_event_unref (src->pending_clut_event);
275 src->pending_clut_event = NULL;
278 GST_LOG_OBJECT (src, "closed DVD");
284 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
285 gint * p_pgc_id, pgc_t ** p_pgc)
290 g_assert (chapter >= 0 && chapter < src->num_chapters);
292 pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
293 pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
294 pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
302 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
303 gint * p_first_cell, gint * p_last_cell)
306 gint pgn, pgc_id, pgn_next_ch;
308 g_assert (chapter >= 0 && chapter < src->num_chapters);
310 cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
312 *p_first_cell = pgc->program_map[pgn - 1] - 1;
314 if (chapter == (src->num_chapters - 1)) {
315 *p_last_cell = pgc->nr_of_cells;
317 pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
318 *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
323 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
327 /* make sure the chapter number is valid for this title */
328 if (chapter < 0 || chapter >= src->num_chapters) {
329 GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
330 chapter, src->num_chapters);
331 chapter = CLAMP (chapter, 0, src->num_chapters - 1);
334 /* determine which program chain we want to watch. This is
335 * based on the chapter number */
336 cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
338 cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
341 GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter,
342 src->start_cell, src->last_cell);
344 /* retrieve position */
346 for (i = 0; i < chapter; i++) {
349 cur_title_get_chapter_bounds (src, i, &c1, &c2);
353 src->cur_pgc->cell_playback[c1].last_sector -
354 src->cur_pgc->cell_playback[c1].first_sector;
359 /* prepare reading for new cell */
360 src->new_cell = TRUE;
361 src->next_cell = src->start_cell;
363 src->chapter = chapter;
365 if (src->pending_clut_event)
366 gst_event_unref (src->pending_clut_event);
368 src->pending_clut_event =
369 gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
375 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
378 gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
384 /* make sure our title number is valid */
385 num_titles = src->tt_srpt->nr_of_srpts;
386 GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
387 if (title < 0 || title >= num_titles)
390 src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
391 GST_INFO_OBJECT (src, "Title %d has %d chapters", title, src->num_chapters);
393 /* make sure the angle number is valid for this title */
394 num_angles = src->tt_srpt->title[title].nr_of_angles;
395 GST_LOG_OBJECT (src, "Title %d has %d angles", title, num_angles);
396 if (angle < 0 || angle >= num_angles) {
397 GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
399 angle = CLAMP (angle, 0, num_angles - 1);
402 /* load the VTS information for the title set our title is in */
403 title_set_nr = src->tt_srpt->title[title].title_set_nr;
404 src->vts_file = ifoOpen (src->dvd, title_set_nr);
405 if (src->vts_file == NULL)
406 goto ifo_open_failed;
408 src->ttn = src->tt_srpt->title[title].vts_ttn;
409 src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
411 /* we've got enough info, time to open the title set data */
412 src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
413 if (src->dvd_title == NULL)
414 goto title_open_failed;
416 GST_INFO_OBJECT (src, "Opened title %d, angle %d", title, angle);
422 if (src->title_lang_event_pending) {
423 gst_event_unref (src->title_lang_event_pending);
424 src->title_lang_event_pending = NULL;
427 s = gst_structure_new ("application/x-gst-dvd",
428 "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
431 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
432 const audio_attr_t *a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
434 t = g_strdup_printf ("audio-%d-format", i);
435 gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
439 t = g_strdup_printf ("audio-%d-language", i);
440 lang_code[0] = (a->lang_code >> 8) & 0xff;
441 lang_code[1] = a->lang_code & 0xff;
442 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
448 GST_INFO_OBJECT (src, "[%02d] Audio %02d: lang='%s', format=%d",
449 src->title, i, lang_code, (gint) a->audio_format);
453 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
454 const subp_attr_t *u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
457 t = g_strdup_printf ("subtitle-%d-language", i);
458 lang_code[0] = (u->lang_code >> 8) & 0xff;
459 lang_code[1] = u->lang_code & 0xff;
460 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
466 GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', format=%d",
467 src->title, i, lang_code);
470 src->title_lang_event_pending =
471 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
478 GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
484 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
485 (_("Could not open DVD title %d"), title_set_nr),
486 ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
491 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
492 (_("Could not open DVD title %d"), title_set_nr),
493 ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
498 /* FIXME: double-check this function, compare against original */
500 gst_dvd_read_src_get_next_cell_for (GstDvdReadSrc * src, gint cell)
502 /* Check if we're entering an angle block. */
503 if (src->cur_pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
506 while (src->cur_pgc->cell_playback[cell].block_mode == BLOCK_MODE_LAST_CELL)
509 return cell + 1; /* really +1? (tpm) */
512 /* Returns true if the pack is a NAV pack. This check is clearly insufficient,
513 * and sometimes we incorrectly think that valid other packs are NAV packs. I
514 * need to make this stronger. */
516 gst_dvd_read_src_is_nav_pack (const guint8 * buffer)
518 return (buffer[41] == 0xbf && buffer[1027] == 0xbf);
524 GST_DVD_READ_ERROR = -1,
525 GST_DVD_READ_EOS = -2,
526 GST_DVD_READ_AGAIN = -3
529 static GstDvdReadReturn
530 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
534 guint8 oneblock[DVD_VIDEO_LB_LEN];
536 guint next_vobu, next_ilvu_start, cur_output_size;
539 /* playback by cell in this pgc, starting at the cell for our chapter */
541 src->cur_cell = src->start_cell;
545 if (src->cur_cell >= src->last_cell) {
546 /* advance to next chapter */
547 if (src->chapter == (src->num_chapters - 1))
550 GST_INFO_OBJECT (src, "end of chapter %d, switch to next", src->chapter);
553 gst_dvd_read_src_goto_chapter (src, src->chapter);
555 return GST_DVD_READ_AGAIN;
558 if (src->new_cell || new_seek) {
560 src->cur_cell = src->next_cell;
561 if (src->cur_cell >= src->last_cell) {
562 GST_LOG_OBJECT (src, "last cell in chapter");
567 /* take angle into account */
568 if (src->cur_pgc->cell_playback[src->cur_cell].block_type
569 == BLOCK_TYPE_ANGLE_BLOCK)
570 src->cur_cell += angle;
572 /* calculate next cell */
573 src->next_cell = gst_dvd_read_src_get_next_cell_for (src, src->cur_cell);
575 /* we loop until we're out of this cell */
576 src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
577 src->new_cell = FALSE;
580 if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
581 src->new_cell = TRUE;
582 GST_LOG_OBJECT (src, "Beyond last sector, go to next cell");
583 return GST_DVD_READ_AGAIN;
586 /* read NAV packet */
589 len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, oneblock);
593 if (!gst_dvd_read_src_is_nav_pack (oneblock)) {
598 /* parse the contained dsi packet */
599 navRead_DSI (&dsi_pack, &oneblock[DSI_START_BYTE]);
600 g_assert (src->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn);
602 /* determine where we go next. These values are the ones we
603 * mostly care about */
604 next_ilvu_start = src->cur_pack + dsi_pack.sml_agli.data[angle].address;
605 cur_output_size = dsi_pack.dsi_gi.vobu_ea;
607 /* If we're not at the end of this cell, we can determine the next
608 * VOBU to display using the VOBU_SRI information section of the
609 * DSI. Using this value correctly follows the current angle,
610 * avoiding the doubled scenes in The Matrix, and makes our life
613 * Otherwise, we set our next address past the end of this cell to
614 * force the code above to go to the next cell in the program. */
615 if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
616 next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
618 next_vobu = src->cur_pack + cur_output_size + 1;
621 g_assert (cur_output_size < 1024);
624 /* create the buffer (TODO: use buffer pool?) */
625 buf = gst_buffer_new_and_alloc (cur_output_size * DVD_VIDEO_LB_LEN);
627 /* read in and output cursize packs */
628 len = DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size,
629 GST_BUFFER_DATA (buf));
631 if (len != cur_output_size)
632 goto block_read_error;
634 GST_BUFFER_SIZE (buf) = cur_output_size * DVD_VIDEO_LB_LEN;
635 /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
637 gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (src)));
641 src->cur_pack = next_vobu;
643 GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
645 return GST_DVD_READ_OK;
650 GST_INFO_OBJECT (src, "last chapter done - EOS");
651 return GST_DVD_READ_EOS;
655 GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
656 return GST_DVD_READ_ERROR;
660 GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
661 cur_output_size, src->cur_pack);
662 gst_buffer_unref (buf);
663 return GST_DVD_READ_ERROR;
668 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
670 GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
674 g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
676 srcpad = GST_BASE_SRC (src)->srcpad;
678 /* handle events, if any */
679 if (src->seek_pend) {
680 if (src->flush_pend) {
681 gst_pad_push_event (srcpad, gst_event_new_flush_start ());
682 gst_pad_push_event (srcpad, gst_event_new_flush_stop ());
683 src->flush_pend = FALSE;
686 if (src->seek_pend_fmt != GST_FORMAT_UNDEFINED) {
687 if (src->seek_pend_fmt == title_format) {
688 gst_dvd_read_src_goto_title (src, src->title, src->angle);
690 gst_dvd_read_src_goto_chapter (src, src->chapter);
692 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
694 if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
695 GST_DEBUG_OBJECT (src, "seek to sector failed, going EOS");
696 gst_pad_push_event (srcpad, gst_event_new_eos ());
697 return GST_FLOW_UNEXPECTED;
701 gst_pad_push_event (srcpad,
702 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
703 src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
705 src->seek_pend = FALSE;
709 gst_dvd_read_src_goto_title (src, src->title, src->angle);
710 gst_dvd_read_src_goto_chapter (src, src->chapter);
712 src->new_seek = FALSE;
713 src->change_cell = TRUE;
716 if (src->title_lang_event_pending) {
717 gst_pad_push_event (srcpad, src->title_lang_event_pending);
718 src->title_lang_event_pending = NULL;
721 if (src->pending_clut_event) {
722 gst_pad_push_event (srcpad, src->pending_clut_event);
723 src->pending_clut_event = NULL;
728 res = gst_dvd_read_src_read (src, src->angle, src->change_cell, p_buf);
729 } while (res == GST_DVD_READ_AGAIN);
732 case GST_DVD_READ_ERROR:{
733 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), (NULL));
734 return GST_FLOW_ERROR;
736 case GST_DVD_READ_EOS:{
737 GST_INFO_OBJECT (src, "Reached EOS");
738 return GST_FLOW_UNEXPECTED;
740 case GST_DVD_READ_OK:{
741 src->change_cell = FALSE;
748 g_return_val_if_reached (GST_FLOW_UNEXPECTED);
752 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
753 const GValue * value, GParamSpec * pspec)
755 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
758 GST_OBJECT_LOCK (src);
759 started = GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED);
764 g_warning ("%s: property '%s' needs to be set before the device is "
765 "opened", GST_ELEMENT_NAME (src), pspec->name);
769 g_free (src->location);
770 /* clear the filename if we get a NULL (is that possible?) */
771 if (g_value_get_string (value) == NULL) {
772 src->location = g_strdup ("/dev/dvd");
774 src->location = g_strdup (g_value_get_string (value));
779 src->uri_title = g_value_get_int (value);
781 src->title = src->uri_title - 1;
782 src->new_seek = TRUE;
786 src->uri_chapter = g_value_get_int (value);
788 src->chapter = src->uri_chapter - 1;
789 src->new_seek = TRUE;
793 src->uri_angle = g_value_get_int (value);
795 src->angle = src->uri_angle - 1;
799 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
803 GST_OBJECT_UNLOCK (src);
807 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
810 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
812 GST_OBJECT_LOCK (src);
816 g_value_set_string (value, src->location);
819 g_value_set_int (value, src->uri_title);
822 g_value_set_int (value, src->uri_chapter);
825 g_value_set_int (value, src->uri_angle);
828 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
832 GST_OBJECT_UNLOCK (src);
836 gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size)
838 gboolean ret = FALSE;
840 if (src->dvd_title) {
843 blocks = DVDFileSize (src->dvd_title);
845 *size = (gint64) blocks *DVD_VIDEO_LB_LEN;
849 GST_WARNING_OBJECT (src, "DVDFileSize(%p) failed!", src->dvd_title);
856 /*** Querying and seeking ***/
859 gst_dvd_read_src_do_seek (GstDvdReadSrc * src, GstEvent * event)
862 GstSeekType cur_type, end_type;
863 gint64 new_off, total, cur;
869 srcpad = GST_BASE_SRC (src)->srcpad;
871 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
874 if (end_type != GST_SEEK_TYPE_NONE) {
875 GST_WARNING_OBJECT (src, "End seek type not supported, will be ignored");
879 case GST_FORMAT_BYTES:
882 if (format != chapter_format &&
883 format != sector_format &&
884 format != angle_format && format != title_format) {
885 GST_DEBUG_OBJECT (src, "Unsupported seek format %d (%s)", format,
886 gst_format_get_name (format));
893 /* get current offset and length */
894 if (format == GST_FORMAT_BYTES) {
895 GST_OBJECT_LOCK (src);
896 query_ok = gst_dvd_read_src_get_size (src, &total);
897 cur = (gint64) src->cur_pack * DVD_VIDEO_LB_LEN;
898 GST_OBJECT_UNLOCK (src);
900 query_ok = gst_pad_query_duration (srcpad, &format, &total)
901 && gst_pad_query_position (srcpad, &format, &cur);
905 GST_DEBUG_OBJECT (src, "Failed to query duration/position");
909 GST_DEBUG_OBJECT (src, "Current %s: %12" G_GINT64_FORMAT,
910 gst_format_get_name (format), cur);
911 GST_DEBUG_OBJECT (src, "Total %s: %12" G_GINT64_FORMAT,
912 gst_format_get_name (format), total);
916 case GST_SEEK_TYPE_SET:
919 case GST_SEEK_TYPE_CUR:
922 case GST_SEEK_TYPE_END:
923 new_off = total - new_off;
926 GST_DEBUG_OBJECT (src, "Unsupported seek method");
930 if (new_off < 0 || new_off >= total) {
931 GST_DEBUG_OBJECT (src, "Invalid seek position %" G_GINT64_FORMAT, new_off);
935 if (cur == new_off) {
936 GST_DEBUG_OBJECT (src, "We're already at that position!");
940 GST_LOG_OBJECT (src, "Seeking to %s: %12" G_GINT64_FORMAT,
941 gst_format_get_name (format), new_off);
943 GST_OBJECT_LOCK (src);
945 if (format == angle_format) {
946 src->angle = new_off;
950 if (format == sector_format) {
951 src->cur_pack = new_off;
952 } else if (format == GST_FORMAT_BYTES) {
953 src->cur_pack = new_off / DVD_VIDEO_LB_LEN;
954 if ((src->cur_pack * DVD_VIDEO_LB_LEN) != new_off) {
955 GST_LOG_OBJECT (src, "rounded down offset %" G_GINT64_FORMAT " => %"
956 G_GINT64_FORMAT, new_off, (gint64) src->cur_pack * DVD_VIDEO_LB_LEN);
958 } else if (format == chapter_format) {
960 src->chapter = new_off;
961 src->seek_pend_fmt = format;
962 } else if (format == title_format) {
964 src->title = new_off;
966 src->seek_pend_fmt = format;
968 GST_OBJECT_UNLOCK (src);
969 g_return_val_if_reached (FALSE);
972 /* leave for events */
973 src->seek_pend = TRUE;
974 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
975 src->flush_pend = TRUE;
979 GST_OBJECT_UNLOCK (src);
985 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
987 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
990 GST_LOG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
992 switch (GST_EVENT_TYPE (event)) {
994 res = gst_dvd_read_src_do_seek (src, event);
997 res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
1005 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
1008 GstStructure *structure;
1012 structure = gst_structure_new ("application/x-gst-dvd",
1013 "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
1015 /* Create a separate field for each value in the table. */
1016 for (i = 0; i < 16; i++) {
1017 g_snprintf (name, sizeof (name), "clut%02d", i);
1018 gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
1021 /* Create the DVD event and put the structure into it. */
1022 return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
1026 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
1029 const gint64 one_hour = 3600 * GST_SECOND;
1030 const gint64 one_min = 60 * GST_SECOND;
1032 g_return_val_if_fail ((time->hour >> 4) < 0xa
1033 && (time->hour & 0xf) < 0xa, -1);
1034 g_return_val_if_fail ((time->minute >> 4) < 0x7
1035 && (time->minute & 0xf) < 0xa, -1);
1036 g_return_val_if_fail ((time->second >> 4) < 0x7
1037 && (time->second & 0xf) < 0xa, -1);
1039 ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
1040 ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
1041 ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
1047 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
1052 gst_query_parse_duration (query, &format, NULL);
1055 case GST_FORMAT_TIME:{
1056 if (src->cur_pgc == NULL)
1058 val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
1063 case GST_FORMAT_BYTES:{
1064 if (!gst_dvd_read_src_get_size (src, &val))
1069 if (format == sector_format) {
1070 val = DVDFileSize (src->dvd_title);
1071 } else if (format == title_format) {
1072 val = src->tt_srpt->nr_of_srpts;
1073 } else if (format == chapter_format) {
1074 val = src->num_chapters;
1075 } else if (format == angle_format) {
1076 val = src->tt_srpt->title[src->title].nr_of_angles;
1078 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1079 format, gst_format_get_name (format));
1086 GST_LOG_OBJECT (src, "duration = %" G_GINT64_FORMAT " %s", val,
1087 gst_format_get_name (format));
1089 gst_query_set_duration (query, format, val);
1094 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
1099 gst_query_parse_position (query, &format, NULL);
1102 case GST_FORMAT_BYTES:{
1103 val = src->cur_pack * DVD_VIDEO_LB_LEN;
1107 if (format == sector_format) {
1108 val = src->cur_pack;
1109 } else if (format == title_format) {
1111 } else if (format == chapter_format) {
1113 } else if (format == angle_format) {
1116 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1117 format, gst_format_get_name (format));
1124 GST_LOG_OBJECT (src, "position = %" G_GINT64_FORMAT " %s", val,
1125 gst_format_get_name (format));
1127 gst_query_set_position (query, format, val);
1132 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1134 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1136 gboolean res = TRUE;
1138 GST_LOG_OBJECT (src, "handling %s query",
1139 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1141 GST_OBJECT_LOCK (src);
1142 started = (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED));
1143 GST_OBJECT_UNLOCK (src);
1146 GST_DEBUG_OBJECT (src, "query failed: not started");
1150 switch (GST_QUERY_TYPE (query)) {
1151 case GST_QUERY_DURATION:
1152 GST_OBJECT_LOCK (src);
1153 res = gst_dvd_read_src_do_duration_query (src, query);
1154 GST_OBJECT_UNLOCK (src);
1156 case GST_QUERY_POSITION:
1157 GST_OBJECT_LOCK (src);
1158 res = gst_dvd_read_src_do_position_query (src, query);
1159 GST_OBJECT_UNLOCK (src);
1162 res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1170 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1172 gint seek_to = src->cur_pack;
1173 gint chapter, sectors, next, cur, i;
1175 /* retrieve position */
1177 for (i = 0; i < src->num_chapters; i++) {
1180 cur_title_get_chapter_bounds (src, i, &c1, &c2);
1182 for (next = cur = c1; cur < c2;) {
1185 src->cur_pgc->cell_playback[cur].last_sector -
1186 src->cur_pgc->cell_playback[cur].first_sector;
1187 if (src->cur_pack + sectors > seek_to) {
1191 src->cur_pack += sectors;
1194 if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1196 next = gst_dvd_read_src_get_next_cell_for (src, cur);
1200 GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1206 /* so chapter $chapter and cell $cur contain our sector
1207 * of interest. Let's go there! */
1208 GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1211 gst_dvd_read_src_goto_chapter (src, chapter);
1212 src->cur_cell = cur;
1213 src->next_cell = next;
1214 src->new_cell = FALSE;
1215 src->cur_pack = seek_to;
1222 /*** URI interface ***/
1225 gst_dvd_read_src_uri_get_type (void)
1231 gst_dvd_read_src_uri_get_protocols (void)
1233 static gchar *protocols[] = { "dvd", NULL };
1238 static const gchar *
1239 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1241 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1243 GST_OBJECT_LOCK (src);
1245 g_free (src->last_uri);
1246 src->last_uri = g_strdup_printf ("dvd://%d,%d,%d", src->uri_title,
1247 src->uri_chapter, src->uri_angle);
1249 GST_OBJECT_UNLOCK (src);
1251 return src->last_uri;
1255 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1257 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1261 protocol = gst_uri_get_protocol (uri);
1262 ret = (protocol != NULL && g_str_equal (protocol, "dvd"));
1269 /* parse out the new t/c/a and seek to them */
1271 gchar *location = NULL;
1276 location = gst_uri_get_location (uri);
1281 GST_OBJECT_LOCK (src);
1284 src->uri_chapter = 1;
1287 strcur = strs = g_strsplit (location, ",", 0);
1288 while (strcur && *strcur) {
1291 if (!sscanf (*strcur, "%d", &val))
1295 g_warning ("Invalid value %d in URI '%s'. Must be 1 or greater",
1302 src->uri_title = val;
1305 src->uri_chapter = val;
1308 src->uri_angle = val;
1316 if (pos > 0 && GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1317 src->title = src->uri_title - 1;
1318 src->chapter = src->uri_chapter - 1;
1319 src->angle = src->uri_angle - 1;
1320 src->new_seek = TRUE;
1323 GST_OBJECT_UNLOCK (src);
1333 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1335 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1337 iface->get_type = gst_dvd_read_src_uri_get_type;
1338 iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1339 iface->get_uri = gst_dvd_read_src_uri_get_uri;
1340 iface->set_uri = gst_dvd_read_src_uri_set_uri;
1344 gst_dvd_read_src_do_init (GType dvdreadsrc_type)
1346 static const GInterfaceInfo urihandler_info = {
1347 gst_dvd_read_src_uri_handler_init,
1352 g_type_add_interface_static (dvdreadsrc_type, GST_TYPE_URI_HANDLER,
1355 title_format = gst_format_register ("title", "DVD title");
1356 angle_format = gst_format_register ("angle", "DVD angle");
1357 sector_format = gst_format_register ("sector", "DVD sector");
1358 chapter_format = gst_format_register ("chapter", "DVD chapter");
1362 plugin_init (GstPlugin * plugin)
1364 GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1365 "DVD reader element based on dvdreadsrc");
1367 if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,
1368 GST_TYPE_DVD_READ_SRC)) {
1375 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1378 "Access a DVD with dvdread",
1379 plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);