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