d2b17a9d7459b65d25debb1d80c5193b959a9522
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 GstElementDetails gst_dvd_read_src_details = {
53 "DVD Source",
54 "Source/File/DVD",
55 "Access a DVD title/chapter/angle using libdvdread",
56 "Erik Walthinsen <omega@cse.ogi.edu>",
57 };
59 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
60 GST_PAD_SRC,
61 GST_PAD_ALWAYS,
62 GST_STATIC_CAPS ("video/mpeg, mpegversion=2, systemstream=(boolean)true"));
64 static GstFormat title_format;
65 static GstFormat angle_format;
66 static GstFormat sector_format;
67 static GstFormat chapter_format;
69 static gboolean gst_dvd_read_src_start (GstBaseSrc * basesrc);
70 static gboolean gst_dvd_read_src_stop (GstBaseSrc * basesrc);
71 static GstFlowReturn gst_dvd_read_src_create (GstPushSrc * pushsrc,
72 GstBuffer ** buf);
73 static gboolean gst_dvd_read_src_src_query (GstBaseSrc * basesrc,
74 GstQuery * query);
75 static gboolean gst_dvd_read_src_src_event (GstBaseSrc * basesrc,
76 GstEvent * event);
77 static gboolean gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title,
78 gint angle);
79 static gboolean gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src,
80 gint chapter);
81 static gboolean gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, gint angle);
82 static void gst_dvd_read_src_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
84 static void gst_dvd_read_src_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
86 static GstEvent *gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
87 const guint * clut);
88 static gboolean gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size);
89 static gboolean gst_dvd_read_src_do_seek (GstBaseSrc * src, GstSegment * s);
90 static gint64 gst_dvd_read_src_convert_timecode (dvd_time_t * time);
91 static gint gst_dvd_read_src_get_next_cell (GstDvdReadSrc * src,
92 pgc_t * pgc, gint cell);
93 static GstClockTime gst_dvd_read_src_get_time_for_sector (GstDvdReadSrc * src,
94 guint sector);
95 static gint gst_dvd_read_src_get_sector_from_time (GstDvdReadSrc * src,
96 GstClockTime ts);
98 GST_BOILERPLATE_FULL (GstDvdReadSrc, gst_dvd_read_src, GstPushSrc,
99 GST_TYPE_PUSH_SRC, gst_dvd_read_src_do_init);
101 static void
102 gst_dvd_read_src_base_init (gpointer g_class)
103 {
104 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
106 gst_element_class_add_pad_template (element_class,
107 gst_static_pad_template_get (&srctemplate));
109 gst_element_class_set_details (element_class, &gst_dvd_read_src_details);
110 }
112 static void
113 gst_dvd_read_src_finalize (GObject * object)
114 {
115 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
117 g_free (src->location);
118 g_free (src->last_uri);
120 G_OBJECT_CLASS (parent_class)->finalize (object);
121 }
123 static void
124 gst_dvd_read_src_init (GstDvdReadSrc * src, GstDvdReadSrcClass * klass)
125 {
126 src->dvd = NULL;
127 src->vts_file = NULL;
128 src->vmg_file = NULL;
129 src->dvd_title = NULL;
131 src->location = g_strdup ("/dev/dvd");
132 src->last_uri = NULL;
133 src->new_seek = TRUE;
134 src->new_cell = TRUE;
135 src->change_cell = FALSE;
136 src->uri_title = 1;
137 src->uri_chapter = 1;
138 src->uri_angle = 1;
140 src->title_lang_event_pending = NULL;
141 src->pending_clut_event = NULL;
143 gst_pad_use_fixed_caps (GST_BASE_SRC_PAD (src));
144 gst_pad_set_caps (GST_BASE_SRC_PAD (src),
145 gst_static_pad_template_get_caps (&srctemplate));
146 }
148 static gboolean
149 gst_dvd_read_src_is_seekable (GstBaseSrc * src)
150 {
151 return TRUE;
152 }
154 static void
155 gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
156 {
157 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
158 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
159 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
161 gobject_class->finalize = gst_dvd_read_src_finalize;
162 gobject_class->set_property = gst_dvd_read_src_set_property;
163 gobject_class->get_property = gst_dvd_read_src_get_property;
165 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
166 g_param_spec_string ("device", "Device",
167 "DVD device location", NULL, G_PARAM_READWRITE));
168 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TITLE,
169 g_param_spec_int ("title", "title", "title",
170 1, 999, 1, G_PARAM_READWRITE));
171 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHAPTER,
172 g_param_spec_int ("chapter", "chapter", "chapter",
173 1, 999, 1, G_PARAM_READWRITE));
174 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ANGLE,
175 g_param_spec_int ("angle", "angle", "angle",
176 1, 999, 1, G_PARAM_READWRITE));
178 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
179 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
180 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_query);
181 gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_event);
182 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_dvd_read_src_do_seek);
183 gstbasesrc_class->is_seekable =
184 GST_DEBUG_FUNCPTR (gst_dvd_read_src_is_seekable);
186 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dvd_read_src_create);
187 }
189 static gboolean
190 gst_dvd_read_src_start (GstBaseSrc * basesrc)
191 {
192 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
194 g_return_val_if_fail (src->location != NULL, FALSE);
196 GST_DEBUG_OBJECT (src, "Opening DVD '%s'", src->location);
198 if ((src->dvd = DVDOpen (src->location)) == NULL)
199 goto open_failed;
201 /* Load the video manager to find out the information about the titles */
202 GST_DEBUG_OBJECT (src, "Loading VMG info");
204 if (!(src->vmg_file = ifoOpen (src->dvd, 0)))
205 goto ifo_open_failed;
207 src->tt_srpt = src->vmg_file->tt_srpt;
209 src->title = src->uri_title - 1;
210 src->chapter = src->uri_chapter - 1;
211 src->angle = src->uri_angle - 1;
213 if (!gst_dvd_read_src_goto_title (src, src->title, src->angle))
214 goto title_open_failed;
216 if (!gst_dvd_read_src_goto_chapter (src, src->chapter))
217 goto chapter_open_failed;
219 src->new_seek = FALSE;
220 src->change_cell = TRUE;
222 return TRUE;
224 /* ERRORS */
225 open_failed:
226 {
227 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
228 (_("Could not open DVD")),
229 ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
230 return FALSE;
231 }
232 ifo_open_failed:
233 {
234 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
235 (_("Could not open DVD")),
236 ("ifoOpen() failed: %s", g_strerror (errno)));
237 return FALSE;
238 }
239 title_open_failed:
240 {
241 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
242 (_("Could not open DVD title %d"), src->uri_title), (NULL));
243 return FALSE;
244 }
245 chapter_open_failed:
246 {
247 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
248 (_("Failed to go to chapter %d of DVD title %d"),
249 src->uri_chapter, src->uri_title), (NULL));
250 return FALSE;
251 }
252 }
254 static gboolean
255 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
256 {
257 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
259 if (src->vts_file) {
260 ifoClose (src->vts_file);
261 src->vts_file = NULL;
262 }
263 if (src->vmg_file) {
264 ifoClose (src->vmg_file);
265 src->vmg_file = NULL;
266 }
267 if (src->dvd_title) {
268 DVDCloseFile (src->dvd_title);
269 src->dvd_title = NULL;
270 }
271 if (src->dvd) {
272 DVDClose (src->dvd);
273 src->dvd = NULL;
274 }
275 src->new_cell = TRUE;
276 src->new_seek = TRUE;
277 src->change_cell = FALSE;
278 src->chapter = 0;
279 src->title = 0;
280 src->need_newsegment = TRUE;
281 src->vts_tmapt = NULL;
282 if (src->title_lang_event_pending) {
283 gst_event_unref (src->title_lang_event_pending);
284 src->title_lang_event_pending = NULL;
285 }
286 if (src->pending_clut_event) {
287 gst_event_unref (src->pending_clut_event);
288 src->pending_clut_event = NULL;
289 }
290 if (src->chapter_starts) {
291 g_free (src->chapter_starts);
292 src->chapter_starts = NULL;
293 }
295 GST_LOG_OBJECT (src, "closed DVD");
297 return TRUE;
298 }
300 static void
301 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
302 gint * p_pgc_id, pgc_t ** p_pgc)
303 {
304 pgc_t *pgc;
305 gint pgn, pgc_id;
307 g_assert (chapter >= 0 && chapter < src->num_chapters);
309 pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
310 pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
311 pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
313 *p_pgn = pgn;
314 *p_pgc_id = pgc_id;
315 *p_pgc = pgc;
316 }
318 static void
319 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
320 gint * p_first_cell, gint * p_last_cell)
321 {
322 pgc_t *pgc;
323 gint pgn, pgc_id, pgn_next_ch;
325 g_assert (chapter >= 0 && chapter < src->num_chapters);
327 cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
329 *p_first_cell = pgc->program_map[pgn - 1] - 1;
331 if (chapter == (src->num_chapters - 1)) {
332 *p_last_cell = pgc->nr_of_cells;
333 } else {
334 pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
335 *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
336 }
337 }
339 static gboolean
340 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
341 {
342 gint i;
344 /* make sure the chapter number is valid for this title */
345 if (chapter < 0 || chapter >= src->num_chapters) {
346 GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
347 chapter, src->num_chapters);
348 chapter = CLAMP (chapter, 0, src->num_chapters - 1);
349 }
351 /* determine which program chain we want to watch. This is
352 * based on the chapter number */
353 cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
354 &src->cur_pgc);
355 cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
356 &src->last_cell);
358 GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter + 1,
359 src->start_cell, src->last_cell);
361 /* retrieve position */
362 src->cur_pack = 0;
363 for (i = 0; i < chapter; i++) {
364 gint c1, c2;
366 cur_title_get_chapter_bounds (src, i, &c1, &c2);
368 while (c1 < c2) {
369 src->cur_pack +=
370 src->cur_pgc->cell_playback[c1].last_sector -
371 src->cur_pgc->cell_playback[c1].first_sector;
372 ++c1;
373 }
374 }
376 /* prepare reading for new cell */
377 src->new_cell = TRUE;
378 src->next_cell = src->start_cell;
380 src->chapter = chapter;
382 if (src->pending_clut_event)
383 gst_event_unref (src->pending_clut_event);
385 src->pending_clut_event =
386 gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
388 return TRUE;
389 }
391 static void
392 gst_dvd_read_src_get_chapter_starts (GstDvdReadSrc * src)
393 {
394 GstClockTime uptohere;
395 guint c;
397 g_free (src->chapter_starts);
398 src->chapter_starts = g_new (GstClockTime, src->num_chapters);
400 uptohere = (GstClockTime) 0;
401 for (c = 0; c < src->num_chapters; ++c) {
402 GstClockTime chapter_duration = 0;
403 gint cell_start, cell_end, cell;
404 gint pgn, pgc_id;
405 pgc_t *pgc;
407 cur_title_get_chapter_pgc (src, c, &pgn, &pgc_id, &pgc);
408 cur_title_get_chapter_bounds (src, c, &cell_start, &cell_end);
410 cell = cell_start;
411 while (cell < cell_end) {
412 dvd_time_t *cell_duration;
414 cell_duration = &pgc->cell_playback[cell].playback_time;
415 chapter_duration += gst_dvd_read_src_convert_timecode (cell_duration);
416 cell = gst_dvd_read_src_get_next_cell (src, pgc, cell);
417 }
419 src->chapter_starts[c] = uptohere;
421 GST_INFO_OBJECT (src, "[%02u] Chapter %02u starts at %" GST_TIME_FORMAT
422 ", dur = %" GST_TIME_FORMAT ", cells %d-%d", src->title + 1, c + 1,
423 GST_TIME_ARGS (uptohere), GST_TIME_ARGS (chapter_duration),
424 cell_start, cell_end);
426 uptohere += chapter_duration;
427 }
428 }
430 static gboolean
431 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
432 {
433 GstStructure *s;
434 gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
435 gint title_set_nr;
436 gint num_titles;
437 gint i;
439 /* make sure our title number is valid */
440 num_titles = src->tt_srpt->nr_of_srpts;
441 GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
442 if (title < 0 || title >= num_titles)
443 goto invalid_title;
445 src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
446 GST_INFO_OBJECT (src, "Title %d has %d chapters", title + 1,
447 src->num_chapters);
449 /* make sure the angle number is valid for this title */
450 src->num_angles = src->tt_srpt->title[title].nr_of_angles;
451 GST_LOG_OBJECT (src, "Title %d has %d angles", title + 1, src->num_angles);
452 if (angle < 0 || angle >= src->num_angles) {
453 GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
454 angle, src->num_angles);
455 angle = CLAMP (angle, 0, src->num_angles - 1);
456 }
458 /* load the VTS information for the title set our title is in */
459 title_set_nr = src->tt_srpt->title[title].title_set_nr;
460 src->vts_file = ifoOpen (src->dvd, title_set_nr);
461 if (src->vts_file == NULL)
462 goto ifo_open_failed;
464 src->ttn = src->tt_srpt->title[title].vts_ttn;
465 src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
467 /* interactive title? */
468 if (src->num_chapters > 0 &&
469 src->vts_ptt_srpt->title[src->ttn - 1].ptt[0].pgn == 0) {
470 goto commands_only_pgc;
471 }
473 /* we've got enough info, time to open the title set data */
474 src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
475 if (src->dvd_title == NULL)
476 goto title_open_failed;
478 GST_INFO_OBJECT (src, "Opened title %d, angle %d", title + 1, angle);
479 src->title = title;
480 src->angle = angle;
482 /* build event */
484 if (src->title_lang_event_pending) {
485 gst_event_unref (src->title_lang_event_pending);
486 src->title_lang_event_pending = NULL;
487 }
489 s = gst_structure_new ("application/x-gst-dvd",
490 "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
492 /* audio */
493 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
494 const audio_attr_t *a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
496 t = g_strdup_printf ("audio-%d-format", i);
497 gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
498 g_free (t);
500 if (a->lang_type) {
501 t = g_strdup_printf ("audio-%d-language", i);
502 lang_code[0] = (a->lang_code >> 8) & 0xff;
503 lang_code[1] = a->lang_code & 0xff;
504 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
505 g_free (t);
506 } else {
507 lang_code[0] = '\0';
508 }
510 GST_INFO_OBJECT (src, "[%02d] Audio %02d: lang='%s', format=%d",
511 src->title + 1, i, lang_code, (gint) a->audio_format);
512 }
514 /* subtitle */
515 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
516 const subp_attr_t *u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
518 if (u->type) {
519 t = g_strdup_printf ("subtitle-%d-language", i);
520 lang_code[0] = (u->lang_code >> 8) & 0xff;
521 lang_code[1] = u->lang_code & 0xff;
522 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
523 g_free (t);
524 } else {
525 lang_code[0] = '\0';
526 }
528 GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', format=%d",
529 src->title + 1, i, lang_code);
530 }
532 src->title_lang_event_pending =
533 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
535 /* dump seek tables */
536 src->vts_tmapt = src->vts_file->vts_tmapt;
537 if (src->vts_tmapt) {
538 gint i, j;
540 GST_LOG_OBJECT (src, "nr_of_tmaps = %d", src->vts_tmapt->nr_of_tmaps);
541 for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
542 GST_LOG_OBJECT (src, "======= Table %d ===================", i);
543 GST_LOG_OBJECT (src, "Offset relative to VTS_TMAPTI: %d",
544 src->vts_tmapt->tmap_offset[i]);
545 GST_LOG_OBJECT (src, "Time unit (seconds) : %d",
546 src->vts_tmapt->tmap[i].tmu);
547 GST_LOG_OBJECT (src, "Number of entries : %d",
548 src->vts_tmapt->tmap[i].nr_of_entries);
549 for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; j++) {
550 guint64 time;
552 time = src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
553 GST_LOG_OBJECT (src, "Time: %" GST_TIME_FORMAT " VOBU "
554 "Sector: 0x%08x %s", GST_TIME_ARGS (time),
555 src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff,
556 (src->vts_tmapt->tmap[i].map_ent[j] >> 31) ? "discontinuity" : "");
557 }
558 }
559 } else {
560 GST_WARNING_OBJECT (src, "no vts_tmapt - seeking will suck");
561 }
563 gst_dvd_read_src_get_chapter_starts (src);
565 return TRUE;
567 /* ERRORS */
568 invalid_title:
569 {
570 GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
571 title, num_titles);
572 return FALSE;
573 }
574 ifo_open_failed:
575 {
576 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
577 (_("Could not open DVD title %d"), title_set_nr),
578 ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
579 return FALSE;
580 }
581 title_open_failed:
582 {
583 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
584 (_("Could not open DVD title %d"), title_set_nr),
585 ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
586 return FALSE;
587 }
588 commands_only_pgc:
589 {
590 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
591 (_("Could not open DVD title %d. Interactive titles are not supported "
592 "by this element"), title_set_nr),
593 ("Commands-only PGC, not supported, use dvdnavsrc"));
594 return FALSE;
595 }
596 }
598 /* FIXME: double-check this function, compare against original */
599 static gint
600 gst_dvd_read_src_get_next_cell (GstDvdReadSrc * src, pgc_t * pgc, gint cell)
601 {
602 /* Check if we're entering an angle block. */
603 if (pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
604 return (cell + 1);
606 while (pgc->cell_playback[cell].block_mode == BLOCK_MODE_LAST_CELL)
607 ++cell;
609 return cell + 1; /* really +1? (tpm) */
610 }
612 /* Returns true if the pack is a NAV pack */
613 static gboolean
614 gst_dvd_read_src_is_nav_pack (const guint8 * data)
615 {
616 if (GST_READ_UINT32_BE (data + 0x26) != 0x000001BF)
617 return FALSE;
619 /* Check that this is substream 0 (PCI) */
620 if (data[0x2c] != 0)
621 return FALSE;
623 if (GST_READ_UINT32_BE (data + 0x400) != 0x000001BF)
624 return FALSE;
626 /* Check that this is substream 1 (DSI) */
627 if (data[0x406] != 1)
628 return FALSE;
630 /* Check sizes of PCI and DSI packets */
631 if (GST_READ_UINT16_BE (data + 0x2a) != 0x03d4)
632 return FALSE;
634 if (GST_READ_UINT16_BE (data + 0x404) != 0x03fa)
635 return FALSE;
637 return TRUE;
638 }
640 /* find time for sector from index, returns NONE if there is no exact match */
641 static GstClockTime
642 gst_dvd_read_src_get_time_for_sector (GstDvdReadSrc * src, guint sector)
643 {
644 gint i, j;
646 if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps == 0)
647 return GST_CLOCK_TIME_NONE;
649 for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
650 for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; ++j) {
651 if ((src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff) == sector)
652 return src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
653 }
654 }
656 if (sector == 0)
657 return (GstClockTime) 0;
659 return GST_CLOCK_TIME_NONE;
660 }
662 /* returns the sector in the index at (or before) the given time, or -1 */
663 static gint
664 gst_dvd_read_src_get_sector_from_time (GstDvdReadSrc * src, GstClockTime ts)
665 {
666 gint sector, i, j;
668 if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps == 0)
669 return -1;
671 sector = 0;
672 for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
673 for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; ++j) {
674 GstClockTime entry_time;
676 entry_time = src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
677 if (entry_time <= ts) {
678 sector = src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff;
679 }
680 if (entry_time >= ts) {
681 return sector;
682 }
683 }
684 }
686 if (ts == 0)
687 return 0;
689 return -1;
690 }
692 typedef enum
693 {
694 GST_DVD_READ_OK = 0,
695 GST_DVD_READ_ERROR = -1,
696 GST_DVD_READ_EOS = -2,
697 GST_DVD_READ_AGAIN = -3
698 } GstDvdReadReturn;
700 static GstDvdReadReturn
701 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
702 GstBuffer ** p_buf)
703 {
704 GstBuffer *buf;
705 guint8 oneblock[DVD_VIDEO_LB_LEN];
706 dsi_t dsi_pack;
707 guint next_vobu, next_ilvu_start, cur_output_size;
708 gint len;
710 /* playback by cell in this pgc, starting at the cell for our chapter */
711 if (new_seek)
712 src->cur_cell = src->start_cell;
714 again:
716 if (src->cur_cell >= src->last_cell) {
717 /* advance to next chapter */
718 if (src->chapter == (src->num_chapters - 1))
719 goto eos;
721 GST_INFO_OBJECT (src, "end of chapter %d, switch to next",
722 src->chapter + 1);
724 ++src->chapter;
725 gst_dvd_read_src_goto_chapter (src, src->chapter);
727 return GST_DVD_READ_AGAIN;
728 }
730 if (src->new_cell || new_seek) {
731 if (!new_seek) {
732 src->cur_cell = src->next_cell;
733 if (src->cur_cell >= src->last_cell) {
734 GST_LOG_OBJECT (src, "last cell in chapter");
735 goto again;
736 }
737 }
739 /* take angle into account */
740 if (src->cur_pgc->cell_playback[src->cur_cell].block_type
741 == BLOCK_TYPE_ANGLE_BLOCK)
742 src->cur_cell += angle;
744 /* calculate next cell */
745 src->next_cell =
746 gst_dvd_read_src_get_next_cell (src, src->cur_pgc, src->cur_cell);
748 /* we loop until we're out of this cell */
749 src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
750 src->new_cell = FALSE;
751 }
753 if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
754 src->new_cell = TRUE;
755 GST_LOG_OBJECT (src, "Beyond last sector, go to next cell");
756 return GST_DVD_READ_AGAIN;
757 }
759 /* read NAV packet */
760 nav_retry:
762 len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, oneblock);
763 if (len == 0)
764 goto read_error;
766 if (!gst_dvd_read_src_is_nav_pack (oneblock)) {
767 src->cur_pack++;
768 goto nav_retry;
769 }
771 /* parse the contained dsi packet */
772 navRead_DSI (&dsi_pack, &oneblock[DSI_START_BYTE]);
773 g_assert (src->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn);
775 /* determine where we go next. These values are the ones we
776 * mostly care about */
777 next_ilvu_start = src->cur_pack + dsi_pack.sml_agli.data[angle].address;
778 cur_output_size = dsi_pack.dsi_gi.vobu_ea;
780 /* If we're not at the end of this cell, we can determine the next
781 * VOBU to display using the VOBU_SRI information section of the
782 * DSI. Using this value correctly follows the current angle,
783 * avoiding the doubled scenes in The Matrix, and makes our life
784 * really happy.
785 *
786 * Otherwise, we set our next address past the end of this cell to
787 * force the code above to go to the next cell in the program. */
788 if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
789 next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
790 } else {
791 next_vobu = src->cur_pack + cur_output_size + 1;
792 }
794 g_assert (cur_output_size < 1024);
795 ++src->cur_pack;
797 /* create the buffer (TODO: use buffer pool?) */
798 buf = gst_buffer_new_and_alloc (cur_output_size * DVD_VIDEO_LB_LEN);
800 /* read in and output cursize packs */
801 len = DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size,
802 GST_BUFFER_DATA (buf));
804 if (len != cur_output_size)
805 goto block_read_error;
807 GST_BUFFER_SIZE (buf) = cur_output_size * DVD_VIDEO_LB_LEN;
808 /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
809 GST_BUFFER_TIMESTAMP (buf) =
810 gst_dvd_read_src_get_time_for_sector (src, src->cur_pack);
812 gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (src)));
814 *p_buf = buf;
816 src->cur_pack = next_vobu;
818 GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
820 return GST_DVD_READ_OK;
822 /* ERRORS */
823 eos:
824 {
825 GST_INFO_OBJECT (src, "last chapter done - EOS");
826 return GST_DVD_READ_EOS;
827 }
828 read_error:
829 {
830 GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
831 return GST_DVD_READ_ERROR;
832 }
833 block_read_error:
834 {
835 GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
836 cur_output_size, src->cur_pack);
837 gst_buffer_unref (buf);
838 return GST_DVD_READ_ERROR;
839 }
840 }
842 static GstFlowReturn
843 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
844 {
845 GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
846 GstPad *srcpad;
847 gint res;
849 g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
851 srcpad = GST_BASE_SRC (src)->srcpad;
853 if (src->need_newsegment) {
854 gst_pad_push_event (srcpad,
855 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
856 src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
857 src->need_newsegment = FALSE;
858 }
860 if (src->new_seek) {
861 gst_dvd_read_src_goto_title (src, src->title, src->angle);
862 gst_dvd_read_src_goto_chapter (src, src->chapter);
864 src->new_seek = FALSE;
865 src->change_cell = TRUE;
866 }
868 if (src->title_lang_event_pending) {
869 gst_pad_push_event (srcpad, src->title_lang_event_pending);
870 src->title_lang_event_pending = NULL;
871 }
873 if (src->pending_clut_event) {
874 gst_pad_push_event (srcpad, src->pending_clut_event);
875 src->pending_clut_event = NULL;
876 }
878 /* read it in */
879 do {
880 res = gst_dvd_read_src_read (src, src->angle, src->change_cell, p_buf);
881 } while (res == GST_DVD_READ_AGAIN);
883 switch (res) {
884 case GST_DVD_READ_ERROR:{
885 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), (NULL));
886 return GST_FLOW_ERROR;
887 }
888 case GST_DVD_READ_EOS:{
889 GST_INFO_OBJECT (src, "Reached EOS");
890 return GST_FLOW_UNEXPECTED;
891 }
892 case GST_DVD_READ_OK:{
893 src->change_cell = FALSE;
894 return GST_FLOW_OK;
895 }
896 default:
897 break;
898 }
900 g_return_val_if_reached (GST_FLOW_UNEXPECTED);
901 }
903 static void
904 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
905 const GValue * value, GParamSpec * pspec)
906 {
907 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
908 gboolean started;
910 GST_OBJECT_LOCK (src);
911 started = GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED);
913 switch (prop_id) {
914 case ARG_DEVICE:{
915 if (started) {
916 g_warning ("%s: property '%s' needs to be set before the device is "
917 "opened", GST_ELEMENT_NAME (src), pspec->name);
918 break;;
919 }
921 g_free (src->location);
922 /* clear the filename if we get a NULL (is that possible?) */
923 if (g_value_get_string (value) == NULL) {
924 src->location = g_strdup ("/dev/dvd");
925 } else {
926 src->location = g_strdup (g_value_get_string (value));
927 }
928 break;
929 }
930 case ARG_TITLE:
931 src->uri_title = g_value_get_int (value);
932 if (started) {
933 src->title = src->uri_title - 1;
934 src->new_seek = TRUE;
935 }
936 break;
937 case ARG_CHAPTER:
938 src->uri_chapter = g_value_get_int (value);
939 if (started) {
940 src->chapter = src->uri_chapter - 1;
941 src->new_seek = TRUE;
942 }
943 break;
944 case ARG_ANGLE:
945 src->uri_angle = g_value_get_int (value);
946 if (started) {
947 src->angle = src->uri_angle - 1;
948 }
949 break;
950 default:
951 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
952 break;
953 }
955 GST_OBJECT_UNLOCK (src);
956 }
958 static void
959 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
960 GParamSpec * pspec)
961 {
962 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
964 GST_OBJECT_LOCK (src);
966 switch (prop_id) {
967 case ARG_DEVICE:
968 g_value_set_string (value, src->location);
969 break;
970 case ARG_TITLE:
971 g_value_set_int (value, src->uri_title);
972 break;
973 case ARG_CHAPTER:
974 g_value_set_int (value, src->uri_chapter);
975 break;
976 case ARG_ANGLE:
977 g_value_set_int (value, src->uri_angle);
978 break;
979 default:
980 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
981 break;
982 }
984 GST_OBJECT_UNLOCK (src);
985 }
987 static gboolean
988 gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size)
989 {
990 gboolean ret = FALSE;
992 if (src->dvd_title) {
993 gsize blocks;
995 blocks = DVDFileSize (src->dvd_title);
996 if (blocks >= 0) {
997 *size = (gint64) blocks *DVD_VIDEO_LB_LEN;
999 ret = TRUE;
1000 } else {
1001 GST_WARNING_OBJECT (src, "DVDFileSize(%p) failed!", src->dvd_title);
1002 }
1003 }
1005 return ret;
1006 }
1008 /*** Querying and seeking ***/
1010 static gboolean
1011 gst_dvd_read_src_handle_seek_event (GstDvdReadSrc * src, GstEvent * event)
1012 {
1013 GstSeekFlags flags;
1014 GstSeekType cur_type, end_type;
1015 gint64 new_off, total;
1016 GstFormat format;
1017 GstPad *srcpad;
1018 gboolean query_ok;
1019 gdouble rate;
1021 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
1022 &end_type, NULL);
1024 if (rate <= 0.0) {
1025 GST_DEBUG_OBJECT (src, "cannot do backwards playback yet");
1026 return FALSE;
1027 }
1029 if ((flags & GST_SEEK_FLAG_SEGMENT) != 0) {
1030 GST_DEBUG_OBJECT (src, "segment seek not supported");
1031 return FALSE;
1032 }
1034 if ((flags & GST_SEEK_FLAG_FLUSH) == 0) {
1035 GST_DEBUG_OBJECT (src, "can only do flushing seeks at the moment");
1036 return FALSE;
1037 }
1039 if (end_type != GST_SEEK_TYPE_NONE) {
1040 GST_DEBUG_OBJECT (src, "end seek type not supported");
1041 return FALSE;
1042 }
1044 if (cur_type != GST_SEEK_TYPE_SET) {
1045 GST_DEBUG_OBJECT (src, "only SEEK_TYPE_SET is supported");
1046 return FALSE;
1047 }
1049 if (format == angle_format) {
1050 GST_OBJECT_LOCK (src);
1051 if (new_off < 0 || new_off >= src->num_angles) {
1052 GST_OBJECT_UNLOCK (src);
1053 GST_DEBUG_OBJECT (src, "invalid angle %d, only %d available",
1054 src->num_angles);
1055 return FALSE;
1056 }
1057 src->angle = (gint) new_off;
1058 GST_OBJECT_UNLOCK (src);
1059 GST_DEBUG_OBJECT (src, "switched to angle %d", (gint) new_off + 1);
1060 return TRUE;
1061 }
1063 if (format != chapter_format && format != title_format &&
1064 format != GST_FORMAT_BYTES && format != GST_FORMAT_TIME) {
1065 GST_DEBUG_OBJECT (src, "unsupported seek format %d (%s)", format,
1066 gst_format_get_name (format));
1067 return FALSE;
1068 }
1070 if (format == GST_FORMAT_BYTES) {
1071 GST_DEBUG_OBJECT (src, "Requested seek to byte %" G_GUINT64_FORMAT,
1072 new_off);
1073 } else if (format == GST_FORMAT_TIME) {
1074 GST_DEBUG_OBJECT (src, "Requested seek to time %" GST_TIME_FORMAT,
1075 GST_TIME_ARGS (new_off));
1076 if (gst_dvd_read_src_get_sector_from_time (src, new_off) < 0) {
1077 GST_DEBUG_OBJECT (src, "Can't find sector for requested time");
1078 return FALSE;
1079 }
1080 }
1082 srcpad = GST_BASE_SRC_PAD (src);
1084 /* check whether the seek looks reasonable (ie within possible range) */
1085 if (format == GST_FORMAT_BYTES) {
1086 GST_OBJECT_LOCK (src);
1087 query_ok = gst_dvd_read_src_get_size (src, &total);
1088 GST_OBJECT_UNLOCK (src);
1089 } else {
1090 query_ok = gst_pad_query_duration (srcpad, &format, &total);
1091 }
1093 if (!query_ok) {
1094 GST_DEBUG_OBJECT (src, "Failed to query duration in format %s",
1095 gst_format_get_name (format));
1096 return FALSE;
1097 }
1099 GST_DEBUG_OBJECT (src, "Total %s: %12" G_GINT64_FORMAT,
1100 gst_format_get_name (format), total);
1101 GST_DEBUG_OBJECT (src, "Seek to %s: %12" G_GINT64_FORMAT,
1102 gst_format_get_name (format), new_off);
1104 if (new_off >= total) {
1105 GST_DEBUG_OBJECT (src, "Seek position out of range");
1106 return FALSE;
1107 }
1109 /* set segment to seek format; this allows us to use the do_seek
1110 * virtual function and let the base source handle all the tricky
1111 * stuff for us. We don't use the segment internally anyway */
1112 /* FIXME: can't take the stream lock here - what to do? */
1113 GST_OBJECT_LOCK (src);
1114 GST_BASE_SRC (src)->segment.format = format;
1115 GST_BASE_SRC (src)->segment.start = 0;
1116 GST_BASE_SRC (src)->segment.stop = total;
1117 GST_BASE_SRC (src)->segment.duration = total;
1118 GST_OBJECT_UNLOCK (src);
1120 return GST_BASE_SRC_CLASS (parent_class)->event (GST_BASE_SRC (src), event);
1121 }
1123 static gboolean
1124 gst_dvd_read_src_do_seek (GstBaseSrc * basesrc, GstSegment * s)
1125 {
1126 GstDvdReadSrc *src;
1128 src = GST_DVD_READ_SRC (basesrc);
1130 GST_DEBUG_OBJECT (src, "Seeking to %s: %12" G_GINT64_FORMAT,
1131 gst_format_get_name (s->format), s->last_stop);
1133 if (s->format == sector_format || s->format == GST_FORMAT_BYTES
1134 || s->format == GST_FORMAT_TIME) {
1135 guint old;
1137 old = src->cur_pack;
1139 if (s->format == sector_format) {
1140 src->cur_pack = s->last_stop;
1141 } else if (s->format == GST_FORMAT_TIME) {
1142 gint sector;
1144 sector = gst_dvd_read_src_get_sector_from_time (src, s->last_stop);
1146 GST_DEBUG_OBJECT (src, "Time %" GST_TIME_FORMAT " => sector %d",
1147 GST_TIME_ARGS (s->last_stop), sector);
1149 /* really shouldn't happen, we've checked this earlier ... */
1150 g_return_val_if_fail (sector >= 0, FALSE);
1152 src->cur_pack = sector;
1153 } else {
1154 /* byte format */
1155 src->cur_pack = s->last_stop / DVD_VIDEO_LB_LEN;
1156 if ((src->cur_pack * DVD_VIDEO_LB_LEN) != s->last_stop) {
1157 GST_LOG_OBJECT (src, "rounded down offset %" G_GINT64_FORMAT " => %"
1158 G_GINT64_FORMAT, s->last_stop,
1159 (gint64) src->cur_pack * DVD_VIDEO_LB_LEN);
1160 }
1161 }
1163 if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
1164 GST_DEBUG_OBJECT (src, "seek to sector 0x%08x failed", src->cur_pack);
1165 src->cur_pack = old;
1166 return FALSE;
1167 }
1169 GST_LOG_OBJECT (src, "seek to sector 0x%08x ok", src->cur_pack);
1170 } else if (s->format == chapter_format) {
1171 if (!gst_dvd_read_src_goto_chapter (src, (gint) s->last_stop)) {
1172 GST_DEBUG_OBJECT (src, "seek to chapter %d failed",
1173 (gint) s->last_stop + 1);
1174 return FALSE;
1175 }
1176 GST_INFO_OBJECT (src, "seek to chapter %d ok", (gint) s->last_stop + 1);
1177 src->chapter = s->last_stop;
1178 } else if (s->format == title_format) {
1179 if (!gst_dvd_read_src_goto_title (src, (gint) s->last_stop, src->angle) ||
1180 !gst_dvd_read_src_goto_chapter (src, 0)) {
1181 GST_DEBUG_OBJECT (src, "seek to title %d failed", (gint) s->last_stop);
1182 return FALSE;
1183 }
1184 src->title = (gint) s->last_stop;
1185 src->chapter = 0;
1186 GST_INFO_OBJECT (src, "seek to title %d ok", src->title + 1);
1187 } else {
1188 g_return_val_if_reached (FALSE);
1189 }
1191 src->need_newsegment = TRUE;
1192 return TRUE;
1193 }
1195 static gboolean
1196 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
1197 {
1198 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1199 gboolean res;
1201 GST_LOG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
1203 switch (GST_EVENT_TYPE (event)) {
1204 case GST_EVENT_SEEK:
1205 res = gst_dvd_read_src_handle_seek_event (src, event);
1206 break;
1207 default:
1208 res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
1209 break;
1210 }
1212 return res;
1213 }
1215 static GstEvent *
1216 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
1217 const guint * clut)
1218 {
1219 GstStructure *structure;
1220 gchar name[16];
1221 gint i;
1223 structure = gst_structure_new ("application/x-gst-dvd",
1224 "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
1226 /* Create a separate field for each value in the table. */
1227 for (i = 0; i < 16; i++) {
1228 g_snprintf (name, sizeof (name), "clut%02d", i);
1229 gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
1230 }
1232 /* Create the DVD event and put the structure into it. */
1233 return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
1234 }
1236 static gint64
1237 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
1238 {
1239 gint64 ret_time;
1240 const gint64 one_hour = 3600 * GST_SECOND;
1241 const gint64 one_min = 60 * GST_SECOND;
1243 g_return_val_if_fail ((time->hour >> 4) < 0xa
1244 && (time->hour & 0xf) < 0xa, -1);
1245 g_return_val_if_fail ((time->minute >> 4) < 0x7
1246 && (time->minute & 0xf) < 0xa, -1);
1247 g_return_val_if_fail ((time->second >> 4) < 0x7
1248 && (time->second & 0xf) < 0xa, -1);
1250 ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
1251 ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
1252 ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
1254 return ret_time;
1255 }
1257 static gboolean
1258 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
1259 {
1260 GstFormat format;
1261 gint64 val;
1263 gst_query_parse_duration (query, &format, NULL);
1265 switch (format) {
1266 case GST_FORMAT_TIME:{
1267 if (src->cur_pgc == NULL)
1268 return FALSE;
1269 val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
1270 if (val < 0)
1271 return FALSE;
1272 break;
1273 }
1274 case GST_FORMAT_BYTES:{
1275 if (!gst_dvd_read_src_get_size (src, &val))
1276 return FALSE;
1277 break;
1278 }
1279 default:{
1280 if (format == sector_format) {
1281 val = DVDFileSize (src->dvd_title);
1282 } else if (format == title_format) {
1283 val = src->tt_srpt->nr_of_srpts;
1284 } else if (format == chapter_format) {
1285 val = src->num_chapters;
1286 } else if (format == angle_format) {
1287 val = src->tt_srpt->title[src->title].nr_of_angles;
1288 } else {
1289 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1290 format, gst_format_get_name (format));
1291 return FALSE;
1292 }
1293 break;
1294 }
1295 }
1297 GST_LOG_OBJECT (src, "duration = %" G_GINT64_FORMAT " %s", val,
1298 gst_format_get_name (format));
1300 gst_query_set_duration (query, format, val);
1301 return TRUE;
1302 }
1304 static gboolean
1305 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
1306 {
1307 GstFormat format;
1308 gint64 val;
1310 gst_query_parse_position (query, &format, NULL);
1312 switch (format) {
1313 case GST_FORMAT_BYTES:{
1314 val = src->cur_pack * DVD_VIDEO_LB_LEN;
1315 break;
1316 }
1317 default:{
1318 if (format == sector_format) {
1319 val = src->cur_pack;
1320 } else if (format == title_format) {
1321 val = src->title;
1322 } else if (format == chapter_format) {
1323 val = src->chapter;
1324 } else if (format == angle_format) {
1325 val = src->angle;
1326 } else {
1327 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1328 format, gst_format_get_name (format));
1329 return FALSE;
1330 }
1331 break;
1332 }
1333 }
1335 GST_LOG_OBJECT (src, "position = %" G_GINT64_FORMAT " %s", val,
1336 gst_format_get_name (format));
1338 gst_query_set_position (query, format, val);
1339 return TRUE;
1340 }
1342 static gboolean
1343 gst_dvd_read_src_do_convert_query (GstBaseSrc * basesrc, GstQuery * query)
1344 {
1345 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1346 GstFormat src_format, dest_format;
1347 gboolean ret = FALSE;
1348 gint64 src_val, dest_val;
1350 gst_query_parse_convert (query, &src_format, &src_val, &dest_format, NULL);
1352 if (src_format == dest_format) {
1353 dest_val = src_val;
1354 ret = TRUE;
1355 goto done;
1356 }
1358 /* Formats to consider: TIME, DEFAULT, BYTES, title, chapter, sector.
1359 * Note: title and chapter are counted as starting from 0 here, just like
1360 * in the context of seek events. Another note: DEFAULT format is undefined */
1362 if (src_format == GST_FORMAT_BYTES) {
1363 src_format = sector_format;
1364 src_val /= DVD_VIDEO_LB_LEN;
1365 }
1367 if (src_format == sector_format) {
1368 /* SECTOR => xyz */
1369 if (dest_format == GST_FORMAT_TIME && src_val < G_MAXUINT) {
1370 dest_val = gst_dvd_read_src_get_time_for_sector (src, (guint) src_val);
1371 ret = (dest_val >= 0);
1372 } else if (dest_format == GST_FORMAT_BYTES) {
1373 dest_val = src_val * DVD_VIDEO_LB_LEN;
1374 ret = TRUE;
1375 } else {
1376 ret = FALSE;
1377 }
1378 } else if (src_format == title_format) {
1379 /* TITLE => xyz */
1380 if (dest_format == GST_FORMAT_TIME) {
1381 /* not really true, but we use this to trick the base source into
1382 * handling seeks in title-format for us (the source won't know that
1383 * we changed the title in this case) (changing titles should really
1384 * be done with an interface rather than a seek, but for now we're
1385 * stuck with this mechanism. Fix in 0.11) */
1386 dest_val = (GstClockTime) 0;
1387 ret = TRUE;
1388 } else {
1389 ret = FALSE;
1390 }
1391 } else if (src_format == chapter_format) {
1392 /* CHAPTER => xyz */
1393 if (dest_format == GST_FORMAT_TIME) {
1394 if (src->num_chapters >= 0 && src_val < src->num_chapters) {
1395 dest_val = src->chapter_starts[src_val];
1396 ret = TRUE;
1397 }
1398 } else if (dest_format == sector_format) {
1399 } else {
1400 ret = FALSE;
1401 }
1402 } else if (src_format == GST_FORMAT_TIME) {
1403 /* TIME => xyz */
1404 if (dest_format == sector_format || dest_format == GST_FORMAT_BYTES) {
1405 dest_val = gst_dvd_read_src_get_sector_from_time (src, src_val);
1406 ret = (dest_val >= 0);
1407 if (dest_format == GST_FORMAT_BYTES)
1408 dest_val *= DVD_VIDEO_LB_LEN;
1409 } else if (dest_format == chapter_format) {
1410 if (src->chapter_starts != NULL) {
1411 gint i;
1413 for (i = src->num_chapters - 1; i >= 0; --i) {
1414 if (src->chapter_starts && src->chapter_starts[i] >= src_val) {
1415 dest_val = i;
1416 ret = TRUE;
1417 break;
1418 }
1419 }
1420 } else {
1421 ret = FALSE;
1422 }
1423 } else {
1424 ret = FALSE;
1425 }
1426 } else {
1427 ret = FALSE;
1428 }
1430 done:
1432 if (ret) {
1433 gst_query_set_convert (query, src_format, src_val, dest_format, dest_val);
1434 }
1436 return ret;
1437 }
1439 static gboolean
1440 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1441 {
1442 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1443 gboolean started;
1444 gboolean res = TRUE;
1446 GST_LOG_OBJECT (src, "handling %s query",
1447 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1449 GST_OBJECT_LOCK (src);
1450 started = (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED));
1451 GST_OBJECT_UNLOCK (src);
1453 if (!started) {
1454 GST_DEBUG_OBJECT (src, "query failed: not started");
1455 return FALSE;
1456 }
1458 switch (GST_QUERY_TYPE (query)) {
1459 case GST_QUERY_DURATION:
1460 GST_OBJECT_LOCK (src);
1461 res = gst_dvd_read_src_do_duration_query (src, query);
1462 GST_OBJECT_UNLOCK (src);
1463 break;
1464 case GST_QUERY_POSITION:
1465 GST_OBJECT_LOCK (src);
1466 res = gst_dvd_read_src_do_position_query (src, query);
1467 GST_OBJECT_UNLOCK (src);
1468 break;
1469 case GST_QUERY_CONVERT:
1470 GST_OBJECT_LOCK (src);
1471 res = gst_dvd_read_src_do_convert_query (src, query);
1472 GST_OBJECT_UNLOCK (src);
1473 break;
1474 default:
1475 res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1476 break;
1477 }
1479 return res;
1480 }
1482 static gboolean
1483 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1484 {
1485 gint seek_to = src->cur_pack;
1486 gint chapter, sectors, next, cur, i;
1488 /* retrieve position */
1489 src->cur_pack = 0;
1490 for (i = 0; i < src->num_chapters; i++) {
1491 gint c1, c2;
1493 cur_title_get_chapter_bounds (src, i, &c1, &c2);
1495 for (next = cur = c1; cur < c2;) {
1496 if (next != cur) {
1497 sectors =
1498 src->cur_pgc->cell_playback[cur].last_sector -
1499 src->cur_pgc->cell_playback[cur].first_sector;
1500 if (src->cur_pack + sectors > seek_to) {
1501 chapter = i;
1502 goto done;
1503 }
1504 src->cur_pack += sectors;
1505 }
1506 cur = next;
1507 if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1508 cur += angle;
1509 next = gst_dvd_read_src_get_next_cell (src, src->cur_pgc, cur);
1510 }
1511 }
1513 GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1515 return FALSE;
1517 done:
1518 {
1519 /* so chapter $chapter and cell $cur contain our sector
1520 * of interest. Let's go there! */
1521 GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1522 chapter + 1, cur);
1524 gst_dvd_read_src_goto_chapter (src, chapter);
1525 src->cur_cell = cur;
1526 src->next_cell = next;
1527 src->new_cell = FALSE;
1528 src->cur_pack = seek_to;
1530 return TRUE;
1531 }
1532 }
1535 /*** URI interface ***/
1537 static GstURIType
1538 gst_dvd_read_src_uri_get_type (void)
1539 {
1540 return GST_URI_SRC;
1541 }
1543 static gchar **
1544 gst_dvd_read_src_uri_get_protocols (void)
1545 {
1546 static gchar *protocols[] = { "dvd", NULL };
1548 return protocols;
1549 }
1551 static const gchar *
1552 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1553 {
1554 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1556 GST_OBJECT_LOCK (src);
1558 g_free (src->last_uri);
1559 src->last_uri = g_strdup_printf ("dvd://%d,%d,%d", src->uri_title,
1560 src->uri_chapter, src->uri_angle);
1562 GST_OBJECT_UNLOCK (src);
1564 return src->last_uri;
1565 }
1567 static gboolean
1568 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1569 {
1570 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1571 gboolean ret;
1572 gchar *protocol;
1574 protocol = gst_uri_get_protocol (uri);
1575 ret = (protocol != NULL && g_str_equal (protocol, "dvd"));
1576 g_free (protocol);
1577 protocol = NULL;
1579 if (!ret)
1580 return ret;
1582 /* parse out the new t/c/a and seek to them */
1583 {
1584 gchar *location = NULL;
1585 gchar **strs;
1586 gchar **strcur;
1587 gint pos = 0;
1589 location = gst_uri_get_location (uri);
1591 if (!location)
1592 return ret;
1594 GST_OBJECT_LOCK (src);
1596 src->uri_title = 1;
1597 src->uri_chapter = 1;
1598 src->uri_angle = 1;
1600 strcur = strs = g_strsplit (location, ",", 0);
1601 while (strcur && *strcur) {
1602 gint val;
1604 if (!sscanf (*strcur, "%d", &val))
1605 break;
1607 if (val <= 0) {
1608 g_warning ("Invalid value %d in URI '%s'. Must be 1 or greater",
1609 val, location);
1610 break;
1611 }
1613 switch (pos) {
1614 case 0:
1615 src->uri_title = val;
1616 break;
1617 case 1:
1618 src->uri_chapter = val;
1619 break;
1620 case 2:
1621 src->uri_angle = val;
1622 break;
1623 }
1625 strcur++;
1626 pos++;
1627 }
1629 if (pos > 0 && GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1630 src->title = src->uri_title - 1;
1631 src->chapter = src->uri_chapter - 1;
1632 src->angle = src->uri_angle - 1;
1633 src->new_seek = TRUE;
1634 }
1636 GST_OBJECT_UNLOCK (src);
1638 g_strfreev (strs);
1639 g_free (location);
1640 }
1642 return ret;
1643 }
1645 static void
1646 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1647 {
1648 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1650 iface->get_type = gst_dvd_read_src_uri_get_type;
1651 iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1652 iface->get_uri = gst_dvd_read_src_uri_get_uri;
1653 iface->set_uri = gst_dvd_read_src_uri_set_uri;
1654 }
1656 static void
1657 gst_dvd_read_src_do_init (GType dvdreadsrc_type)
1658 {
1659 static const GInterfaceInfo urihandler_info = {
1660 gst_dvd_read_src_uri_handler_init,
1661 NULL,
1662 NULL
1663 };
1665 g_type_add_interface_static (dvdreadsrc_type, GST_TYPE_URI_HANDLER,
1666 &urihandler_info);
1668 title_format = gst_format_register ("title", "DVD title");
1669 angle_format = gst_format_register ("angle", "DVD angle");
1670 sector_format = gst_format_register ("sector", "DVD sector");
1671 chapter_format = gst_format_register ("chapter", "DVD chapter");
1672 }
1674 static gboolean
1675 plugin_init (GstPlugin * plugin)
1676 {
1677 GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1678 "DVD reader element based on dvdreadsrc");
1680 #ifdef ENABLE_NLS
1681 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1682 LOCALEDIR);
1683 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1684 #endif /* ENABLE_NLS */
1686 if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,
1687 GST_TYPE_DVD_READ_SRC)) {
1688 return FALSE;
1689 }
1691 return TRUE;
1692 }
1694 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1695 GST_VERSION_MINOR,
1696 "dvdread",
1697 "Access a DVD with dvdread",
1698 plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);