ext/dvdread/dvdreadsrc.c: Make check stronger.
[platform/upstream/gstreamer.git] / ext / dvdread / dvdreadsrc.c
1 /* GStreamer
2  * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2001 Billy Biggs <vektor@dumbterm.net>.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "_stdint.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include "dvdreadsrc.h"
34
35 /* #include <gst/gst-i18n-plugin.h> */
36 /* FIXME: remove once GETTEXT_PACKAGE etc. is set */
37 #define _(s) s
38
39 GST_DEBUG_CATEGORY_STATIC (gstgst_dvd_read_src_debug);
40 #define GST_CAT_DEFAULT (gstgst_dvd_read_src_debug)
41
42 static void gst_dvd_read_src_do_init (GType dvdreadsrc_type);
43
44 enum
45 {
46   ARG_0,
47   ARG_DEVICE,
48   ARG_TITLE,
49   ARG_CHAPTER,
50   ARG_ANGLE
51 };
52
53 static GstElementDetails gst_dvd_read_src_details = {
54   "DVD Source",
55   "Source/File/DVD",
56   "Access a DVD title/chapter/angle using libdvdread",
57   "Erik Walthinsen <omega@cse.ogi.edu>",
58 };
59
60 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("video/mpeg, mpegversion=2, systemstream=(boolean)true"));
64
65 static GstFormat title_format;
66 static GstFormat angle_format;
67 static GstFormat sector_format;
68 static GstFormat chapter_format;
69
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,
73     GstBuffer ** buf);
74 static gboolean gst_dvd_read_src_src_query (GstBaseSrc * basesrc,
75     GstQuery * query);
76 static gboolean gst_dvd_read_src_src_event (GstBaseSrc * basesrc,
77     GstEvent * event);
78 static gboolean gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title,
79     gint angle);
80 static gboolean gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src,
81     gint chapter);
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,
88     const guint * clut);
89 static gboolean gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size);
90
91 GST_BOILERPLATE_FULL (GstDvdReadSrc, gst_dvd_read_src, GstPushSrc,
92     GST_TYPE_PUSH_SRC, gst_dvd_read_src_do_init);
93
94 static void
95 gst_dvd_read_src_base_init (gpointer g_class)
96 {
97   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
98
99   gst_element_class_add_pad_template (element_class,
100       gst_static_pad_template_get (&srctemplate));
101
102   gst_element_class_set_details (element_class, &gst_dvd_read_src_details);
103 }
104
105 static void
106 gst_dvd_read_src_finalize (GObject * object)
107 {
108   GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
109
110   g_free (src->location);
111   g_free (src->last_uri);
112
113   G_OBJECT_CLASS (parent_class)->finalize (object);
114 }
115
116 static void
117 gst_dvd_read_src_init (GstDvdReadSrc * src, GstDvdReadSrcClass * klass)
118 {
119   src->dvd = NULL;
120   src->vts_file = NULL;
121   src->vmg_file = NULL;
122   src->dvd_title = NULL;
123
124   src->location = g_strdup ("/dev/dvd");
125   src->last_uri = NULL;
126   src->new_seek = TRUE;
127   src->new_cell = TRUE;
128   src->change_cell = FALSE;
129   src->uri_title = 1;
130   src->uri_chapter = 1;
131   src->uri_angle = 1;
132
133   src->seek_pend = FALSE;
134   src->flush_pend = FALSE;
135   src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
136   src->title_lang_event_pending = NULL;
137   src->pending_clut_event = NULL;
138
139   gst_pad_use_fixed_caps (GST_BASE_SRC_PAD (src));
140   gst_pad_set_caps (GST_BASE_SRC_PAD (src),
141       gst_static_pad_template_get_caps (&srctemplate));
142 }
143
144 static void
145 gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
146 {
147   GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
148   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
149   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
150
151   gobject_class->finalize = gst_dvd_read_src_finalize;
152   gobject_class->set_property = gst_dvd_read_src_set_property;
153   gobject_class->get_property = gst_dvd_read_src_get_property;
154
155   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
156       g_param_spec_string ("device", "Device",
157           "DVD device location", NULL, G_PARAM_READWRITE));
158   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TITLE,
159       g_param_spec_int ("title", "title", "title",
160           1, 999, 1, G_PARAM_READWRITE));
161   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHAPTER,
162       g_param_spec_int ("chapter", "chapter", "chapter",
163           1, 999, 1, G_PARAM_READWRITE));
164   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ANGLE,
165       g_param_spec_int ("angle", "angle", "angle",
166           1, 999, 1, G_PARAM_READWRITE));
167
168   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
169   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
170   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_query);
171   gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_event);
172
173   gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dvd_read_src_create);
174 }
175
176 static gboolean
177 gst_dvd_read_src_start (GstBaseSrc * basesrc)
178 {
179   GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
180
181   g_return_val_if_fail (src->location != NULL, FALSE);
182
183   GST_DEBUG_OBJECT (src, "Opening DVD '%s'", src->location);
184
185   if ((src->dvd = DVDOpen (src->location)) == NULL)
186     goto open_failed;
187
188   /* Load the video manager to find out the information about the titles */
189   GST_DEBUG_OBJECT (src, "Loading VMG info");
190
191   if (!(src->vmg_file = ifoOpen (src->dvd, 0)))
192     goto ifo_open_failed;
193
194   src->tt_srpt = src->vmg_file->tt_srpt;
195
196   src->title = src->uri_title - 1;
197   src->chapter = src->uri_chapter - 1;
198   src->angle = src->uri_angle - 1;
199
200   if (!gst_dvd_read_src_goto_title (src, src->title, src->angle))
201     goto title_open_failed;
202
203   if (!gst_dvd_read_src_goto_chapter (src, src->chapter))
204     goto chapter_open_failed;
205
206   src->new_seek = FALSE;
207   src->change_cell = TRUE;
208
209   return TRUE;
210
211   /* ERRORS */
212 open_failed:
213   {
214     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
215         (_("Could not open DVD")),
216         ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
217     return FALSE;
218   }
219 ifo_open_failed:
220   {
221     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
222         (_("Could not open DVD")),
223         ("ifoOpen() failed: %s", g_strerror (errno)));
224     return FALSE;
225   }
226 title_open_failed:
227   {
228     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
229         (_("Could not open DVD title %d"), src->uri_title), (NULL));
230     return FALSE;
231   }
232 chapter_open_failed:
233   {
234     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
235         (_("Failed to go to chapter %d of DVD title %d"),
236             src->uri_chapter, src->uri_title), (NULL));
237     return FALSE;
238   }
239 }
240
241 static gboolean
242 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
243 {
244   GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
245
246   if (src->vts_file) {
247     ifoClose (src->vts_file);
248     src->vts_file = NULL;
249   }
250   if (src->vmg_file) {
251     ifoClose (src->vmg_file);
252     src->vmg_file = NULL;
253   }
254   if (src->dvd_title) {
255     DVDCloseFile (src->dvd_title);
256     src->dvd_title = NULL;
257   }
258   if (src->dvd) {
259     DVDClose (src->dvd);
260     src->dvd = NULL;
261   }
262   src->new_cell = TRUE;
263   src->new_seek = TRUE;
264   src->change_cell = FALSE;
265   src->chapter = 0;
266   src->title = 0;
267   src->flush_pend = FALSE;
268   src->seek_pend = FALSE;
269   src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
270   if (src->title_lang_event_pending) {
271     gst_event_unref (src->title_lang_event_pending);
272     src->title_lang_event_pending = NULL;
273   }
274   if (src->pending_clut_event) {
275     gst_event_unref (src->pending_clut_event);
276     src->pending_clut_event = NULL;
277   }
278
279   GST_LOG_OBJECT (src, "closed DVD");
280
281   return TRUE;
282 }
283
284 static void
285 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
286     gint * p_pgc_id, pgc_t ** p_pgc)
287 {
288   pgc_t *pgc;
289   gint pgn, pgc_id;
290
291   g_assert (chapter >= 0 && chapter < src->num_chapters);
292
293   pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
294   pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
295   pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
296
297   *p_pgn = pgn;
298   *p_pgc_id = pgc_id;
299   *p_pgc = pgc;
300 }
301
302 static void
303 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
304     gint * p_first_cell, gint * p_last_cell)
305 {
306   pgc_t *pgc;
307   gint pgn, pgc_id, pgn_next_ch;
308
309   g_assert (chapter >= 0 && chapter < src->num_chapters);
310
311   cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
312
313   *p_first_cell = pgc->program_map[pgn - 1] - 1;
314
315   if (chapter == (src->num_chapters - 1)) {
316     *p_last_cell = pgc->nr_of_cells;
317   } else {
318     pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
319     *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
320   }
321 }
322
323 static gboolean
324 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
325 {
326   gint i;
327
328   /* make sure the chapter number is valid for this title */
329   if (chapter < 0 || chapter >= src->num_chapters) {
330     GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
331         chapter, src->num_chapters);
332     chapter = CLAMP (chapter, 0, src->num_chapters - 1);
333   }
334
335   /* determine which program chain we want to watch. This is
336    * based on the chapter number */
337   cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
338       &src->cur_pgc);
339   cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
340       &src->last_cell);
341
342   GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter,
343       src->start_cell, src->last_cell);
344
345   /* retrieve position */
346   src->cur_pack = 0;
347   for (i = 0; i < chapter; i++) {
348     gint c1, c2;
349
350     cur_title_get_chapter_bounds (src, i, &c1, &c2);
351
352     while (c1 < c2) {
353       src->cur_pack +=
354           src->cur_pgc->cell_playback[c1].last_sector -
355           src->cur_pgc->cell_playback[c1].first_sector;
356       ++c1;
357     }
358   }
359
360   /* prepare reading for new cell */
361   src->new_cell = TRUE;
362   src->next_cell = src->start_cell;
363
364   src->chapter = chapter;
365
366   if (src->pending_clut_event)
367     gst_event_unref (src->pending_clut_event);
368
369   src->pending_clut_event =
370       gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
371
372   return TRUE;
373 }
374
375 static gboolean
376 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
377 {
378   GstStructure *s;
379   gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
380   gint title_set_nr;
381   gint num_titles;
382   gint num_angles;
383   gint i;
384
385   /* make sure our title number is valid */
386   num_titles = src->tt_srpt->nr_of_srpts;
387   GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
388   if (title < 0 || title >= num_titles)
389     goto invalid_title;
390
391   src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
392   GST_INFO_OBJECT (src, "Title %d has %d chapters", title, src->num_chapters);
393
394   /* make sure the angle number is valid for this title */
395   num_angles = src->tt_srpt->title[title].nr_of_angles;
396   GST_LOG_OBJECT (src, "Title %d has %d angles", title, num_angles);
397   if (angle < 0 || angle >= num_angles) {
398     GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
399         angle, num_angles);
400     angle = CLAMP (angle, 0, num_angles - 1);
401   }
402
403   /* load the VTS information for the title set our title is in */
404   title_set_nr = src->tt_srpt->title[title].title_set_nr;
405   src->vts_file = ifoOpen (src->dvd, title_set_nr);
406   if (src->vts_file == NULL)
407     goto ifo_open_failed;
408
409   src->ttn = src->tt_srpt->title[title].vts_ttn;
410   src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
411
412   /* we've got enough info, time to open the title set data */
413   src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
414   if (src->dvd_title == NULL)
415     goto title_open_failed;
416
417   GST_INFO_OBJECT (src, "Opened title %d, angle %d", title, angle);
418   src->title = title;
419   src->angle = angle;
420
421   /* build event */
422
423   if (src->title_lang_event_pending) {
424     gst_event_unref (src->title_lang_event_pending);
425     src->title_lang_event_pending = NULL;
426   }
427
428   s = gst_structure_new ("application/x-gst-dvd",
429       "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
430
431   /* audio */
432   for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
433     const audio_attr_t *a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
434
435     t = g_strdup_printf ("audio-%d-format", i);
436     gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
437     g_free (t);
438
439     if (a->lang_type) {
440       t = g_strdup_printf ("audio-%d-language", i);
441       lang_code[0] = (a->lang_code >> 8) & 0xff;
442       lang_code[1] = a->lang_code & 0xff;
443       gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
444       g_free (t);
445     } else {
446       lang_code[0] = '\0';
447     }
448
449     GST_INFO_OBJECT (src, "[%02d] Audio    %02d: lang='%s', format=%d",
450         src->title, i, lang_code, (gint) a->audio_format);
451   }
452
453   /* subtitle */
454   for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
455     const subp_attr_t *u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
456
457     if (u->type) {
458       t = g_strdup_printf ("subtitle-%d-language", i);
459       lang_code[0] = (u->lang_code >> 8) & 0xff;
460       lang_code[1] = u->lang_code & 0xff;
461       gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
462       g_free (t);
463     } else {
464       lang_code[0] = '\0';
465     }
466
467     GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', format=%d",
468         src->title, i, lang_code);
469   }
470
471   src->title_lang_event_pending =
472       gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
473
474   return TRUE;
475
476   /* ERRORS */
477 invalid_title:
478   {
479     GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
480         title, num_titles);
481     return FALSE;
482   }
483 ifo_open_failed:
484   {
485     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
486         (_("Could not open DVD title %d"), title_set_nr),
487         ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
488     return FALSE;
489   }
490 title_open_failed:
491   {
492     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
493         (_("Could not open DVD title %d"), title_set_nr),
494         ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
495     return FALSE;
496   }
497 }
498
499 /* FIXME: double-check this function, compare against original */
500 static gint
501 gst_dvd_read_src_get_next_cell_for (GstDvdReadSrc * src, gint cell)
502 {
503   /* Check if we're entering an angle block. */
504   if (src->cur_pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
505     return (cell + 1);
506
507   while (src->cur_pgc->cell_playback[cell].block_mode == BLOCK_MODE_LAST_CELL)
508     ++cell;
509
510   return cell + 1;              /* really +1? (tpm) */
511 }
512
513 /* Returns true if the pack is a NAV pack */
514 static gboolean
515 gst_dvd_read_src_is_nav_pack (const guint8 * data)
516 {
517   if (GST_READ_UINT32_BE (data + 0x26) != 0x000001BF)
518     return FALSE;
519
520   /* Check that this is substream 0 (PCI) */
521   if (data[0x2c] != 0)
522     return FALSE;
523
524   if (GST_READ_UINT32_BE (data + 0x400) != 0x000001BF)
525     return FALSE;
526
527   /* Check that this is substream 1 (DSI) */
528   if (data[0x406] != 1)
529     return FALSE;
530
531   /* Check sizes of PCI and DSI packets */
532   if (GST_READ_UINT16_BE (data + 0x2a) != 0x03d4)
533     return FALSE;
534
535   if (GST_READ_UINT16_BE (data + 0x404) != 0x03fa)
536     return FALSE;
537
538   return TRUE;
539 }
540
541 typedef enum
542 {
543   GST_DVD_READ_OK = 0,
544   GST_DVD_READ_ERROR = -1,
545   GST_DVD_READ_EOS = -2,
546   GST_DVD_READ_AGAIN = -3
547 } GstDvdReadReturn;
548
549 static GstDvdReadReturn
550 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
551     GstBuffer ** p_buf)
552 {
553   GstBuffer *buf;
554   guint8 oneblock[DVD_VIDEO_LB_LEN];
555   dsi_t dsi_pack;
556   guint next_vobu, next_ilvu_start, cur_output_size;
557   gint len;
558
559   /* playback by cell in this pgc, starting at the cell for our chapter */
560   if (new_seek)
561     src->cur_cell = src->start_cell;
562
563 again:
564
565   if (src->cur_cell >= src->last_cell) {
566     /* advance to next chapter */
567     if (src->chapter == (src->num_chapters - 1))
568       goto eos;
569
570     GST_INFO_OBJECT (src, "end of chapter %d, switch to next", src->chapter);
571
572     ++src->chapter;
573     gst_dvd_read_src_goto_chapter (src, src->chapter);
574
575     return GST_DVD_READ_AGAIN;
576   }
577
578   if (src->new_cell || new_seek) {
579     if (!new_seek) {
580       src->cur_cell = src->next_cell;
581       if (src->cur_cell >= src->last_cell) {
582         GST_LOG_OBJECT (src, "last cell in chapter");
583         goto again;
584       }
585     }
586
587     /* take angle into account */
588     if (src->cur_pgc->cell_playback[src->cur_cell].block_type
589         == BLOCK_TYPE_ANGLE_BLOCK)
590       src->cur_cell += angle;
591
592     /* calculate next cell */
593     src->next_cell = gst_dvd_read_src_get_next_cell_for (src, src->cur_cell);
594
595     /* we loop until we're out of this cell */
596     src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
597     src->new_cell = FALSE;
598   }
599
600   if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
601     src->new_cell = TRUE;
602     GST_LOG_OBJECT (src, "Beyond last sector, go to next cell");
603     return GST_DVD_READ_AGAIN;
604   }
605
606   /* read NAV packet */
607 nav_retry:
608
609   len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, oneblock);
610   if (len == 0)
611     goto read_error;
612
613   if (!gst_dvd_read_src_is_nav_pack (oneblock)) {
614     src->cur_pack++;
615     goto nav_retry;
616   }
617
618   /* parse the contained dsi packet */
619   navRead_DSI (&dsi_pack, &oneblock[DSI_START_BYTE]);
620   g_assert (src->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn);
621
622   /* determine where we go next. These values are the ones we
623    * mostly care about */
624   next_ilvu_start = src->cur_pack + dsi_pack.sml_agli.data[angle].address;
625   cur_output_size = dsi_pack.dsi_gi.vobu_ea;
626
627   /* If we're not at the end of this cell, we can determine the next
628    * VOBU to display using the VOBU_SRI information section of the
629    * DSI.  Using this value correctly follows the current angle,
630    * avoiding the doubled scenes in The Matrix, and makes our life
631    * really happy.
632    *
633    * Otherwise, we set our next address past the end of this cell to
634    * force the code above to go to the next cell in the program. */
635   if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
636     next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
637   } else {
638     next_vobu = src->cur_pack + cur_output_size + 1;
639   }
640
641   g_assert (cur_output_size < 1024);
642   ++src->cur_pack;
643
644   /* create the buffer (TODO: use buffer pool?) */
645   buf = gst_buffer_new_and_alloc (cur_output_size * DVD_VIDEO_LB_LEN);
646
647   /* read in and output cursize packs */
648   len = DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size,
649       GST_BUFFER_DATA (buf));
650
651   if (len != cur_output_size)
652     goto block_read_error;
653
654   GST_BUFFER_SIZE (buf) = cur_output_size * DVD_VIDEO_LB_LEN;
655   /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
656
657   gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (src)));
658
659   *p_buf = buf;
660
661   src->cur_pack = next_vobu;
662
663   GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
664
665   return GST_DVD_READ_OK;
666
667   /* ERRORS */
668 eos:
669   {
670     GST_INFO_OBJECT (src, "last chapter done - EOS");
671     return GST_DVD_READ_EOS;
672   }
673 read_error:
674   {
675     GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
676     return GST_DVD_READ_ERROR;
677   }
678 block_read_error:
679   {
680     GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
681         cur_output_size, src->cur_pack);
682     gst_buffer_unref (buf);
683     return GST_DVD_READ_ERROR;
684   }
685 }
686
687 static GstFlowReturn
688 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
689 {
690   GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
691   GstPad *srcpad;
692   gint res;
693
694   g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
695
696   srcpad = GST_BASE_SRC (src)->srcpad;
697
698   /* handle events, if any */
699   if (src->seek_pend) {
700     if (src->flush_pend) {
701       gst_pad_push_event (srcpad, gst_event_new_flush_start ());
702       gst_pad_push_event (srcpad, gst_event_new_flush_stop ());
703       src->flush_pend = FALSE;
704     }
705
706     if (src->seek_pend_fmt != GST_FORMAT_UNDEFINED) {
707       if (src->seek_pend_fmt == title_format) {
708         gst_dvd_read_src_goto_title (src, src->title, src->angle);
709       }
710       gst_dvd_read_src_goto_chapter (src, src->chapter);
711
712       src->seek_pend_fmt = GST_FORMAT_UNDEFINED;
713     } else {
714       if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
715         GST_DEBUG_OBJECT (src, "seek to sector failed, going EOS");
716         gst_pad_push_event (srcpad, gst_event_new_eos ());
717         return GST_FLOW_UNEXPECTED;
718       }
719     }
720
721     gst_pad_push_event (srcpad,
722         gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
723             src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
724
725     src->seek_pend = FALSE;
726   }
727
728   if (src->new_seek) {
729     gst_dvd_read_src_goto_title (src, src->title, src->angle);
730     gst_dvd_read_src_goto_chapter (src, src->chapter);
731
732     src->new_seek = FALSE;
733     src->change_cell = TRUE;
734   }
735
736   if (src->title_lang_event_pending) {
737     gst_pad_push_event (srcpad, src->title_lang_event_pending);
738     src->title_lang_event_pending = NULL;
739   }
740
741   if (src->pending_clut_event) {
742     gst_pad_push_event (srcpad, src->pending_clut_event);
743     src->pending_clut_event = NULL;
744   }
745
746   /* read it in */
747   do {
748     res = gst_dvd_read_src_read (src, src->angle, src->change_cell, p_buf);
749   } while (res == GST_DVD_READ_AGAIN);
750
751   switch (res) {
752     case GST_DVD_READ_ERROR:{
753       GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), (NULL));
754       return GST_FLOW_ERROR;
755     }
756     case GST_DVD_READ_EOS:{
757       GST_INFO_OBJECT (src, "Reached EOS");
758       return GST_FLOW_UNEXPECTED;
759     }
760     case GST_DVD_READ_OK:{
761       src->change_cell = FALSE;
762       return GST_FLOW_OK;
763     }
764     default:
765       break;
766   }
767
768   g_return_val_if_reached (GST_FLOW_UNEXPECTED);
769 }
770
771 static void
772 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
773     const GValue * value, GParamSpec * pspec)
774 {
775   GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
776   gboolean started;
777
778   GST_OBJECT_LOCK (src);
779   started = GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED);
780
781   switch (prop_id) {
782     case ARG_DEVICE:{
783       if (started) {
784         g_warning ("%s: property '%s' needs to be set before the device is "
785             "opened", GST_ELEMENT_NAME (src), pspec->name);
786         break;;
787       }
788
789       g_free (src->location);
790       /* clear the filename if we get a NULL (is that possible?) */
791       if (g_value_get_string (value) == NULL) {
792         src->location = g_strdup ("/dev/dvd");
793       } else {
794         src->location = g_strdup (g_value_get_string (value));
795       }
796       break;
797     }
798     case ARG_TITLE:
799       src->uri_title = g_value_get_int (value);
800       if (started) {
801         src->title = src->uri_title - 1;
802         src->new_seek = TRUE;
803       }
804       break;
805     case ARG_CHAPTER:
806       src->uri_chapter = g_value_get_int (value);
807       if (started) {
808         src->chapter = src->uri_chapter - 1;
809         src->new_seek = TRUE;
810       }
811       break;
812     case ARG_ANGLE:
813       src->uri_angle = g_value_get_int (value);
814       if (started) {
815         src->angle = src->uri_angle - 1;
816       }
817       break;
818     default:
819       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
820       break;
821   }
822
823   GST_OBJECT_UNLOCK (src);
824 }
825
826 static void
827 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
828     GParamSpec * pspec)
829 {
830   GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
831
832   GST_OBJECT_LOCK (src);
833
834   switch (prop_id) {
835     case ARG_DEVICE:
836       g_value_set_string (value, src->location);
837       break;
838     case ARG_TITLE:
839       g_value_set_int (value, src->uri_title);
840       break;
841     case ARG_CHAPTER:
842       g_value_set_int (value, src->uri_chapter);
843       break;
844     case ARG_ANGLE:
845       g_value_set_int (value, src->uri_angle);
846       break;
847     default:
848       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
849       break;
850   }
851
852   GST_OBJECT_UNLOCK (src);
853 }
854
855 static gboolean
856 gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size)
857 {
858   gboolean ret = FALSE;
859
860   if (src->dvd_title) {
861     gsize blocks;
862
863     blocks = DVDFileSize (src->dvd_title);
864     if (blocks >= 0) {
865       *size = (gint64) blocks *DVD_VIDEO_LB_LEN;
866
867       ret = TRUE;
868     } else {
869       GST_WARNING_OBJECT (src, "DVDFileSize(%p) failed!", src->dvd_title);
870     }
871   }
872
873   return ret;
874 }
875
876 /*** Querying and seeking ***/
877
878 static gboolean
879 gst_dvd_read_src_do_seek (GstDvdReadSrc * src, GstEvent * event)
880 {
881   GstSeekFlags flags;
882   GstSeekType cur_type, end_type;
883   gint64 new_off, total, cur;
884   GstFormat format;
885   GstPad *srcpad;
886   gboolean query_ok;
887   gdouble rate;
888
889   srcpad = GST_BASE_SRC (src)->srcpad;
890
891   gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
892       &end_type, NULL);
893
894   if (end_type != GST_SEEK_TYPE_NONE) {
895     GST_WARNING_OBJECT (src, "End seek type not supported, will be ignored");
896   }
897
898   switch (format) {
899     case GST_FORMAT_BYTES:
900       break;
901     default:{
902       if (format != chapter_format &&
903           format != sector_format &&
904           format != angle_format && format != title_format) {
905         GST_DEBUG_OBJECT (src, "Unsupported seek format %d (%s)", format,
906             gst_format_get_name (format));
907         return FALSE;
908       }
909       break;
910     }
911   }
912
913   /* get current offset and length */
914   if (format == GST_FORMAT_BYTES) {
915     GST_OBJECT_LOCK (src);
916     query_ok = gst_dvd_read_src_get_size (src, &total);
917     cur = (gint64) src->cur_pack * DVD_VIDEO_LB_LEN;
918     GST_OBJECT_UNLOCK (src);
919   } else {
920     query_ok = gst_pad_query_duration (srcpad, &format, &total)
921         && gst_pad_query_position (srcpad, &format, &cur);
922   }
923
924   if (!query_ok) {
925     GST_DEBUG_OBJECT (src, "Failed to query duration/position");
926     return FALSE;
927   }
928
929   GST_DEBUG_OBJECT (src, "Current    %s: %12" G_GINT64_FORMAT,
930       gst_format_get_name (format), cur);
931   GST_DEBUG_OBJECT (src, "Total      %s: %12" G_GINT64_FORMAT,
932       gst_format_get_name (format), total);
933
934   /* get absolute */
935   switch (cur_type) {
936     case GST_SEEK_TYPE_SET:
937       /* no-op */
938       break;
939     case GST_SEEK_TYPE_CUR:
940       new_off += cur;
941       break;
942     case GST_SEEK_TYPE_END:
943       new_off = total - new_off;
944       break;
945     default:
946       GST_DEBUG_OBJECT (src, "Unsupported seek method");
947       return FALSE;
948   }
949
950   if (new_off < 0 || new_off >= total) {
951     GST_DEBUG_OBJECT (src, "Invalid seek position %" G_GINT64_FORMAT, new_off);
952     return FALSE;
953   }
954
955   if (cur == new_off) {
956     GST_DEBUG_OBJECT (src, "We're already at that position!");
957     return TRUE;
958   }
959
960   GST_LOG_OBJECT (src, "Seeking to %s: %12" G_GINT64_FORMAT,
961       gst_format_get_name (format), new_off);
962
963   GST_OBJECT_LOCK (src);
964
965   if (format == angle_format) {
966     src->angle = new_off;
967     goto done;
968   }
969
970   if (format == sector_format) {
971     src->cur_pack = new_off;
972   } else if (format == GST_FORMAT_BYTES) {
973     src->cur_pack = new_off / DVD_VIDEO_LB_LEN;
974     if ((src->cur_pack * DVD_VIDEO_LB_LEN) != new_off) {
975       GST_LOG_OBJECT (src, "rounded down offset %" G_GINT64_FORMAT " => %"
976           G_GINT64_FORMAT, new_off, (gint64) src->cur_pack * DVD_VIDEO_LB_LEN);
977     }
978   } else if (format == chapter_format) {
979     src->cur_pack = 0;
980     src->chapter = new_off;
981     src->seek_pend_fmt = format;
982   } else if (format == title_format) {
983     src->cur_pack = 0;
984     src->title = new_off;
985     src->chapter = 0;
986     src->seek_pend_fmt = format;
987   } else {
988     GST_OBJECT_UNLOCK (src);
989     g_return_val_if_reached (FALSE);
990   }
991
992   /* leave for events */
993   src->seek_pend = TRUE;
994   if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
995     src->flush_pend = TRUE;
996
997 done:
998
999   GST_OBJECT_UNLOCK (src);
1000
1001   return TRUE;
1002 }
1003
1004 static gboolean
1005 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
1006 {
1007   GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1008   gboolean res;
1009
1010   GST_LOG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
1011
1012   switch (GST_EVENT_TYPE (event)) {
1013     case GST_EVENT_SEEK:
1014       res = gst_dvd_read_src_do_seek (src, event);
1015       break;
1016     default:
1017       res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
1018       break;
1019   }
1020
1021   return res;
1022 }
1023
1024 static GstEvent *
1025 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
1026     const guint * clut)
1027 {
1028   GstStructure *structure;
1029   gchar name[16];
1030   gint i;
1031
1032   structure = gst_structure_new ("application/x-gst-dvd",
1033       "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
1034
1035   /* Create a separate field for each value in the table. */
1036   for (i = 0; i < 16; i++) {
1037     g_snprintf (name, sizeof (name), "clut%02d", i);
1038     gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
1039   }
1040
1041   /* Create the DVD event and put the structure into it. */
1042   return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
1043 }
1044
1045 static gint64
1046 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
1047 {
1048   gint64 ret_time;
1049   const gint64 one_hour = 3600 * GST_SECOND;
1050   const gint64 one_min = 60 * GST_SECOND;
1051
1052   g_return_val_if_fail ((time->hour >> 4) < 0xa
1053       && (time->hour & 0xf) < 0xa, -1);
1054   g_return_val_if_fail ((time->minute >> 4) < 0x7
1055       && (time->minute & 0xf) < 0xa, -1);
1056   g_return_val_if_fail ((time->second >> 4) < 0x7
1057       && (time->second & 0xf) < 0xa, -1);
1058
1059   ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
1060   ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
1061   ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
1062
1063   return ret_time;
1064 }
1065
1066 static gboolean
1067 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
1068 {
1069   GstFormat format;
1070   gint64 val;
1071
1072   gst_query_parse_duration (query, &format, NULL);
1073
1074   switch (format) {
1075     case GST_FORMAT_TIME:{
1076       if (src->cur_pgc == NULL)
1077         return FALSE;
1078       val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
1079       if (val < 0)
1080         return FALSE;
1081       break;
1082     }
1083     case GST_FORMAT_BYTES:{
1084       if (!gst_dvd_read_src_get_size (src, &val))
1085         return FALSE;
1086       break;
1087     }
1088     default:{
1089       if (format == sector_format) {
1090         val = DVDFileSize (src->dvd_title);
1091       } else if (format == title_format) {
1092         val = src->tt_srpt->nr_of_srpts;
1093       } else if (format == chapter_format) {
1094         val = src->num_chapters;
1095       } else if (format == angle_format) {
1096         val = src->tt_srpt->title[src->title].nr_of_angles;
1097       } else {
1098         GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1099             format, gst_format_get_name (format));
1100         return FALSE;
1101       }
1102       break;
1103     }
1104   }
1105
1106   GST_LOG_OBJECT (src, "duration = %" G_GINT64_FORMAT " %s", val,
1107       gst_format_get_name (format));
1108
1109   gst_query_set_duration (query, format, val);
1110   return TRUE;
1111 }
1112
1113 static gboolean
1114 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
1115 {
1116   GstFormat format;
1117   gint64 val;
1118
1119   gst_query_parse_position (query, &format, NULL);
1120
1121   switch (format) {
1122     case GST_FORMAT_BYTES:{
1123       val = src->cur_pack * DVD_VIDEO_LB_LEN;
1124       break;
1125     }
1126     default:{
1127       if (format == sector_format) {
1128         val = src->cur_pack;
1129       } else if (format == title_format) {
1130         val = src->title;
1131       } else if (format == chapter_format) {
1132         val = src->chapter;
1133       } else if (format == angle_format) {
1134         val = src->angle;
1135       } else {
1136         GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1137             format, gst_format_get_name (format));
1138         return FALSE;
1139       }
1140       break;
1141     }
1142   }
1143
1144   GST_LOG_OBJECT (src, "position = %" G_GINT64_FORMAT " %s", val,
1145       gst_format_get_name (format));
1146
1147   gst_query_set_position (query, format, val);
1148   return TRUE;
1149 }
1150
1151 static gboolean
1152 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1153 {
1154   GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1155   gboolean started;
1156   gboolean res = TRUE;
1157
1158   GST_LOG_OBJECT (src, "handling %s query",
1159       gst_query_type_get_name (GST_QUERY_TYPE (query)));
1160
1161   GST_OBJECT_LOCK (src);
1162   started = (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED));
1163   GST_OBJECT_UNLOCK (src);
1164
1165   if (!started) {
1166     GST_DEBUG_OBJECT (src, "query failed: not started");
1167     return FALSE;
1168   }
1169
1170   switch (GST_QUERY_TYPE (query)) {
1171     case GST_QUERY_DURATION:
1172       GST_OBJECT_LOCK (src);
1173       res = gst_dvd_read_src_do_duration_query (src, query);
1174       GST_OBJECT_UNLOCK (src);
1175       break;
1176     case GST_QUERY_POSITION:
1177       GST_OBJECT_LOCK (src);
1178       res = gst_dvd_read_src_do_position_query (src, query);
1179       GST_OBJECT_UNLOCK (src);
1180       break;
1181     default:
1182       res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1183       break;
1184   }
1185
1186   return res;
1187 }
1188
1189 static gboolean
1190 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1191 {
1192   gint seek_to = src->cur_pack;
1193   gint chapter, sectors, next, cur, i;
1194
1195   /* retrieve position */
1196   src->cur_pack = 0;
1197   for (i = 0; i < src->num_chapters; i++) {
1198     gint c1, c2;
1199
1200     cur_title_get_chapter_bounds (src, i, &c1, &c2);
1201
1202     for (next = cur = c1; cur < c2;) {
1203       if (next != cur) {
1204         sectors =
1205             src->cur_pgc->cell_playback[cur].last_sector -
1206             src->cur_pgc->cell_playback[cur].first_sector;
1207         if (src->cur_pack + sectors > seek_to) {
1208           chapter = i;
1209           goto done;
1210         }
1211         src->cur_pack += sectors;
1212       }
1213       cur = next;
1214       if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1215         cur += angle;
1216       next = gst_dvd_read_src_get_next_cell_for (src, cur);
1217     }
1218   }
1219
1220   GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1221
1222   return FALSE;
1223
1224 done:
1225   {
1226     /* so chapter $chapter and cell $cur contain our sector
1227      * of interest. Let's go there! */
1228     GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1229         chapter, cur);
1230
1231     gst_dvd_read_src_goto_chapter (src, chapter);
1232     src->cur_cell = cur;
1233     src->next_cell = next;
1234     src->new_cell = FALSE;
1235     src->cur_pack = seek_to;
1236
1237     return TRUE;
1238   }
1239 }
1240
1241
1242 /*** URI interface ***/
1243
1244 static GstURIType
1245 gst_dvd_read_src_uri_get_type (void)
1246 {
1247   return GST_URI_SRC;
1248 }
1249
1250 static gchar **
1251 gst_dvd_read_src_uri_get_protocols (void)
1252 {
1253   static gchar *protocols[] = { "dvd", NULL };
1254
1255   return protocols;
1256 }
1257
1258 static const gchar *
1259 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1260 {
1261   GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1262
1263   GST_OBJECT_LOCK (src);
1264
1265   g_free (src->last_uri);
1266   src->last_uri = g_strdup_printf ("dvd://%d,%d,%d", src->uri_title,
1267       src->uri_chapter, src->uri_angle);
1268
1269   GST_OBJECT_UNLOCK (src);
1270
1271   return src->last_uri;
1272 }
1273
1274 static gboolean
1275 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1276 {
1277   GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1278   gboolean ret;
1279   gchar *protocol;
1280
1281   protocol = gst_uri_get_protocol (uri);
1282   ret = (protocol != NULL && g_str_equal (protocol, "dvd"));
1283   g_free (protocol);
1284   protocol = NULL;
1285
1286   if (!ret)
1287     return ret;
1288
1289   /* parse out the new t/c/a and seek to them */
1290   {
1291     gchar *location = NULL;
1292     gchar **strs;
1293     gchar **strcur;
1294     gint pos = 0;
1295
1296     location = gst_uri_get_location (uri);
1297
1298     if (!location)
1299       return ret;
1300
1301     GST_OBJECT_LOCK (src);
1302
1303     src->uri_title = 1;
1304     src->uri_chapter = 1;
1305     src->uri_angle = 1;
1306
1307     strcur = strs = g_strsplit (location, ",", 0);
1308     while (strcur && *strcur) {
1309       gint val;
1310
1311       if (!sscanf (*strcur, "%d", &val))
1312         break;
1313
1314       if (val <= 0) {
1315         g_warning ("Invalid value %d in URI '%s'. Must be 1 or greater",
1316             val, location);
1317         break;
1318       }
1319
1320       switch (pos) {
1321         case 0:
1322           src->uri_title = val;
1323           break;
1324         case 1:
1325           src->uri_chapter = val;
1326           break;
1327         case 2:
1328           src->uri_angle = val;
1329           break;
1330       }
1331
1332       strcur++;
1333       pos++;
1334     }
1335
1336     if (pos > 0 && GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1337       src->title = src->uri_title - 1;
1338       src->chapter = src->uri_chapter - 1;
1339       src->angle = src->uri_angle - 1;
1340       src->new_seek = TRUE;
1341     }
1342
1343     GST_OBJECT_UNLOCK (src);
1344
1345     g_strfreev (strs);
1346     g_free (location);
1347   }
1348
1349   return ret;
1350 }
1351
1352 static void
1353 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1354 {
1355   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1356
1357   iface->get_type = gst_dvd_read_src_uri_get_type;
1358   iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1359   iface->get_uri = gst_dvd_read_src_uri_get_uri;
1360   iface->set_uri = gst_dvd_read_src_uri_set_uri;
1361 }
1362
1363 static void
1364 gst_dvd_read_src_do_init (GType dvdreadsrc_type)
1365 {
1366   static const GInterfaceInfo urihandler_info = {
1367     gst_dvd_read_src_uri_handler_init,
1368     NULL,
1369     NULL
1370   };
1371
1372   g_type_add_interface_static (dvdreadsrc_type, GST_TYPE_URI_HANDLER,
1373       &urihandler_info);
1374
1375   title_format = gst_format_register ("title", "DVD title");
1376   angle_format = gst_format_register ("angle", "DVD angle");
1377   sector_format = gst_format_register ("sector", "DVD sector");
1378   chapter_format = gst_format_register ("chapter", "DVD chapter");
1379 }
1380
1381 static gboolean
1382 plugin_init (GstPlugin * plugin)
1383 {
1384   GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1385       "DVD reader element based on dvdreadsrc");
1386
1387   if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,
1388           GST_TYPE_DVD_READ_SRC)) {
1389     return FALSE;
1390   }
1391
1392   return TRUE;
1393 }
1394
1395 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1396     GST_VERSION_MINOR,
1397     "dvdread",
1398     "Access a DVD with dvdread",
1399     plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);