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 (GstBaseSrc * bsrc, guint64 * size);
90 static gboolean gst_dvd_read_src_seekable (GstBaseSrc * bsrc);
92 GST_BOILERPLATE_FULL (GstDvdReadSrc, gst_dvd_read_src, GstPushSrc,
93 GST_TYPE_PUSH_SRC, gst_dvd_read_src_do_init)
95 static void gst_dvd_read_src_base_init (gpointer g_class)
97 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
99 gst_element_class_add_pad_template (element_class,
100 gst_static_pad_template_get (&srctemplate));
102 gst_element_class_set_details (element_class, &gst_dvd_read_src_details);
106 gst_dvd_read_src_finalize (GObject * object)
108 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
110 g_free (src->location);
111 g_free (src->last_uri);
113 G_OBJECT_CLASS (parent_class)->finalize (object);
117 gst_dvd_read_src_init (GstDvdReadSrc * src, GstDvdReadSrcClass * klass)
120 src->vts_file = NULL;
121 src->vmg_file = NULL;
122 src->dvd_title = NULL;
124 src->location = g_strdup ("/dev/dvd");
125 src->last_uri = NULL;
126 src->new_seek = TRUE;
127 src->new_cell = TRUE;
128 src->change_cell = FALSE;
133 src->seek_pend = FALSE;
134 src->flush_pend = FALSE;
135 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
136 src->title_lang_event_pending = NULL;
137 src->pending_clut_event = NULL;
141 gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
143 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
144 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
145 GObjectClass *gobject_class = G_OBJECT_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, G_PARAM_READWRITE));
154 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TITLE,
155 g_param_spec_int ("title", "title", "title",
156 0, 999, 0, G_PARAM_READWRITE));
157 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHAPTER,
158 g_param_spec_int ("chapter", "chapter", "chapter",
159 0, 999, 0, G_PARAM_READWRITE));
160 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ANGLE,
161 g_param_spec_int ("angle", "angle", "angle",
162 0, 999, 0, G_PARAM_READWRITE));
164 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
165 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
166 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_query);
167 gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_event);
168 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_dvd_read_src_get_size);
169 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_dvd_read_src_seekable);
171 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dvd_read_src_create);
175 gst_dvd_read_src_start (GstBaseSrc * basesrc)
177 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
179 g_return_val_if_fail (src->location != NULL, FALSE);
181 GST_DEBUG_OBJECT (src, "Opening DVD '%s'", src->location);
183 src->dvd = DVDOpen (src->location);
184 if (src->dvd == NULL) {
185 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
186 (_("Could not open DVD")),
187 ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
191 /* Load the video manager to find out the information about the titles */
192 GST_DEBUG_OBJECT (src, "Loading VMG info");
194 src->vmg_file = ifoOpen (src->dvd, 0);
195 if (!src->vmg_file) {
196 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
197 (_("Could not open DVD")),
198 ("ifoOpen() failed: %s", g_strerror (errno)));
202 src->tt_srpt = src->vmg_file->tt_srpt;
204 src->seek_pend_fmt = title_format;
205 src->seek_pend = TRUE;
211 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
213 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
216 ifoClose (src->vts_file);
217 src->vts_file = NULL;
220 ifoClose (src->vmg_file);
221 src->vmg_file = NULL;
223 if (src->dvd_title) {
224 DVDCloseFile (src->dvd_title);
225 src->dvd_title = NULL;
231 src->new_cell = TRUE;
232 src->new_seek = TRUE;
233 src->change_cell = FALSE;
236 src->flush_pend = FALSE;
237 src->seek_pend = FALSE;
238 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
239 if (src->title_lang_event_pending) {
240 gst_event_unref (src->title_lang_event_pending);
241 src->title_lang_event_pending = NULL;
243 if (src->pending_clut_event) {
244 gst_event_unref (src->pending_clut_event);
245 src->pending_clut_event = NULL;
252 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
253 gint * p_pgc_id, pgc_t ** p_pgc)
258 g_assert (chapter >= 0 && chapter < src->num_chapters);
260 pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
261 pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
262 pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
270 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
271 gint * p_first_cell, gint * p_last_cell)
274 gint pgn, pgc_id, pgn_next_ch;
276 g_assert (chapter >= 0 && chapter < src->num_chapters);
278 cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
280 *p_first_cell = pgc->program_map[pgn - 1] - 1;
282 if (chapter == (src->num_chapters - 1)) {
283 *p_last_cell = pgc->nr_of_cells;
285 pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
286 *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
291 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
295 /* make sure the chapter number is valid for this title */
296 if (chapter < 0 || chapter >= src->num_chapters) {
297 GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
298 chapter, src->num_chapters);
299 chapter = CLAMP (chapter, 0, src->num_chapters - 1);
302 /* determine which program chain we want to watch. This is
303 * based on the chapter number */
304 cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
306 cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
309 GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter,
310 src->start_cell, src->last_cell);
312 /* retrieve position */
314 for (i = 0; i < chapter; i++) {
317 cur_title_get_chapter_bounds (src, i, &c1, &c2);
321 src->cur_pgc->cell_playback[c1].last_sector -
322 src->cur_pgc->cell_playback[c1].first_sector;
327 /* prepare reading for new cell */
328 src->new_cell = TRUE;
329 src->next_cell = src->start_cell;
331 src->chapter = chapter;
333 if (src->pending_clut_event)
334 gst_event_unref (src->pending_clut_event);
336 src->pending_clut_event =
337 gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
343 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
346 gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
352 /* make sure our title number is valid */
353 num_titles = src->tt_srpt->nr_of_srpts;
354 GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
355 if (title < 0 || title >= num_titles) {
356 GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
358 title = CLAMP (title, 0, num_titles - 1);
361 src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
362 GST_INFO_OBJECT (src, "Title %d has %d chapters", title, src->num_chapters);
364 /* make sure the angle number is valid for this title */
365 num_angles = src->tt_srpt->title[title].nr_of_angles;
366 GST_LOG_OBJECT (src, "Title %d has %d angles", title, num_angles);
367 if (angle < 0 || angle >= num_angles) {
368 GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
370 angle = CLAMP (angle, 0, num_angles - 1);
373 /* load the VTS information for the title set our title is in */
374 title_set_nr = src->tt_srpt->title[title].title_set_nr;
375 src->vts_file = ifoOpen (src->dvd, title_set_nr);
376 if (src->vts_file == NULL) {
377 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
378 (_("Could not open DVD title %d"), title_set_nr),
379 ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
383 src->ttn = src->tt_srpt->title[title].vts_ttn;
384 src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
386 /* we've got enough info, time to open the title set data */
387 src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
388 if (src->dvd_title == NULL) {
389 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
390 (_("Could not open DVD title %d"), title_set_nr),
391 ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
395 GST_INFO_OBJECT (src, "Opened title %d, angle %d", title, angle);
401 if (src->title_lang_event_pending) {
402 gst_event_unref (src->title_lang_event_pending);
403 src->title_lang_event_pending = NULL;
406 s = gst_structure_new ("application/x-gst-event",
407 "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
410 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
411 const audio_attr_t *a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
413 t = g_strdup_printf ("audio-%d-format", i);
414 gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
418 t = g_strdup_printf ("audio-%d-language", i);
419 lang_code[0] = (a->lang_code >> 8) & 0xff;
420 lang_code[1] = a->lang_code & 0xff;
421 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
427 GST_INFO_OBJECT (src, "[%02d] Audio %02d: lang='%s', format=%d",
428 src->title, i, lang_code, (gint) a->audio_format);
432 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
433 const subp_attr_t *u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
436 t = g_strdup_printf ("subtitle-%d-language", i);
437 lang_code[0] = (u->lang_code >> 8) & 0xff;
438 lang_code[1] = u->lang_code & 0xff;
439 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
445 GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', format=%d",
446 src->title, i, lang_code);
449 src->title_lang_event_pending =
450 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
455 /* FIXME: double-check this function, compare against original */
457 gst_dvd_read_src_get_next_cell_for (GstDvdReadSrc * src, gint cell)
459 /* Check if we're entering an angle block. */
460 if (src->cur_pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
463 while (src->cur_pgc->cell_playback[cell].block_mode == BLOCK_MODE_LAST_CELL)
466 return cell + 1; /* really +1? (tpm) */
469 /* Returns true if the pack is a NAV pack. This check is clearly insufficient,
470 * and sometimes we incorrectly think that valid other packs are NAV packs. I
471 * need to make this stronger. */
473 gst_dvd_read_src_is_nav_pack (const guint8 * buffer)
475 return (buffer[41] == 0xbf && buffer[1027] == 0xbf);
481 GST_DVD_READ_ERROR = -1,
482 GST_DVD_READ_EOS = -2,
483 GST_DVD_READ_AGAIN = -3
486 static GstDvdReadReturn
487 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
492 guint next_vobu, next_ilvu_start, cur_output_size;
495 /* playback by cell in this pgc, starting at the cell for our chapter */
497 src->cur_cell = src->start_cell;
501 if (src->cur_cell >= src->last_cell) {
502 /* advance to next chapter */
503 if (src->chapter == (src->num_chapters - 1)) {
504 GST_INFO_OBJECT (src, "last chapter done - EOS");
505 return GST_DVD_READ_EOS;
508 GST_INFO_OBJECT (src, "end of chapter %d, switch to next", src->chapter);
511 gst_dvd_read_src_goto_chapter (src, src->chapter);
513 return GST_DVD_READ_AGAIN;
516 if (src->new_cell || new_seek) {
518 src->cur_cell = src->next_cell;
519 if (src->cur_cell >= src->last_cell) {
520 GST_LOG_OBJECT (src, "last cell in chapter");
525 /* take angle into account */
526 if (src->cur_pgc->cell_playback[src->cur_cell].block_type
527 == BLOCK_TYPE_ANGLE_BLOCK)
528 src->cur_cell += angle;
530 /* calculate next cell */
531 src->next_cell = gst_dvd_read_src_get_next_cell_for (src, src->cur_cell);
533 /* we loop until we're out of this cell */
534 src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
535 src->new_cell = FALSE;
538 if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
539 src->new_cell = TRUE;
540 GST_LOG_OBJECT (src, "Beyond last sector, go to next cell");
541 return GST_DVD_READ_AGAIN;
544 /* read NAV packet */
545 data = GST_BUFFER_DATA (buf);
549 len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, data);
551 GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
552 return GST_DVD_READ_ERROR;
555 if (!gst_dvd_read_src_is_nav_pack (data)) {
560 /* parse the contained dsi packet */
561 navRead_DSI (&dsi_pack, &data[DSI_START_BYTE]);
562 g_assert (src->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn);
564 /* determine where we go next. These values are the ones we
565 * mostly care about */
566 next_ilvu_start = src->cur_pack + dsi_pack.sml_agli.data[angle].address;
567 cur_output_size = dsi_pack.dsi_gi.vobu_ea;
569 /* If we're not at the end of this cell, we can determine the next
570 * VOBU to display using the VOBU_SRI information section of the
571 * DSI. Using this value correctly follows the current angle,
572 * avoiding the doubled scenes in The Matrix, and makes our life
575 * Otherwise, we set our next address past the end of this cell to
576 * force the code above to go to the next cell in the program. */
577 if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
578 next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
580 next_vobu = src->cur_pack + cur_output_size + 1;
583 g_assert (cur_output_size < 1024);
586 /* read in and output cursize packs */
587 len = DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size, data);
588 if (len != cur_output_size) {
589 GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
590 cur_output_size, src->cur_pack);
591 return GST_DVD_READ_ERROR;
594 GST_BUFFER_SIZE (buf) = cur_output_size * DVD_VIDEO_LB_LEN;
595 /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
597 src->cur_pack = next_vobu;
599 GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
600 return GST_DVD_READ_OK;
604 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
606 GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
611 g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
613 srcpad = GST_BASE_SRC (src)->srcpad;
615 /* handle events, if any */
616 if (src->seek_pend) {
617 if (src->flush_pend) {
618 gst_pad_push_event (srcpad, gst_event_new_flush_start ());
619 gst_pad_push_event (srcpad, gst_event_new_flush_stop ());
620 src->flush_pend = FALSE;
623 if (src->seek_pend_fmt != GST_FORMAT_UNDEFINED) {
624 if (src->seek_pend_fmt == title_format) {
625 gst_dvd_read_src_goto_title (src, src->title, src->angle);
627 gst_dvd_read_src_goto_chapter (src, src->chapter);
629 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
631 if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
632 GST_DEBUG_OBJECT (src, "seek to sector failed, going EOS");
633 gst_pad_push_event (srcpad, gst_event_new_eos ());
634 return GST_FLOW_UNEXPECTED;
638 gst_pad_push_event (srcpad,
639 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
640 src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
642 src->seek_pend = FALSE;
646 gst_dvd_read_src_goto_title (src, src->title, src->angle);
647 gst_dvd_read_src_goto_chapter (src, src->chapter);
649 src->new_seek = FALSE;
650 src->change_cell = TRUE;
653 if (src->title_lang_event_pending) {
654 gst_pad_push_event (srcpad, src->title_lang_event_pending);
655 src->title_lang_event_pending = NULL;
658 if (src->pending_clut_event) {
659 gst_pad_push_event (srcpad, src->pending_clut_event);
660 src->pending_clut_event = NULL;
663 /* create the buffer (TODO: use buffer pool?) */
664 buf = gst_buffer_new_and_alloc (1024 * DVD_VIDEO_LB_LEN);
668 res = gst_dvd_read_src_read (src, src->angle, src->change_cell, buf);
669 } while (res == GST_DVD_READ_AGAIN);
672 case GST_DVD_READ_ERROR:{
673 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), (NULL));
674 gst_buffer_unref (buf);
675 return GST_FLOW_ERROR;
677 case GST_DVD_READ_EOS:{
678 GST_INFO_OBJECT (src, "Reached EOS");
679 gst_pad_push_event (GST_BASE_SRC (src)->srcpad, gst_event_new_eos ());
680 gst_buffer_unref (buf);
681 return GST_FLOW_UNEXPECTED;
683 case GST_DVD_READ_OK:{
684 src->change_cell = FALSE;
692 g_return_val_if_reached (GST_FLOW_UNEXPECTED);
696 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
697 const GValue * value, GParamSpec * pspec)
699 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
701 GST_OBJECT_LOCK (src);
705 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
706 g_warning ("%s: property '%s' needs to be set before the device is "
707 "opened", GST_ELEMENT_NAME (src), pspec->name);
711 g_free (src->location);
712 /* clear the filename if we get a NULL (is that possible?) */
713 if (g_value_get_string (value) == NULL) {
714 src->location = g_strdup ("/dev/dvd");
716 src->location = g_strdup (g_value_get_string (value));
721 src->title = g_value_get_int (value);
722 src->new_seek = TRUE;
725 src->chapter = g_value_get_int (value);
726 src->new_seek = TRUE;
729 src->angle = g_value_get_int (value);
732 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
736 GST_OBJECT_UNLOCK (src);
740 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
743 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
745 GST_OBJECT_LOCK (src);
749 g_value_set_string (value, src->location);
752 g_value_set_int (value, src->title);
755 g_value_set_int (value, src->chapter);
758 g_value_set_int (value, src->angle);
761 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
765 GST_OBJECT_UNLOCK (src);
769 gst_dvd_read_src_seekable (GstBaseSrc * basesrc)
775 gst_dvd_read_src_get_size (GstBaseSrc * basesrc, guint64 * size)
777 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
778 gboolean ret = FALSE;
780 if (src->dvd_title) {
783 blocks = DVDFileSize (src->dvd_title);
785 *size = (guint64) blocks *DVD_VIDEO_LB_LEN;
789 GST_WARNING_OBJECT (src, "DVDFileSize(%p) failed!", src->dvd_title);
796 /*** Querying and seeking ***/
799 gst_dvd_read_src_do_seek (GstDvdReadSrc * src, GstEvent * event)
802 GstSeekType cur_type, end_type;
803 gint64 new_off, total, cur;
808 srcpad = GST_BASE_SRC (src)->srcpad;
810 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
813 if (end_type != GST_SEEK_TYPE_NONE) {
814 GST_WARNING_OBJECT (src, "End seek type not supported, will be ignored");
818 case GST_FORMAT_BYTES:{
819 new_off /= DVD_VIDEO_LB_LEN;
820 format = sector_format;
824 if (format != chapter_format &&
825 format != sector_format &&
826 format != angle_format && format != title_format) {
827 GST_DEBUG_OBJECT (src, "Unsupported seek format %d (%s)", format,
828 gst_format_get_name (format));
835 /* get current offset and length */
836 gst_pad_query_duration (srcpad, &format, &total);
837 gst_pad_query_position (srcpad, &format, &cur);
839 GST_DEBUG_OBJECT (src, "Current %s: %" G_GINT64_FORMAT,
840 gst_format_get_name (format), cur);
841 GST_DEBUG_OBJECT (src, "Total %s: %" G_GINT64_FORMAT,
842 gst_format_get_name (format), total);
844 if (cur == new_off) {
845 GST_DEBUG_OBJECT (src, "We're already at that position!");
851 case GST_SEEK_TYPE_SET:
854 case GST_SEEK_TYPE_CUR:
857 case GST_SEEK_TYPE_END:
858 new_off = total - new_off;
861 GST_DEBUG_OBJECT (src, "Unsupported seek method");
865 if (new_off < 0 || new_off >= total) {
866 GST_DEBUG_OBJECT (src, "Invalid seek position %" G_GINT64_FORMAT, new_off);
870 GST_LOG_OBJECT (src, "Seeking to unit %" G_GINT64_FORMAT, new_off);
872 GST_OBJECT_LOCK (src);
874 if (format == angle_format) {
875 src->angle = new_off;
876 GST_OBJECT_UNLOCK (src);
880 if (format == sector_format) {
881 src->cur_pack = new_off;
882 } else if (format == chapter_format) {
884 src->chapter = new_off;
885 src->seek_pend_fmt = format;
886 } else if (format == title_format) {
888 src->title = new_off;
890 src->seek_pend_fmt = format;
892 GST_OBJECT_UNLOCK (src);
893 g_return_val_if_reached (FALSE);
896 /* leave for events */
897 src->seek_pend = TRUE;
898 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
899 src->flush_pend = TRUE;
901 GST_OBJECT_UNLOCK (src);
907 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
909 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
912 GST_DEBUG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
914 switch (GST_EVENT_TYPE (event)) {
916 res = gst_dvd_read_src_do_seek (src, event);
919 res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
927 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
930 GstStructure *structure;
934 structure = gst_structure_new ("application/x-gst-dvd",
935 "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
937 /* Create a separate field for each value in the table. */
938 for (i = 0; i < 16; i++) {
939 g_snprintf (name, sizeof (name), "clut%02d", i);
940 gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
943 /* Create the DVD event and put the structure into it. */
944 return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
948 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
951 const gint64 one_hour = 3600 * GST_SECOND;
952 const gint64 one_min = 60 * GST_SECOND;
954 g_return_val_if_fail ((time->hour >> 4) < 0xa
955 && (time->hour & 0xf) < 0xa, -1);
956 g_return_val_if_fail ((time->minute >> 4) < 0x7
957 && (time->minute & 0xf) < 0xa, -1);
958 g_return_val_if_fail ((time->second >> 4) < 0x7
959 && (time->second & 0xf) < 0xa, -1);
961 ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
962 ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
963 ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
969 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
974 gst_query_parse_duration (query, &format, NULL);
977 case GST_FORMAT_TIME:{
978 if (src->cur_pgc == NULL)
980 val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
985 case GST_FORMAT_BYTES:{
988 if (!gst_dvd_read_src_get_size (GST_BASE_SRC (src), &size))
995 if (format == sector_format) {
996 val = DVDFileSize (src->dvd_title);
997 } else if (format == title_format) {
998 val = src->tt_srpt->nr_of_srpts;
999 } else if (format == chapter_format) {
1000 val = src->num_chapters;
1001 } else if (format == angle_format) {
1002 val = src->tt_srpt->title[src->title].nr_of_angles;
1004 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1005 format, gst_format_get_name (format));
1012 GST_LOG_OBJECT (src, "duration = %" G_GINT64_FORMAT " %s", val,
1013 gst_format_get_name (format));
1015 gst_query_set_duration (query, format, val);
1020 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
1025 gst_query_parse_position (query, &format, NULL);
1028 case GST_FORMAT_BYTES:{
1029 val = src->cur_pack * DVD_VIDEO_LB_LEN;
1033 if (format == sector_format) {
1034 val = src->cur_pack;
1035 } else if (format == title_format) {
1037 } else if (format == chapter_format) {
1039 } else if (format == angle_format) {
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, "position = %" G_GINT64_FORMAT " %s", val,
1051 gst_format_get_name (format));
1053 gst_query_set_position (query, format, val);
1058 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1060 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1062 gboolean res = TRUE;
1064 GST_LOG_OBJECT (src, "handling %s query",
1065 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1067 GST_OBJECT_LOCK (src);
1068 started = (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED));
1069 GST_OBJECT_UNLOCK (src);
1072 GST_DEBUG_OBJECT (src, "query failed: not started");
1076 switch (GST_QUERY_TYPE (query)) {
1077 case GST_QUERY_DURATION:
1078 GST_OBJECT_LOCK (src);
1079 res = gst_dvd_read_src_do_duration_query (src, query);
1080 GST_OBJECT_UNLOCK (src);
1082 case GST_QUERY_POSITION:
1083 GST_OBJECT_LOCK (src);
1084 res = gst_dvd_read_src_do_position_query (src, query);
1085 GST_OBJECT_UNLOCK (src);
1088 res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1096 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1098 gint seek_to = src->cur_pack;
1099 gint chapter, sectors, next, cur, i;
1101 /* retrieve position */
1103 for (i = 0; i < src->num_chapters; i++) {
1106 cur_title_get_chapter_bounds (src, i, &c1, &c2);
1108 for (next = cur = c1; cur < c2;) {
1111 src->cur_pgc->cell_playback[cur].last_sector -
1112 src->cur_pgc->cell_playback[cur].first_sector;
1113 if (src->cur_pack + sectors > seek_to) {
1117 src->cur_pack += sectors;
1120 if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1122 next = gst_dvd_read_src_get_next_cell_for (src, cur);
1126 GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1130 /* so chapter $chapter and cell $cur contain our sector
1131 * of interest. Let's go there! */
1132 GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1135 gst_dvd_read_src_goto_chapter (src, chapter);
1136 src->cur_cell = cur;
1137 src->next_cell = next;
1138 src->new_cell = FALSE;
1139 src->cur_pack = seek_to;
1145 /*** URI interface ***/
1148 gst_dvd_read_src_uri_get_type (void)
1154 gst_dvd_read_src_uri_get_protocols (void)
1156 static gchar *protocols[] = { "dvd", NULL };
1161 static const gchar *
1162 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1164 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1166 g_free (src->last_uri);
1168 g_strdup_printf ("dvd://%d,%d,%d", src->title, src->chapter, src->angle);
1170 return src->last_uri;
1174 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1176 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1180 protocol = gst_uri_get_protocol (uri);
1181 ret = (protocol != NULL && g_str_equal (protocol, "dvd"));
1188 /* parse out the new t/c/a and seek to them */
1190 gchar *location = NULL;
1195 location = gst_uri_get_location (uri);
1200 strcur = strs = g_strsplit (location, ",", 0);
1201 while (strcur && *strcur) {
1204 if (!sscanf (*strcur, "%d", &val))
1209 if (val != src->title) {
1211 src->new_seek = TRUE;
1215 if (val != src->chapter) {
1217 src->new_seek = TRUE;
1237 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1239 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1241 iface->get_type = gst_dvd_read_src_uri_get_type;
1242 iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1243 iface->get_uri = gst_dvd_read_src_uri_get_uri;
1244 iface->set_uri = gst_dvd_read_src_uri_set_uri;
1248 gst_dvd_read_src_do_init (GType dvdreadsrc_type)
1250 static const GInterfaceInfo urihandler_info = {
1251 gst_dvd_read_src_uri_handler_init,
1256 g_type_add_interface_static (dvdreadsrc_type, GST_TYPE_URI_HANDLER,
1259 title_format = gst_format_register ("title", "DVD title");
1260 angle_format = gst_format_register ("angle", "DVD angle");
1261 sector_format = gst_format_register ("sector", "DVD sector");
1262 chapter_format = gst_format_register ("chapter", "DVD chapter");
1266 plugin_init (GstPlugin * plugin)
1268 GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1269 "DVD reader element based on dvdreadsrc");
1271 if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,
1272 GST_TYPE_DVD_READ_SRC)) {
1279 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1282 "Access a DVD with dvdread",
1283 plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN)