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