$OpenBSD: patch-import_dvd_reader_c,v 1.1.1.1 2005/01/25 21:58:59 naddy Exp $
--- import/dvd_reader.c.orig	Tue Nov  2 19:45:41 2004
+++ import/dvd_reader.c	Fri Dec 31 01:13:17 2004
@@ -1090,6 +1090,10 @@ int dvd_stream(int arg_title,int arg_cha
     unsigned long cur_pack=0, max_sectors=0, blocks_left=0, blocks_written=0, first_block=0;
     int ttn, pgn;
 
+    int end_cell;
+    int e_pgc_id, e_pgn;
+    pgc_t *e_cur_pgc;
+
     dvd_file_t *title;
     ifo_handle_t *vmg_file;
     tt_srpt_t *tt_srpt;
@@ -1171,6 +1175,15 @@ int dvd_stream(int arg_title,int arg_cha
     start_cell = cur_pgc->program_map[ pgn - 1 ] - 1;
 
 
+    if ( chapid+1 >= tt_srpt->title[ titleid ].nr_of_ptts ) {
+      end_cell = cur_pgc->nr_of_cells - 1;
+    } else {
+      e_pgc_id = vts_ptt_srpt->title[ ttn - 1 ].ptt[ chapid+1 ].pgcn;
+      e_pgn = vts_ptt_srpt->title[ ttn - 1 ].ptt[ chapid+1 ].pgn;
+      e_cur_pgc = vts_file->vts_pgcit->pgci_srp[ e_pgc_id - 1 ].pgc;
+      end_cell = ( e_cur_pgc->program_map[ e_pgn - 1 ] - 1 ) -1;
+    }
+
     /**
      * We've got enough info, time to open the title set data.
      */
@@ -1189,13 +1202,16 @@ int dvd_stream(int arg_title,int arg_cha
     /**
      * Playback the cells for our title
      */
+    if (start_cell==end_cell)
+      fprintf(stderr,"(%s) Title %d in VTS %02d is defined by PGC %d with %d cells, exporting cell %d\n",
+	      __FILE__,titleid+1,tt_srpt->title[ titleid ].title_set_nr,pgc_id,cur_pgc->nr_of_cells,start_cell+1);
+    else
+      fprintf(stderr,"(%s) Title %d in VTS %02d is defined by PGC %d with %d cells, exporting from cell %d to cell %d\n",
+	      __FILE__,titleid+1,tt_srpt->title[ titleid ].title_set_nr,pgc_id,cur_pgc->nr_of_cells,start_cell+1,end_cell+1);
 
-    fprintf(stderr,"(%s) Title %d in VTS %02d is defined by PGC %d with %d cells, exporting cell %d\n",
-      __FILE__,titleid+1,tt_srpt->title[ titleid ].title_set_nr,pgc_id,cur_pgc->nr_of_cells,chapid+1);
+    cur_pack = cur_pgc->cell_playback[start_cell].first_sector;
 
-    cur_pack = cur_pgc->cell_playback[chapid].first_sector;
-
-    max_sectors = (long) cur_pgc->cell_playback[chapid].last_sector;
+    max_sectors = (long) cur_pgc->cell_playback[end_cell].last_sector;
     fprintf(stderr,"(%s) From block %ld to block %ld\n",__FILE__,(long)cur_pack,(long)max_sectors);
 
     first_block = cur_pack;
