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;
140 gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
142 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
143 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
144 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
146 gobject_class->finalize = gst_dvd_read_src_finalize;
147 gobject_class->set_property = gst_dvd_read_src_set_property;
148 gobject_class->get_property = gst_dvd_read_src_get_property;
150 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
151 g_param_spec_string ("device", "Device",
152 "DVD device location", NULL, G_PARAM_READWRITE));
153 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TITLE,
154 g_param_spec_int ("title", "title", "title",
155 1, 999, 1, G_PARAM_READWRITE));
156 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHAPTER,
157 g_param_spec_int ("chapter", "chapter", "chapter",
158 1, 999, 1, G_PARAM_READWRITE));
159 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ANGLE,
160 g_param_spec_int ("angle", "angle", "angle",
161 1, 999, 1, G_PARAM_READWRITE));
163 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
164 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
165 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_query);
166 gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_event);
168 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dvd_read_src_create);
172 gst_dvd_read_src_start (GstBaseSrc * basesrc)
174 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
176 g_return_val_if_fail (src->location != NULL, FALSE);
178 GST_DEBUG_OBJECT (src, "Opening DVD '%s'", src->location);
180 src->dvd = DVDOpen (src->location);
181 if (src->dvd == NULL) {
182 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
183 (_("Could not open DVD")),
184 ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
188 /* Load the video manager to find out the information about the titles */
189 GST_DEBUG_OBJECT (src, "Loading VMG info");
191 src->vmg_file = ifoOpen (src->dvd, 0);
192 if (!src->vmg_file) {
193 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
194 (_("Could not open DVD")),
195 ("ifoOpen() failed: %s", g_strerror (errno)));
199 src->tt_srpt = src->vmg_file->tt_srpt;
201 src->title = src->uri_title - 1;
202 src->chapter = src->uri_chapter - 1;
203 src->angle = src->uri_angle - 1;
205 if (!gst_dvd_read_src_goto_title (src, src->title, src->angle)) {
206 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
207 (_("Could not open DVD title %d"), src->uri_title), (NULL));
211 if (!gst_dvd_read_src_goto_chapter (src, src->chapter)) {
212 GST_ERROR_OBJECT (src, "Failed to go to chapter %d of DVD title %d",
213 src->uri_chapter, src->uri_title);
216 src->new_seek = FALSE;
217 src->change_cell = TRUE;
223 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
225 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
228 ifoClose (src->vts_file);
229 src->vts_file = NULL;
232 ifoClose (src->vmg_file);
233 src->vmg_file = NULL;
235 if (src->dvd_title) {
236 DVDCloseFile (src->dvd_title);
237 src->dvd_title = NULL;
243 src->new_cell = TRUE;
244 src->new_seek = TRUE;
245 src->change_cell = FALSE;
248 src->flush_pend = FALSE;
249 src->seek_pend = FALSE;
250 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
251 if (src->title_lang_event_pending) {
252 gst_event_unref (src->title_lang_event_pending);
253 src->title_lang_event_pending = NULL;
255 if (src->pending_clut_event) {
256 gst_event_unref (src->pending_clut_event);
257 src->pending_clut_event = NULL;
260 GST_LOG_OBJECT (src, "closed DVD");
266 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
267 gint * p_pgc_id, pgc_t ** p_pgc)
272 g_assert (chapter >= 0 && chapter < src->num_chapters);
274 pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
275 pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
276 pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
284 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
285 gint * p_first_cell, gint * p_last_cell)
288 gint pgn, pgc_id, pgn_next_ch;
290 g_assert (chapter >= 0 && chapter < src->num_chapters);
292 cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
294 *p_first_cell = pgc->program_map[pgn - 1] - 1;
296 if (chapter == (src->num_chapters - 1)) {
297 *p_last_cell = pgc->nr_of_cells;
299 pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
300 *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
305 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
309 /* make sure the chapter number is valid for this title */
310 if (chapter < 0 || chapter >= src->num_chapters) {
311 GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
312 chapter, src->num_chapters);
313 chapter = CLAMP (chapter, 0, src->num_chapters - 1);
316 /* determine which program chain we want to watch. This is
317 * based on the chapter number */
318 cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
320 cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
323 GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter,
324 src->start_cell, src->last_cell);
326 /* retrieve position */
328 for (i = 0; i < chapter; i++) {
331 cur_title_get_chapter_bounds (src, i, &c1, &c2);
335 src->cur_pgc->cell_playback[c1].last_sector -
336 src->cur_pgc->cell_playback[c1].first_sector;
341 /* prepare reading for new cell */
342 src->new_cell = TRUE;
343 src->next_cell = src->start_cell;
345 src->chapter = chapter;
347 if (src->pending_clut_event)
348 gst_event_unref (src->pending_clut_event);
350 src->pending_clut_event =
351 gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
357 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
360 gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
366 /* make sure our title number is valid */
367 num_titles = src->tt_srpt->nr_of_srpts;
368 GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
369 if (title < 0 || title >= num_titles) {
370 GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
375 src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
376 GST_INFO_OBJECT (src, "Title %d has %d chapters", title, src->num_chapters);
378 /* make sure the angle number is valid for this title */
379 num_angles = src->tt_srpt->title[title].nr_of_angles;
380 GST_LOG_OBJECT (src, "Title %d has %d angles", title, num_angles);
381 if (angle < 0 || angle >= num_angles) {
382 GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
384 angle = CLAMP (angle, 0, num_angles - 1);
387 /* load the VTS information for the title set our title is in */
388 title_set_nr = src->tt_srpt->title[title].title_set_nr;
389 src->vts_file = ifoOpen (src->dvd, title_set_nr);
390 if (src->vts_file == NULL) {
391 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
392 (_("Could not open DVD title %d"), title_set_nr),
393 ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
397 src->ttn = src->tt_srpt->title[title].vts_ttn;
398 src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
400 /* we've got enough info, time to open the title set data */
401 src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
402 if (src->dvd_title == NULL) {
403 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
404 (_("Could not open DVD title %d"), title_set_nr),
405 ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
409 GST_INFO_OBJECT (src, "Opened title %d, angle %d", title, angle);
415 if (src->title_lang_event_pending) {
416 gst_event_unref (src->title_lang_event_pending);
417 src->title_lang_event_pending = NULL;
420 s = gst_structure_new ("application/x-gst-event",
421 "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
424 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
425 const audio_attr_t *a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
427 t = g_strdup_printf ("audio-%d-format", i);
428 gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
432 t = g_strdup_printf ("audio-%d-language", i);
433 lang_code[0] = (a->lang_code >> 8) & 0xff;
434 lang_code[1] = a->lang_code & 0xff;
435 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
441 GST_INFO_OBJECT (src, "[%02d] Audio %02d: lang='%s', format=%d",
442 src->title, i, lang_code, (gint) a->audio_format);
446 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
447 const subp_attr_t *u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
450 t = g_strdup_printf ("subtitle-%d-language", i);
451 lang_code[0] = (u->lang_code >> 8) & 0xff;
452 lang_code[1] = u->lang_code & 0xff;
453 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
459 GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', format=%d",
460 src->title, i, lang_code);
463 src->title_lang_event_pending =
464 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
469 /* FIXME: double-check this function, compare against original */
471 gst_dvd_read_src_get_next_cell_for (GstDvdReadSrc * src, gint cell)
473 /* Check if we're entering an angle block. */
474 if (src->cur_pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
477 while (src->cur_pgc->cell_playback[cell].block_mode == BLOCK_MODE_LAST_CELL)
480 return cell + 1; /* really +1? (tpm) */
483 /* Returns true if the pack is a NAV pack. This check is clearly insufficient,
484 * and sometimes we incorrectly think that valid other packs are NAV packs. I
485 * need to make this stronger. */
487 gst_dvd_read_src_is_nav_pack (const guint8 * buffer)
489 return (buffer[41] == 0xbf && buffer[1027] == 0xbf);
495 GST_DVD_READ_ERROR = -1,
496 GST_DVD_READ_EOS = -2,
497 GST_DVD_READ_AGAIN = -3
500 static GstDvdReadReturn
501 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
505 guint8 oneblock[DVD_VIDEO_LB_LEN];
507 guint next_vobu, next_ilvu_start, cur_output_size;
510 /* playback by cell in this pgc, starting at the cell for our chapter */
512 src->cur_cell = src->start_cell;
516 if (src->cur_cell >= src->last_cell) {
517 /* advance to next chapter */
518 if (src->chapter == (src->num_chapters - 1)) {
519 GST_INFO_OBJECT (src, "last chapter done - EOS");
520 return GST_DVD_READ_EOS;
523 GST_INFO_OBJECT (src, "end of chapter %d, switch to next", src->chapter);
526 gst_dvd_read_src_goto_chapter (src, src->chapter);
528 return GST_DVD_READ_AGAIN;
531 if (src->new_cell || new_seek) {
533 src->cur_cell = src->next_cell;
534 if (src->cur_cell >= src->last_cell) {
535 GST_LOG_OBJECT (src, "last cell in chapter");
540 /* take angle into account */
541 if (src->cur_pgc->cell_playback[src->cur_cell].block_type
542 == BLOCK_TYPE_ANGLE_BLOCK)
543 src->cur_cell += angle;
545 /* calculate next cell */
546 src->next_cell = gst_dvd_read_src_get_next_cell_for (src, src->cur_cell);
548 /* we loop until we're out of this cell */
549 src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
550 src->new_cell = FALSE;
553 if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
554 src->new_cell = TRUE;
555 GST_LOG_OBJECT (src, "Beyond last sector, go to next cell");
556 return GST_DVD_READ_AGAIN;
559 /* read NAV packet */
562 len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, oneblock);
564 GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
565 return GST_DVD_READ_ERROR;
568 if (!gst_dvd_read_src_is_nav_pack (oneblock)) {
573 /* parse the contained dsi packet */
574 navRead_DSI (&dsi_pack, &oneblock[DSI_START_BYTE]);
575 g_assert (src->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn);
577 /* determine where we go next. These values are the ones we
578 * mostly care about */
579 next_ilvu_start = src->cur_pack + dsi_pack.sml_agli.data[angle].address;
580 cur_output_size = dsi_pack.dsi_gi.vobu_ea;
582 /* If we're not at the end of this cell, we can determine the next
583 * VOBU to display using the VOBU_SRI information section of the
584 * DSI. Using this value correctly follows the current angle,
585 * avoiding the doubled scenes in The Matrix, and makes our life
588 * Otherwise, we set our next address past the end of this cell to
589 * force the code above to go to the next cell in the program. */
590 if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
591 next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
593 next_vobu = src->cur_pack + cur_output_size + 1;
596 g_assert (cur_output_size < 1024);
599 /* create the buffer (TODO: use buffer pool?) */
600 buf = gst_buffer_new_and_alloc (cur_output_size * DVD_VIDEO_LB_LEN);
602 /* read in and output cursize packs */
603 len = DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size,
604 GST_BUFFER_DATA (buf));
606 if (len != cur_output_size) {
607 GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
608 cur_output_size, src->cur_pack);
609 gst_buffer_unref (buf);
610 return GST_DVD_READ_ERROR;
613 GST_BUFFER_SIZE (buf) = cur_output_size * DVD_VIDEO_LB_LEN;
614 /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
618 src->cur_pack = next_vobu;
620 GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
621 return GST_DVD_READ_OK;
625 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
627 GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
631 g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
633 srcpad = GST_BASE_SRC (src)->srcpad;
635 /* handle events, if any */
636 if (src->seek_pend) {
637 if (src->flush_pend) {
638 gst_pad_push_event (srcpad, gst_event_new_flush_start ());
639 gst_pad_push_event (srcpad, gst_event_new_flush_stop ());
640 src->flush_pend = FALSE;
643 if (src->seek_pend_fmt != GST_FORMAT_UNDEFINED) {
644 if (src->seek_pend_fmt == title_format) {
645 gst_dvd_read_src_goto_title (src, src->title, src->angle);
647 gst_dvd_read_src_goto_chapter (src, src->chapter);
649 src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
651 if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
652 GST_DEBUG_OBJECT (src, "seek to sector failed, going EOS");
653 gst_pad_push_event (srcpad, gst_event_new_eos ());
654 return GST_FLOW_UNEXPECTED;
658 gst_pad_push_event (srcpad,
659 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
660 src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
662 src->seek_pend = FALSE;
666 gst_dvd_read_src_goto_title (src, src->title, src->angle);
667 gst_dvd_read_src_goto_chapter (src, src->chapter);
669 src->new_seek = FALSE;
670 src->change_cell = TRUE;
673 if (src->title_lang_event_pending) {
674 gst_pad_push_event (srcpad, src->title_lang_event_pending);
675 src->title_lang_event_pending = NULL;
678 if (src->pending_clut_event) {
679 gst_pad_push_event (srcpad, src->pending_clut_event);
680 src->pending_clut_event = NULL;
685 res = gst_dvd_read_src_read (src, src->angle, src->change_cell, p_buf);
686 } while (res == GST_DVD_READ_AGAIN);
689 case GST_DVD_READ_ERROR:{
690 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), (NULL));
691 return GST_FLOW_ERROR;
693 case GST_DVD_READ_EOS:{
694 GST_INFO_OBJECT (src, "Reached EOS");
695 gst_pad_push_event (GST_BASE_SRC (src)->srcpad, gst_event_new_eos ());
696 return GST_FLOW_UNEXPECTED;
698 case GST_DVD_READ_OK:{
699 src->change_cell = FALSE;
706 g_return_val_if_reached (GST_FLOW_UNEXPECTED);
710 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
711 const GValue * value, GParamSpec * pspec)
713 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
716 GST_OBJECT_LOCK (src);
717 started = GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED);
722 g_warning ("%s: property '%s' needs to be set before the device is "
723 "opened", GST_ELEMENT_NAME (src), pspec->name);
727 g_free (src->location);
728 /* clear the filename if we get a NULL (is that possible?) */
729 if (g_value_get_string (value) == NULL) {
730 src->location = g_strdup ("/dev/dvd");
732 src->location = g_strdup (g_value_get_string (value));
737 src->uri_title = g_value_get_int (value);
739 src->title = src->uri_title - 1;
740 src->new_seek = TRUE;
744 src->uri_chapter = g_value_get_int (value);
746 src->chapter = src->uri_chapter - 1;
747 src->new_seek = TRUE;
751 src->uri_angle = g_value_get_int (value);
753 src->angle = src->uri_angle - 1;
757 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
761 GST_OBJECT_UNLOCK (src);
765 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
768 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
770 GST_OBJECT_LOCK (src);
774 g_value_set_string (value, src->location);
777 g_value_set_int (value, src->uri_title);
780 g_value_set_int (value, src->uri_chapter);
783 g_value_set_int (value, src->uri_angle);
786 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
790 GST_OBJECT_UNLOCK (src);
794 gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size)
796 gboolean ret = FALSE;
798 if (src->dvd_title) {
801 blocks = DVDFileSize (src->dvd_title);
803 *size = (gint64) blocks *DVD_VIDEO_LB_LEN;
807 GST_WARNING_OBJECT (src, "DVDFileSize(%p) failed!", src->dvd_title);
814 /*** Querying and seeking ***/
817 gst_dvd_read_src_do_seek (GstDvdReadSrc * src, GstEvent * event)
820 GstSeekType cur_type, end_type;
821 gint64 new_off, total, cur;
827 srcpad = GST_BASE_SRC (src)->srcpad;
829 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
832 if (end_type != GST_SEEK_TYPE_NONE) {
833 GST_WARNING_OBJECT (src, "End seek type not supported, will be ignored");
837 case GST_FORMAT_BYTES:
840 if (format != chapter_format &&
841 format != sector_format &&
842 format != angle_format && format != title_format) {
843 GST_DEBUG_OBJECT (src, "Unsupported seek format %d (%s)", format,
844 gst_format_get_name (format));
851 /* get current offset and length */
852 if (format == GST_FORMAT_BYTES) {
853 GST_OBJECT_LOCK (src);
854 query_ok = gst_dvd_read_src_get_size (src, &total);
855 cur = (gint64) src->cur_pack * DVD_VIDEO_LB_LEN;
856 GST_OBJECT_UNLOCK (src);
858 query_ok = gst_pad_query_duration (srcpad, &format, &total)
859 && gst_pad_query_position (srcpad, &format, &cur);
863 GST_DEBUG_OBJECT (src, "Failed to query duration/position");
867 GST_DEBUG_OBJECT (src, "Current %s: %12" G_GINT64_FORMAT,
868 gst_format_get_name (format), cur);
869 GST_DEBUG_OBJECT (src, "Total %s: %12" G_GINT64_FORMAT,
870 gst_format_get_name (format), total);
874 case GST_SEEK_TYPE_SET:
877 case GST_SEEK_TYPE_CUR:
880 case GST_SEEK_TYPE_END:
881 new_off = total - new_off;
884 GST_DEBUG_OBJECT (src, "Unsupported seek method");
888 if (new_off < 0 || new_off >= total) {
889 GST_DEBUG_OBJECT (src, "Invalid seek position %" G_GINT64_FORMAT, new_off);
893 if (cur == new_off) {
894 GST_DEBUG_OBJECT (src, "We're already at that position!");
898 GST_LOG_OBJECT (src, "Seeking to %s: %12" G_GINT64_FORMAT,
899 gst_format_get_name (format), new_off);
901 GST_OBJECT_LOCK (src);
903 if (format == angle_format) {
904 src->angle = new_off;
908 if (format == sector_format) {
909 src->cur_pack = new_off;
910 } else if (format == GST_FORMAT_BYTES) {
911 src->cur_pack = new_off / DVD_VIDEO_LB_LEN;
912 if ((src->cur_pack * DVD_VIDEO_LB_LEN) != new_off) {
913 GST_LOG_OBJECT (src, "rounded down offset %" G_GINT64_FORMAT " => %"
914 G_GINT64_FORMAT, new_off, (gint64) src->cur_pack * DVD_VIDEO_LB_LEN);
916 } else if (format == chapter_format) {
918 src->chapter = new_off;
919 src->seek_pend_fmt = format;
920 } else if (format == title_format) {
922 src->title = new_off;
924 src->seek_pend_fmt = format;
926 GST_OBJECT_UNLOCK (src);
927 g_return_val_if_reached (FALSE);
930 /* leave for events */
931 src->seek_pend = TRUE;
932 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
933 src->flush_pend = TRUE;
937 GST_OBJECT_UNLOCK (src);
943 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
945 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
948 GST_LOG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
950 switch (GST_EVENT_TYPE (event)) {
952 res = gst_dvd_read_src_do_seek (src, event);
955 res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
963 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
966 GstStructure *structure;
970 structure = gst_structure_new ("application/x-gst-dvd",
971 "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
973 /* Create a separate field for each value in the table. */
974 for (i = 0; i < 16; i++) {
975 g_snprintf (name, sizeof (name), "clut%02d", i);
976 gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
979 /* Create the DVD event and put the structure into it. */
980 return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
984 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
987 const gint64 one_hour = 3600 * GST_SECOND;
988 const gint64 one_min = 60 * GST_SECOND;
990 g_return_val_if_fail ((time->hour >> 4) < 0xa
991 && (time->hour & 0xf) < 0xa, -1);
992 g_return_val_if_fail ((time->minute >> 4) < 0x7
993 && (time->minute & 0xf) < 0xa, -1);
994 g_return_val_if_fail ((time->second >> 4) < 0x7
995 && (time->second & 0xf) < 0xa, -1);
997 ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
998 ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
999 ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
1005 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
1010 gst_query_parse_duration (query, &format, NULL);
1013 case GST_FORMAT_TIME:{
1014 if (src->cur_pgc == NULL)
1016 val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
1021 case GST_FORMAT_BYTES:{
1022 if (!gst_dvd_read_src_get_size (src, &val))
1027 if (format == sector_format) {
1028 val = DVDFileSize (src->dvd_title);
1029 } else if (format == title_format) {
1030 val = src->tt_srpt->nr_of_srpts;
1031 } else if (format == chapter_format) {
1032 val = src->num_chapters;
1033 } else if (format == angle_format) {
1034 val = src->tt_srpt->title[src->title].nr_of_angles;
1036 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1037 format, gst_format_get_name (format));
1044 GST_LOG_OBJECT (src, "duration = %" G_GINT64_FORMAT " %s", val,
1045 gst_format_get_name (format));
1047 gst_query_set_duration (query, format, val);
1052 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
1057 gst_query_parse_position (query, &format, NULL);
1060 case GST_FORMAT_BYTES:{
1061 val = src->cur_pack * DVD_VIDEO_LB_LEN;
1065 if (format == sector_format) {
1066 val = src->cur_pack;
1067 } else if (format == title_format) {
1069 } else if (format == chapter_format) {
1071 } else if (format == angle_format) {
1074 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1075 format, gst_format_get_name (format));
1082 GST_LOG_OBJECT (src, "position = %" G_GINT64_FORMAT " %s", val,
1083 gst_format_get_name (format));
1085 gst_query_set_position (query, format, val);
1090 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1092 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1094 gboolean res = TRUE;
1096 GST_LOG_OBJECT (src, "handling %s query",
1097 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1099 GST_OBJECT_LOCK (src);
1100 started = (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED));
1101 GST_OBJECT_UNLOCK (src);
1104 GST_DEBUG_OBJECT (src, "query failed: not started");
1108 switch (GST_QUERY_TYPE (query)) {
1109 case GST_QUERY_DURATION:
1110 GST_OBJECT_LOCK (src);
1111 res = gst_dvd_read_src_do_duration_query (src, query);
1112 GST_OBJECT_UNLOCK (src);
1114 case GST_QUERY_POSITION:
1115 GST_OBJECT_LOCK (src);
1116 res = gst_dvd_read_src_do_position_query (src, query);
1117 GST_OBJECT_UNLOCK (src);
1120 res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1128 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1130 gint seek_to = src->cur_pack;
1131 gint chapter, sectors, next, cur, i;
1133 /* retrieve position */
1135 for (i = 0; i < src->num_chapters; i++) {
1138 cur_title_get_chapter_bounds (src, i, &c1, &c2);
1140 for (next = cur = c1; cur < c2;) {
1143 src->cur_pgc->cell_playback[cur].last_sector -
1144 src->cur_pgc->cell_playback[cur].first_sector;
1145 if (src->cur_pack + sectors > seek_to) {
1149 src->cur_pack += sectors;
1152 if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1154 next = gst_dvd_read_src_get_next_cell_for (src, cur);
1158 GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1162 /* so chapter $chapter and cell $cur contain our sector
1163 * of interest. Let's go there! */
1164 GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1167 gst_dvd_read_src_goto_chapter (src, chapter);
1168 src->cur_cell = cur;
1169 src->next_cell = next;
1170 src->new_cell = FALSE;
1171 src->cur_pack = seek_to;
1177 /*** URI interface ***/
1180 gst_dvd_read_src_uri_get_type (void)
1186 gst_dvd_read_src_uri_get_protocols (void)
1188 static gchar *protocols[] = { "dvd", NULL };
1193 static const gchar *
1194 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1196 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1198 GST_OBJECT_LOCK (src);
1200 g_free (src->last_uri);
1201 src->last_uri = g_strdup_printf ("dvd://%d,%d,%d", src->uri_title,
1202 src->uri_chapter, src->uri_angle);
1204 GST_OBJECT_UNLOCK (src);
1206 return src->last_uri;
1210 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1212 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1216 protocol = gst_uri_get_protocol (uri);
1217 ret = (protocol != NULL && g_str_equal (protocol, "dvd"));
1224 /* parse out the new t/c/a and seek to them */
1226 gchar *location = NULL;
1231 location = gst_uri_get_location (uri);
1236 GST_OBJECT_LOCK (src);
1239 src->uri_chapter = 1;
1242 strcur = strs = g_strsplit (location, ",", 0);
1243 while (strcur && *strcur) {
1246 if (!sscanf (*strcur, "%d", &val))
1250 g_warning ("Invalid value %d in URI '%s'. Must be 1 or greater",
1257 src->uri_title = val;
1260 src->uri_chapter = val;
1263 src->uri_angle = val;
1271 if (pos > 0 && GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1272 src->title = src->uri_title - 1;
1273 src->chapter = src->uri_chapter - 1;
1274 src->angle = src->uri_angle - 1;
1275 src->new_seek = TRUE;
1278 GST_OBJECT_UNLOCK (src);
1288 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1290 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1292 iface->get_type = gst_dvd_read_src_uri_get_type;
1293 iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1294 iface->get_uri = gst_dvd_read_src_uri_get_uri;
1295 iface->set_uri = gst_dvd_read_src_uri_set_uri;
1299 gst_dvd_read_src_do_init (GType dvdreadsrc_type)
1301 static const GInterfaceInfo urihandler_info = {
1302 gst_dvd_read_src_uri_handler_init,
1307 g_type_add_interface_static (dvdreadsrc_type, GST_TYPE_URI_HANDLER,
1310 title_format = gst_format_register ("title", "DVD title");
1311 angle_format = gst_format_register ("angle", "DVD angle");
1312 sector_format = gst_format_register ("sector", "DVD sector");
1313 chapter_format = gst_format_register ("chapter", "DVD chapter");
1317 plugin_init (GstPlugin * plugin)
1319 GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1320 "DVD reader element based on dvdreadsrc");
1322 if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,
1323 GST_TYPE_DVD_READ_SRC)) {
1330 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1333 "Access a DVD with dvdread",
1334 plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN)