Debian Codegraph @

src/searching.c

raw
1 /*
2 * Copyright (C) 2000 Rich Wareham <[email protected]>
3 *
4 * This file is part of libdvdnav, a DVD navigation library.
5 *
6 * libdvdnav is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * libdvdnav 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with libdvdnav; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <assert.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #include "dvdnav/dvdnav.h"
33 #include <dvdread/nav_types.h>
34 #include "vm/decoder.h"
35 #include "vm/vm.h"
36 #include "dvdnav_internal.h"
37 #include "logger.h"
38 #include <dvdread/ifo_read.h>
39
40 /*
41 #define LOG_DEBUG
42 */
43
44 /* Searching API calls */
45
46 /* Scan the ADMAP for a particular block number. */
47 /* Return placed in vobu. */
48 /* Returns error status */
49 /* FIXME: Maybe need to handle seeking outside current cell. */
50 static dvdnav_status_t dvdnav_scan_admap(dvdnav_t *this, int32_t domain, uint32_t seekto_block, int next, uint32_t *vobu) {
51 vobu_admap_t *admap = NULL;
52
53 #ifdef LOG_DEBUG
54 Log3(this, "Seeking to target %u ...", seekto_block);
55 #endif
56 *vobu = -1;
57
58 /* Search through the VOBU_ADMAP for the nearest VOBU
59 * to the target block */
60 switch(domain) {
61 case DVD_DOMAIN_FirstPlay:
62 case DVD_DOMAIN_VMGM:
63 admap = this->vm->vmgi->menu_vobu_admap;
64 break;
65 case DVD_DOMAIN_VTSMenu:
66 admap = this->vm->vtsi->menu_vobu_admap;
67 break;
68 case DVD_DOMAIN_VTSTitle:
69 admap = this->vm->vtsi->vts_vobu_admap;
70 break;
71 default:
72 Log0(this, "Error: Unknown domain for seeking.");
73 }
74 if(admap) {
75 uint32_t address = 0;
76 uint32_t vobu_start, next_vobu = 0;
77 uint32_t admap_entries = (admap->last_byte + 1 - VOBU_ADMAP_SIZE)/VOBU_ADMAP_SIZE;
78
79 /* Search through ADMAP for best sector */
80 vobu_start = SRI_END_OF_CELL;
81 /* FIXME: Implement a faster search algorithm */
82 while(address < admap_entries) {
83 next_vobu = admap->vobu_start_sectors[address];
84
85 /* fprintf(MSG_OUT, "libdvdnav: Found block %u\n", next_vobu); */
86
87 if(vobu_start <= seekto_block && next_vobu > seekto_block) {
88 /* We expect the ADMAP to be sorted in order of increasing
89 * sector number, but some discs store out-of-order values in
90 * cells that would not normally be playable, cells which
91 * themselves contain bad sectors. The straightforward linear
92 * search would get "stuck" on those values, essentially
93 * forcing playback of the bad sectors on any sector seek
94 * anywhere within the title. To prevent this, scan the next
95 * few entries to make sure they're all really later than the
96 * current entry, and skip over the out-of-order entries if
97 * not. */
98 uint32_t ooo_address = 0;
99 uint32_t ooo_vobu = SRI_END_OF_CELL;
100 uint32_t check;
101 for( check = 1 ; check <= 32 && address+check < admap_entries ; check++ ) {
102 uint32_t check_vobu = admap->vobu_start_sectors[address+check];
103 if (check_vobu < next_vobu && check_vobu < ooo_vobu) {
104 ooo_address = address+check;
105 ooo_vobu = check_vobu;
106 }
107 }
108 if (ooo_vobu < next_vobu) {
109 address = ooo_address;
110 next_vobu = ooo_vobu;
111 } else {
112 break;
113 }
114 }
115 vobu_start = next_vobu;
116 address++;
117 }
118 *vobu = next ? next_vobu : vobu_start;
119 return DVDNAV_STATUS_OK;
120 }
121 Log0(this, "admap not located");
122 return DVDNAV_STATUS_ERR;
123 }
124
125 /* FIXME: right now, this function does not use the time tables but interpolates
126 only the cell times */
127 dvdnav_status_t dvdnav_time_search(dvdnav_t *this,
128 uint64_t time) {
129
130 uint64_t target = time;
131 uint64_t length = 0;
132 uint32_t first_cell_nr, last_cell_nr, cell_nr;
133 int32_t found;
134 const cell_playback_t *cell;
135 dvd_state_t *state;
136
137 if(this->position_current.still != 0) {
138 printerr("Cannot seek in a still frame.");
139 return DVDNAV_STATUS_ERR;
140 }
141
142 pthread_mutex_lock(&this->vm_lock);
143 state = &(this->vm->state);
144 if(!state->pgc) {
145 printerr("No current PGC.");
146 pthread_mutex_unlock(&this->vm_lock);
147 return DVDNAV_STATUS_ERR;
148 }
149
150
151 this->cur_cell_time = 0;
152 if (this->pgc_based) {
153 first_cell_nr = 1;
154 last_cell_nr = state->pgc->nr_of_cells;
155 } else {
156 /* Find start cell of program. */
157 first_cell_nr = state->pgc->program_map[state->pgN-1];
158 /* Find end cell of program */
159 if(state->pgN < state->pgc->nr_of_programs)
160 last_cell_nr = state->pgc->program_map[state->pgN] - 1;
161 else
162 last_cell_nr = state->pgc->nr_of_cells;
163 }
164
165 found = 0;
166 for(cell_nr = first_cell_nr; (cell_nr <= last_cell_nr) && !found; cell_nr ++) {
167 cell = &(state->pgc->cell_playback[cell_nr-1]);
168 if(cell->block_type == BLOCK_TYPE_ANGLE_BLOCK && cell->block_mode != BLOCK_MODE_FIRST_CELL)
169 continue;
170 length = dvdnav_convert_time(&cell->playback_time);
171 if (target >= length) {
172 target -= length;
173 } else {
174 /* FIXME: there must be a better way than interpolation */
175 target = target * (cell->last_sector - cell->first_sector + 1) / length;
176 target += cell->first_sector;
177
178 found = 1;
179 break;
180 }
181 }
182
183 if(found) {
184 uint32_t vobu;
185 #ifdef LOG_DEBUG
186 Log3(this, "Seeking to cell %i from choice of %i to %i",
187 cell_nr, first_cell_nr, last_cell_nr);
188 #endif
189 if (dvdnav_scan_admap(this, state->domain, target, 0, &vobu) == DVDNAV_STATUS_OK) {
190 uint32_t start = state->pgc->cell_playback[cell_nr-1].first_sector;
191
192 if (vm_jump_cell_block(this->vm, cell_nr, vobu - start)) {
193 #ifdef LOG_DEBUG
194 Log3(this, "After cellN=%u blockN=%u target=%x vobu=%x start=%x" ,
195 state->cellN, state->blockN, target, vobu, start);
196 #endif
197 this->vm->hop_channel += HOP_SEEK;
198 pthread_mutex_unlock(&this->vm_lock);
199 return DVDNAV_STATUS_OK;
200 }
201 }
202 }
203
204 Log0(this, "Error when seeking");
205 printerr("Error when seeking.");
206 pthread_mutex_unlock(&this->vm_lock);
207 return DVDNAV_STATUS_ERR;
208 }
209
210 dvdnav_status_t dvdnav_sector_search(dvdnav_t *this,
211 int64_t offset, int32_t origin) {
212 uint32_t target = 0;
213 uint32_t current_pos;
214 uint32_t cur_sector;
215 uint32_t cur_cell_nr;
216 uint32_t length = 0;
217 uint32_t first_cell_nr, last_cell_nr, cell_nr;
218 int32_t found;
219 int forward = 0;
220 const cell_playback_t *cell;
221 dvd_state_t *state;
222 dvdnav_status_t result;
223
224 if(this->position_current.still != 0) {
225 printerr("Cannot seek in a still frame.");
226 return DVDNAV_STATUS_ERR;
227 }
228
229 result = dvdnav_get_position(this, &target, &length);
230 if(!result) {
231 printerr("Cannot get current position.");
232 return DVDNAV_STATUS_ERR;
233 }
234
235 pthread_mutex_lock(&this->vm_lock);
236 state = &(this->vm->state);
237 if(!state->pgc) {
238 printerr("No current PGC.");
239 pthread_mutex_unlock(&this->vm_lock);
240 return DVDNAV_STATUS_ERR;
241 }
242 #ifdef LOG_DEBUG
243 Log3(this, "seeking to offset=%lld pos=%u length=%u", offset, target, length);
244 Log3(this, "Before cellN=%u blockN=%u", state->cellN, state->blockN);
245 #endif
246
247 current_pos = target;
248 cur_sector = this->vobu.vobu_start + this->vobu.blockN;
249 cur_cell_nr = state->cellN;
250
251 switch(origin) {
252 case SEEK_SET:
253 if(offset >= length) {
254 printerr("Request to seek behind end.");
255 pthread_mutex_unlock(&this->vm_lock);
256 return DVDNAV_STATUS_ERR;
257 }
258 target = offset;
259 break;
260 case SEEK_CUR:
261 if((signed)target + offset >= length) {
262 printerr("Request to seek behind end.");
263 pthread_mutex_unlock(&this->vm_lock);
264 return DVDNAV_STATUS_ERR;
265 }
266 if((signed)target + offset < 0) {
267 printerr("Request to seek before start.");
268 pthread_mutex_unlock(&this->vm_lock);
269 return DVDNAV_STATUS_ERR;
270 }
271 target += offset;
272 break;
273 case SEEK_END:
274 if(length < offset) {
275 printerr("Request to seek before start.");
276 pthread_mutex_unlock(&this->vm_lock);
277 return DVDNAV_STATUS_ERR;
278 }
279 target = length - offset;
280 break;
281 default:
282 /* Error occurred */
283 printerr("Illegal seek mode.");
284 pthread_mutex_unlock(&this->vm_lock);
285 return DVDNAV_STATUS_ERR;
286 }
287 forward = target > current_pos;
288
289 this->cur_cell_time = 0;
290 if (this->pgc_based) {
291 first_cell_nr = 1;
292 last_cell_nr = state->pgc->nr_of_cells;
293 } else {
294 /* Find start cell of program. */
295 first_cell_nr = state->pgc->program_map[state->pgN-1];
296 /* Find end cell of program */
297 if(state->pgN < state->pgc->nr_of_programs)
298 last_cell_nr = state->pgc->program_map[state->pgN] - 1;
299 else
300 last_cell_nr = state->pgc->nr_of_cells;
301 }
302
303 found = 0;
304 for(cell_nr = first_cell_nr; (cell_nr <= last_cell_nr) && !found; cell_nr ++) {
305 cell = &(state->pgc->cell_playback[cell_nr-1]);
306 if(cell->block_type == BLOCK_TYPE_ANGLE_BLOCK && cell->block_mode != BLOCK_MODE_FIRST_CELL)
307 continue;
308 length = cell->last_sector - cell->first_sector + 1;
309 if (target >= length) {
310 target -= length;
311 } else {
312 /* convert the target sector from Cell-relative to absolute physical sector */
313 target += cell->first_sector;
314 if (forward && (cell_nr == cur_cell_nr)) {
315 uint32_t vobu;
316 /* if we are seeking forward from the current position, make sure
317 * we move to a new position that is after our current position.
318 * simply truncating to the vobu will go backwards */
319 if (dvdnav_scan_admap(this, state->domain, target, 0, &vobu) != DVDNAV_STATUS_OK)
320 break;
321 if (vobu <= cur_sector) {
322 if (dvdnav_scan_admap(this, state->domain, target, 1, &vobu) != DVDNAV_STATUS_OK)
323 break;
324 if (vobu > cell->last_sector) {
325 if (cell_nr == last_cell_nr)
326 break;
327 cell_nr++;
328 cell = &(state->pgc->cell_playback[cell_nr-1]);
329 target = cell->first_sector;
330 } else {
331 target = vobu;
332 }
333 }
334 }
335 found = 1;
336 break;
337 }
338 }
339
340 if(found) {
341 uint32_t vobu;
342 #ifdef LOG_DEBUG
343 Log3(this, "Seeking to cell %i from choice of %i to %i",
344 cell_nr, first_cell_nr, last_cell_nr);
345 #endif
346 if (dvdnav_scan_admap(this, state->domain, target, 0, &vobu) == DVDNAV_STATUS_OK) {
347 int32_t start = state->pgc->cell_playback[cell_nr-1].first_sector;
348
349 if (vm_jump_cell_block(this->vm, cell_nr, vobu - start)) {
350 #ifdef LOG_DEBUG
351 Log3(this, "After cellN=%u blockN=%u target=%x vobu=%x start=%x" ,
352 state->cellN, state->blockN, target, vobu, start);
353 #endif
354 this->vm->hop_channel += HOP_SEEK;
355 pthread_mutex_unlock(&this->vm_lock);
356 return DVDNAV_STATUS_OK;
357 }
358 }
359 }
360
361 Log0(this, "Error when seeking");
362 Log1(this, "FIXME: Implement seeking to location %u", target);
363 printerr("Error when seeking.");
364 pthread_mutex_unlock(&this->vm_lock);
365 return DVDNAV_STATUS_ERR;
366 }
367
368 dvdnav_status_t dvdnav_part_search(dvdnav_t *this, int32_t part) {
369 int32_t title, old_part;
370
371 if (dvdnav_current_title_info(this, &title, &old_part) == DVDNAV_STATUS_OK)
372 return dvdnav_part_play(this, title, part);
373 return DVDNAV_STATUS_ERR;
374 }
375
376 dvdnav_status_t dvdnav_prev_pg_search(dvdnav_t *this) {
377 pthread_mutex_lock(&this->vm_lock);
378 if(!this->vm->state.pgc) {
379 printerr("No current PGC.");
380 pthread_mutex_unlock(&this->vm_lock);
381 return DVDNAV_STATUS_ERR;
382 }
383
384 #ifdef LOG_DEBUG
385 Log3(this, "previous chapter");
386 #endif
387 if (!vm_jump_prev_pg(this->vm)) {
388 Log0(this, "previous chapter failed.");
389 printerr("Skip to previous chapter failed.");
390 pthread_mutex_unlock(&this->vm_lock);
391 return DVDNAV_STATUS_ERR;
392 }
393 this->cur_cell_time = 0;
394 this->position_current.still = 0;
395 this->vm->hop_channel++;
396 #ifdef LOG_DEBUG
397 Log3(this, "previous chapter done");
398 #endif
399 pthread_mutex_unlock(&this->vm_lock);
400
401 return DVDNAV_STATUS_OK;
402 }
403
404 dvdnav_status_t dvdnav_top_pg_search(dvdnav_t *this) {
405 pthread_mutex_lock(&this->vm_lock);
406 if(!this->vm->state.pgc) {
407 printerr("No current PGC.");
408 pthread_mutex_unlock(&this->vm_lock);
409 return DVDNAV_STATUS_ERR;
410 }
411
412 #ifdef LOG_DEBUG
413 Log3(this, "top chapter");
414 #endif
415 if (!vm_jump_top_pg(this->vm)) {
416 Log0(this, "top chapter failed.");
417 printerr("Skip to top chapter failed.");
418 pthread_mutex_unlock(&this->vm_lock);
419 return DVDNAV_STATUS_ERR;
420 }
421 this->cur_cell_time = 0;
422 this->position_current.still = 0;
423 this->vm->hop_channel++;
424 #ifdef LOG_DEBUG
425 Log3(this, "top chapter done");
426 #endif
427 pthread_mutex_unlock(&this->vm_lock);
428
429 return DVDNAV_STATUS_OK;
430 }
431
432 dvdnav_status_t dvdnav_next_pg_search(dvdnav_t *this) {
433 vm_t *try_vm;
434
435 pthread_mutex_lock(&this->vm_lock);
436 if(!this->vm->state.pgc) {
437 printerr("No current PGC.");
438 goto fail;
439 }
440
441 #ifdef LOG_DEBUG
442 Log3(this, "next chapter");
443 #endif
444 /* make a copy of current VM and try to navigate the copy to the next PG */
445 try_vm = vm_new_copy(this->vm);
446 if (try_vm == NULL) {
447 printerr("Unable to copy the VM.");
448 goto fail;
449 }
450
451 if (!vm_jump_next_pg(try_vm) || try_vm->stopped) {
452 vm_free_copy(try_vm);
453 /* next_pg failed, try to jump at least to the next cell */
454 try_vm = vm_new_copy(this->vm);
455 if (try_vm == NULL) {
456 printerr("Unable to copy the VM.");
457 goto fail;
458 }
459 vm_get_next_cell(try_vm);
460 if (try_vm->stopped) {
461 vm_free_copy(try_vm);
462 Log0(this, "next chapter failed.");
463 printerr("Skip to next chapter failed.");
464 goto fail;
465 }
466 }
467 this->cur_cell_time = 0;
468 /* merge changes on success */
469 vm_merge(this->vm, try_vm);
470 vm_free_copy(try_vm);
471 this->position_current.still = 0;
472 this->vm->hop_channel++;
473 #ifdef LOG_DEBUG
474 Log3(this, "next chapter done");
475 #endif
476 pthread_mutex_unlock(&this->vm_lock);
477
478 return DVDNAV_STATUS_OK;
479
480 fail:
481 pthread_mutex_unlock(&this->vm_lock);
482 return DVDNAV_STATUS_ERR;
483 }
484
485 dvdnav_status_t dvdnav_menu_call(dvdnav_t *this, DVDMenuID_t menu) {
486 vm_t *try_vm;
487
488 pthread_mutex_lock(&this->vm_lock);
489 if(!this->vm->state.pgc) {
490 printerr("No current PGC.");
491 goto fail;
492 }
493
494 this->cur_cell_time = 0;
495 /* make a copy of current VM and try to navigate the copy to the menu */
496 try_vm = vm_new_copy(this->vm);
497 if (try_vm == NULL) {
498 printerr("Unable to copy VM.");
499 goto fail;
500 }
501
502 if ( (menu == DVD_MENU_Escape) && (this->vm->state.domain != DVD_DOMAIN_VTSTitle)) {
503 /* Try resume */
504 if (vm_jump_resume(try_vm) && !try_vm->stopped) {
505 /* merge changes on success */
506 vm_merge(this->vm, try_vm);
507 vm_free_copy(try_vm);
508 this->position_current.still = 0;
509 this->vm->hop_channel++;
510 pthread_mutex_unlock(&this->vm_lock);
511 return DVDNAV_STATUS_OK;
512 }
513 }
514 if (menu == DVD_MENU_Escape) menu = DVD_MENU_Root;
515
516 if (vm_jump_menu(try_vm, menu) && !try_vm->stopped) {
517 /* merge changes on success */
518 vm_merge(this->vm, try_vm);
519 vm_free_copy(try_vm);
520 this->position_current.still = 0;
521 this->vm->hop_channel++;
522 pthread_mutex_unlock(&this->vm_lock);
523 return DVDNAV_STATUS_OK;
524 } else {
525 vm_free_copy(try_vm);
526 printerr("No such menu or menu not reachable.");
527 goto fail;
528 }
529
530 fail:
531 pthread_mutex_unlock(&this->vm_lock);
532 return DVDNAV_STATUS_ERR;
533 }
534
535 dvdnav_status_t dvdnav_get_position(dvdnav_t *this, uint32_t *pos,
536 uint32_t *len) {
537 uint32_t cur_sector;
538 int32_t cell_nr, first_cell_nr, last_cell_nr;
539 const cell_playback_t *cell;
540 dvd_state_t *state;
541
542 if(!this->started) {
543 printerr("Virtual DVD machine not started.");
544 return DVDNAV_STATUS_ERR;
545 }
546
547 pthread_mutex_lock(&this->vm_lock);
548 state = &(this->vm->state);
549 if(!state->pgc || this->vm->stopped) {
550 printerr("No current PGC.");
551 pthread_mutex_unlock(&this->vm_lock);
552 return DVDNAV_STATUS_ERR;
553 }
554 if (this->position_current.hop_channel != this->vm->hop_channel ||
555 this->position_current.domain != state->domain ||
556 this->position_current.vts != state->vtsN ||
557 this->position_current.cell_restart != state->cell_restart) {
558 printerr("New position not yet determined.");
559 pthread_mutex_unlock(&this->vm_lock);
560 return DVDNAV_STATUS_ERR;
561 }
562
563 /* Get current sector */
564 cur_sector = this->vobu.vobu_start + this->vobu.blockN;
565
566 if (this->pgc_based) {
567 first_cell_nr = 1;
568 last_cell_nr = state->pgc->nr_of_cells;
569 } else {
570 /* Find start cell of program. */
571 first_cell_nr = state->pgc->program_map[state->pgN-1];
572 /* Find end cell of program */
573 if(state->pgN < state->pgc->nr_of_programs)
574 last_cell_nr = state->pgc->program_map[state->pgN] - 1;
575 else
576 last_cell_nr = state->pgc->nr_of_cells;
577 }
578
579 *pos = -1;
580 *len = 0;
581 for (cell_nr = first_cell_nr; cell_nr <= last_cell_nr; cell_nr++) {
582 cell = &(state->pgc->cell_playback[cell_nr-1]);
583 if(cell->block_type == BLOCK_TYPE_ANGLE_BLOCK && cell->block_mode != BLOCK_MODE_FIRST_CELL)
584 continue;
585 if (cell_nr == state->cellN) {
586 /* the current sector is in this cell,
587 * pos is length of PG up to here + sector's offset in this cell */
588 *pos = *len + cur_sector - cell->first_sector;
589 }
590 *len += cell->last_sector - cell->first_sector + 1;
591 }
592
593 pthread_mutex_unlock(&this->vm_lock);
594
595 if((signed)*pos == -1)
596 return DVDNAV_STATUS_ERR;
597
598 return DVDNAV_STATUS_OK;
599 }
600
601 dvdnav_status_t dvdnav_get_position_in_title(dvdnav_t *this,
602 uint32_t *pos,
603 uint32_t *len) {
604 uint32_t cur_sector;
605 uint32_t first_cell_nr;
606 uint32_t last_cell_nr;
607 const cell_playback_t *first_cell;
608 const cell_playback_t *last_cell;
609 dvd_state_t *state;
610
611 state = &(this->vm->state);
612 if(!state->pgc) {
613 printerr("No current PGC.");
614 return DVDNAV_STATUS_ERR;
615 }
616
617 if (state->pgc->program_map == NULL) {
618 printerr("Program map missing.");
619 return DVDNAV_STATUS_ERR;
620 }
621
622 /* Get current sector */
623 cur_sector = this->vobu.vobu_start + this->vobu.blockN;
624
625 /* Now find first and last cells in title. */
626 first_cell_nr = state->pgc->program_map[0];
627 first_cell = &(state->pgc->cell_playback[first_cell_nr-1]);
628 last_cell_nr = state->pgc->nr_of_cells;
629 last_cell = &(state->pgc->cell_playback[last_cell_nr-1]);
630
631 *pos = cur_sector - first_cell->first_sector;
632 *len = last_cell->last_sector - first_cell->first_sector;
633
634 return DVDNAV_STATUS_OK;
635 }
636
637 uint32_t dvdnav_describe_title_chapters(dvdnav_t *this, int32_t title, uint64_t **times, uint64_t *duration) {
638 int32_t retval=0;
639 uint16_t parts, i;
640 const title_info_t *ptitle = NULL;
641 const ptt_info_t *ptt = NULL;
642 ifo_handle_t *ifo = NULL;
643 const pgc_t *pgc;
644 const cell_playback_t *cell;
645 uint64_t length, *tmp=NULL;
646
647 *times = NULL;
648 *duration = 0;
649 pthread_mutex_lock(&this->vm_lock);
650 if(!this->vm->vmgi) {
651 printerr("Bad VM state or missing VTSI.");
652 goto fail;
653 }
654 if(!this->started) {
655 /* don't report an error but be nice */
656 vm_start(this->vm);
657 this->started = 1;
658 }
659 ifo = vm_get_title_ifo(this->vm, title);
660 if(!ifo || !ifo->vts_pgcit) {
661 printerr("Couldn't open IFO for chosen title, exit.");
662 retval = 0;
663 goto fail;
664 }
665
666 ptitle = &this->vm->vmgi->tt_srpt->title[title-1];
667 parts = ptitle->nr_of_ptts;
668 if(ptitle->vts_ttn == 0)
669 goto fail;
670 if (ptitle->vts_ttn > ifo->vts_ptt_srpt->nr_of_srpts) {
671 printerr("Requested title not found.");
672 goto fail;
673 }
674 ptt = ifo->vts_ptt_srpt->title[ptitle->vts_ttn-1].ptt;
675 if (parts > ifo->vts_ptt_srpt->title[ptitle->vts_ttn-1].nr_of_ptts) {
676 printerr("Too many parts, using the ones available.");
677 parts = ifo->vts_ptt_srpt->title[ptitle->vts_ttn-1].nr_of_ptts;
678 }
679
680 tmp = calloc(1, sizeof(uint64_t)*parts);
681 if(!tmp)
682 goto fail;
683
684 if(!ptt) {
685 printerr("ptt NULL");
686 goto fail;
687 }
688
689 length = 0;
690 for(i=0; i<parts; i++) {
691 uint32_t cellnr, endcellnr;
692 if (ptt[i].pgcn == 0 || ptt[i].pgcn > ifo->vts_pgcit->nr_of_pgci_srp) {
693 printerr("PGCN out of bounds.");
694 continue;
695 }
696 if (ifo->vts_pgcit->pgci_srp[ptt[i].pgcn-1].pgc_start_byte >= ifo->vts_pgcit->last_byte) {
697 printerr("PGC start out of bounds");
698 continue;
699 }
700 if (0 == ifo->vts_pgcit->pgci_srp[ptt[i].pgcn-1].pgc_start_byte) {
701 printerr("PGC start zero.");
702 continue;
703 }
704 if (0 != (ifo->vts_pgcit->pgci_srp[ptt[i].pgcn-1].pgc_start_byte & 1)) {
705 printerr("PGC start unaligned.");
706 continue;
707 }
708 if (0 != ((uintptr_t)(ifo->vts_pgcit->pgci_srp[ptt[i].pgcn-1].pgc) & 1)) {
709 printerr("PGC pointer unaligned.");
710 continue;
711 }
712 pgc = ifo->vts_pgcit->pgci_srp[ptt[i].pgcn-1].pgc;
713 if (pgc == NULL) {
714 printerr("PGC missing.");
715 continue;
716 }
717 if (pgc->program_map == NULL) {
718 printerr("Program map missing.");
719 continue;
720 }
721 if(ptt[i].pgn == 0 || ptt[i].pgn > pgc->nr_of_programs) {
722 printerr("WRONG part number.");
723 goto fail;
724 }
725
726 if (pgc->nr_of_cells == 0) {
727 printerr("Number of cells cannot be 0");
728 continue;
729 }
730 if ((cellnr = pgc->program_map[ptt[i].pgn-1]) == 0) {
731 printerr("Cell new row cannot be 0");
732 continue;
733 }
734 if (pgc->cell_playback == NULL) {
735 printerr("Cell missing");
736 continue;
737 }
738
739 if(ptt[i].pgn < pgc->nr_of_programs)
740 endcellnr = pgc->program_map[ptt[i].pgn];
741 else
742 endcellnr = 0;
743
744 do {
745 cell = &pgc->cell_playback[cellnr-1];
746 if(!(cell->block_type == BLOCK_TYPE_ANGLE_BLOCK &&
747 cell->block_mode != BLOCK_MODE_FIRST_CELL
748 ))
749 {
750 tmp[i] = length + dvdnav_convert_time(&cell->playback_time);
751 length = tmp[i];
752 }
753 cellnr++;
754 } while(cellnr < endcellnr);
755 }
756 *duration = length;
757 vm_ifo_close(ifo);
758 ifo = NULL;
759 retval = parts;
760 *times = tmp;
761
762 fail:
763 pthread_mutex_unlock(&this->vm_lock);
764 if(!retval && ifo)
765 vm_ifo_close(ifo);
766 if(!retval && tmp)
767 free(tmp);
768 return retval;
769 }
770
771 /* Get an admap and admap_len */
772 static vobu_admap_t* dvdnav_admap_get(dvdnav_t *this, dvd_state_t *state,
773 int32_t *admap_len) {
774 vobu_admap_t *admap = NULL;
775 switch(state->domain) {
776 case DVD_DOMAIN_FirstPlay:
777 case DVD_DOMAIN_VMGM:
778 admap = this->vm->vmgi->menu_vobu_admap;
779 break;
780 case DVD_DOMAIN_VTSMenu:
781 admap = this->vm->vtsi->menu_vobu_admap;
782 break;
783 case DVD_DOMAIN_VTSTitle:
784 admap = this->vm->vtsi->vts_vobu_admap;
785 break;
786 default: {
787 Log1(this, "Unknown domain");
788 return NULL;
789 }
790 }
791 if (admap == NULL) return NULL;
792
793 *admap_len = (admap->last_byte + 1 - VOBU_ADMAP_SIZE) / VOBU_ADMAP_SIZE;
794 if (*admap_len <= 0) {
795 Log1(this, "admap_len <= 0");
796 return NULL;
797 }
798 return admap;
799 }
800
801 /* Get a tmap, tmap_len and tmap_interval */
802 static const vts_tmap_t* dvdnav_tmap_get(dvdnav_t *this, dvd_state_t *state,
803 int32_t *tmap_len, int32_t *tmap_interval) {
804 int32_t domain;
805 ifo_handle_t *ifo = NULL;
806 vts_tmapt_t *tmapt = NULL;
807 uint16_t tmap_count = 0;
808 int32_t pgcN = 0;
809 const vts_tmap_t *tmap = NULL;
810 int32_t result = 0;
811
812 domain = state->domain;
813 switch(domain) {
814 case DVD_DOMAIN_FirstPlay:
815 case DVD_DOMAIN_VTSMenu:
816 case DVD_DOMAIN_VMGM: {
817 ifo = this->vm->vmgi;
818 break;
819 }
820 case DVD_DOMAIN_VTSTitle: {
821 ifo = this->vm->vtsi;
822 break;
823 }
824 default: {
825 Log1(this, "unknown domain for tmap");
826 return NULL;
827 }
828 }
829 if (ifo == NULL) return NULL;
830 tmapt = ifo->vts_tmapt;
831 /* HACK: ifo->vts_tmapt is NULL b/c ifo_read.c never loads it
832 * load ifo->vts_tmapt directly*/
833 if (tmapt == NULL) {
834 result = ifoRead_VTS_TMAPT(ifo);
835 if (!result) {
836 return NULL;
837 }
838 tmapt = ifo->vts_tmapt;
839 if (tmapt == NULL) return NULL;
840 }
841
842 tmap_count = tmapt->nr_of_tmaps;
843 pgcN = state->pgcN - 1; /* -1 b/c pgcN is base1 */
844 if (pgcN < 0) {
845 Log1(this, "pgcN < 0");
846 return NULL;
847 }
848
849 /* get tmap */
850 switch(domain) {
851 case DVD_DOMAIN_FirstPlay:
852 case DVD_DOMAIN_VMGM:
853 case DVD_DOMAIN_VTSMenu: {
854 if (tmap_count == 0) {
855 Log1(this, "tmap_count == 0");
856 return NULL;
857 }
858 tmap = &tmapt->tmap[0]; /* ASSUME: vmgi only has one time map */
859 break;
860 }
861 case DVD_DOMAIN_VTSTitle: {
862 if (pgcN >= tmap_count) {
863 Log1(this, "pgcN >= tmap_count; pgcN=%i tmap_count=%i",
864 pgcN, tmap_count);
865 return NULL;
866 }
867 tmap = &tmapt->tmap[pgcN];
868 break;
869 }
870 }
871 if (tmap == NULL) return NULL;
872
873 /* tmap->tmu is in seconds; convert to millisecs */
874 *tmap_interval = tmap->tmu * 1000;
875 if (*tmap_interval == 0) {
876 Log1(this, "tmap_interval == 0");
877 return NULL;
878 }
879 *tmap_len = tmap->nr_of_entries;
880 if (*tmap_len == 0) {
881 Log1(this, "tmap_len == 0");
882 return NULL;
883 }
884 return tmap;
885 }
886
887 /* Get a sector from a tmap */
888 static int32_t dvdnav_tmap_get_entry(dvdnav_t *this,
889 const vts_tmap_t *tmap, uint16_t tmap_len,
890 int32_t idx, uint32_t *sector) {
891 /* tmaps start at idx 0 which represents a sector at time 1 * tmap_interval
892 * this creates a "fake" tmap index at idx -1 for sector 0 */
893 if (idx == TMAP_IDX_EDGE_BGN) {
894 *sector = 0;
895 return 1;
896 }
897 if (idx < TMAP_IDX_EDGE_BGN || idx >= tmap_len) {
898 Log1(this, "idx out of bounds idx=%i %i", idx, tmap_len);
899 return 0;
900 }
901 /* 0x7fffffff unsets discontinuity bit if present */
902 *sector = tmap->map_ent[idx] & 0x7fffffff;
903 return 1;
904 }
905
906 /* Do a binary search for earlier admap index near find_sector */
907 static int32_t dvdnav_admap_search(vobu_admap_t *admap, uint32_t admap_len,
908 uint32_t find_sector, uint32_t *vobu) {
909 int32_t adj = 1;
910 int32_t prv_pos = 0;
911 int32_t prv_len = admap_len;
912 int32_t cur_len = 0;
913 int32_t cur_idx = 0;
914 uint32_t cur_sector = 0;
915 while (1) {
916 cur_len = prv_len / 2;
917 /* need to add 1 when prv_len == 3 (cur_len should go to 2, not 1) */
918 if (prv_len % 2 == 1) ++cur_len;
919 cur_idx = prv_pos + (cur_len * adj);
920 if (cur_idx < 0)
921 cur_idx = 0;
922 else if (cur_idx >= (int32_t)admap_len)
923 cur_idx = admap_len - 1;
924
925 cur_sector = admap->vobu_start_sectors[cur_idx];
926 if (find_sector < cur_sector) adj = -1;
927 else if (find_sector > cur_sector) adj = 1;
928 else if (find_sector == cur_sector) {
929 *vobu = cur_idx;
930 return 1;
931 }
932 if (cur_len == 1) {/* no smaller intervals left */
933 if (adj == -1) {/* last comparison was greater; take lesser */
934 cur_idx -= 1;
935 cur_sector = admap->vobu_start_sectors[cur_idx];
936 }
937 *vobu = cur_idx;
938 return 1;
939 }
940 prv_len = cur_len;
941 prv_pos = cur_idx;
942 }
943 }
944
945 /* Do a binary search for the earlier tmap entry near find_sector */
946 static int32_t dvdnav_tmap_search(dvdnav_t *this,
947 const vts_tmap_t *tmap, uint32_t tmap_len,
948 uint32_t find_sector, int32_t *tmap_idx, uint32_t *sector) {
949 int32_t adj = 1;
950 int32_t prv_pos = 0;
951 int32_t prv_len = tmap_len;
952 int32_t result = 0;
953 int32_t cur_len = 0;
954 int32_t cur_idx = 0;
955 uint32_t cur_sector = 0;
956 while (1) {
957 cur_len = prv_len / 2;
958 /* need to add 1 when prv_len == 3 (cur_len should go to 2, not 1) */
959 if (prv_len % 2 == 1) ++cur_len;
960 cur_idx = prv_pos + (cur_len * adj);
961 if (cur_idx < 0)
962 cur_idx = 0;
963 else if (cur_idx >= (int32_t)tmap_len)
964 cur_idx = tmap_len - 1;
965 cur_sector = 0;
966 result = dvdnav_tmap_get_entry(this, tmap, tmap_len, cur_idx, &cur_sector);
967 if (!result) return 0;
968 if (find_sector < cur_sector) adj = -1;
969 else if (find_sector > cur_sector) adj = 1;
970 else if (find_sector == cur_sector) {
971 *tmap_idx = cur_idx;
972 *sector = cur_sector;
973 return 1;
974 }
975 if (cur_len == 1) {/* no smaller intervals left */
976 if (adj == -1) {/* last comparison was greater; take lesser */
977 if (cur_idx == 0) { /* fake tmap index for sector 0 */
978 cur_idx = TMAP_IDX_EDGE_BGN;
979 cur_sector = 0;
980 }
981 else {
982 cur_idx -= 1;
983 result = dvdnav_tmap_get_entry(this, tmap, tmap_len, cur_idx, &cur_sector);
984 if (!result) return 0;
985 }
986 }
987 *tmap_idx = cur_idx;
988 *sector = cur_sector;
989 return 1;
990 }
991 prv_len = cur_len;
992 prv_pos = cur_idx;
993 }
994 }
995
996 /* Find the cell for a given time */
997 static int32_t dvdnav_cell_find(dvdnav_t *this, dvd_state_t *state,
998 uint64_t find_val, dvdnav_cell_data_t *cell_data) {
999 uint32_t cells_len = 0;
1000 uint32_t cells_bgn = 0;
1001 uint32_t cells_end = 0;
1002 uint32_t cell_idx = 0;
1003 const pgc_t *pgc = NULL;
1004 int pgN = 0;
1005 const cell_playback_t *cell = NULL;
1006 int found = 0;
1007
1008 pgc = state->pgc;
1009 if (pgc == NULL) return 0;
1010 cells_len = pgc->nr_of_cells;
1011 if (cells_len == 0) {
1012 Log1(this, "cells_len == 0");
1013 return 0;
1014 }
1015
1016 /* get cells_bgn, cells_end */
1017 if (this->pgc_based) {
1018 cells_bgn = 1;
1019 cells_end = cells_len;
1020 }
1021 else {
1022 pgN = state->pgN;
1023 cells_bgn = pgc->program_map[pgN - 1]; /* -1 b/c pgN is 1 based? */
1024 if (pgN < pgc->nr_of_programs) {
1025 cells_end = pgc->program_map[pgN] - 1;
1026 }
1027 else {
1028 cells_end = cells_len;
1029 }
1030 }
1031
1032 /* search cells */
1033 for (cell_idx = cells_bgn; cell_idx <= cells_end; cell_idx++) {
1034 cell = &(pgc->cell_playback[cell_idx - 1]); /* -1 b/c cell is base1 */
1035 /* if angle block, only consider first angleBlock
1036 * (others are "redundant" for purpose of search) */
1037 if ( cell->block_type == BLOCK_TYPE_ANGLE_BLOCK
1038 && cell->block_mode != BLOCK_MODE_FIRST_CELL) {
1039 continue;
1040 }
1041 cell_data->bgn->sector = cell->first_sector;
1042 cell_data->end->sector = cell->last_sector;
1043
1044 /* 90 pts to ms */
1045 cell_data->end->time += (dvdnav_convert_time(&cell->playback_time) / 90);
1046 if ( find_val >= cell_data->bgn->time
1047 && find_val <= cell_data->end->time) {
1048 found = 1;
1049 break;
1050 }
1051 cell_data->bgn->time = cell_data->end->time;
1052 }
1053
1054 /* found cell: set var */
1055 if (found) {
1056 cell_data->idx = cell_idx;
1057 }
1058 else
1059 Log1(this, "cell not found; find=%"PRId64, find_val);
1060 return found;
1061 }
1062
1063 /* Given two sectors and a fraction, calc the corresponding vobu */
1064 static int32_t dvdnav_admap_interpolate_vobu(dvdnav_t *this,
1065 dvdnav_jump_args_t *args,
1066 dvdnav_pos_data_t *bgn, dvdnav_pos_data_t *end, uint32_t fraction,
1067 uint32_t *jump_sector) {
1068 int32_t result = 0;
1069 uint32_t vobu_len = 0;
1070 uint32_t vobu_adj = 0;
1071 uint32_t vobu_idx = 0;
1072
1073 /* get bgn->vobu_idx */
1074 result = dvdnav_admap_search(args->admap, args->admap_len,
1075 bgn->sector, &bgn->vobu_idx);
1076 if (!result) {
1077 Log1(this, "admap_interpolate: could not find sector_bgn");
1078 return 0;
1079 }
1080
1081 /* get end->vobu_idx */
1082 result = dvdnav_admap_search(args->admap, args->admap_len,
1083 end->sector, &end->vobu_idx);
1084 if (!result) {
1085 Log1(this, "admap_interpolate: could not find sector_end");
1086 return 0;
1087 }
1088
1089 vobu_len = end->vobu_idx - bgn->vobu_idx;
1090 /* +500 to round up else 74% of a 4 sec interval = 2 sec */
1091 vobu_adj = ((fraction * vobu_len) + 500) / 1000;
1092 /* HACK: need to add +1, or else will land too soon (not sure why) */
1093 vobu_adj++;
1094 vobu_idx = bgn->vobu_idx + vobu_adj;
1095 if ((int32_t)vobu_idx >= args->admap_len) {
1096 Log1(this, "admap_interpolate: vobu_idx >= admap_len");
1097 return 0;
1098 }
1099 *jump_sector = args->admap->vobu_start_sectors[vobu_idx];
1100 return 1;
1101 }
1102
1103 /* Given two tmap entries and a time, calc the time for the lo tmap entry */
1104 static int32_t dvdnav_tmap_calc_time_for_tmap_entry(dvdnav_t *this,
1105 dvdnav_jump_args_t *args,
1106 dvdnav_pos_data_t *lo, dvdnav_pos_data_t *hi,
1107 dvdnav_pos_data_t *pos, uint64_t *out_time) {
1108 int32_t result = 0;
1109 int32_t vobu_pct = 0;
1110 uint64_t time_adj = 0;
1111
1112 if (lo->sector == hi->sector) {
1113 Log1(this, "lo->sector == hi->sector: %i", lo->sector);
1114 return 0;
1115 }
1116
1117 /* get vobus corresponding to lo, hi, pos */
1118 result = dvdnav_admap_search(args->admap, args->admap_len,
1119 lo->sector, &lo->vobu_idx);
1120 if (!result) {
1121 Log1(this, "lo->vobu: lo->sector=%i", lo->sector);
1122 return 0;
1123 }
1124 result = dvdnav_admap_search(args->admap, args->admap_len,
1125 hi->sector, &hi->vobu_idx);
1126 if (!result) {
1127 Log1(this, "hi->vobu: hi->sector=%i", hi->sector);
1128 return 0;
1129 }
1130 result = dvdnav_admap_search(args->admap, args->admap_len,
1131 pos->sector, &pos->vobu_idx);
1132 if (!result) {
1133 Log1(this, "pos->vobu: pos->sector=%i", pos->sector);
1134 return 0;
1135 }
1136
1137 /* calc position of cell relative to lo */
1138 if (hi->vobu_idx == lo->vobu_idx) {
1139 /* We are at the very end - pos should also equal lo so force that
1140 * rather than hit the divide-by-zero. */
1141 vobu_pct = 0;
1142 } else {
1143 vobu_pct = ((pos->vobu_idx - lo->vobu_idx) * 1000)
1144 / ( hi->vobu_idx - lo->vobu_idx);
1145 }
1146 if (vobu_pct < 0 || vobu_pct > 1000) {
1147 Log1(this, "vobu_pct must be between 0 and 1000");
1148 return 0;
1149 }
1150
1151 /* calc time of lo */
1152 time_adj = ((uint64_t)args->tmap_interval * vobu_pct) / 1000;
1153 *out_time = pos->time - time_adj;
1154 return 1;
1155 }
1156
1157 /* Find the tmap entries on either side of a given sector */
1158 static int32_t dvdnav_tmap_get_entries_for_sector(
1159 dvdnav_t *this,
1160 dvdnav_jump_args_t *args,
1161 dvdnav_cell_data_t *cell_data, uint32_t find_sector,
1162 dvdnav_pos_data_t *lo, dvdnav_pos_data_t *hi) {
1163 int32_t result = 0;
1164
1165 result = dvdnav_tmap_search(this,
1166 args->tmap, args->tmap_len, find_sector,
1167 &lo->tmap_idx, &lo->sector);
1168 if (!result) {
1169 Log1(this, "could not find lo idx: %i", find_sector);
1170 return 0;
1171 }
1172
1173 /* HACK: Most DVDs have a tmap that starts at sector 0
1174 * However, some have initial dummy cells that are not seekable
1175 * (restricted = y).
1176 * These cells will throw off the tmap calcs when in the first playable cell.
1177 * For now, assume that lo->sector is equal to the cell->bgn->sector
1178 * Note that for most DVDs this will be 0
1179 * (Since they will have no dummy cells and cell 1 will start at sector 0)
1180 */
1181 if (lo->tmap_idx == TMAP_IDX_EDGE_BGN) {
1182 lo->sector = cell_data->bgn->sector;
1183 }
1184
1185 if (lo->tmap_idx == args->tmap_len - 1) {
1186 /* lo is last tmap entry; "fake" entry for one beyond
1187 * and mark it with cell_end_sector */
1188 hi->tmap_idx = TMAP_IDX_EDGE_END;
1189 hi->sector = cell_data->end->sector;
1190 }
1191 else {
1192 hi->tmap_idx = lo->tmap_idx + 1;
1193 result = dvdnav_tmap_get_entry(this, args->tmap, args->tmap_len,
1194 hi->tmap_idx, &hi->sector);
1195 if (!result) {
1196 Log1(this, "could not find hi idx: %i", find_sector);
1197 return 0;
1198 }
1199 }
1200 return 1;
1201 }
1202
1203 /* Find the nearest vobu by using the tmap */
1204 static int32_t dvdnav_find_vobu_by_tmap(dvdnav_t *this, dvd_state_t *state,
1205 dvdnav_jump_args_t *args, dvdnav_cell_data_t *cell_data,
1206 dvdnav_pos_data_t *jump) {
1207 uint64_t seek_offset = 0;
1208 uint32_t seek_idx = 0;
1209 int32_t result = 0;
1210 dvdnav_pos_data_t *cell_bgn_lo = NULL;
1211 dvdnav_pos_data_t *cell_bgn_hi = NULL;
1212 dvdnav_pos_data_t *jump_lo = NULL;
1213 dvdnav_pos_data_t *jump_hi = NULL;
1214
1215 /* get tmap, tmap_len, tmap_interval */
1216 args->tmap = dvdnav_tmap_get(this, state,
1217 &args->tmap_len, &args->tmap_interval);
1218 if (args->tmap == NULL) return 0;
1219
1220 /* get tmap entries on either side of cell_bgn */
1221 cell_bgn_lo = &(dvdnav_pos_data_t){0};
1222 cell_bgn_hi = &(dvdnav_pos_data_t){0};
1223 result = dvdnav_tmap_get_entries_for_sector(this, args, cell_data,
1224 cell_data->bgn->sector, cell_bgn_lo, cell_bgn_hi);
1225 if (!result) return 0;
1226
1227 /* calc time of cell_bgn_lo */
1228 result = dvdnav_tmap_calc_time_for_tmap_entry(this,
1229 args, cell_bgn_lo, cell_bgn_hi,
1230 cell_data->bgn, &cell_bgn_lo->time);
1231 if (!result) return 0;
1232
1233 /* calc time of jump_time relative to cell_bgn_lo */
1234 seek_offset = jump->time - cell_bgn_lo->time;
1235 seek_idx = (uint32_t)(seek_offset / args->tmap_interval);
1236 uint32_t seek_remainder = seek_offset - (seek_idx * args->tmap_interval);
1237 uint32_t seek_pct = (seek_remainder * 1000) / args->tmap_interval;
1238
1239 /* get tmap entries on either side of jump_time */
1240 jump_lo = &(dvdnav_pos_data_t){0};
1241 jump_hi = &(dvdnav_pos_data_t){0};
1242
1243 /* if seek_idx == 0, then tmap_indexes are the same, do not re-get
1244 * also, note cell_bgn_lo will already have sector if TMAP_IDX_EDGE_BGN */
1245 if (seek_idx == 0) {
1246 jump_lo = cell_bgn_lo;
1247 jump_hi = cell_bgn_hi;
1248 }
1249 else {
1250 jump_lo->tmap_idx = (uint32_t)(cell_bgn_lo->tmap_idx + seek_idx);
1251 result = dvdnav_tmap_get_entry(this, args->tmap, args->tmap_len,
1252 jump_lo->tmap_idx, &jump_lo->sector);
1253 if (!result) return 0;
1254
1255 /* +1 handled by dvdnav_tmap_get_entry */
1256 jump_hi->tmap_idx = jump_lo->tmap_idx + 1;
1257 result = dvdnav_tmap_get_entry(this, args->tmap, args->tmap_len,
1258 jump_hi->tmap_idx, &jump_hi->sector);
1259 if (!result) return 0;
1260 }
1261
1262 /* interpolate sector */
1263 result = dvdnav_admap_interpolate_vobu(this, args, jump_lo, jump_hi,
1264 seek_pct, &jump->sector);
1265
1266 return result;
1267 }
1268
1269 /* Find the nearest vobu by using the cell boundaries */
1270 static int32_t dvdnav_find_vobu_by_cell_boundaries(dvdnav_t *this,
1271 dvdnav_jump_args_t *args, dvdnav_cell_data_t *cell_data,
1272 dvdnav_pos_data_t *jump) {
1273 int64_t jump_offset = 0;
1274 int64_t cell_len = 0;
1275 uint32_t jump_pct = 0;
1276 int32_t result = 0;
1277
1278 /* get jump_offset */
1279 jump_offset = jump->time - cell_data->bgn->time;
1280 if (jump_offset < 0) {
1281 Log1(this, "jump_offset < 0");
1282 return 0;
1283 }
1284 cell_len = cell_data->end->time - cell_data->bgn->time;
1285 if (cell_len < 0) {
1286 Log1(this, "cell_len < 0");
1287 return 0;
1288 }
1289 jump_pct = (jump_offset * 1000) / cell_len;
1290
1291 /* get sector */
1292 /* NOTE: end cell sector in VTS_PGC is last sector of cell
1293 * this last sector is not the start of a VOBU
1294 * +1 to get sector that is the start of a VOBU
1295 * start of a VOBU is needed in order to index into admap */
1296 cell_data->end->sector += 1;
1297 result = dvdnav_admap_interpolate_vobu(this, args,
1298 cell_data->bgn, cell_data->end, jump_pct, &jump->sector);
1299 if (!result) {
1300 Log1(this, "find_by_admap.interpolate");
1301 return 0;
1302 }
1303 return 1;
1304 }
1305
1306 /* Jump to sector by time */
1307 /* NOTE: Mode is currently unimplemented. Only 0 should be passed. */
1308 /* 1 and -1 are for future implementation */
1309 /* 0: Default. Jump to a time which may be either <> time_in_pts_ticks */
1310 /* 1: After. Always jump to a time that is > time_in_pts_ticks */
1311 /* -1: Before. Always jump to a time that is < time_in_pts_ticks */
1312 dvdnav_status_t dvdnav_jump_to_sector_by_time(dvdnav_t *this,
1313 uint64_t time_in_pts_ticks, int32_t mode) {
1314 if (mode != JUMP_MODE_TIME_DEFAULT) return DVDNAV_STATUS_ERR;
1315 int32_t result = DVDNAV_STATUS_ERR;
1316 dvd_state_t *state = NULL;
1317 uint32_t sector_off = 0;
1318 dvdnav_pos_data_t *jump = NULL;
1319 dvdnav_cell_data_t *cell_data = NULL;
1320 dvdnav_jump_args_t *args = NULL;
1321
1322 jump = &(dvdnav_pos_data_t){0};
1323 /* convert time to milliseconds */
1324 jump->time = time_in_pts_ticks / 90;
1325
1326 /* get variables that will be used across both functions */
1327 state = &(this->vm->state);
1328 if (state == NULL) goto exit;
1329
1330 /* get cell info */
1331 cell_data = &(dvdnav_cell_data_t){0};
1332 cell_data->bgn = &(dvdnav_pos_data_t){0};
1333 cell_data->end = &(dvdnav_pos_data_t){0};
1334 result = dvdnav_cell_find(this, state, jump->time, cell_data);
1335 if (!result) goto exit;
1336
1337 /* get admap */
1338 args = &(dvdnav_jump_args_t){0};
1339 args->admap = dvdnav_admap_get(this, state, &args->admap_len);
1340 if (args->admap == NULL) goto exit;
1341
1342 /* find sector */
1343 result = dvdnav_find_vobu_by_tmap(this, state, args, cell_data, jump);
1344 if (!result) {/* bad tmap; interpolate over cell */
1345 result = dvdnav_find_vobu_by_cell_boundaries(this, args, cell_data, jump);
1346 if (!result) {
1347 goto exit;
1348 }
1349 }
1350
1351 /* jump to sector */
1352 sector_off = jump->sector - cell_data->bgn->sector;
1353 result = vm_jump_cell_block(this->vm, cell_data->idx, sector_off);
1354 pthread_mutex_lock(&this->vm_lock);
1355 this->cur_cell_time = 0;
1356 if (result) { /* vm_jump_cell_block was successful */
1357 this->vm->hop_channel += HOP_SEEK;
1358 }
1359 pthread_mutex_unlock(&this->vm_lock);
1360
1361 exit:
1362 return result;
1363 }
1364