]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugins-ugly0-10.git/blob - ext/dvdread/dvdreadsrc.c
dvdreadsrc: fix off by one in cell calculation for the last chapter
[glsdk/gst-plugins-ugly0-10.git] / ext / dvdread / dvdreadsrc.c
1 /* GStreamer DVD title source
2  * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2001 Billy Biggs <vektor@dumbterm.net>.
4  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include "_stdint.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
34 #include "dvdreadsrc.h"
36 #include <gst/gst-i18n-plugin.h>
38 GST_DEBUG_CATEGORY_STATIC (gstgst_dvd_read_src_debug);
39 #define GST_CAT_DEFAULT (gstgst_dvd_read_src_debug)
41 static void gst_dvd_read_src_do_init (GType dvdreadsrc_type);
43 enum
44 {
45   ARG_0,
46   ARG_DEVICE,
47   ARG_TITLE,
48   ARG_CHAPTER,
49   ARG_ANGLE
50 };
52 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("video/mpeg, mpegversion=2, systemstream=(boolean)true"));
57 static GstFormat title_format;
58 static GstFormat angle_format;
59 static GstFormat sector_format;
60 static GstFormat chapter_format;
62 static gboolean gst_dvd_read_src_start (GstBaseSrc * basesrc);
63 static gboolean gst_dvd_read_src_stop (GstBaseSrc * basesrc);
64 static GstFlowReturn gst_dvd_read_src_create (GstPushSrc * pushsrc,
65     GstBuffer ** buf);
66 static gboolean gst_dvd_read_src_src_query (GstBaseSrc * basesrc,
67     GstQuery * query);
68 static gboolean gst_dvd_read_src_src_event (GstBaseSrc * basesrc,
69     GstEvent * event);
70 static gboolean gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title,
71     gint angle);
72 static gboolean gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src,
73     gint chapter);
74 static gboolean gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, gint angle);
75 static void gst_dvd_read_src_set_property (GObject * object, guint prop_id,
76     const GValue * value, GParamSpec * pspec);
77 static void gst_dvd_read_src_get_property (GObject * object, guint prop_id,
78     GValue * value, GParamSpec * pspec);
79 static GstEvent *gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
80     const guint * clut);
81 static gboolean gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size);
82 static gboolean gst_dvd_read_src_do_seek (GstBaseSrc * src, GstSegment * s);
83 static gint64 gst_dvd_read_src_convert_timecode (dvd_time_t * time);
84 static gint gst_dvd_read_src_get_next_cell (GstDvdReadSrc * src,
85     pgc_t * pgc, gint cell);
86 static GstClockTime gst_dvd_read_src_get_time_for_sector (GstDvdReadSrc * src,
87     guint sector);
88 static gint gst_dvd_read_src_get_sector_from_time (GstDvdReadSrc * src,
89     GstClockTime ts);
91 GST_BOILERPLATE_FULL (GstDvdReadSrc, gst_dvd_read_src, GstPushSrc,
92     GST_TYPE_PUSH_SRC, gst_dvd_read_src_do_init);
94 static void
95 gst_dvd_read_src_base_init (gpointer g_class)
96 {
97   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
99   gst_element_class_add_static_pad_template (element_class, &srctemplate);
101   gst_element_class_set_details_simple (element_class, "DVD Source",
102       "Source/File/DVD",
103       "Access a DVD title/chapter/angle using libdvdread",
104       "Erik Walthinsen <omega@cse.ogi.edu>");
107 static void
108 gst_dvd_read_src_finalize (GObject * object)
110   GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
112   g_free (src->location);
113   g_free (src->last_uri);
115   G_OBJECT_CLASS (parent_class)->finalize (object);
118 static void
119 gst_dvd_read_src_init (GstDvdReadSrc * src, GstDvdReadSrcClass * klass)
121   src->dvd = NULL;
122   src->vts_file = NULL;
123   src->vmg_file = NULL;
124   src->dvd_title = NULL;
126   src->location = g_strdup ("/dev/dvd");
127   src->last_uri = NULL;
128   src->new_seek = TRUE;
129   src->new_cell = TRUE;
130   src->change_cell = FALSE;
131   src->uri_title = 1;
132   src->uri_chapter = 1;
133   src->uri_angle = 1;
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));
143 static gboolean
144 gst_dvd_read_src_is_seekable (GstBaseSrc * src)
146   return TRUE;
149 static void
150 gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
152   GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
153   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
154   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
156   gobject_class->finalize = gst_dvd_read_src_finalize;
157   gobject_class->set_property = gst_dvd_read_src_set_property;
158   gobject_class->get_property = gst_dvd_read_src_get_property;
160   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
161       g_param_spec_string ("device", "Device",
162           "DVD device location", NULL,
163           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TITLE,
165       g_param_spec_int ("title", "title", "title",
166           1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHAPTER,
168       g_param_spec_int ("chapter", "chapter", "chapter",
169           1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ANGLE,
171       g_param_spec_int ("angle", "angle", "angle",
172           1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
175   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
176   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_query);
177   gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_event);
178   gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_dvd_read_src_do_seek);
179   gstbasesrc_class->is_seekable =
180       GST_DEBUG_FUNCPTR (gst_dvd_read_src_is_seekable);
182   gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dvd_read_src_create);
185 static gboolean
186 gst_dvd_read_src_start (GstBaseSrc * basesrc)
188   GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
190   g_return_val_if_fail (src->location != NULL, FALSE);
192   GST_DEBUG_OBJECT (src, "Opening DVD '%s'", src->location);
194   if ((src->dvd = DVDOpen (src->location)) == NULL)
195     goto open_failed;
197   /* Load the video manager to find out the information about the titles */
198   GST_DEBUG_OBJECT (src, "Loading VMG info");
200   if (!(src->vmg_file = ifoOpen (src->dvd, 0)))
201     goto ifo_open_failed;
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     goto title_open_failed;
212   if (!gst_dvd_read_src_goto_chapter (src, src->chapter))
213     goto chapter_open_failed;
215   src->new_seek = FALSE;
216   src->change_cell = TRUE;
218   return TRUE;
220   /* ERRORS */
221 open_failed:
222   {
223     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
224         (_("Could not open DVD")),
225         ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
226     return FALSE;
227   }
228 ifo_open_failed:
229   {
230     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
231         (_("Could not open DVD")),
232         ("ifoOpen() failed: %s", g_strerror (errno)));
233     return FALSE;
234   }
235 title_open_failed:
236   {
237     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
238         (_("Could not open DVD title %d"), src->uri_title), (NULL));
239     return FALSE;
240   }
241 chapter_open_failed:
242   {
243     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
244         (_("Failed to go to chapter %d of DVD title %d"),
245             src->uri_chapter, src->uri_title), (NULL));
246     return FALSE;
247   }
250 static gboolean
251 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
253   GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
255   if (src->vts_file) {
256     ifoClose (src->vts_file);
257     src->vts_file = NULL;
258   }
259   if (src->vmg_file) {
260     ifoClose (src->vmg_file);
261     src->vmg_file = NULL;
262   }
263   if (src->dvd_title) {
264     DVDCloseFile (src->dvd_title);
265     src->dvd_title = NULL;
266   }
267   if (src->dvd) {
268     DVDClose (src->dvd);
269     src->dvd = NULL;
270   }
271   src->new_cell = TRUE;
272   src->new_seek = TRUE;
273   src->change_cell = FALSE;
274   src->chapter = 0;
275   src->title = 0;
276   src->need_newsegment = TRUE;
277   src->vts_tmapt = NULL;
278   if (src->title_lang_event_pending) {
279     gst_event_unref (src->title_lang_event_pending);
280     src->title_lang_event_pending = NULL;
281   }
282   if (src->pending_clut_event) {
283     gst_event_unref (src->pending_clut_event);
284     src->pending_clut_event = NULL;
285   }
286   if (src->chapter_starts) {
287     g_free (src->chapter_starts);
288     src->chapter_starts = NULL;
289   }
291   GST_LOG_OBJECT (src, "closed DVD");
293   return TRUE;
296 static void
297 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
298     gint * p_pgc_id, pgc_t ** p_pgc)
300   pgc_t *pgc;
301   gint pgn, pgc_id;
303   g_assert (chapter >= 0 && chapter < src->num_chapters);
305   pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
306   pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
307   pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
309   *p_pgn = pgn;
310   *p_pgc_id = pgc_id;
311   *p_pgc = pgc;
314 static void
315 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
316     gint * p_first_cell, gint * p_last_cell)
318   pgc_t *pgc;
319   gint pgn, pgc_id, pgn_next_ch;
321   g_assert (chapter >= 0 && chapter < src->num_chapters);
323   cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
325   *p_first_cell = pgc->program_map[pgn - 1] - 1;
327   if (chapter == (src->num_chapters - 1)) {
328     *p_last_cell = pgc->nr_of_cells - 1;
329   } else {
330     pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
331     *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
332   }
334   GST_DEBUG_OBJECT (src, "Chapter %d bounds: %d %d (within %d cells)",
335       chapter, *p_first_cell, *p_last_cell, pgc->nr_of_cells);
338 static gboolean
339 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
341   gint i;
343   /* make sure the chapter number is valid for this title */
344   if (chapter < 0 || chapter >= src->num_chapters) {
345     GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
346         chapter, src->num_chapters);
347     chapter = CLAMP (chapter, 0, src->num_chapters - 1);
348   }
350   /* determine which program chain we want to watch. This is
351    * based on the chapter number */
352   cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
353       &src->cur_pgc);
354   cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
355       &src->last_cell);
357   GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter + 1,
358       src->start_cell, src->last_cell);
360   /* retrieve position */
361   src->cur_pack = 0;
362   for (i = 0; i < chapter; i++) {
363     gint c1, c2;
365     cur_title_get_chapter_bounds (src, i, &c1, &c2);
367     while (c1 < c2) {
368       src->cur_pack +=
369           src->cur_pgc->cell_playback[c1].last_sector -
370           src->cur_pgc->cell_playback[c1].first_sector;
371       ++c1;
372     }
373   }
375   /* prepare reading for new cell */
376   src->new_cell = TRUE;
377   src->next_cell = src->start_cell;
379   src->chapter = chapter;
381   if (src->pending_clut_event)
382     gst_event_unref (src->pending_clut_event);
384   src->pending_clut_event =
385       gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
387   return TRUE;
390 static void
391 gst_dvd_read_src_get_chapter_starts (GstDvdReadSrc * src)
393   GstClockTime uptohere;
394   guint c;
396   g_free (src->chapter_starts);
397   src->chapter_starts = g_new (GstClockTime, src->num_chapters);
399   uptohere = (GstClockTime) 0;
400   for (c = 0; c < src->num_chapters; ++c) {
401     GstClockTime chapter_duration = 0;
402     gint cell_start, cell_end, cell;
403     gint pgn, pgc_id;
404     pgc_t *pgc;
406     cur_title_get_chapter_pgc (src, c, &pgn, &pgc_id, &pgc);
407     cur_title_get_chapter_bounds (src, c, &cell_start, &cell_end);
409     cell = cell_start;
410     while (cell < cell_end) {
411       dvd_time_t *cell_duration;
413       cell_duration = &pgc->cell_playback[cell].playback_time;
414       chapter_duration += gst_dvd_read_src_convert_timecode (cell_duration);
415       cell = gst_dvd_read_src_get_next_cell (src, pgc, cell);
416     }
418     src->chapter_starts[c] = uptohere;
420     GST_INFO_OBJECT (src, "[%02u] Chapter %02u starts at %" GST_TIME_FORMAT
421         ", dur = %" GST_TIME_FORMAT ", cells %d-%d", src->title + 1, c + 1,
422         GST_TIME_ARGS (uptohere), GST_TIME_ARGS (chapter_duration),
423         cell_start, cell_end);
425     uptohere += chapter_duration;
426   }
429 static gboolean
430 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
432   GstStructure *s;
433   gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
434   pgc_t *pgc0;
435   gint title_set_nr;
436   gint num_titles;
437   gint pgn0, pgc0_id;
438   gint i;
440   /* make sure our title number is valid */
441   num_titles = src->tt_srpt->nr_of_srpts;
442   GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
443   if (title < 0 || title >= num_titles)
444     goto invalid_title;
446   src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
447   GST_INFO_OBJECT (src, "Title %d has %d chapters", title + 1,
448       src->num_chapters);
450   /* make sure the angle number is valid for this title */
451   src->num_angles = src->tt_srpt->title[title].nr_of_angles;
452   GST_LOG_OBJECT (src, "Title %d has %d angles", title + 1, src->num_angles);
453   if (angle < 0 || angle >= src->num_angles) {
454     GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
455         angle, src->num_angles);
456     angle = CLAMP (angle, 0, src->num_angles - 1);
457   }
459   /* load the VTS information for the title set our title is in */
460   title_set_nr = src->tt_srpt->title[title].title_set_nr;
461   src->vts_file = ifoOpen (src->dvd, title_set_nr);
462   if (src->vts_file == NULL)
463     goto ifo_open_failed;
465   src->ttn = src->tt_srpt->title[title].vts_ttn;
466   src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
468   /* interactive title? */
469   if (src->num_chapters > 0 &&
470       src->vts_ptt_srpt->title[src->ttn - 1].ptt[0].pgn == 0) {
471     goto commands_only_pgc;
472   }
474   /* we've got enough info, time to open the title set data */
475   src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
476   if (src->dvd_title == NULL)
477     goto title_open_failed;
479   GST_INFO_OBJECT (src, "Opened title %d, angle %d", title + 1, angle);
480   src->title = title;
481   src->angle = angle;
483   /* build event */
485   if (src->title_lang_event_pending) {
486     gst_event_unref (src->title_lang_event_pending);
487     src->title_lang_event_pending = NULL;
488   }
490   s = gst_structure_new ("application/x-gst-dvd",
491       "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
493   /* so we can filter out invalid/unused streams (same for all chapters) */
494   cur_title_get_chapter_pgc (src, 0, &pgn0, &pgc0_id, &pgc0);
496   /* audio */
497   for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
498     const audio_attr_t *a;
500     /* audio stream present? */
501     if (pgc0 != NULL && (pgc0->audio_control[i] & 0x8000) == 0)
502       continue;
504     a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
506     t = g_strdup_printf ("audio-%d-format", i);
507     gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
508     g_free (t);
510     if (a->lang_type) {
511       t = g_strdup_printf ("audio-%d-language", i);
512       lang_code[0] = (a->lang_code >> 8) & 0xff;
513       lang_code[1] = a->lang_code & 0xff;
514       gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
515       g_free (t);
516     } else {
517       lang_code[0] = '\0';
518     }
520     GST_INFO_OBJECT (src, "[%02d] Audio    %02d: lang='%s', format=%d",
521         src->title + 1, i, lang_code, (gint) a->audio_format);
522   }
524   /* subtitle */
525   for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
526     const subp_attr_t *u;
527     const video_attr_t *v;
528     gint sid;
530     /* subpicture stream present? */
531     if (pgc0 != NULL && (pgc0->subp_control[i] & 0x80000000) == 0)
532       continue;
534     u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
535     v = &src->vts_file->vtsi_mat->vts_video_attr;
537     sid = i;
538     if (pgc0 != NULL) {
539       if (v->display_aspect_ratio == 0)         /* 4:3 */
540         sid = (pgc0->subp_control[i] >> 24) & 0x1f;
541       else if (v->display_aspect_ratio == 3)    /* 16:9 */
542         sid = (pgc0->subp_control[i] >> 8) & 0x1f;
543     }
545     if (u->type) {
546       t = g_strdup_printf ("subtitle-%d-language", sid);
547       lang_code[0] = (u->lang_code >> 8) & 0xff;
548       lang_code[1] = u->lang_code & 0xff;
549       gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
550       g_free (t);
551     } else {
552       lang_code[0] = '\0';
553     }
555     GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', type=%d",
556         src->title + 1, sid, lang_code, u->type);
557   }
559   src->title_lang_event_pending =
560       gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
562   /* dump seek tables */
563   src->vts_tmapt = src->vts_file->vts_tmapt;
564   if (src->vts_tmapt) {
565     gint i, j;
567     GST_LOG_OBJECT (src, "nr_of_tmaps = %d", src->vts_tmapt->nr_of_tmaps);
568     for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
569       GST_LOG_OBJECT (src, "======= Table %d ===================", i);
570       GST_LOG_OBJECT (src, "Offset relative to VTS_TMAPTI: %d",
571           src->vts_tmapt->tmap_offset[i]);
572       GST_LOG_OBJECT (src, "Time unit (seconds)          : %d",
573           src->vts_tmapt->tmap[i].tmu);
574       GST_LOG_OBJECT (src, "Number of entries            : %d",
575           src->vts_tmapt->tmap[i].nr_of_entries);
576       for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; j++) {
577         guint64 time;
579         time = src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
580         GST_LOG_OBJECT (src, "Time: %" GST_TIME_FORMAT " VOBU "
581             "Sector: 0x%08x %s", GST_TIME_ARGS (time),
582             src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff,
583             (src->vts_tmapt->tmap[i].map_ent[j] >> 31) ? "discontinuity" : "");
584       }
585     }
586   } else {
587     GST_WARNING_OBJECT (src, "no vts_tmapt - seeking will suck");
588   }
590   gst_dvd_read_src_get_chapter_starts (src);
592   return TRUE;
594   /* ERRORS */
595 invalid_title:
596   {
597     GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
598         title, num_titles);
599     return FALSE;
600   }
601 ifo_open_failed:
602   {
603     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
604         (_("Could not open DVD title %d"), title_set_nr),
605         ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
606     return FALSE;
607   }
608 title_open_failed:
609   {
610     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
611         (_("Could not open DVD title %d"), title_set_nr),
612         ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
613     return FALSE;
614   }
615 commands_only_pgc:
616   {
617     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
618         (_("Could not open DVD title %d. Interactive titles are not supported "
619                 "by this element"), title_set_nr),
620         ("Commands-only PGC, not supported, use rsndvdbin"));
621     return FALSE;
622   }
625 /* FIXME: double-check this function, compare against original */
626 static gint
627 gst_dvd_read_src_get_next_cell (GstDvdReadSrc * src, pgc_t * pgc, gint cell)
629   /* Check if we're entering an angle block. */
630   if (pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
631     return (cell + 1);
633   while (pgc->cell_playback[cell].block_mode != BLOCK_MODE_LAST_CELL)
634     ++cell;
636   return cell + 1;
639 /* Returns true if the pack is a NAV pack */
640 static gboolean
641 gst_dvd_read_src_is_nav_pack (const guint8 * data, gint lbn, dsi_t * dsi_pack)
643   if (GST_READ_UINT32_BE (data + 0x26) != 0x000001BF)
644     return FALSE;
646   /* Check that this is substream 0 (PCI) */
647   if (data[0x2c] != 0)
648     return FALSE;
650   if (GST_READ_UINT32_BE (data + 0x400) != 0x000001BF)
651     return FALSE;
653   /* Check that this is substream 1 (DSI) */
654   if (data[0x406] != 1)
655     return FALSE;
657   /* Check sizes of PCI and DSI packets */
658   if (GST_READ_UINT16_BE (data + 0x2a) != 0x03d4)
659     return FALSE;
661   if (GST_READ_UINT16_BE (data + 0x404) != 0x03fa)
662     return FALSE;
664   /* Read the DSI packet into the provided struct and check it */
665   navRead_DSI (dsi_pack, (unsigned char *) data + DSI_START_BYTE);
666   if (lbn != dsi_pack->dsi_gi.nv_pck_lbn)
667     return FALSE;
669   return TRUE;
672 /* find time for sector from index, returns NONE if there is no exact match */
673 static GstClockTime
674 gst_dvd_read_src_get_time_for_sector (GstDvdReadSrc * src, guint sector)
676   gint i, j;
678   if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps == 0)
679     return GST_CLOCK_TIME_NONE;
681   for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
682     for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; ++j) {
683       if ((src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff) == sector)
684         return src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
685     }
686   }
688   if (sector == 0)
689     return (GstClockTime) 0;
691   return GST_CLOCK_TIME_NONE;
694 /* returns the sector in the index at (or before) the given time, or -1 */
695 static gint
696 gst_dvd_read_src_get_sector_from_time (GstDvdReadSrc * src, GstClockTime ts)
698   gint sector, j;
700   if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps < src->ttn)
701     return -1;
703   sector = 0;
704   for (j = 0; j < src->vts_tmapt->tmap[src->ttn - 1].nr_of_entries; ++j) {
705     GstClockTime entry_time;
707     entry_time = src->vts_tmapt->tmap[src->ttn - 1].tmu * (j + 1) * GST_SECOND;
708     if (entry_time <= ts) {
709       sector = src->vts_tmapt->tmap[src->ttn - 1].map_ent[j] & 0x7fffffff;
710     }
711     if (entry_time >= ts) {
712       return sector;
713     }
714   }
716   if (ts == 0)
717     return 0;
719   return -1;
722 typedef enum
724   GST_DVD_READ_OK = 0,
725   GST_DVD_READ_ERROR = -1,
726   GST_DVD_READ_EOS = -2,
727   GST_DVD_READ_AGAIN = -3
728 } GstDvdReadReturn;
730 static GstDvdReadReturn
731 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
732     GstBuffer ** p_buf)
734   GstBuffer *buf;
735   GstSegment *seg;
736   guint8 oneblock[DVD_VIDEO_LB_LEN];
737   dsi_t dsi_pack;
738   guint next_vobu, cur_output_size;
739   gint len;
740   gint retries;
741   gint64 next_time;
743   seg = &(GST_BASE_SRC (src)->segment);
745   /* playback by cell in this pgc, starting at the cell for our chapter */
746   if (new_seek)
747     src->cur_cell = src->start_cell;
749 again:
751   if (src->cur_cell >= src->last_cell) {
752     /* advance to next chapter */
753     if (src->chapter == (src->num_chapters - 1) ||
754         (seg->format == chapter_format && seg->stop != -1 &&
755             src->chapter == (seg->stop - 1))) {
756       GST_DEBUG_OBJECT (src, "end of chapter segment");
757       goto eos;
758     }
760     GST_INFO_OBJECT (src, "end of chapter %d, switch to next",
761         src->chapter + 1);
763     ++src->chapter;
764     gst_dvd_read_src_goto_chapter (src, src->chapter);
766     return GST_DVD_READ_AGAIN;
767   }
769   if (src->new_cell || new_seek) {
770     if (!new_seek) {
771       src->cur_cell = src->next_cell;
772       if (src->cur_cell >= src->last_cell) {
773         GST_LOG_OBJECT (src, "last cell in chapter");
774         goto again;
775       }
776     }
778     /* take angle into account */
779     if (src->cur_pgc->cell_playback[src->cur_cell].block_type
780         == BLOCK_TYPE_ANGLE_BLOCK)
781       src->cur_cell += angle;
783     /* calculate next cell */
784     src->next_cell =
785         gst_dvd_read_src_get_next_cell (src, src->cur_pgc, src->cur_cell);
787     /* we loop until we're out of this cell */
788     src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
789     src->new_cell = FALSE;
790     GST_DEBUG_OBJECT (src, "Starting new cell %d @ pack %d", src->cur_cell,
791         src->cur_pack);
792   }
794   if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
795     src->new_cell = TRUE;
796     GST_LOG_OBJECT (src, "Beyond last sector for cell %d, going to next cell",
797         src->cur_cell);
798     return GST_DVD_READ_AGAIN;
799   }
801   /* read NAV packet */
802   retries = 0;
803 nav_retry:
804   retries++;
806   len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, oneblock);
807   if (len != 1)
808     goto read_error;
810   if (!gst_dvd_read_src_is_nav_pack (oneblock, src->cur_pack, &dsi_pack)) {
811     GST_LOG_OBJECT (src, "Skipping nav packet @ pack %d", src->cur_pack);
812     src->cur_pack++;
814     if (retries < 2000) {
815       goto nav_retry;
816     } else {
817       GST_LOG_OBJECT (src, "No nav packet @ pack %d after 2000 blocks",
818           src->cur_pack);
819       goto read_error;
820     }
821   }
823   /* determine where we go next. These values are the ones we
824    * mostly care about */
825   cur_output_size = dsi_pack.dsi_gi.vobu_ea + 1;
827   /* If we're not at the end of this cell, we can determine the next
828    * VOBU to display using the VOBU_SRI information section of the
829    * DSI.  Using this value correctly follows the current angle,
830    * avoiding the doubled scenes in The Matrix, and makes our life
831    * really happy.
832    *
833    * Otherwise, we set our next address past the end of this cell to
834    * force the code above to go to the next cell in the program. */
835   if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
836     next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
837   } else {
838     next_vobu = src->cur_pgc->cell_playback[src->cur_cell].last_sector + 1;
839   }
841   g_assert (cur_output_size < 1024);
843   /* create the buffer (TODO: use buffer pool?) */
844   buf = gst_buffer_new_and_alloc (cur_output_size * DVD_VIDEO_LB_LEN);
846   GST_LOG_OBJECT (src, "Going to read %u sectors @ pack %d", cur_output_size,
847       src->cur_pack);
849   /* read in and output cursize packs */
850   len = DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size,
851       GST_BUFFER_DATA (buf));
853   if (len != cur_output_size)
854     goto block_read_error;
856   GST_BUFFER_SIZE (buf) = cur_output_size * DVD_VIDEO_LB_LEN;
857   /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
858   GST_BUFFER_TIMESTAMP (buf) =
859       gst_dvd_read_src_get_time_for_sector (src, src->cur_pack);
861   gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (src)));
863   *p_buf = buf;
865   GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
867   src->cur_pack = next_vobu;
869   next_time = GST_BUFFER_TIMESTAMP (buf);
870   if (GST_CLOCK_TIME_IS_VALID (next_time) && seg->format == GST_FORMAT_TIME &&
871       GST_CLOCK_TIME_IS_VALID (seg->stop) &&
872       next_time > seg->stop + 5 * GST_SECOND) {
873     GST_DEBUG_OBJECT (src, "end of TIME segment");
874     goto eos;
875   }
877   return GST_DVD_READ_OK;
879   /* ERRORS */
880 eos:
881   {
882     GST_INFO_OBJECT (src, "Reached end-of-segment/stream - EOS");
883     return GST_DVD_READ_EOS;
884   }
885 read_error:
886   {
887     GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
888     return GST_DVD_READ_ERROR;
889   }
890 block_read_error:
891   {
892     GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
893         cur_output_size, src->cur_pack);
894     gst_buffer_unref (buf);
895     return GST_DVD_READ_ERROR;
896   }
899 /* we don't cache the result on purpose */
900 static gboolean
901 gst_dvd_read_descrambler_available (void)
903   GModule *module;
904   gpointer sym;
905   gsize res;
907   module = g_module_open ("libdvdcss", 0);
908   if (module != NULL) {
909     res = g_module_symbol (module, "dvdcss_open", &sym);
910     g_module_close (module);
911   } else {
912     res = FALSE;
913   }
915   return res;
918 static GstFlowReturn
919 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
921   GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
922   GstPad *srcpad;
923   gint res;
925   g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
927   srcpad = GST_BASE_SRC (src)->srcpad;
929   if (src->need_newsegment) {
930     gst_pad_push_event (srcpad,
931         gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
932             (gint64) src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
933     src->need_newsegment = FALSE;
934   }
936   if (src->new_seek) {
937     gst_dvd_read_src_goto_title (src, src->title, src->angle);
938     gst_dvd_read_src_goto_chapter (src, src->chapter);
940     src->new_seek = FALSE;
941     src->change_cell = TRUE;
942   }
944   if (src->title_lang_event_pending) {
945     gst_pad_push_event (srcpad, src->title_lang_event_pending);
946     src->title_lang_event_pending = NULL;
947   }
949   if (src->pending_clut_event) {
950     gst_pad_push_event (srcpad, src->pending_clut_event);
951     src->pending_clut_event = NULL;
952   }
954   /* read it in */
955   do {
956     res = gst_dvd_read_src_read (src, src->angle, src->change_cell, p_buf);
957   } while (res == GST_DVD_READ_AGAIN);
959   switch (res) {
960     case GST_DVD_READ_ERROR:{
961       /* FIXME: figure out a way to detect if scrambling is the problem */
962       if (!gst_dvd_read_descrambler_available ()) {
963         GST_ELEMENT_ERROR (src, RESOURCE, READ,
964             (_("Could not read DVD. This may be because the DVD is encrypted "
965                     "and a DVD decryption library is not installed.")), (NULL));
966       } else {
967         GST_ELEMENT_ERROR (src, RESOURCE, READ, (_("Could not read DVD.")),
968             (NULL));
969       }
970       return GST_FLOW_ERROR;
971     }
972     case GST_DVD_READ_EOS:{
973       return GST_FLOW_UNEXPECTED;
974     }
975     case GST_DVD_READ_OK:{
976       src->change_cell = FALSE;
977       return GST_FLOW_OK;
978     }
979     default:
980       break;
981   }
983   g_return_val_if_reached (GST_FLOW_UNEXPECTED);
986 static void
987 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
988     const GValue * value, GParamSpec * pspec)
990   GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
991   gboolean started;
993   GST_OBJECT_LOCK (src);
994   started = GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED);
996   switch (prop_id) {
997     case ARG_DEVICE:{
998       if (started) {
999         g_warning ("%s: property '%s' needs to be set before the device is "
1000             "opened", GST_ELEMENT_NAME (src), pspec->name);
1001         break;;
1002       }
1004       g_free (src->location);
1005       /* clear the filename if we get a NULL (is that possible?) */
1006       if (g_value_get_string (value) == NULL) {
1007         src->location = g_strdup ("/dev/dvd");
1008       } else {
1009         src->location = g_strdup (g_value_get_string (value));
1010       }
1011       break;
1012     }
1013     case ARG_TITLE:
1014       src->uri_title = g_value_get_int (value);
1015       if (started) {
1016         src->title = src->uri_title - 1;
1017         src->new_seek = TRUE;
1018       }
1019       break;
1020     case ARG_CHAPTER:
1021       src->uri_chapter = g_value_get_int (value);
1022       if (started) {
1023         src->chapter = src->uri_chapter - 1;
1024         src->new_seek = TRUE;
1025       }
1026       break;
1027     case ARG_ANGLE:
1028       src->uri_angle = g_value_get_int (value);
1029       if (started) {
1030         src->angle = src->uri_angle - 1;
1031       }
1032       break;
1033     default:
1034       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1035       break;
1036   }
1038   GST_OBJECT_UNLOCK (src);
1041 static void
1042 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
1043     GParamSpec * pspec)
1045   GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
1047   GST_OBJECT_LOCK (src);
1049   switch (prop_id) {
1050     case ARG_DEVICE:
1051       g_value_set_string (value, src->location);
1052       break;
1053     case ARG_TITLE:
1054       g_value_set_int (value, src->uri_title);
1055       break;
1056     case ARG_CHAPTER:
1057       g_value_set_int (value, src->uri_chapter);
1058       break;
1059     case ARG_ANGLE:
1060       g_value_set_int (value, src->uri_angle);
1061       break;
1062     default:
1063       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1064       break;
1065   }
1067   GST_OBJECT_UNLOCK (src);
1070 static gboolean
1071 gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size)
1073   gboolean ret = FALSE;
1075   if (src->dvd_title) {
1076     gsize blocks;
1078     blocks = DVDFileSize (src->dvd_title);
1079     if (blocks >= 0) {
1080       *size = (gint64) blocks *DVD_VIDEO_LB_LEN;
1082       ret = TRUE;
1083     } else {
1084       GST_WARNING_OBJECT (src, "DVDFileSize(%p) failed!", src->dvd_title);
1085     }
1086   }
1088   return ret;
1091 /*** Querying and seeking ***/
1093 static gboolean
1094 gst_dvd_read_src_handle_seek_event (GstDvdReadSrc * src, GstEvent * event)
1096   GstSeekFlags flags;
1097   GstSeekType cur_type, end_type;
1098   gint64 new_off, total;
1099   GstFormat format;
1100   GstPad *srcpad;
1101   gboolean query_ok;
1102   gdouble rate;
1104   gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
1105       &end_type, NULL);
1107   if (rate <= 0.0) {
1108     GST_DEBUG_OBJECT (src, "cannot do backwards playback yet");
1109     return FALSE;
1110   }
1112   if (end_type != GST_SEEK_TYPE_NONE) {
1113     if ((format != chapter_format && format != GST_FORMAT_TIME) ||
1114         end_type != GST_SEEK_TYPE_SET) {
1115       GST_DEBUG_OBJECT (src, "end seek type not supported");
1116       return FALSE;
1117     }
1118   }
1120   if (cur_type != GST_SEEK_TYPE_SET) {
1121     GST_DEBUG_OBJECT (src, "only SEEK_TYPE_SET is supported");
1122     return FALSE;
1123   }
1125   if (format == angle_format) {
1126     GST_OBJECT_LOCK (src);
1127     if (new_off < 0 || new_off >= src->num_angles) {
1128       GST_OBJECT_UNLOCK (src);
1129       GST_DEBUG_OBJECT (src, "invalid angle %d, only %d available",
1130           src->num_angles, src->num_angles);
1131       return FALSE;
1132     }
1133     src->angle = (gint) new_off;
1134     GST_OBJECT_UNLOCK (src);
1135     GST_DEBUG_OBJECT (src, "switched to angle %d", (gint) new_off + 1);
1136     return TRUE;
1137   }
1139   if (format != chapter_format && format != title_format &&
1140       format != GST_FORMAT_BYTES && format != GST_FORMAT_TIME) {
1141     GST_DEBUG_OBJECT (src, "unsupported seek format %d (%s)", format,
1142         gst_format_get_name (format));
1143     return FALSE;
1144   }
1146   if (format == GST_FORMAT_BYTES) {
1147     GST_DEBUG_OBJECT (src, "Requested seek to byte %" G_GUINT64_FORMAT,
1148         new_off);
1149   } else if (format == GST_FORMAT_TIME) {
1150     GST_DEBUG_OBJECT (src, "Requested seek to time %" GST_TIME_FORMAT,
1151         GST_TIME_ARGS (new_off));
1152     if (gst_dvd_read_src_get_sector_from_time (src, new_off) < 0) {
1153       GST_DEBUG_OBJECT (src, "Can't find sector for requested time");
1154       return FALSE;
1155     }
1156   }
1158   srcpad = GST_BASE_SRC_PAD (src);
1160   /* check whether the seek looks reasonable (ie within possible range) */
1161   if (format == GST_FORMAT_BYTES) {
1162     GST_OBJECT_LOCK (src);
1163     query_ok = gst_dvd_read_src_get_size (src, &total);
1164     GST_OBJECT_UNLOCK (src);
1165   } else {
1166     query_ok = gst_pad_query_duration (srcpad, &format, &total);
1167   }
1169   if (!query_ok) {
1170     GST_DEBUG_OBJECT (src, "Failed to query duration in format %s",
1171         gst_format_get_name (format));
1172     return FALSE;
1173   }
1175   GST_DEBUG_OBJECT (src, "Total      %s: %12" G_GINT64_FORMAT,
1176       gst_format_get_name (format), total);
1177   GST_DEBUG_OBJECT (src, "Seek to    %s: %12" G_GINT64_FORMAT,
1178       gst_format_get_name (format), new_off);
1180   if (new_off >= total) {
1181     GST_DEBUG_OBJECT (src, "Seek position out of range");
1182     return FALSE;
1183   }
1185   /* set segment to seek format; this allows us to use the do_seek
1186    * virtual function and let the base source handle all the tricky
1187    * stuff for us. We don't use the segment internally anyway */
1188   /* FIXME: can't take the stream lock here - what to do? */
1189   GST_OBJECT_LOCK (src);
1190   GST_BASE_SRC (src)->segment.format = format;
1191   GST_BASE_SRC (src)->segment.start = 0;
1192   GST_BASE_SRC (src)->segment.stop = total;
1193   GST_BASE_SRC (src)->segment.duration = total;
1194   GST_OBJECT_UNLOCK (src);
1196   return GST_BASE_SRC_CLASS (parent_class)->event (GST_BASE_SRC (src), event);
1199 static void
1200 gst_dvd_read_src_get_sector_bounds (GstDvdReadSrc * src, gint * first,
1201     gint * last)
1203   gint c1, c2, tmp;
1204   cur_title_get_chapter_bounds (src, 0, &c1, &tmp);
1205   cur_title_get_chapter_bounds (src, src->num_chapters - 1, &tmp, &c2);
1206   *first = src->cur_pgc->cell_playback[c1].first_sector;
1207   *last = src->cur_pgc->cell_playback[c2].last_sector;
1210 static gboolean
1211 gst_dvd_read_src_do_seek (GstBaseSrc * basesrc, GstSegment * s)
1213   GstDvdReadSrc *src;
1215   src = GST_DVD_READ_SRC (basesrc);
1217   GST_DEBUG_OBJECT (src, "Seeking to %s: %12" G_GINT64_FORMAT,
1218       gst_format_get_name (s->format), s->last_stop);
1220   if (s->format == sector_format || s->format == GST_FORMAT_BYTES
1221       || s->format == GST_FORMAT_TIME) {
1222     guint old;
1224     old = src->cur_pack;
1226     if (s->format == sector_format) {
1227       gint first, last;
1228       gst_dvd_read_src_get_sector_bounds (src, &first, &last);
1229       GST_DEBUG_OBJECT (src, "Format is sector, seeking to %" G_GINT64_FORMAT,
1230           s->last_stop);
1231       src->cur_pack = s->last_stop;
1232       if (src->cur_pack < first)
1233         src->cur_pack = first;
1234       if (src->cur_pack > last)
1235         src->cur_pack = last;
1236     } else if (s->format == GST_FORMAT_TIME) {
1237       gint sector;
1238       GST_DEBUG_OBJECT (src, "Format is time");
1240       sector = gst_dvd_read_src_get_sector_from_time (src, s->last_stop);
1242       GST_DEBUG_OBJECT (src, "Time %" GST_TIME_FORMAT " => sector %d",
1243           GST_TIME_ARGS (s->last_stop), sector);
1245       /* really shouldn't happen, we've checked this earlier ... */
1246       g_return_val_if_fail (sector >= 0, FALSE);
1248       src->cur_pack = sector;
1249     } else {
1250       /* byte format */
1251       gint first, last;
1252       gst_dvd_read_src_get_sector_bounds (src, &first, &last);
1253       GST_DEBUG_OBJECT (src, "Format is byte");
1254       src->cur_pack = s->last_stop / DVD_VIDEO_LB_LEN;
1255       if (((gint64) src->cur_pack * DVD_VIDEO_LB_LEN) != s->last_stop) {
1256         GST_LOG_OBJECT (src, "rounded down offset %" G_GINT64_FORMAT " => %"
1257             G_GINT64_FORMAT, s->last_stop,
1258             (gint64) src->cur_pack * DVD_VIDEO_LB_LEN);
1259       }
1260       src->cur_pack += first;
1261     }
1263     if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
1264       GST_DEBUG_OBJECT (src, "seek to sector 0x%08x failed", src->cur_pack);
1265       src->cur_pack = old;
1266       return FALSE;
1267     }
1269     GST_LOG_OBJECT (src, "seek to sector 0x%08x ok", src->cur_pack);
1270   } else if (s->format == chapter_format) {
1271     if (!gst_dvd_read_src_goto_chapter (src, (gint) s->last_stop)) {
1272       GST_DEBUG_OBJECT (src, "seek to chapter %d failed",
1273           (gint) s->last_stop + 1);
1274       return FALSE;
1275     }
1276     GST_INFO_OBJECT (src, "seek to chapter %d ok", (gint) s->last_stop + 1);
1277     src->chapter = s->last_stop;
1278   } else if (s->format == title_format) {
1279     if (!gst_dvd_read_src_goto_title (src, (gint) s->last_stop, src->angle) ||
1280         !gst_dvd_read_src_goto_chapter (src, 0)) {
1281       GST_DEBUG_OBJECT (src, "seek to title %d failed", (gint) s->last_stop);
1282       return FALSE;
1283     }
1284     src->title = (gint) s->last_stop;
1285     src->chapter = 0;
1286     GST_INFO_OBJECT (src, "seek to title %d ok", src->title + 1);
1287   } else {
1288     g_return_val_if_reached (FALSE);
1289   }
1291   src->need_newsegment = TRUE;
1292   return TRUE;
1295 static gboolean
1296 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
1298   GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1299   gboolean res;
1301   GST_LOG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
1303   switch (GST_EVENT_TYPE (event)) {
1304     case GST_EVENT_SEEK:
1305       res = gst_dvd_read_src_handle_seek_event (src, event);
1306       break;
1307     default:
1308       res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
1309       break;
1310   }
1312   return res;
1315 static GstEvent *
1316 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
1317     const guint * clut)
1319   GstStructure *structure;
1320   gchar name[16];
1321   gint i;
1323   structure = gst_structure_new ("application/x-gst-dvd",
1324       "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
1326   /* Create a separate field for each value in the table. */
1327   for (i = 0; i < 16; i++) {
1328     g_snprintf (name, sizeof (name), "clut%02d", i);
1329     gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
1330   }
1332   /* Create the DVD event and put the structure into it. */
1333   return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
1336 static gint64
1337 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
1339   gint64 ret_time;
1340   const gint64 one_hour = 3600 * GST_SECOND;
1341   const gint64 one_min = 60 * GST_SECOND;
1343   g_return_val_if_fail ((time->hour >> 4) < 0xa
1344       && (time->hour & 0xf) < 0xa, -1);
1345   g_return_val_if_fail ((time->minute >> 4) < 0x7
1346       && (time->minute & 0xf) < 0xa, -1);
1347   g_return_val_if_fail ((time->second >> 4) < 0x7
1348       && (time->second & 0xf) < 0xa, -1);
1350   ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
1351   ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
1352   ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
1354   return ret_time;
1357 static gboolean
1358 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
1360   GstFormat format;
1361   gint64 val;
1363   gst_query_parse_duration (query, &format, NULL);
1365   switch (format) {
1366     case GST_FORMAT_TIME:{
1367       if (src->cur_pgc == NULL)
1368         return FALSE;
1369       val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
1370       if (val < 0)
1371         return FALSE;
1372       break;
1373     }
1374     case GST_FORMAT_BYTES:{
1375       if (!gst_dvd_read_src_get_size (src, &val))
1376         return FALSE;
1377       break;
1378     }
1379     default:{
1380       if (format == sector_format) {
1381         val = DVDFileSize (src->dvd_title);
1382       } else if (format == title_format) {
1383         val = src->tt_srpt->nr_of_srpts;
1384       } else if (format == chapter_format) {
1385         val = src->num_chapters;
1386       } else if (format == angle_format) {
1387         val = src->tt_srpt->title[src->title].nr_of_angles;
1388       } else {
1389         GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1390             format, gst_format_get_name (format));
1391         return FALSE;
1392       }
1393       break;
1394     }
1395   }
1397   GST_LOG_OBJECT (src, "duration = %" G_GINT64_FORMAT " %s", val,
1398       gst_format_get_name (format));
1400   gst_query_set_duration (query, format, val);
1401   return TRUE;
1404 static gboolean
1405 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
1407   GstFormat format;
1408   gint64 val;
1410   gst_query_parse_position (query, &format, NULL);
1412   switch (format) {
1413     case GST_FORMAT_BYTES:{
1414       val = (gint64) src->cur_pack * DVD_VIDEO_LB_LEN;
1415       break;
1416     }
1417     default:{
1418       if (format == sector_format) {
1419         val = src->cur_pack;
1420       } else if (format == title_format) {
1421         val = src->title;
1422       } else if (format == chapter_format) {
1423         val = src->chapter;
1424       } else if (format == angle_format) {
1425         val = src->angle;
1426       } else {
1427         GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1428             format, gst_format_get_name (format));
1429         return FALSE;
1430       }
1431       break;
1432     }
1433   }
1435   GST_LOG_OBJECT (src, "position = %" G_GINT64_FORMAT " %s", val,
1436       gst_format_get_name (format));
1438   gst_query_set_position (query, format, val);
1439   return TRUE;
1442 static gboolean
1443 gst_dvd_read_src_do_convert_query (GstDvdReadSrc * src, GstQuery * query)
1445   GstFormat src_format, dest_format;
1446   gboolean ret = FALSE;
1447   gint64 src_val, dest_val = -1;
1449   gst_query_parse_convert (query, &src_format, &src_val, &dest_format, NULL);
1451   if (src_format == dest_format) {
1452     dest_val = src_val;
1453     ret = TRUE;
1454     goto done;
1455   }
1457   /* Formats to consider: TIME, DEFAULT, BYTES, title, chapter, sector.
1458    * Note: title and chapter are counted as starting from 0 here, just like
1459    * in the context of seek events. Another note: DEFAULT format is undefined */
1461   if (src_format == GST_FORMAT_BYTES) {
1462     src_format = sector_format;
1463     src_val /= DVD_VIDEO_LB_LEN;
1464   }
1466   if (src_format == sector_format) {
1467     /* SECTOR => xyz */
1468     if (dest_format == GST_FORMAT_TIME && src_val < G_MAXUINT) {
1469       dest_val = gst_dvd_read_src_get_time_for_sector (src, (guint) src_val);
1470       ret = (dest_val >= 0);
1471     } else if (dest_format == GST_FORMAT_BYTES) {
1472       dest_val = src_val * DVD_VIDEO_LB_LEN;
1473       ret = TRUE;
1474     } else {
1475       ret = FALSE;
1476     }
1477   } else if (src_format == title_format) {
1478     /* TITLE => xyz */
1479     if (dest_format == GST_FORMAT_TIME) {
1480       /* not really true, but we use this to trick the base source into
1481        * handling seeks in title-format for us (the source won't know that
1482        * we changed the title in this case) (changing titles should really
1483        * be done with an interface rather than a seek, but for now we're
1484        * stuck with this mechanism. Fix in 0.11) */
1485       dest_val = (GstClockTime) 0;
1486       ret = TRUE;
1487     } else {
1488       ret = FALSE;
1489     }
1490   } else if (src_format == chapter_format) {
1491     /* CHAPTER => xyz */
1492     if (dest_format == GST_FORMAT_TIME) {
1493       if (src->num_chapters >= 0 && src_val < src->num_chapters) {
1494         dest_val = src->chapter_starts[src_val];
1495         ret = TRUE;
1496       }
1497     } else if (dest_format == sector_format) {
1498     } else {
1499       ret = FALSE;
1500     }
1501   } else if (src_format == GST_FORMAT_TIME) {
1502     /* TIME => xyz */
1503     if (dest_format == sector_format || dest_format == GST_FORMAT_BYTES) {
1504       dest_val = gst_dvd_read_src_get_sector_from_time (src, src_val);
1505       ret = (dest_val >= 0);
1506       if (dest_format == GST_FORMAT_BYTES)
1507         dest_val *= DVD_VIDEO_LB_LEN;
1508     } else if (dest_format == chapter_format) {
1509       if (src->chapter_starts != NULL) {
1510         gint i;
1512         for (i = src->num_chapters - 1; i >= 0; --i) {
1513           if (src->chapter_starts && src->chapter_starts[i] >= src_val) {
1514             dest_val = i;
1515             ret = TRUE;
1516             break;
1517           }
1518         }
1519       } else {
1520         ret = FALSE;
1521       }
1522     } else {
1523       ret = FALSE;
1524     }
1525   } else {
1526     ret = FALSE;
1527   }
1529 done:
1531   if (ret) {
1532     gst_query_set_convert (query, src_format, src_val, dest_format, dest_val);
1533   }
1535   return ret;
1538 static gboolean
1539 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1541   GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1542   gboolean started;
1543   gboolean res = TRUE;
1545   GST_LOG_OBJECT (src, "handling %s query",
1546       gst_query_type_get_name (GST_QUERY_TYPE (query)));
1548   GST_OBJECT_LOCK (src);
1549   started = (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED));
1550   GST_OBJECT_UNLOCK (src);
1552   if (!started) {
1553     GST_DEBUG_OBJECT (src, "query failed: not started");
1554     return FALSE;
1555   }
1557   switch (GST_QUERY_TYPE (query)) {
1558     case GST_QUERY_DURATION:
1559       GST_OBJECT_LOCK (src);
1560       res = gst_dvd_read_src_do_duration_query (src, query);
1561       GST_OBJECT_UNLOCK (src);
1562       break;
1563     case GST_QUERY_POSITION:
1564       GST_OBJECT_LOCK (src);
1565       res = gst_dvd_read_src_do_position_query (src, query);
1566       GST_OBJECT_UNLOCK (src);
1567       break;
1568     case GST_QUERY_CONVERT:
1569       GST_OBJECT_LOCK (src);
1570       res = gst_dvd_read_src_do_convert_query (src, query);
1571       GST_OBJECT_UNLOCK (src);
1572       break;
1573     default:
1574       res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1575       break;
1576   }
1578   return res;
1581 static gboolean
1582 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1584   gint seek_to = src->cur_pack;
1585   gint chapter, next, cur, i;
1587   /* retrieve position */
1588   src->cur_pack = 0;
1589   GST_DEBUG_OBJECT (src, "Goto sector %d, angle %d, within %d chapters",
1590       seek_to, angle, src->num_chapters);
1592   for (i = 0; i < src->num_chapters; i++) {
1593     gint c1, c2;
1595     cur_title_get_chapter_bounds (src, i, &c1, &c2);
1596     GST_DEBUG_OBJECT (src, " Looking in chapter %d, bounds: %d %d", i, c1, c2);
1598     for (next = cur = c1; cur < c2;) {
1599       gint first = src->cur_pgc->cell_playback[cur].first_sector;
1600       gint last = src->cur_pgc->cell_playback[cur].last_sector;
1601       GST_DEBUG_OBJECT (src, "Cell %d sector bounds: %d %d", cur, first, last);
1602       /* seeking to 0 should end up at first chapter in any case */
1603       if ((seek_to >= first && seek_to <= last) || (seek_to == 0 && i == 0)) {
1604         GST_DEBUG_OBJECT (src, "Seek target found in chapter %d", i);
1605         chapter = i;
1606         goto done;
1607       }
1608       cur = next;
1609       if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1610         cur += angle;
1611       next = gst_dvd_read_src_get_next_cell (src, src->cur_pgc, cur);
1612     }
1613   }
1615   GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1617   return FALSE;
1619 done:
1620   {
1621     /* so chapter $chapter and cell $cur contain our sector
1622      * of interest. Let's go there! */
1623     GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1624         chapter + 1, cur);
1626     gst_dvd_read_src_goto_chapter (src, chapter);
1627     src->cur_cell = cur;
1628     src->next_cell = next;
1629     src->new_cell = FALSE;
1630     src->cur_pack = seek_to;
1632     return TRUE;
1633   }
1637 /*** URI interface ***/
1639 static GstURIType
1640 gst_dvd_read_src_uri_get_type (void)
1642   return GST_URI_SRC;
1645 static gchar **
1646 gst_dvd_read_src_uri_get_protocols (void)
1648   static gchar *protocols[] = { (gchar *) "dvd", NULL };
1650   return protocols;
1653 static const gchar *
1654 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1656   GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1658   GST_OBJECT_LOCK (src);
1660   g_free (src->last_uri);
1661   src->last_uri = g_strdup_printf ("dvd://%d,%d,%d", src->uri_title,
1662       src->uri_chapter, src->uri_angle);
1664   GST_OBJECT_UNLOCK (src);
1666   return src->last_uri;
1669 static gboolean
1670 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1672   GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1673   gboolean ret;
1674   gchar *protocol;
1676   protocol = gst_uri_get_protocol (uri);
1677   ret = (protocol != NULL && g_str_equal (protocol, "dvd"));
1678   g_free (protocol);
1679   protocol = NULL;
1681   if (!ret)
1682     return ret;
1684   /* parse out the new t/c/a and seek to them */
1685   {
1686     gchar *location = NULL;
1687     gchar **strs;
1688     gchar **strcur;
1689     gint pos = 0;
1691     location = gst_uri_get_location (uri);
1693     if (!location)
1694       return ret;
1696     GST_OBJECT_LOCK (src);
1698     src->uri_title = 1;
1699     src->uri_chapter = 1;
1700     src->uri_angle = 1;
1702     strcur = strs = g_strsplit (location, ",", 0);
1703     while (strcur && *strcur) {
1704       gint val;
1706       if (!sscanf (*strcur, "%d", &val))
1707         break;
1709       if (val <= 0) {
1710         g_warning ("Invalid value %d in URI '%s'. Must be 1 or greater",
1711             val, location);
1712         break;
1713       }
1715       switch (pos) {
1716         case 0:
1717           src->uri_title = val;
1718           break;
1719         case 1:
1720           src->uri_chapter = val;
1721           break;
1722         case 2:
1723           src->uri_angle = val;
1724           break;
1725       }
1727       strcur++;
1728       pos++;
1729     }
1731     if (pos > 0 && GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1732       src->title = src->uri_title - 1;
1733       src->chapter = src->uri_chapter - 1;
1734       src->angle = src->uri_angle - 1;
1735       src->new_seek = TRUE;
1736     }
1738     GST_OBJECT_UNLOCK (src);
1740     g_strfreev (strs);
1741     g_free (location);
1742   }
1744   return ret;
1747 static void
1748 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1750   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1752   iface->get_type = gst_dvd_read_src_uri_get_type;
1753   iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1754   iface->get_uri = gst_dvd_read_src_uri_get_uri;
1755   iface->set_uri = gst_dvd_read_src_uri_set_uri;
1758 static void
1759 gst_dvd_read_src_do_init (GType dvdreadsrc_type)
1761   static const GInterfaceInfo urihandler_info = {
1762     gst_dvd_read_src_uri_handler_init,
1763     NULL,
1764     NULL
1765   };
1767   g_type_add_interface_static (dvdreadsrc_type, GST_TYPE_URI_HANDLER,
1768       &urihandler_info);
1770   title_format = gst_format_register ("title", "DVD title");
1771   angle_format = gst_format_register ("angle", "DVD angle");
1772   sector_format = gst_format_register ("sector", "DVD sector");
1773   chapter_format = gst_format_register ("chapter", "DVD chapter");
1776 static gboolean
1777 plugin_init (GstPlugin * plugin)
1779   GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1780       "DVD reader element based on dvdreadsrc");
1782 #ifdef ENABLE_NLS
1783   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1784       LOCALEDIR);
1785   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1786   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1787 #endif /* ENABLE_NLS */
1789   if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,
1790           GST_TYPE_DVD_READ_SRC)) {
1791     return FALSE;
1792   }
1794   return TRUE;
1797 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1798     GST_VERSION_MINOR,
1799     "dvdread",
1800     "Access a DVD with dvdread",
1801     plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);