]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugins-ugly0-10.git/blobdiff - ext/dvdread/dvdreadsrc.c
dvdreadsrc: fix off by one in cell calculation for the last chapter
[glsdk/gst-plugins-ugly0-10.git] / ext / dvdread / dvdreadsrc.c
index c915980df956b6fe9474cb5b58bf7ab4946381e8..9877f072478a54ebfc40cbc443d3e767ce40411a 100644 (file)
@@ -49,13 +49,6 @@ enum
   ARG_ANGLE
 };
 
-static GstElementDetails gst_dvd_read_src_details = {
-  "DVD Source",
-  "Source/File/DVD",
-  "Access a DVD title/chapter/angle using libdvdread",
-  "Erik Walthinsen <omega@cse.ogi.edu>",
-};
-
 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
     GST_PAD_SRC,
     GST_PAD_ALWAYS,
@@ -103,10 +96,12 @@ gst_dvd_read_src_base_init (gpointer g_class)
 {
   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
 
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&srctemplate));
+  gst_element_class_add_static_pad_template (element_class, &srctemplate);
 
-  gst_element_class_set_details (element_class, &gst_dvd_read_src_details);
+  gst_element_class_set_details_simple (element_class, "DVD Source",
+      "Source/File/DVD",
+      "Access a DVD title/chapter/angle using libdvdread",
+      "Erik Walthinsen <omega@cse.ogi.edu>");
 }
 
 static void
@@ -164,16 +159,17 @@ gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
 
   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
       g_param_spec_string ("device", "Device",
-          "DVD device location", NULL, G_PARAM_READWRITE));
+          "DVD device location", NULL,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TITLE,
       g_param_spec_int ("title", "title", "title",
-          1, 999, 1, G_PARAM_READWRITE));
+          1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHAPTER,
       g_param_spec_int ("chapter", "chapter", "chapter",
-          1, 999, 1, G_PARAM_READWRITE));
+          1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ANGLE,
       g_param_spec_int ("angle", "angle", "angle",
-          1, 999, 1, G_PARAM_READWRITE));
+          1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
@@ -329,11 +325,14 @@ cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
   *p_first_cell = pgc->program_map[pgn - 1] - 1;
 
   if (chapter == (src->num_chapters - 1)) {
-    *p_last_cell = pgc->nr_of_cells;
+    *p_last_cell = pgc->nr_of_cells - 1;
   } else {
     pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
     *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
   }
+
+  GST_DEBUG_OBJECT (src, "Chapter %d bounds: %d %d (within %d cells)",
+      chapter, *p_first_cell, *p_last_cell, pgc->nr_of_cells);
 }
 
 static gboolean
@@ -432,8 +431,10 @@ gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
 {
   GstStructure *s;
   gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
+  pgc_t *pgc0;
   gint title_set_nr;
   gint num_titles;
+  gint pgn0, pgc0_id;
   gint i;
 
   /* make sure our title number is valid */
@@ -489,9 +490,18 @@ gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
   s = gst_structure_new ("application/x-gst-dvd",
       "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
 
+  /* so we can filter out invalid/unused streams (same for all chapters) */
+  cur_title_get_chapter_pgc (src, 0, &pgn0, &pgc0_id, &pgc0);
+
   /* audio */
   for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
-    const audio_attr_t *a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
+    const audio_attr_t *a;
+
+    /* audio stream present? */
+    if (pgc0 != NULL && (pgc0->audio_control[i] & 0x8000) == 0)
+      continue;
+
+    a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
 
     t = g_strdup_printf ("audio-%d-format", i);
     gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
@@ -513,10 +523,27 @@ gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
 
   /* subtitle */
   for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
-    const subp_attr_t *u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
+    const subp_attr_t *u;
+    const video_attr_t *v;
+    gint sid;
+
+    /* subpicture stream present? */
+    if (pgc0 != NULL && (pgc0->subp_control[i] & 0x80000000) == 0)
+      continue;
+
+    u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
+    v = &src->vts_file->vtsi_mat->vts_video_attr;
+
+    sid = i;
+    if (pgc0 != NULL) {
+      if (v->display_aspect_ratio == 0)         /* 4:3 */
+        sid = (pgc0->subp_control[i] >> 24) & 0x1f;
+      else if (v->display_aspect_ratio == 3)    /* 16:9 */
+        sid = (pgc0->subp_control[i] >> 8) & 0x1f;
+    }
 
     if (u->type) {
-      t = g_strdup_printf ("subtitle-%d-language", i);
+      t = g_strdup_printf ("subtitle-%d-language", sid);
       lang_code[0] = (u->lang_code >> 8) & 0xff;
       lang_code[1] = u->lang_code & 0xff;
       gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
@@ -525,8 +552,8 @@ gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
       lang_code[0] = '\0';
     }
 
-    GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', format=%d",
-        src->title + 1, i, lang_code);
+    GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', type=%d",
+        src->title + 1, sid, lang_code, u->type);
   }
 
   src->title_lang_event_pending =
@@ -590,7 +617,7 @@ commands_only_pgc:
     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
         (_("Could not open DVD title %d. Interactive titles are not supported "
                 "by this element"), title_set_nr),
-        ("Commands-only PGC, not supported, use dvdnavsrc"));
+        ("Commands-only PGC, not supported, use rsndvdbin"));
     return FALSE;
   }
 }
@@ -603,15 +630,15 @@ gst_dvd_read_src_get_next_cell (GstDvdReadSrc * src, pgc_t * pgc, gint cell)
   if (pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
     return (cell + 1);
 
-  while (pgc->cell_playback[cell].block_mode == BLOCK_MODE_LAST_CELL)
+  while (pgc->cell_playback[cell].block_mode != BLOCK_MODE_LAST_CELL)
     ++cell;
 
-  return cell + 1;              /* really +1? (tpm) */
+  return cell + 1;
 }
 
 /* Returns true if the pack is a NAV pack */
 static gboolean
-gst_dvd_read_src_is_nav_pack (const guint8 * data)
+gst_dvd_read_src_is_nav_pack (const guint8 * data, gint lbn, dsi_t * dsi_pack)
 {
   if (GST_READ_UINT32_BE (data + 0x26) != 0x000001BF)
     return FALSE;
@@ -634,6 +661,11 @@ gst_dvd_read_src_is_nav_pack (const guint8 * data)
   if (GST_READ_UINT16_BE (data + 0x404) != 0x03fa)
     return FALSE;
 
+  /* Read the DSI packet into the provided struct and check it */
+  navRead_DSI (dsi_pack, (unsigned char *) data + DSI_START_BYTE);
+  if (lbn != dsi_pack->dsi_gi.nv_pck_lbn)
+    return FALSE;
+
   return TRUE;
 }
 
@@ -663,23 +695,21 @@ gst_dvd_read_src_get_time_for_sector (GstDvdReadSrc * src, guint sector)
 static gint
 gst_dvd_read_src_get_sector_from_time (GstDvdReadSrc * src, GstClockTime ts)
 {
-  gint sector, i, j;
+  gint sector, j;
 
-  if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps == 0)
+  if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps < src->ttn)
     return -1;
 
   sector = 0;
-  for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
-    for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; ++j) {
-      GstClockTime entry_time;
+  for (j = 0; j < src->vts_tmapt->tmap[src->ttn - 1].nr_of_entries; ++j) {
+    GstClockTime entry_time;
 
-      entry_time = src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
-      if (entry_time <= ts) {
-        sector = src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff;
-      }
-      if (entry_time >= ts) {
-        return sector;
-      }
+    entry_time = src->vts_tmapt->tmap[src->ttn - 1].tmu * (j + 1) * GST_SECOND;
+    if (entry_time <= ts) {
+      sector = src->vts_tmapt->tmap[src->ttn - 1].map_ent[j] & 0x7fffffff;
+    }
+    if (entry_time >= ts) {
+      return sector;
     }
   }
 
@@ -702,10 +732,15 @@ gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
     GstBuffer ** p_buf)
 {
   GstBuffer *buf;
+  GstSegment *seg;
   guint8 oneblock[DVD_VIDEO_LB_LEN];
   dsi_t dsi_pack;
-  guint next_vobu, next_ilvu_start, cur_output_size;
+  guint next_vobu, cur_output_size;
   gint len;
+  gint retries;
+  gint64 next_time;
+
+  seg = &(GST_BASE_SRC (src)->segment);
 
   /* playback by cell in this pgc, starting at the cell for our chapter */
   if (new_seek)
@@ -715,8 +750,12 @@ again:
 
   if (src->cur_cell >= src->last_cell) {
     /* advance to next chapter */
-    if (src->chapter == (src->num_chapters - 1))
+    if (src->chapter == (src->num_chapters - 1) ||
+        (seg->format == chapter_format && seg->stop != -1 &&
+            src->chapter == (seg->stop - 1))) {
+      GST_DEBUG_OBJECT (src, "end of chapter segment");
       goto eos;
+    }
 
     GST_INFO_OBJECT (src, "end of chapter %d, switch to next",
         src->chapter + 1);
@@ -748,34 +787,42 @@ again:
     /* we loop until we're out of this cell */
     src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
     src->new_cell = FALSE;
+    GST_DEBUG_OBJECT (src, "Starting new cell %d @ pack %d", src->cur_cell,
+        src->cur_pack);
   }
 
   if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
     src->new_cell = TRUE;
-    GST_LOG_OBJECT (src, "Beyond last sector, go to next cell");
+    GST_LOG_OBJECT (src, "Beyond last sector for cell %d, going to next cell",
+        src->cur_cell);
     return GST_DVD_READ_AGAIN;
   }
 
   /* read NAV packet */
+  retries = 0;
 nav_retry:
+  retries++;
 
   len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, oneblock);
-  if (len == 0)
+  if (len != 1)
     goto read_error;
 
-  if (!gst_dvd_read_src_is_nav_pack (oneblock)) {
+  if (!gst_dvd_read_src_is_nav_pack (oneblock, src->cur_pack, &dsi_pack)) {
+    GST_LOG_OBJECT (src, "Skipping nav packet @ pack %d", src->cur_pack);
     src->cur_pack++;
-    goto nav_retry;
-  }
 
-  /* parse the contained dsi packet */
-  navRead_DSI (&dsi_pack, &oneblock[DSI_START_BYTE]);
-  g_assert (src->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn);
+    if (retries < 2000) {
+      goto nav_retry;
+    } else {
+      GST_LOG_OBJECT (src, "No nav packet @ pack %d after 2000 blocks",
+          src->cur_pack);
+      goto read_error;
+    }
+  }
 
   /* determine where we go next. These values are the ones we
    * mostly care about */
-  next_ilvu_start = src->cur_pack + dsi_pack.sml_agli.data[angle].address;
-  cur_output_size = dsi_pack.dsi_gi.vobu_ea;
+  cur_output_size = dsi_pack.dsi_gi.vobu_ea + 1;
 
   /* If we're not at the end of this cell, we can determine the next
    * VOBU to display using the VOBU_SRI information section of the
@@ -788,15 +835,17 @@ nav_retry:
   if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
     next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
   } else {
-    next_vobu = src->cur_pack + cur_output_size + 1;
+    next_vobu = src->cur_pgc->cell_playback[src->cur_cell].last_sector + 1;
   }
 
   g_assert (cur_output_size < 1024);
-  ++src->cur_pack;
 
   /* create the buffer (TODO: use buffer pool?) */
   buf = gst_buffer_new_and_alloc (cur_output_size * DVD_VIDEO_LB_LEN);
 
+  GST_LOG_OBJECT (src, "Going to read %u sectors @ pack %d", cur_output_size,
+      src->cur_pack);
+
   /* read in and output cursize packs */
   len = DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size,
       GST_BUFFER_DATA (buf));
@@ -813,16 +862,24 @@ nav_retry:
 
   *p_buf = buf;
 
+  GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
+
   src->cur_pack = next_vobu;
 
-  GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
+  next_time = GST_BUFFER_TIMESTAMP (buf);
+  if (GST_CLOCK_TIME_IS_VALID (next_time) && seg->format == GST_FORMAT_TIME &&
+      GST_CLOCK_TIME_IS_VALID (seg->stop) &&
+      next_time > seg->stop + 5 * GST_SECOND) {
+    GST_DEBUG_OBJECT (src, "end of TIME segment");
+    goto eos;
+  }
 
   return GST_DVD_READ_OK;
 
   /* ERRORS */
 eos:
   {
-    GST_INFO_OBJECT (src, "last chapter done - EOS");
+    GST_INFO_OBJECT (src, "Reached end-of-segment/stream - EOS");
     return GST_DVD_READ_EOS;
   }
 read_error:
@@ -839,6 +896,25 @@ block_read_error:
   }
 }
 
+/* we don't cache the result on purpose */
+static gboolean
+gst_dvd_read_descrambler_available (void)
+{
+  GModule *module;
+  gpointer sym;
+  gsize res;
+
+  module = g_module_open ("libdvdcss", 0);
+  if (module != NULL) {
+    res = g_module_symbol (module, "dvdcss_open", &sym);
+    g_module_close (module);
+  } else {
+    res = FALSE;
+  }
+
+  return res;
+}
+
 static GstFlowReturn
 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
 {
@@ -853,7 +929,7 @@ gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
   if (src->need_newsegment) {
     gst_pad_push_event (srcpad,
         gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
-            src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
+            (gint64) src->cur_pack * DVD_VIDEO_LB_LEN, -1, 0));
     src->need_newsegment = FALSE;
   }
 
@@ -882,11 +958,18 @@ gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
 
   switch (res) {
     case GST_DVD_READ_ERROR:{
-      GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), (NULL));
+      /* FIXME: figure out a way to detect if scrambling is the problem */
+      if (!gst_dvd_read_descrambler_available ()) {
+        GST_ELEMENT_ERROR (src, RESOURCE, READ,
+            (_("Could not read DVD. This may be because the DVD is encrypted "
+                    "and a DVD decryption library is not installed.")), (NULL));
+      } else {
+        GST_ELEMENT_ERROR (src, RESOURCE, READ, (_("Could not read DVD.")),
+            (NULL));
+      }
       return GST_FLOW_ERROR;
     }
     case GST_DVD_READ_EOS:{
-      GST_INFO_OBJECT (src, "Reached EOS");
       return GST_FLOW_UNEXPECTED;
     }
     case GST_DVD_READ_OK:{
@@ -1026,19 +1109,12 @@ gst_dvd_read_src_handle_seek_event (GstDvdReadSrc * src, GstEvent * event)
     return FALSE;
   }
 
-  if ((flags & GST_SEEK_FLAG_SEGMENT) != 0) {
-    GST_DEBUG_OBJECT (src, "segment seek not supported");
-    return FALSE;
-  }
-
-  if ((flags & GST_SEEK_FLAG_FLUSH) == 0) {
-    GST_DEBUG_OBJECT (src, "can only do flushing seeks at the moment");
-    return FALSE;
-  }
-
   if (end_type != GST_SEEK_TYPE_NONE) {
-    GST_DEBUG_OBJECT (src, "end seek type not supported");
-    return FALSE;
+    if ((format != chapter_format && format != GST_FORMAT_TIME) ||
+        end_type != GST_SEEK_TYPE_SET) {
+      GST_DEBUG_OBJECT (src, "end seek type not supported");
+      return FALSE;
+    }
   }
 
   if (cur_type != GST_SEEK_TYPE_SET) {
@@ -1051,7 +1127,7 @@ gst_dvd_read_src_handle_seek_event (GstDvdReadSrc * src, GstEvent * event)
     if (new_off < 0 || new_off >= src->num_angles) {
       GST_OBJECT_UNLOCK (src);
       GST_DEBUG_OBJECT (src, "invalid angle %d, only %d available",
-          src->num_angles);
+          src->num_angles, src->num_angles);
       return FALSE;
     }
     src->angle = (gint) new_off;
@@ -1120,6 +1196,17 @@ gst_dvd_read_src_handle_seek_event (GstDvdReadSrc * src, GstEvent * event)
   return GST_BASE_SRC_CLASS (parent_class)->event (GST_BASE_SRC (src), event);
 }
 
+static void
+gst_dvd_read_src_get_sector_bounds (GstDvdReadSrc * src, gint * first,
+    gint * last)
+{
+  gint c1, c2, tmp;
+  cur_title_get_chapter_bounds (src, 0, &c1, &tmp);
+  cur_title_get_chapter_bounds (src, src->num_chapters - 1, &tmp, &c2);
+  *first = src->cur_pgc->cell_playback[c1].first_sector;
+  *last = src->cur_pgc->cell_playback[c2].last_sector;
+}
+
 static gboolean
 gst_dvd_read_src_do_seek (GstBaseSrc * basesrc, GstSegment * s)
 {
@@ -1137,9 +1224,18 @@ gst_dvd_read_src_do_seek (GstBaseSrc * basesrc, GstSegment * s)
     old = src->cur_pack;
 
     if (s->format == sector_format) {
+      gint first, last;
+      gst_dvd_read_src_get_sector_bounds (src, &first, &last);
+      GST_DEBUG_OBJECT (src, "Format is sector, seeking to %" G_GINT64_FORMAT,
+          s->last_stop);
       src->cur_pack = s->last_stop;
+      if (src->cur_pack < first)
+        src->cur_pack = first;
+      if (src->cur_pack > last)
+        src->cur_pack = last;
     } else if (s->format == GST_FORMAT_TIME) {
       gint sector;
+      GST_DEBUG_OBJECT (src, "Format is time");
 
       sector = gst_dvd_read_src_get_sector_from_time (src, s->last_stop);
 
@@ -1152,12 +1248,16 @@ gst_dvd_read_src_do_seek (GstBaseSrc * basesrc, GstSegment * s)
       src->cur_pack = sector;
     } else {
       /* byte format */
+      gint first, last;
+      gst_dvd_read_src_get_sector_bounds (src, &first, &last);
+      GST_DEBUG_OBJECT (src, "Format is byte");
       src->cur_pack = s->last_stop / DVD_VIDEO_LB_LEN;
-      if ((src->cur_pack * DVD_VIDEO_LB_LEN) != s->last_stop) {
+      if (((gint64) src->cur_pack * DVD_VIDEO_LB_LEN) != s->last_stop) {
         GST_LOG_OBJECT (src, "rounded down offset %" G_GINT64_FORMAT " => %"
             G_GINT64_FORMAT, s->last_stop,
             (gint64) src->cur_pack * DVD_VIDEO_LB_LEN);
       }
+      src->cur_pack += first;
     }
 
     if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
@@ -1311,7 +1411,7 @@ gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
 
   switch (format) {
     case GST_FORMAT_BYTES:{
-      val = src->cur_pack * DVD_VIDEO_LB_LEN;
+      val = (gint64) src->cur_pack * DVD_VIDEO_LB_LEN;
       break;
     }
     default:{
@@ -1339,6 +1439,102 @@ gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
   return TRUE;
 }
 
+static gboolean
+gst_dvd_read_src_do_convert_query (GstDvdReadSrc * src, GstQuery * query)
+{
+  GstFormat src_format, dest_format;
+  gboolean ret = FALSE;
+  gint64 src_val, dest_val = -1;
+
+  gst_query_parse_convert (query, &src_format, &src_val, &dest_format, NULL);
+
+  if (src_format == dest_format) {
+    dest_val = src_val;
+    ret = TRUE;
+    goto done;
+  }
+
+  /* Formats to consider: TIME, DEFAULT, BYTES, title, chapter, sector.
+   * Note: title and chapter are counted as starting from 0 here, just like
+   * in the context of seek events. Another note: DEFAULT format is undefined */
+
+  if (src_format == GST_FORMAT_BYTES) {
+    src_format = sector_format;
+    src_val /= DVD_VIDEO_LB_LEN;
+  }
+
+  if (src_format == sector_format) {
+    /* SECTOR => xyz */
+    if (dest_format == GST_FORMAT_TIME && src_val < G_MAXUINT) {
+      dest_val = gst_dvd_read_src_get_time_for_sector (src, (guint) src_val);
+      ret = (dest_val >= 0);
+    } else if (dest_format == GST_FORMAT_BYTES) {
+      dest_val = src_val * DVD_VIDEO_LB_LEN;
+      ret = TRUE;
+    } else {
+      ret = FALSE;
+    }
+  } else if (src_format == title_format) {
+    /* TITLE => xyz */
+    if (dest_format == GST_FORMAT_TIME) {
+      /* not really true, but we use this to trick the base source into
+       * handling seeks in title-format for us (the source won't know that
+       * we changed the title in this case) (changing titles should really
+       * be done with an interface rather than a seek, but for now we're
+       * stuck with this mechanism. Fix in 0.11) */
+      dest_val = (GstClockTime) 0;
+      ret = TRUE;
+    } else {
+      ret = FALSE;
+    }
+  } else if (src_format == chapter_format) {
+    /* CHAPTER => xyz */
+    if (dest_format == GST_FORMAT_TIME) {
+      if (src->num_chapters >= 0 && src_val < src->num_chapters) {
+        dest_val = src->chapter_starts[src_val];
+        ret = TRUE;
+      }
+    } else if (dest_format == sector_format) {
+    } else {
+      ret = FALSE;
+    }
+  } else if (src_format == GST_FORMAT_TIME) {
+    /* TIME => xyz */
+    if (dest_format == sector_format || dest_format == GST_FORMAT_BYTES) {
+      dest_val = gst_dvd_read_src_get_sector_from_time (src, src_val);
+      ret = (dest_val >= 0);
+      if (dest_format == GST_FORMAT_BYTES)
+        dest_val *= DVD_VIDEO_LB_LEN;
+    } else if (dest_format == chapter_format) {
+      if (src->chapter_starts != NULL) {
+        gint i;
+
+        for (i = src->num_chapters - 1; i >= 0; --i) {
+          if (src->chapter_starts && src->chapter_starts[i] >= src_val) {
+            dest_val = i;
+            ret = TRUE;
+            break;
+          }
+        }
+      } else {
+        ret = FALSE;
+      }
+    } else {
+      ret = FALSE;
+    }
+  } else {
+    ret = FALSE;
+  }
+
+done:
+
+  if (ret) {
+    gst_query_set_convert (query, src_format, src_val, dest_format, dest_val);
+  }
+
+  return ret;
+}
+
 static gboolean
 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
 {
@@ -1369,6 +1565,11 @@ gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
       res = gst_dvd_read_src_do_position_query (src, query);
       GST_OBJECT_UNLOCK (src);
       break;
+    case GST_QUERY_CONVERT:
+      GST_OBJECT_LOCK (src);
+      res = gst_dvd_read_src_do_convert_query (src, query);
+      GST_OBJECT_UNLOCK (src);
+      break;
     default:
       res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
       break;
@@ -1381,25 +1582,28 @@ static gboolean
 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
 {
   gint seek_to = src->cur_pack;
-  gint chapter, sectors, next, cur, i;
+  gint chapter, next, cur, i;
 
   /* retrieve position */
   src->cur_pack = 0;
+  GST_DEBUG_OBJECT (src, "Goto sector %d, angle %d, within %d chapters",
+      seek_to, angle, src->num_chapters);
+
   for (i = 0; i < src->num_chapters; i++) {
     gint c1, c2;
 
     cur_title_get_chapter_bounds (src, i, &c1, &c2);
+    GST_DEBUG_OBJECT (src, " Looking in chapter %d, bounds: %d %d", i, c1, c2);
 
     for (next = cur = c1; cur < c2;) {
-      if (next != cur) {
-        sectors =
-            src->cur_pgc->cell_playback[cur].last_sector -
-            src->cur_pgc->cell_playback[cur].first_sector;
-        if (src->cur_pack + sectors > seek_to) {
-          chapter = i;
-          goto done;
-        }
-        src->cur_pack += sectors;
+      gint first = src->cur_pgc->cell_playback[cur].first_sector;
+      gint last = src->cur_pgc->cell_playback[cur].last_sector;
+      GST_DEBUG_OBJECT (src, "Cell %d sector bounds: %d %d", cur, first, last);
+      /* seeking to 0 should end up at first chapter in any case */
+      if ((seek_to >= first && seek_to <= last) || (seek_to == 0 && i == 0)) {
+        GST_DEBUG_OBJECT (src, "Seek target found in chapter %d", i);
+        chapter = i;
+        goto done;
       }
       cur = next;
       if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
@@ -1441,7 +1645,7 @@ gst_dvd_read_src_uri_get_type (void)
 static gchar **
 gst_dvd_read_src_uri_get_protocols (void)
 {
-  static gchar *protocols[] = { "dvd", NULL };
+  static gchar *protocols[] = { (gchar *) "dvd", NULL };
 
   return protocols;
 }
@@ -1579,6 +1783,7 @@ plugin_init (GstPlugin * plugin)
   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
       LOCALEDIR);
   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 #endif /* ENABLE_NLS */
 
   if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,