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 src->dvd = DVDOpen (src->location);
185 if (src->dvd == NULL) {
186 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
187 (_("Could not open DVD")),
188 ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
192 /* Load the video manager to find out the information about the titles */
193 GST_DEBUG_OBJECT (src, "Loading VMG info");
195 src->vmg_file = ifoOpen (src->dvd, 0);
196 if (!src->vmg_file) {
197 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
198 (_("Could not open DVD")),
199 ("ifoOpen() failed: %s", g_strerror (errno)));
203 src->tt_srpt = src->vmg_file->tt_srpt;
205 src->title = src->uri_title - 1;
206 src->chapter = src->uri_chapter - 1;
207 src->angle = src->uri_angle - 1;
209 if (!gst_dvd_read_src_goto_title (src, src->title, src->angle)) {
210 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
211 (_("Could not open DVD title %d"), src->uri_title), (NULL));
215 if (!gst_dvd_read_src_goto_chapter (src, src->chapter)) {
216 GST_ERROR_OBJECT (src, "Failed to go to chapter %d of DVD title %d",
217 src->uri_chapter, src->uri_title);
220 src->new_seek = FALSE;
221 src->change_cell = TRUE;
227 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
229 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
232 ifoClose (src->vts_file);
233 src->vts_file = NULL;
236 ifoClose (src->vmg_file);
237 src->vmg_file = NULL;
239 if (src->dvd_title) {
240 DVDCloseFile (src->dvd_title);
241 src->dvd_title = NULL;
247 src->new_cell = TRUE;
248 src->new_seek = TRUE;
249 src->change_cell = FALSE;
252 src->flush_pend = FALSE;
253 src->seek_pend = FALSE;
254 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
255 if (src->title_lang_event_pending) {
256 gst_event_unref (src->title_lang_event_pending);
257 src->title_lang_event_pending = NULL;
259 if (src->pending_clut_event) {
260 gst_event_unref (src->pending_clut_event);
261 src->pending_clut_event = NULL;
264 GST_LOG_OBJECT (src, "closed DVD");
270 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
271 gint * p_pgc_id, pgc_t ** p_pgc)
276 g_assert (chapter >= 0 && chapter < src->num_chapters);
278 pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
279 pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
280 pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
288 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
289 gint * p_first_cell, gint * p_last_cell)
292 gint pgn, pgc_id, pgn_next_ch;
294 g_assert (chapter >= 0 && chapter < src->num_chapters);
296 cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
298 *p_first_cell = pgc->program_map[pgn - 1] - 1;
300 if (chapter == (src->num_chapters - 1)) {
301 *p_last_cell = pgc->nr_of_cells;
303 pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
304 *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
309 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
313 /* make sure the chapter number is valid for this title */
314 if (chapter < 0 || chapter >= src->num_chapters) {
315 GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
316 chapter, src->num_chapters);
317 chapter = CLAMP (chapter, 0, src->num_chapters - 1);
320 /* determine which program chain we want to watch. This is
321 * based on the chapter number */
322 cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
324 cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
327 GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter,
328 src->start_cell, src->last_cell);
330 /* retrieve position */
332 for (i = 0; i < chapter; i++) {
335 cur_title_get_chapter_bounds (src, i, &c1, &c2);
339 src->cur_pgc->cell_playback[c1].last_sector -
340 src->cur_pgc->cell_playback[c1].first_sector;
345 /* prepare reading for new cell */
346 src->new_cell = TRUE;
347 src->next_cell = src->start_cell;
349 src->chapter = chapter;
351 if (src->pending_clut_event)
352 gst_event_unref (src->pending_clut_event);
354 src->pending_clut_event =
355 gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
361 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
364 gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
370 /* make sure our title number is valid */
371 num_titles = src->tt_srpt->nr_of_srpts;
372 GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
373 if (title < 0 || title >= num_titles) {
374 GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
379 src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
380 GST_INFO_OBJECT (src, "Title %d has %d chapters", title, src->num_chapters);
382 /* make sure the angle number is valid for this title */
383 num_angles = src->tt_srpt->title[title].nr_of_angles;
384 GST_LOG_OBJECT (src, "Title %d has %d angles", title, num_angles);
385 if (angle < 0 || angle >= num_angles) {
386 GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
388 angle = CLAMP (angle, 0, num_angles - 1);
391 /* load the VTS information for the title set our title is in */
392 title_set_nr = src->tt_srpt->title[title].title_set_nr;
393 src->vts_file = ifoOpen (src->dvd, title_set_nr);
394 if (src->vts_file == NULL) {
395 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
396 (_("Could not open DVD title %d"), title_set_nr),
397 ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
401 src->ttn = src->tt_srpt->title[title].vts_ttn;
402 src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
404 /* we've got enough info, time to open the title set data */
405 src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
406 if (src->dvd_title == NULL) {
407 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
408 (_("Could not open DVD title %d"), title_set_nr),
409 ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
413 GST_INFO_OBJECT (src, "Opened title %d, angle %d", title, angle);
419 if (src->title_lang_event_pending) {
420 gst_event_unref (src->title_lang_event_pending);
421 src->title_lang_event_pending = NULL;
424 s = gst_structure_new ("application/x-gst-dvd",
425 "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
428 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
429 const audio_attr_t *a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
431 t = g_strdup_printf ("audio-%d-format", i);
432 gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
436 t = g_strdup_printf ("audio-%d-language", i);
437 lang_code[0] = (a->lang_code >> 8) & 0xff;
438 lang_code[1] = a->lang_code & 0xff;
439 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
445 GST_INFO_OBJECT (src, "[%02d] Audio %02d: lang='%s', format=%d",
446 src->title, i, lang_code, (gint) a->audio_format);
450 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
451 const subp_attr_t *u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
454 t = g_strdup_printf ("subtitle-%d-language", i);
455 lang_code[0] = (u->lang_code >> 8) & 0xff;
456 lang_code[1] = u->lang_code & 0xff;
457 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
463 GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', format=%d",
464 src->title, i, lang_code);
467 src->title_lang_event_pending =
468 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
473 /* FIXME: double-check this function, compare against original */
475 gst_dvd_read_src_get_next_cell_for (GstDvdReadSrc * src, gint cell)
477 /* Check if we're entering an angle block. */
478 if (src->cur_pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
481 while (src->cur_pgc->cell_playback[cell].block_mode == BLOCK_MODE_LAST_CELL)
484 return cell + 1; /* really +1? (tpm) */
487 /* Returns true if the pack is a NAV pack. This check is clearly insufficient,
488 * and sometimes we incorrectly think that valid other packs are NAV packs. I
489 * need to make this stronger. */
491 gst_dvd_read_src_is_nav_pack (const guint8 * buffer)
493 return (buffer[41] == 0xbf && buffer[1027] == 0xbf);
499 GST_DVD_READ_ERROR = -1,
500 GST_DVD_READ_EOS = -2,
501 GST_DVD_READ_AGAIN = -3
504 static GstDvdReadReturn
505 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
509 guint8 oneblock[DVD_VIDEO_LB_LEN];
511 guint next_vobu, next_ilvu_start, cur_output_size;
514 /* playback by cell in this pgc, starting at the cell for our chapter */
516 src->cur_cell = src->start_cell;
520 if (src->cur_cell >= src->last_cell) {
521 /* advance to next chapter */
522 if (src->chapter == (src->num_chapters - 1)) {
523 GST_INFO_OBJECT (src, "last chapter done - EOS");
524 return GST_DVD_READ_EOS;
527 GST_INFO_OBJECT (src, "end of chapter %d, switch to next", src->chapter);
530 gst_dvd_read_src_goto_chapter (src, src->chapter);
532 return GST_DVD_READ_AGAIN;
535 if (src->new_cell || new_seek) {
537 src->cur_cell = src->next_cell;
538 if (src->cur_cell >= src->last_cell) {
539 GST_LOG_OBJECT (src, "last cell in chapter");
544 /* take angle into account */
545 if (src->cur_pgc->cell_playback[src->cur_cell].block_type
546 == BLOCK_TYPE_ANGLE_BLOCK)
547 src->cur_cell += angle;
549 /* calculate next cell */
550 src->next_cell = gst_dvd_read_src_get_next_cell_for (src, src->cur_cell);
552 /* we loop until we're out of this cell */
553 src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
554 src->new_cell = FALSE;
557 if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
558 src->new_cell = TRUE;
559 GST_LOG_OBJECT (src, "Beyond last sector, go to next cell");
560 return GST_DVD_READ_AGAIN;
563 /* read NAV packet */
566 len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, oneblock);
568 GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
569 return GST_DVD_READ_ERROR;
572 if (!gst_dvd_read_src_is_nav_pack (oneblock)) {
577 /* parse the contained dsi packet */
578 navRead_DSI (&dsi_pack, &oneblock[DSI_START_BYTE]);
579 g_assert (src->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn);
581 /* determine where we go next. These values are the ones we
582 * mostly care about */
583 next_ilvu_start = src->cur_pack + dsi_pack.sml_agli.data[angle].address;
584 cur_output_size = dsi_pack.dsi_gi.vobu_ea;
586 /* If we're not at the end of this cell, we can determine the next
587 * VOBU to display using the VOBU_SRI information section of the
588 * DSI. Using this value correctly follows the current angle,
589 * avoiding the doubled scenes in The Matrix, and makes our life
592 * Otherwise, we set our next address past the end of this cell to
593 * force the code above to go to the next cell in the program. */
594 if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
595 next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
597 next_vobu = src->cur_pack + cur_output_size + 1;
600 g_assert (cur_output_size < 1024);
603 /* create the buffer (TODO: use buffer pool?) */
604 buf = gst_buffer_new_and_alloc (cur_output_size * DVD_VIDEO_LB_LEN);
606 /* read in and output cursize packs */
607 len = DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size,
608 GST_BUFFER_DATA (buf));
610 if (len != cur_output_size) {
611 GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
612 cur_output_size, src->cur_pack);
613 gst_buffer_unref (buf);
614 return GST_DVD_READ_ERROR;
617 GST_BUFFER_SIZE (buf) = cur_output_size * DVD_VIDEO_LB_LEN;
618 /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
620 gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (src)));
624 src->cur_pack = next_vobu;
626 GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
627 return GST_DVD_READ_OK;
631 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
633 GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
637 g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
639 srcpad = GST_BASE_SRC (src)->srcpad;
641 /* handle events, if any */
642 if (src->seek_pend) {
643 if (src->flush_pend) {
644 gst_pad_push_event (srcpad, gst_event_new_flush_start ());
645 gst_pad_push_event (srcpad, gst_event_new_flush_stop ());
646 src->flush_pend = FALSE;
649 if (src->seek_pend_fmt != GST_FORMAT_UNDEFINED) {
650 if (src->seek_pend_fmt == title_format) {
651 gst_dvd_read_src_goto_title (src, src->title, src->angle);
653 gst_dvd_read_src_goto_chapter (src, src->chapter);
655 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
657 if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
658 GST_DEBUG_OBJECT (src, "seek to sector failed, going EOS");
659 gst_pad_push_event (srcpad, gst_event_new_eos ());
660 return GST_FLOW_UNEXPECTED;
664 gst_pad_push_event (srcpad,
665 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
666 src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
668 src->seek_pend = FALSE;
672 gst_dvd_read_src_goto_title (src, src->title, src->angle);
673 gst_dvd_read_src_goto_chapter (src, src->chapter);
675 src->new_seek = FALSE;
676 src->change_cell = TRUE;
679 if (src->title_lang_event_pending) {
680 gst_pad_push_event (srcpad, src->title_lang_event_pending);
681 src->title_lang_event_pending = NULL;
684 if (src->pending_clut_event) {
685 gst_pad_push_event (srcpad, src->pending_clut_event);
686 src->pending_clut_event = NULL;
691 res = gst_dvd_read_src_read (src, src->angle, src->change_cell, p_buf);
692 } while (res == GST_DVD_READ_AGAIN);
695 case GST_DVD_READ_ERROR:{
696 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), (NULL));
697 return GST_FLOW_ERROR;
699 case GST_DVD_READ_EOS:{
700 GST_INFO_OBJECT (src, "Reached EOS");
701 gst_pad_push_event (GST_BASE_SRC (src)->srcpad, gst_event_new_eos ());
702 return GST_FLOW_UNEXPECTED;
704 case GST_DVD_READ_OK:{
705 src->change_cell = FALSE;
712 g_return_val_if_reached (GST_FLOW_UNEXPECTED);
716 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
717 const GValue * value, GParamSpec * pspec)
719 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
722 GST_OBJECT_LOCK (src);
723 started = GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED);
728 g_warning ("%s: property '%s' needs to be set before the device is "
729 "opened", GST_ELEMENT_NAME (src), pspec->name);
733 g_free (src->location);
734 /* clear the filename if we get a NULL (is that possible?) */
735 if (g_value_get_string (value) == NULL) {
736 src->location = g_strdup ("/dev/dvd");
738 src->location = g_strdup (g_value_get_string (value));
743 src->uri_title = g_value_get_int (value);
745 src->title = src->uri_title - 1;
746 src->new_seek = TRUE;
750 src->uri_chapter = g_value_get_int (value);
752 src->chapter = src->uri_chapter - 1;
753 src->new_seek = TRUE;
757 src->uri_angle = g_value_get_int (value);
759 src->angle = src->uri_angle - 1;
763 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
767 GST_OBJECT_UNLOCK (src);
771 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
774 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
776 GST_OBJECT_LOCK (src);
780 g_value_set_string (value, src->location);
783 g_value_set_int (value, src->uri_title);
786 g_value_set_int (value, src->uri_chapter);
789 g_value_set_int (value, src->uri_angle);
792 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
796 GST_OBJECT_UNLOCK (src);
800 gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size)
802 gboolean ret = FALSE;
804 if (src->dvd_title) {
807 blocks = DVDFileSize (src->dvd_title);
809 *size = (gint64) blocks *DVD_VIDEO_LB_LEN;
813 GST_WARNING_OBJECT (src, "DVDFileSize(%p) failed!", src->dvd_title);
820 /*** Querying and seeking ***/
823 gst_dvd_read_src_do_seek (GstDvdReadSrc * src, GstEvent * event)
826 GstSeekType cur_type, end_type;
827 gint64 new_off, total, cur;
833 srcpad = GST_BASE_SRC (src)->srcpad;
835 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
838 if (end_type != GST_SEEK_TYPE_NONE) {
839 GST_WARNING_OBJECT (src, "End seek type not supported, will be ignored");
843 case GST_FORMAT_BYTES:
846 if (format != chapter_format &&
847 format != sector_format &&
848 format != angle_format && format != title_format) {
849 GST_DEBUG_OBJECT (src, "Unsupported seek format %d (%s)", format,
850 gst_format_get_name (format));
857 /* get current offset and length */
858 if (format == GST_FORMAT_BYTES) {
859 GST_OBJECT_LOCK (src);
860 query_ok = gst_dvd_read_src_get_size (src, &total);
861 cur = (gint64) src->cur_pack * DVD_VIDEO_LB_LEN;
862 GST_OBJECT_UNLOCK (src);
864 query_ok = gst_pad_query_duration (srcpad, &format, &total)
865 && gst_pad_query_position (srcpad, &format, &cur);
869 GST_DEBUG_OBJECT (src, "Failed to query duration/position");
873 GST_DEBUG_OBJECT (src, "Current %s: %12" G_GINT64_FORMAT,
874 gst_format_get_name (format), cur);
875 GST_DEBUG_OBJECT (src, "Total %s: %12" G_GINT64_FORMAT,
876 gst_format_get_name (format), total);
880 case GST_SEEK_TYPE_SET:
883 case GST_SEEK_TYPE_CUR:
886 case GST_SEEK_TYPE_END:
887 new_off = total - new_off;
890 GST_DEBUG_OBJECT (src, "Unsupported seek method");
894 if (new_off < 0 || new_off >= total) {
895 GST_DEBUG_OBJECT (src, "Invalid seek position %" G_GINT64_FORMAT, new_off);
899 if (cur == new_off) {
900 GST_DEBUG_OBJECT (src, "We're already at that position!");
904 GST_LOG_OBJECT (src, "Seeking to %s: %12" G_GINT64_FORMAT,
905 gst_format_get_name (format), new_off);
907 GST_OBJECT_LOCK (src);
909 if (format == angle_format) {
910 src->angle = new_off;
914 if (format == sector_format) {
915 src->cur_pack = new_off;
916 } else if (format == GST_FORMAT_BYTES) {
917 src->cur_pack = new_off / DVD_VIDEO_LB_LEN;
918 if ((src->cur_pack * DVD_VIDEO_LB_LEN) != new_off) {
919 GST_LOG_OBJECT (src, "rounded down offset %" G_GINT64_FORMAT " => %"
920 G_GINT64_FORMAT, new_off, (gint64) src->cur_pack * DVD_VIDEO_LB_LEN);
922 } else if (format == chapter_format) {
924 src->chapter = new_off;
925 src->seek_pend_fmt = format;
926 } else if (format == title_format) {
928 src->title = new_off;
930 src->seek_pend_fmt = format;
932 GST_OBJECT_UNLOCK (src);
933 g_return_val_if_reached (FALSE);
936 /* leave for events */
937 src->seek_pend = TRUE;
938 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
939 src->flush_pend = TRUE;
943 GST_OBJECT_UNLOCK (src);
949 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
951 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
954 GST_LOG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
956 switch (GST_EVENT_TYPE (event)) {
958 res = gst_dvd_read_src_do_seek (src, event);
961 res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
969 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
972 GstStructure *structure;
976 structure = gst_structure_new ("application/x-gst-dvd",
977 "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
979 /* Create a separate field for each value in the table. */
980 for (i = 0; i < 16; i++) {
981 g_snprintf (name, sizeof (name), "clut%02d", i);
982 gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
985 /* Create the DVD event and put the structure into it. */
986 return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
990 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
993 const gint64 one_hour = 3600 * GST_SECOND;
994 const gint64 one_min = 60 * GST_SECOND;
996 g_return_val_if_fail ((time->hour >> 4) < 0xa
997 && (time->hour & 0xf) < 0xa, -1);
998 g_return_val_if_fail ((time->minute >> 4) < 0x7
999 && (time->minute & 0xf) < 0xa, -1);
1000 g_return_val_if_fail ((time->second >> 4) < 0x7
1001 && (time->second & 0xf) < 0xa, -1);
1003 ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
1004 ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
1005 ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
1011 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
1016 gst_query_parse_duration (query, &format, NULL);
1019 case GST_FORMAT_TIME:{
1020 if (src->cur_pgc == NULL)
1022 val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
1027 case GST_FORMAT_BYTES:{
1028 if (!gst_dvd_read_src_get_size (src, &val))
1033 if (format == sector_format) {
1034 val = DVDFileSize (src->dvd_title);
1035 } else if (format == title_format) {
1036 val = src->tt_srpt->nr_of_srpts;
1037 } else if (format == chapter_format) {
1038 val = src->num_chapters;
1039 } else if (format == angle_format) {
1040 val = src->tt_srpt->title[src->title].nr_of_angles;
1042 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1043 format, gst_format_get_name (format));
1050 GST_LOG_OBJECT (src, "duration = %" G_GINT64_FORMAT " %s", val,
1051 gst_format_get_name (format));
1053 gst_query_set_duration (query, format, val);
1058 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
1063 gst_query_parse_position (query, &format, NULL);
1066 case GST_FORMAT_BYTES:{
1067 val = src->cur_pack * DVD_VIDEO_LB_LEN;
1071 if (format == sector_format) {
1072 val = src->cur_pack;
1073 } else if (format == title_format) {
1075 } else if (format == chapter_format) {
1077 } else if (format == angle_format) {
1080 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1081 format, gst_format_get_name (format));
1088 GST_LOG_OBJECT (src, "position = %" G_GINT64_FORMAT " %s", val,
1089 gst_format_get_name (format));
1091 gst_query_set_position (query, format, val);
1096 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1098 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1100 gboolean res = TRUE;
1102 GST_LOG_OBJECT (src, "handling %s query",
1103 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1105 GST_OBJECT_LOCK (src);
1106 started = (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED));
1107 GST_OBJECT_UNLOCK (src);
1110 GST_DEBUG_OBJECT (src, "query failed: not started");
1114 switch (GST_QUERY_TYPE (query)) {
1115 case GST_QUERY_DURATION:
1116 GST_OBJECT_LOCK (src);
1117 res = gst_dvd_read_src_do_duration_query (src, query);
1118 GST_OBJECT_UNLOCK (src);
1120 case GST_QUERY_POSITION:
1121 GST_OBJECT_LOCK (src);
1122 res = gst_dvd_read_src_do_position_query (src, query);
1123 GST_OBJECT_UNLOCK (src);
1126 res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1134 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1136 gint seek_to = src->cur_pack;
1137 gint chapter, sectors, next, cur, i;
1139 /* retrieve position */
1141 for (i = 0; i < src->num_chapters; i++) {
1144 cur_title_get_chapter_bounds (src, i, &c1, &c2);
1146 for (next = cur = c1; cur < c2;) {
1149 src->cur_pgc->cell_playback[cur].last_sector -
1150 src->cur_pgc->cell_playback[cur].first_sector;
1151 if (src->cur_pack + sectors > seek_to) {
1155 src->cur_pack += sectors;
1158 if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1160 next = gst_dvd_read_src_get_next_cell_for (src, cur);
1164 GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1168 /* so chapter $chapter and cell $cur contain our sector
1169 * of interest. Let's go there! */
1170 GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1173 gst_dvd_read_src_goto_chapter (src, chapter);
1174 src->cur_cell = cur;
1175 src->next_cell = next;
1176 src->new_cell = FALSE;
1177 src->cur_pack = seek_to;
1183 /*** URI interface ***/
1186 gst_dvd_read_src_uri_get_type (void)
1192 gst_dvd_read_src_uri_get_protocols (void)
1194 static gchar *protocols[] = { "dvd", NULL };
1199 static const gchar *
1200 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1202 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1204 GST_OBJECT_LOCK (src);
1206 g_free (src->last_uri);
1207 src->last_uri = g_strdup_printf ("dvd://%d,%d,%d", src->uri_title,
1208 src->uri_chapter, src->uri_angle);
1210 GST_OBJECT_UNLOCK (src);
1212 return src->last_uri;
1216 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1218 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1222 protocol = gst_uri_get_protocol (uri);
1223 ret = (protocol != NULL && g_str_equal (protocol, "dvd"));
1230 /* parse out the new t/c/a and seek to them */
1232 gchar *location = NULL;
1237 location = gst_uri_get_location (uri);
1242 GST_OBJECT_LOCK (src);
1245 src->uri_chapter = 1;
1248 strcur = strs = g_strsplit (location, ",", 0);
1249 while (strcur && *strcur) {
1252 if (!sscanf (*strcur, "%d", &val))
1256 g_warning ("Invalid value %d in URI '%s'. Must be 1 or greater",
1263 src->uri_title = val;
1266 src->uri_chapter = val;
1269 src->uri_angle = val;
1277 if (pos > 0 && GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1278 src->title = src->uri_title - 1;
1279 src->chapter = src->uri_chapter - 1;
1280 src->angle = src->uri_angle - 1;
1281 src->new_seek = TRUE;
1284 GST_OBJECT_UNLOCK (src);
1294 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1296 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1298 iface->get_type = gst_dvd_read_src_uri_get_type;
1299 iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1300 iface->get_uri = gst_dvd_read_src_uri_get_uri;
1301 iface->set_uri = gst_dvd_read_src_uri_set_uri;
1305 gst_dvd_read_src_do_init (GType dvdreadsrc_type)
1307 static const GInterfaceInfo urihandler_info = {
1308 gst_dvd_read_src_uri_handler_init,
1313 g_type_add_interface_static (dvdreadsrc_type, GST_TYPE_URI_HANDLER,
1316 title_format = gst_format_register ("title", "DVD title");
1317 angle_format = gst_format_register ("angle", "DVD angle");
1318 sector_format = gst_format_register ("sector", "DVD sector");
1319 chapter_format = gst_format_register ("chapter", "DVD chapter");
1323 plugin_init (GstPlugin * plugin)
1325 GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1326 "DVD reader element based on dvdreadsrc");
1328 if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,
1329 GST_TYPE_DVD_READ_SRC)) {
1336 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1339 "Access a DVD with dvdread",
1340 plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);