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,
90 GST_BOILERPLATE_FULL (GstDvdReadSrc, gst_dvd_read_src, GstPushSrc,
91 GST_TYPE_PUSH_SRC, gst_dvd_read_src_do_init)
93 static void gst_dvd_read_src_base_init (gpointer g_class)
95 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
97 gst_element_class_add_pad_template (element_class,
98 gst_static_pad_template_get (&srctemplate));
100 gst_element_class_set_details (element_class, &gst_dvd_read_src_details);
104 gst_dvd_read_src_finalize (GObject * object)
106 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
108 g_free (src->location);
109 g_free (src->last_uri);
111 G_OBJECT_CLASS (parent_class)->finalize (object);
115 gst_dvd_read_src_init (GstDvdReadSrc * src, GstDvdReadSrcClass * klass)
118 src->vts_file = NULL;
119 src->vmg_file = NULL;
120 src->dvd_title = NULL;
122 src->location = g_strdup ("/dev/dvd");
123 src->last_uri = NULL;
124 src->new_seek = TRUE;
125 src->new_cell = TRUE;
126 src->change_cell = FALSE;
131 src->seek_pend = FALSE;
132 src->flush_pend = FALSE;
133 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
134 src->title_lang_event_pending = NULL;
135 src->pending_clut_event = NULL;
139 gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
141 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
142 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
143 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
145 gobject_class->finalize = gst_dvd_read_src_finalize;
146 gobject_class->set_property = gst_dvd_read_src_set_property;
147 gobject_class->get_property = gst_dvd_read_src_get_property;
149 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
150 g_param_spec_string ("device", "Device",
151 "DVD device location", NULL, G_PARAM_READWRITE));
152 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TITLE,
153 g_param_spec_int ("title", "title", "title",
154 0, 999, 0, G_PARAM_READWRITE));
155 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHAPTER,
156 g_param_spec_int ("chapter", "chapter", "chapter",
157 0, 999, 0, G_PARAM_READWRITE));
158 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ANGLE,
159 g_param_spec_int ("angle", "angle", "angle",
160 0, 999, 0, G_PARAM_READWRITE));
162 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
163 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
164 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_query);
165 gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_event);
167 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dvd_read_src_create);
171 gst_dvd_read_src_start (GstBaseSrc * basesrc)
173 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
175 g_return_val_if_fail (src->location != NULL, FALSE);
177 GST_DEBUG_OBJECT (src, "Opening DVD '%s'", src->location);
179 src->dvd = DVDOpen (src->location);
180 if (src->dvd == NULL) {
181 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
182 (_("Could not open DVD")),
183 ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
187 /* Load the video manager to find out the information about the titles */
188 GST_DEBUG_OBJECT (src, "Loading VMG info");
190 src->vmg_file = ifoOpen (src->dvd, 0);
191 if (!src->vmg_file) {
192 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
193 (_("Could not open DVD")),
194 ("ifoOpen() failed: %s", g_strerror (errno)));
198 src->tt_srpt = src->vmg_file->tt_srpt;
200 src->seek_pend_fmt = title_format;
201 src->seek_pend = TRUE;
207 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
209 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
212 ifoClose (src->vts_file);
213 src->vts_file = NULL;
216 ifoClose (src->vmg_file);
217 src->vmg_file = NULL;
219 if (src->dvd_title) {
220 DVDCloseFile (src->dvd_title);
221 src->dvd_title = NULL;
227 src->new_cell = TRUE;
228 src->new_seek = TRUE;
229 src->change_cell = FALSE;
232 src->flush_pend = FALSE;
233 src->seek_pend = FALSE;
234 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
235 if (src->title_lang_event_pending) {
236 gst_event_unref (src->title_lang_event_pending);
237 src->title_lang_event_pending = NULL;
239 if (src->pending_clut_event) {
240 gst_event_unref (src->pending_clut_event);
241 src->pending_clut_event = NULL;
248 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
249 gint * p_pgc_id, pgc_t ** p_pgc)
254 g_assert (chapter >= 0 && chapter < src->num_chapters);
256 pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
257 pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
258 pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
266 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
267 gint * p_first_cell, gint * p_last_cell)
270 gint pgn, pgc_id, pgn_next_ch;
272 g_assert (chapter >= 0 && chapter < src->num_chapters);
274 cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
276 *p_first_cell = pgc->program_map[pgn - 1] - 1;
278 if (chapter == (src->num_chapters - 1)) {
279 *p_last_cell = pgc->nr_of_cells;
281 pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
282 *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
287 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
291 /* make sure the chapter number is valid for this title */
292 if (chapter < 0 || chapter >= src->num_chapters) {
293 GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
294 chapter, src->num_chapters);
295 chapter = CLAMP (chapter, 0, src->num_chapters - 1);
298 /* determine which program chain we want to watch. This is
299 * based on the chapter number */
300 cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
302 cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
305 GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter,
306 src->start_cell, src->last_cell);
308 /* retrieve position */
310 for (i = 0; i < chapter; i++) {
313 cur_title_get_chapter_bounds (src, i, &c1, &c2);
317 src->cur_pgc->cell_playback[c1].last_sector -
318 src->cur_pgc->cell_playback[c1].first_sector;
323 /* prepare reading for new cell */
324 src->new_cell = TRUE;
325 src->next_cell = src->start_cell;
327 src->chapter = chapter;
329 if (src->pending_clut_event)
330 gst_event_unref (src->pending_clut_event);
332 src->pending_clut_event =
333 gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
339 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
342 gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
348 /* make sure our title number is valid */
349 num_titles = src->tt_srpt->nr_of_srpts;
350 GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
351 if (title < 0 || title >= num_titles) {
352 GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
354 title = CLAMP (title, 0, num_titles - 1);
357 src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
358 GST_INFO_OBJECT (src, "Title %d has %d chapters", title, src->num_chapters);
360 /* make sure the angle number is valid for this title */
361 num_angles = src->tt_srpt->title[title].nr_of_angles;
362 GST_LOG_OBJECT (src, "Title %d has %d angles", title, num_angles);
363 if (angle < 0 || angle >= num_angles) {
364 GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
366 angle = CLAMP (angle, 0, num_angles - 1);
369 /* load the VTS information for the title set our title is in */
370 title_set_nr = src->tt_srpt->title[title].title_set_nr;
371 src->vts_file = ifoOpen (src->dvd, title_set_nr);
372 if (src->vts_file == NULL) {
373 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
374 (_("Could not open DVD title %d"), title_set_nr),
375 ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
379 src->ttn = src->tt_srpt->title[title].vts_ttn;
380 src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
382 /* we've got enough info, time to open the title set data */
383 src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
384 if (src->dvd_title == NULL) {
385 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
386 (_("Could not open DVD title %d"), title_set_nr),
387 ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
391 GST_INFO_OBJECT (src, "Opened title %d, angle %d", title, angle);
397 if (src->title_lang_event_pending) {
398 gst_event_unref (src->title_lang_event_pending);
399 src->title_lang_event_pending = NULL;
402 s = gst_structure_new ("application/x-gst-event",
403 "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
406 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
407 const audio_attr_t *a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
409 t = g_strdup_printf ("audio-%d-format", i);
410 gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
414 t = g_strdup_printf ("audio-%d-language", i);
415 lang_code[0] = (a->lang_code >> 8) & 0xff;
416 lang_code[1] = a->lang_code & 0xff;
417 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
423 GST_INFO_OBJECT (src, "[%02d] Audio %02d: lang='%s', format=%d",
424 src->title, i, lang_code, (gint) a->audio_format);
428 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
429 const subp_attr_t *u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
432 t = g_strdup_printf ("subtitle-%d-language", i);
433 lang_code[0] = (u->lang_code >> 8) & 0xff;
434 lang_code[1] = u->lang_code & 0xff;
435 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
441 GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', format=%d",
442 src->title, i, lang_code);
445 src->title_lang_event_pending =
446 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
451 /* FIXME: double-check this function, compare against original */
453 gst_dvd_read_src_get_next_cell_for (GstDvdReadSrc * src, gint cell)
455 /* Check if we're entering an angle block. */
456 if (src->cur_pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
459 while (src->cur_pgc->cell_playback[cell].block_mode == BLOCK_MODE_LAST_CELL)
462 return cell + 1; /* really +1? (tpm) */
465 /* Returns true if the pack is a NAV pack. This check is clearly insufficient,
466 * and sometimes we incorrectly think that valid other packs are NAV packs. I
467 * need to make this stronger. */
469 gst_dvd_read_src_is_nav_pack (const guint8 * buffer)
471 return (buffer[41] == 0xbf && buffer[1027] == 0xbf);
477 GST_DVD_READ_ERROR = -1,
478 GST_DVD_READ_EOS = -2,
479 GST_DVD_READ_AGAIN = -3
482 static GstDvdReadReturn
483 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
488 guint next_vobu, next_ilvu_start, cur_output_size;
491 /* playback by cell in this pgc, starting at the cell for our chapter */
493 src->cur_cell = src->start_cell;
497 if (src->cur_cell >= src->last_cell) {
498 /* advance to next chapter */
499 if (src->chapter == (src->num_chapters - 1)) {
500 GST_INFO_OBJECT (src, "last chapter done - EOS");
501 return GST_DVD_READ_EOS;
504 GST_INFO_OBJECT (src, "end of chapter %d, switch to next", src->chapter);
507 gst_dvd_read_src_goto_chapter (src, src->chapter);
509 return GST_DVD_READ_AGAIN;
512 if (src->new_cell || new_seek) {
514 src->cur_cell = src->next_cell;
515 if (src->cur_cell >= src->last_cell) {
516 GST_LOG_OBJECT (src, "last cell in chapter");
521 /* take angle into account */
522 if (src->cur_pgc->cell_playback[src->cur_cell].block_type
523 == BLOCK_TYPE_ANGLE_BLOCK)
524 src->cur_cell += angle;
526 /* calculate next cell */
527 src->next_cell = gst_dvd_read_src_get_next_cell_for (src, src->cur_cell);
529 /* we loop until we're out of this cell */
530 src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
531 src->new_cell = FALSE;
534 if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
535 src->new_cell = TRUE;
536 GST_LOG_OBJECT (src, "Beyond last sector, go to next cell");
537 return GST_DVD_READ_AGAIN;
540 /* read NAV packet */
541 data = GST_BUFFER_DATA (buf);
545 len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, data);
547 GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
548 return GST_DVD_READ_ERROR;
551 if (!gst_dvd_read_src_is_nav_pack (data)) {
556 /* parse the contained dsi packet */
557 navRead_DSI (&dsi_pack, &data[DSI_START_BYTE]);
558 g_assert (src->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn);
560 /* determine where we go next. These values are the ones we
561 * mostly care about */
562 next_ilvu_start = src->cur_pack + dsi_pack.sml_agli.data[angle].address;
563 cur_output_size = dsi_pack.dsi_gi.vobu_ea;
565 /* If we're not at the end of this cell, we can determine the next
566 * VOBU to display using the VOBU_SRI information section of the
567 * DSI. Using this value correctly follows the current angle,
568 * avoiding the doubled scenes in The Matrix, and makes our life
571 * Otherwise, we set our next address past the end of this cell to
572 * force the code above to go to the next cell in the program. */
573 if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
574 next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
576 next_vobu = src->cur_pack + cur_output_size + 1;
579 g_assert (cur_output_size < 1024);
582 /* read in and output cursize packs */
583 len = DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size, data);
584 if (len != cur_output_size) {
585 GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
586 cur_output_size, src->cur_pack);
587 return GST_DVD_READ_ERROR;
590 GST_BUFFER_SIZE (buf) = cur_output_size * DVD_VIDEO_LB_LEN;
591 /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
593 src->cur_pack = next_vobu;
595 GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
596 return GST_DVD_READ_OK;
600 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
602 GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
607 g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
609 srcpad = GST_BASE_SRC (src)->srcpad;
611 /* handle events, if any */
612 if (src->seek_pend) {
613 if (src->flush_pend) {
614 gst_pad_push_event (srcpad, gst_event_new_flush_start ());
615 gst_pad_push_event (srcpad, gst_event_new_flush_stop ());
616 src->flush_pend = FALSE;
619 if (src->seek_pend_fmt != GST_FORMAT_UNDEFINED) {
620 if (src->seek_pend_fmt == title_format) {
621 gst_dvd_read_src_goto_title (src, src->title, src->angle);
623 gst_dvd_read_src_goto_chapter (src, src->chapter);
625 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
627 if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
628 GST_DEBUG_OBJECT (src, "seek to sector failed, going EOS");
629 gst_pad_push_event (srcpad, gst_event_new_eos ());
630 return GST_FLOW_UNEXPECTED;
634 gst_pad_push_event (srcpad,
635 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
636 src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
638 src->seek_pend = FALSE;
642 gst_dvd_read_src_goto_title (src, src->title, src->angle);
643 gst_dvd_read_src_goto_chapter (src, src->chapter);
645 src->new_seek = FALSE;
646 src->change_cell = TRUE;
649 if (src->title_lang_event_pending) {
650 gst_pad_push_event (srcpad, src->title_lang_event_pending);
651 src->title_lang_event_pending = NULL;
654 if (src->pending_clut_event) {
655 gst_pad_push_event (srcpad, src->pending_clut_event);
656 src->pending_clut_event = NULL;
659 /* create the buffer (TODO: use buffer pool?) */
660 buf = gst_buffer_new_and_alloc (1024 * DVD_VIDEO_LB_LEN);
664 res = gst_dvd_read_src_read (src, src->angle, src->change_cell, buf);
665 } while (res == GST_DVD_READ_AGAIN);
668 case GST_DVD_READ_ERROR:{
669 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), (NULL));
670 gst_buffer_unref (buf);
671 return GST_FLOW_ERROR;
673 case GST_DVD_READ_EOS:{
674 GST_INFO_OBJECT (src, "Reached EOS");
675 gst_pad_push_event (GST_BASE_SRC (src)->srcpad, gst_event_new_eos ());
676 gst_buffer_unref (buf);
677 return GST_FLOW_UNEXPECTED;
679 case GST_DVD_READ_OK:{
680 src->change_cell = FALSE;
688 g_return_val_if_reached (GST_FLOW_UNEXPECTED);
692 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
693 const GValue * value, GParamSpec * pspec)
695 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
697 GST_OBJECT_LOCK (src);
701 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
702 g_warning ("%s: property '%s' needs to be set before the device is "
703 "opened", GST_ELEMENT_NAME (src), pspec->name);
707 g_free (src->location);
708 /* clear the filename if we get a NULL (is that possible?) */
709 if (g_value_get_string (value) == NULL) {
710 src->location = g_strdup ("/dev/dvd");
712 src->location = g_strdup (g_value_get_string (value));
717 src->title = g_value_get_int (value);
718 src->new_seek = TRUE;
721 src->chapter = g_value_get_int (value);
722 src->new_seek = TRUE;
725 src->angle = g_value_get_int (value);
728 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
732 GST_OBJECT_UNLOCK (src);
736 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
739 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
741 GST_OBJECT_LOCK (src);
745 g_value_set_string (value, src->location);
748 g_value_set_int (value, src->title);
751 g_value_set_int (value, src->chapter);
754 g_value_set_int (value, src->angle);
757 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
761 GST_OBJECT_UNLOCK (src);
764 /*** Querying and seeking ***/
767 gst_dvd_read_src_do_seek (GstDvdReadSrc * src, GstEvent * event)
770 GstSeekType cur_type, end_type;
771 gint64 new_off, total, cur;
776 srcpad = GST_BASE_SRC (src)->srcpad;
778 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
781 if (end_type != GST_SEEK_TYPE_NONE) {
782 GST_WARNING_OBJECT (src, "End seek type not supported, will be ignored");
786 case GST_FORMAT_BYTES:{
787 new_off /= DVD_VIDEO_LB_LEN;
788 format = sector_format;
792 if (format != chapter_format &&
793 format != sector_format &&
794 format != angle_format && format != title_format) {
795 GST_DEBUG_OBJECT (src, "Unsupported seek format %d (%s)", format,
796 gst_format_get_name (format));
803 /* get current offset and length */
804 gst_pad_query_duration (srcpad, &format, &total);
805 gst_pad_query_position (srcpad, &format, &cur);
807 GST_DEBUG_OBJECT (src, "Current %s: %" G_GINT64_FORMAT,
808 gst_format_get_name (format), cur);
809 GST_DEBUG_OBJECT (src, "Total %s: %" G_GINT64_FORMAT,
810 gst_format_get_name (format), total);
812 if (cur == new_off) {
813 GST_DEBUG_OBJECT (src, "We're already at that position!");
819 case GST_SEEK_TYPE_SET:
822 case GST_SEEK_TYPE_CUR:
825 case GST_SEEK_TYPE_END:
826 new_off = total - new_off;
829 GST_DEBUG_OBJECT (src, "Unsupported seek method");
833 if (new_off < 0 || new_off >= total) {
834 GST_DEBUG_OBJECT (src, "Invalid seek position %" G_GINT64_FORMAT, new_off);
838 GST_LOG_OBJECT (src, "Seeking to unit %" G_GINT64_FORMAT, new_off);
840 GST_OBJECT_LOCK (src);
842 if (format == angle_format) {
843 src->angle = new_off;
844 GST_OBJECT_UNLOCK (src);
848 if (format == sector_format) {
849 src->cur_pack = new_off;
850 } else if (format == chapter_format) {
852 src->chapter = new_off;
853 src->seek_pend_fmt = format;
854 } else if (format == title_format) {
856 src->title = new_off;
858 src->seek_pend_fmt = format;
860 g_return_val_if_reached (FALSE);
863 /* leave for events */
864 src->seek_pend = TRUE;
865 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
866 src->flush_pend = TRUE;
868 GST_OBJECT_UNLOCK (src);
874 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
876 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
879 GST_DEBUG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
881 switch (GST_EVENT_TYPE (event)) {
883 res = gst_dvd_read_src_do_seek (src, event);
886 res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
894 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
897 GstStructure *structure;
901 structure = gst_structure_new ("application/x-gst-dvd",
902 "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
904 /* Create a separate field for each value in the table. */
905 for (i = 0; i < 16; i++) {
906 g_snprintf (name, sizeof (name), "clut%02d", i);
907 gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
910 /* Create the DVD event and put the structure into it. */
911 return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
915 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
918 const gint64 one_hour = 3600 * GST_SECOND;
919 const gint64 one_min = 60 * GST_SECOND;
921 g_return_val_if_fail ((time->hour >> 4) < 0xa
922 && (time->hour & 0xf) < 0xa, -1);
923 g_return_val_if_fail ((time->minute >> 4) < 0x7
924 && (time->minute & 0xf) < 0xa, -1);
925 g_return_val_if_fail ((time->second >> 4) < 0x7
926 && (time->second & 0xf) < 0xa, -1);
928 ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
929 ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
930 ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
936 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
941 gst_query_parse_duration (query, &format, NULL);
944 case GST_FORMAT_TIME:{
945 if (src->cur_pgc == NULL)
947 val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
952 case GST_FORMAT_BYTES:{
953 val = DVDFileSize (src->dvd_title) * DVD_VIDEO_LB_LEN;
957 if (format == sector_format) {
958 val = DVDFileSize (src->dvd_title);
959 } else if (format == title_format) {
960 val = src->tt_srpt->nr_of_srpts;
961 } else if (format == chapter_format) {
962 val = src->num_chapters;
963 } else if (format == angle_format) {
964 val = src->tt_srpt->title[src->title].nr_of_angles;
966 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
967 format, gst_format_get_name (format));
974 gst_query_set_duration (query, format, val);
979 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
984 gst_query_parse_position (query, &format, NULL);
987 case GST_FORMAT_BYTES:{
988 val = src->cur_pack * DVD_VIDEO_LB_LEN;
992 if (format == sector_format) {
994 } else if (format == title_format) {
996 } else if (format == chapter_format) {
998 } else if (format == angle_format) {
1001 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1002 format, gst_format_get_name (format));
1009 gst_query_set_position (query, format, val);
1014 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1016 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1018 gboolean res = TRUE;
1020 GST_LOG_OBJECT (src, "handling %s query",
1021 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1023 GST_OBJECT_LOCK (src);
1024 started = (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED));
1025 GST_OBJECT_UNLOCK (src);
1028 GST_DEBUG_OBJECT (src, "query failed: not started");
1032 switch (GST_QUERY_TYPE (query)) {
1033 case GST_QUERY_DURATION:
1034 GST_OBJECT_LOCK (src);
1035 res = gst_dvd_read_src_do_duration_query (src, query);
1036 GST_OBJECT_UNLOCK (src);
1038 case GST_QUERY_POSITION:
1039 GST_OBJECT_LOCK (src);
1040 res = gst_dvd_read_src_do_position_query (src, query);
1041 GST_OBJECT_UNLOCK (src);
1044 res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1052 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1054 gint seek_to = src->cur_pack;
1055 gint chapter, sectors, next, cur, i;
1057 /* retrieve position */
1059 for (i = 0; i < src->num_chapters; i++) {
1062 cur_title_get_chapter_bounds (src, i, &c1, &c2);
1064 for (next = cur = c1; cur < c2;) {
1067 src->cur_pgc->cell_playback[cur].last_sector -
1068 src->cur_pgc->cell_playback[cur].first_sector;
1069 if (src->cur_pack + sectors > seek_to) {
1073 src->cur_pack += sectors;
1076 if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1078 next = gst_dvd_read_src_get_next_cell_for (src, cur);
1082 GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1086 /* so chapter $chapter and cell $cur contain our sector
1087 * of interest. Let's go there! */
1088 GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1091 gst_dvd_read_src_goto_chapter (src, chapter);
1092 src->cur_cell = cur;
1093 src->next_cell = next;
1094 src->new_cell = FALSE;
1095 src->cur_pack = seek_to;
1101 /*** URI interface ***/
1104 gst_dvd_read_src_uri_get_type (void)
1110 gst_dvd_read_src_uri_get_protocols (void)
1112 static gchar *protocols[] = { "dvd", NULL };
1117 static const gchar *
1118 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1120 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1122 g_free (src->last_uri);
1124 g_strdup_printf ("dvd://%d,%d,%d", src->title, src->chapter, src->angle);
1126 return src->last_uri;
1130 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1132 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1136 protocol = gst_uri_get_protocol (uri);
1137 ret = (protocol != NULL && g_str_equal (protocol, "dvd"));
1144 /* parse out the new t/c/a and seek to them */
1146 gchar *location = NULL;
1151 location = gst_uri_get_location (uri);
1156 strcur = strs = g_strsplit (location, ",", 0);
1157 while (strcur && *strcur) {
1160 if (!sscanf (*strcur, "%d", &val))
1165 if (val != src->title) {
1167 src->new_seek = TRUE;
1171 if (val != src->chapter) {
1173 src->new_seek = TRUE;
1193 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1195 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1197 iface->get_type = gst_dvd_read_src_uri_get_type;
1198 iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1199 iface->get_uri = gst_dvd_read_src_uri_get_uri;
1200 iface->set_uri = gst_dvd_read_src_uri_set_uri;
1204 gst_dvd_read_src_do_init (GType dvdreadsrc_type)
1206 static const GInterfaceInfo urihandler_info = {
1207 gst_dvd_read_src_uri_handler_init,
1212 g_type_add_interface_static (dvdreadsrc_type, GST_TYPE_URI_HANDLER,
1215 title_format = gst_format_register ("title", "DVD title");
1216 angle_format = gst_format_register ("angle", "DVD angle");
1217 sector_format = gst_format_register ("sector", "DVD sector");
1218 chapter_format = gst_format_register ("chapter", "DVD chapter");
1222 plugin_init (GstPlugin * plugin)
1224 GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1225 "DVD reader element based on dvdreadsrc");
1227 if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,
1228 GST_TYPE_DVD_READ_SRC)) {
1235 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1238 "Access a DVD with dvdread",
1239 plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN)