Hi all. I decided to shorten my username. My apologies for any confusion.
Anyway, as Rémi indicated, accurate seeking is not simple.
I think I have an approach that will work for most dvds. It works on those I directly tested (so far, 20). I believe the rationale behind the approach is valid. I detail more below. The code for searching.c follows afterwards.
My question for the VLC devs: what is the correct procedure for submitting a libdvdnav patch? Is mplayer the current custodians of the lib, and should I contact them? Do I then submit the VLC patch after the libdvdnav one gets integrated? (The VLC patch will look a lot like an earlier post, except it will call the new libdvdnav method: dvdnav_jump_to_sector_by_time)
Also, I would like to provide an option to fall-back on the current get/set time by sector. Although it does not correlate accurately to time, it is consistent: i.e.: seeking to sector 520 will always bring you to sector 520, though sector 520 is not 52 seconds. I would rather not eliminate the current functionality because of (a) backward compatibility and (b) general usefulness. Is this something I can include in the dvdnav.c patch (and presumably, add a VLC variable for it?)
Thanks for any info.
---------
DETAIL
---------
First some background. I used to use the MS DirectDvd library. I believe it's the same library that Windows MediaPlayer uses (the behavior is exactly the same).
It does accurate seeking for most dvds, but there are some DVDs (< 5%) that it will fail and give a Division by Zero error (it will freeze the app).
By my best analysis, I think DirectDVD uses the timestamps in the VOB PCI pack (c_eltm?)
http://dvd.sourceforge.net/dvdinfo/pci_pkt.html. It doesn't seem to use the IFO at all.
From what I've seen, I think this may be the best of all possible approaches. The info in the IFO will be correct for most dvds, but there are some dvds where the time map (VTS_TMAPTI) is corrupt or missing (5% of my collection). I imagine there are some dvds that have similiar issues with VTS_VOBU_ADMAP (have not seen any yet).
That said, the div-by-zero errors shows that the VOB PCI pack is not always correct (again, about 5% of my collection. some overlap with the prior 5%). However, I think other info in the VOB PCI pack can be used to correct this corruption (vobu_s_ptm; vobu_e_ptm; vobu_se_e_ptm). I'm assuming that DirectDVD simply didn't bother enough to fix them (especially when considering that div-by-zero freezes the app).
Unfortunately, scanning the VOB files for this data is a project beyond my current ability/time.
So in its place, I devised an approach that uses the IFOs only. It's not ideal, and will probably not work for poorly-authored DVDs. I'm hoping that segment is small (< 1%). For those DVDs, I think there is no other recourse other than to do VOB PCI pack analysis.
The IFO approach works as follows:
* Try the time_maps (should work for 95% of dvds)
- for a given time, find the entry in the time_map table (VTS_TMAPTI)
The time_map will break down a pgc into intervals of n seconds.
Unfortunately n is generally 4 seconds, so...
- lookup the corresponding sectors in the ad_map table (VTS_VOBU_ADMAP)
The tmap sectors will produce a range in ad_map of 6 - 10 VOBUs.
- do time interpolation over the 6 - 10 to get the time
6 - 10 will be small enough that time interpolation will be accurate
* If there is no good time_map, try the ad_map (should work for the remaining 5% of dvds)
- for a given time, find the cell in the pgc
- get the sector bounds of the cell
- lookup the corresponding sectors from the ad_map table (VTS_VOBU_ADMAP)
Unfortunately, the ad_map range will be much larger: a few hundred to a few thousand VOBUs.
- do time interpoloation over the range
time interpolation is not ideal, as the range is very large
however, VOBUs are sized between .4 and 1.5 seconds. Within a given cell, they appear to be reasonably consistent (i.e.: VOBUs may hover around 15 frames)
* If there is no good ad_map, then just use the cell (same as dvdnav_time_search)
- for a given time, find the cell in the pgc
- get the sector bounds of the cell
- do time interpolation over the sector bounds
this will rarely be accurate. It is there to handle the outlyer dvds (< 1%).
Here's a specific example for the time_map approach. It tries to find an @sector for a @time of 5 seconds
1. look at VTS_TMAPTI
@time_interval = 4
Code: Select all
Entries = tmap_idx sector time
0 711 4
1 1776 8
2 2790 12
since 5 seconds is between 4 and 8, @time is between tmap_idx 0 and 1
@sector_bgn = 711
@sector_end = 1776
2. look at VTS_VOBU_ADMAP
Code: Select all
Entries = vobu sector
0 0
1 43
etc.
9 711
10 831
11 948
12 1080
13 1213
14 1349
15 1493
16 1642
17 1776
@vobu_bgn = 9
@vobu_end = 17
3. interpolate
there are 8 vobus between 9 and 17
5 is 1/4 of the way between 4 and 8
1/4 of the way between 9 and 17 is 11
so, @sector = 948
The code follows next. It is long, but there are a lot of comments and fprintfs.
Code: Select all
/*
* Copyright (C) 2000 Rich Wareham <richwareham@users.sourceforge.net>
*
* This file is part of libdvdnav, a DVD navigation library.
*
* libdvdnav is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* libdvdnav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* $Id: searching.c 1135 2008-09-06 21:55:51Z rathann $
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
//#include "dvd_types.h"
#include "dvdnav/dvdnav.h"
#include <dvdread/nav_types.h>
#include <dvdread/ifo_types.h>
#include "remap.h"
#include "vm/decoder.h"
#include "vm/vm.h"
//#include "dvdnav.h"
#include "dvdnav_internal.h"
/*
#define LOG_DEBUG
*/
#include <dvdread/ifo_read.h>
#define RESULT_FALSE 0
#define RESULT_TRUE 1
#define CELL_FIND_SECTOR 1
#define CELL_FIND_TIME 2
/*
proc:
check_null
in:
@val: pointer to check
@val_name: name of pointer
out:
RESULT_TRUE if null
RESULT_FALSE if not-null
notes:
this proc...
- returns true if null
calling procs can then handle null refs accordingly
EX: if (check_null(&ptr)) {return;}
- assists logging
*/
static int32_t check_null(void *val, const char *val_name) {
char msg_str[255];
if (val) {
strcpy (msg_str, " ");
strcat (msg_str, val_name);
strcat (msg_str, "=y");
fprintf(MSG_OUT, msg_str);
return RESULT_FALSE;
}
else {
strcpy (msg_str, "\nlibdvdnav.time_search: ERR: NULL REF: ");
strcat (msg_str, val_name);
fprintf(MSG_OUT, msg_str);
return RESULT_TRUE;
}
}
/*
proc:
dvdnav_tmap_get_entry
in:
@tmap: tmap instance
@entry_count: used for bounds check
@entry_idx: index to lookup
@out_sector: sector val
out:
RESULT_TRUE if found
RESULT_FALSE if not
notes:
this proc...
- does bounds checking
- gets a sector from a tmap
- unsets discontinuity bit (if present)
*/
static int32_t dvdnav_tmap_get_entry(vts_tmap_t *tmap, uint16_t entry_count, uint32_t entry_idx, uint32_t *out_sector) {
if (entry_idx >= entry_count) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: entry_idx >= entry_count");
return RESULT_FALSE;
}
int32_t sector = tmap->map_ent[entry_idx];
if ((sector & (1 << 31)) != 0) { // if discontinuity bit is set
sector &= ~(1 << 31); // unset bit
}
if (sector < 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: sector < 0; discontinuity?");
return RESULT_FALSE;
}
*out_sector = sector;
return RESULT_TRUE;
}
/*
proc:
dvdnav_cell_find
in:
@this: dvdnav instance
@find_mode:
- CELL_FIND_SECTOR: find cell that has this sector
- CELL_FIND_TIME: find cell that has this time
@find_val: value to find
@out_cell_idx: index of found cell
@out_sector_bgn: sector_bgn of found cell; needed when CELL_FIND_TIME
@out_sector_end: sector_end of found cell; needed when CELL_FIND_TIME
@out_time_bgn: time_bgn of found cell; needed when CELL_FIND_SECTOR
@out_time_end: time_end of found cell; needed when CELL_FIND_SECTOR
out:
RESULT_TRUE if found
RESULT_FALSE if not
notes:
this proc finds a cell, according to the find_args
the @find_mode/@find_val design is sloppy
however, there is specific cell traversal logic that I did not want to duplicate
*/
static int32_t dvdnav_cell_find(dvdnav_t *this, int32_t find_mode, uint64_t find_val, int32_t *out_cell_idx, uint32_t *out_sector_bgn, uint32_t *out_sector_end, uint64_t *out_time_bgn, uint64_t *out_time_end) {
// output msg
if (find_mode == CELL_FIND_SECTOR)
fprintf(MSG_OUT, " find_mode=sector");
else if (find_mode == CELL_FIND_TIME)
fprintf(MSG_OUT, " find_mode=time");
fprintf(MSG_OUT, " find_val=%"PRId64, find_val);
// get state
dvd_state_t *state = &(this->vm->state);
if (check_null(&state, "state")) return RESULT_FALSE;
// get pgc
pgc_t *pgc = state->pgc;
if (check_null(&pgc, "pgc")) return RESULT_FALSE;
// get cell_count
uint32_t cell_count = pgc->nr_of_cells;
if (cell_count == 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: cell_count == 0");
return RESULT_FALSE;
}
// get cell_bgn, cell_end
uint32_t cell_bgn, cell_end;
if (this->pgc_based) {
fprintf(MSG_OUT, " pgc_based=y");
cell_bgn = 1;
cell_end = cell_count;
}
else {
int pgN = state->pgN;
fprintf(MSG_OUT, " pgN=%i", pgN);
cell_bgn = pgc->program_map[pgN - 1]; // -1 b/c pgN is 1 based?
int program_count = pgc->nr_of_programs;
fprintf(MSG_OUT, " program_count=%i", program_count);
if (pgN < program_count) {
fprintf(MSG_OUT, " pgN < program_count=y");
cell_end = pgc->program_map[pgN] - 1;
}
else {
fprintf(MSG_OUT, " pgN >= program_count=y");
cell_end = cell_count;
}
}
fprintf(MSG_OUT, " cell_bgn=%i", cell_bgn);
fprintf(MSG_OUT, " cell_end=%i", cell_end);
// search cells
uint32_t cell_idx, sector_bgn = 0, sector_end = 0;
uint64_t time_bgn = 0, time_end = 0;
cell_playback_t *cell;
int located = RESULT_FALSE;
for (cell_idx = cell_bgn; cell_idx <= cell_end; cell_idx++) {
cell = &(pgc->cell_playback[cell_idx-1]); // -1 b/c cell is base1
// if angle block, only consider first angleBlock (others are "redundant" for purpose of search)
if (cell->block_type == BLOCK_TYPE_ANGLE_BLOCK && cell->block_mode != BLOCK_MODE_FIRST_CELL) {
continue;
}
sector_bgn = cell->first_sector;
sector_end = cell->last_sector;
time_end += (dvdnav_convert_time(&cell->playback_time) / 90); // 90: pts to ms
if (find_mode == CELL_FIND_SECTOR) {
if (find_val >= sector_bgn && find_val <= sector_end) {
located = RESULT_TRUE;
break;
}
}
else if (find_mode == CELL_FIND_TIME) {
if (find_val >= time_bgn && find_val <= time_end) {
located = RESULT_TRUE;
break;
}
}
time_bgn = time_end;
}
// found cell: set *out vars
if (located == RESULT_TRUE) {
fprintf(MSG_OUT, " found cell; cell_idx=%i", cell_idx);
fprintf(MSG_OUT, " sector_bgn=%i", sector_bgn);
fprintf(MSG_OUT, " sector_end=%i", sector_end);
fprintf(MSG_OUT, " time_bgn=%"PRId64, time_bgn);
fprintf(MSG_OUT, " time_end=%"PRId64, time_end);
*out_cell_idx = cell_idx;
*out_sector_bgn = sector_bgn;
*out_sector_end = sector_end;
*out_time_bgn = time_bgn;
*out_time_end = time_end;
}
else
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: cell not found");
return located;
}
/*
proc:
dvdnav_tmap_is_valid
in:
@this: dvdnav instance
@tmap: tmap instance
@entry_count: # of entries in tmap; assumed to be valid
@time_interval: time (in seconds) separating entries; assumed to be valid
out:
RESULT_TRUE if valid
RESULT_FALSE if not
notes:
this proc currently validates a time map according to two criteria
1. checks if 0 entries
2. checks entry0 to make certain it is valid in context of its cell
this may be easier explained by example
entry0 lists sector 4500
time_interval is 4
this means that time of 4 seconds should be in a cell with
- a sector_bgn < 4500
- a sector_end > 4500
so, iterate over the cells to find cell for 4 secs
@cell_idx for 4 secs is 8: it has time_bgn of 3 secs and time_end of 5 secs
however, it has a sector_bgn of 3000 and a sector_end of 4000
since this contradicts the time_map, the time_map is considered not valid
*/
static int32_t dvdnav_tmap_is_valid(dvdnav_t *this, vts_tmap_t *tmap, uint32_t entry_count, uint32_t time_interval) {
// tmap with 0 entries are invalid
if (entry_count == 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: tmap_entry_count == 0");
return RESULT_FALSE;
}
// get cell corresponding to entry0
int32_t cell_idx;
uint32_t sector_bgn = 0, sector_end = 0;
uint64_t time_bgn, time_end;
int32_t result;
result = dvdnav_cell_find(this, CELL_FIND_TIME, time_interval, &cell_idx, §or_bgn, §or_end, &time_bgn, &time_end);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: tmap_valid cell_find");
fprintf(MSG_OUT, " time=%i", time_interval);
return RESULT_FALSE;
}
// get sector for entry 0
uint32_t entry0_sector = 0;
result = dvdnav_tmap_get_entry(tmap, entry_count, 0, &entry0_sector);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: tmap_valid tmap_get_sector");
fprintf(MSG_OUT, " idx=%i", 0);
return RESULT_FALSE;
}
// check entry0_sector is between corresponding cell_sectors
if (entry0_sector >= sector_bgn && entry0_sector <= sector_end) {
fprintf(MSG_OUT, " tmap_valid=y");
return RESULT_TRUE;
}
else {
fprintf(MSG_OUT, " tmap_valid=n");
fprintf(MSG_OUT, " entry0_sector=%i", entry0_sector);
fprintf(MSG_OUT, " sector_bgn=%i", sector_bgn);
fprintf(MSG_OUT, " sector_end=%i", sector_end);
return RESULT_FALSE;
}
}
/*
proc:
dvdnav_tmap_find_by_time
in:
@this: dvdnav instance
@time_in_ms: time to find
@out_tmap_idx: index of found entry
@out_time_interval: time_interval in map
@out_sector_bgn: sector of found entry
@out_sector_end: sector of next extry
out:
RESULT_TRUE if found
RESULT_FALSE if not
todo:
error handling for ifo retrieval
notes:
this proc scans the time_map to find the entry for @time_in_ms
*/
static int32_t dvdnav_tmap_find_by_time(dvdnav_t *this, uint32_t time_in_ms, uint32_t *out_tmap_idx, uint32_t *out_time_interval, uint32_t *out_sector_bgn, uint32_t *out_sector_end) {
fprintf(MSG_OUT, " dvdnav_tmap_find_by_time time_in_ms=%i", time_in_ms);
// get state
dvd_state_t *state = &(this->vm->state);
if (check_null(&state, "state")) return RESULT_FALSE;
// get domain
domain_t domain = state->domain;
fprintf(MSG_OUT, " domain=%i", domain);
// get vts_idx
int32_t vts_idx = state->vtsN; // int
fprintf(MSG_OUT, " vts_idx=%i", vts_idx);
// get ifo
// TODO: error handling for ifo retrieval
ifo_handle_t *ifo = NULL;
switch(domain) {
case FP_DOMAIN:
case VTSM_DOMAIN:
case VMGM_DOMAIN: {
// NOTE: ifo = this->vm->vmgi does not work
ifo = ifoOpenVMGI(this->vm->dvd);
fprintf(MSG_OUT, " ifo=non-title");
break;
}
case VTS_DOMAIN: {
// NOTE: ifo = this->vm->vtsi doesn't work
ifo = ifoOpen(this->vm->dvd, vts_idx);
fprintf(MSG_OUT, " ifo=title");
break;
}
default: {// NOTE: will not check out-of-range domain again
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: Unknown domain");
return RESULT_FALSE;
}
}
if (check_null(&ifo, "ifo")) return RESULT_FALSE;
// get tmapt
vts_tmapt_t *tmapt = ifo->vts_tmapt;
if (check_null(&tmapt, "tmapt")) return RESULT_FALSE;
// get tmap_count
uint16_t tmap_count = tmapt->nr_of_tmaps;
fprintf(MSG_OUT, " tmap_count=%i", tmap_count);
// get pgcN; -1 b/c pgcN is base1
int32_t pgcN = (state->pgcN) - 1; // int
fprintf(MSG_OUT, " pgcN=%i", pgcN);
if (pgcN < 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: pgcN < 0");
return RESULT_FALSE;
}
// get tmap
vts_tmap_t *tmap = NULL;
switch(domain) {
case FP_DOMAIN:
case VMGM_DOMAIN:
case VTSM_DOMAIN: {
if (tmap_count == 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: tmap_count == 0");
return RESULT_FALSE;
}
tmap = &(tmapt->tmap[0]); // ASSUME: vmgi only has one time map
fprintf(MSG_OUT, " tmap=non-title");
break;
}
case VTS_DOMAIN: {
if (pgcN >= tmap_count) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: pgcN >= tmap_count");
return RESULT_FALSE;
}
tmap = &(tmapt->tmap[pgcN]);
fprintf(MSG_OUT, " tmap=title");
break;
}
}
if (check_null(&tmap, "tmap")) return RESULT_FALSE;
// get time_interval; tmap->tmu is in seconds
uint32_t time_interval = (tmap->tmu) * 1000; // * 1000 converts to millisecs
fprintf(MSG_OUT, " time_interval=%i", time_interval);
if (time_interval == 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: time_interval == 0");
return RESULT_FALSE;
}
// check tmap is valid
if (!dvdnav_tmap_is_valid(this, tmap, time_in_ms, time_interval)) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: tmap not valid");
return RESULT_FALSE;
}
// get entry_count
uint16_t entry_count = tmap->nr_of_entries;
fprintf(MSG_OUT, " entry_count=%i", entry_count);
// get tmap_idx, tmap_idx_end: -1 b/c tmap is base1
// EX: idx time
// 0 4
// 1 8
uint32_t tmap_idx_bgn = (time_in_ms / time_interval) - 1;// -1 for base1
fprintf(MSG_OUT, " tmap_idx_bgn=%i", tmap_idx_bgn);
int32_t tmap_idx_end = tmap_idx_bgn + 1;
fprintf(MSG_OUT, " tmap_idx_end=%i", tmap_idx_end);
// get sector_bgn, sector_end
uint32_t sector_bgn, sector_end;
int32_t result = dvdnav_tmap_get_entry(tmap, entry_count, tmap_idx_bgn, §or_bgn);
fprintf(MSG_OUT, " sector_bgn=%i", sector_bgn);
if (!result) return result;
result = dvdnav_tmap_get_entry(tmap, entry_count, tmap_idx_end, §or_end);
fprintf(MSG_OUT, " sector_end=%i", sector_end);
if (!result) return result;
// set *out vars
*out_tmap_idx = tmap_idx_bgn;
*out_time_interval = time_interval;
*out_sector_bgn = sector_bgn;
*out_sector_end = sector_end;
fprintf(MSG_OUT, " found tmap_idx=%i", tmap_idx_bgn);
fprintf(MSG_OUT, " time_interval=%i", time_interval);
fprintf(MSG_OUT, " sector_bgn=%i", sector_bgn);
fprintf(MSG_OUT, " sector_end=%i", sector_end);
return RESULT_TRUE;
}
/*
proc:
dvdnav_admap_find_by_sector
in:
@admap: admap instance
@entry_count: total # of entries in table
@sector_to_find: sector to find in table
@out_admap_idx: index for corresponding sector
out:
RESULT_TRUE if found
RESULT_FALSE if not
notes:
this proc uses a binary search to find @sector_to_find
note that an admap usually has an entry_count = # of seconds * 2
for a 2 hour film, entry_count will be 14k (2 * 60 * 60 * 2)
theoretical max is 64k entries
note that other methods in searching.c will iterate over the admap one at a time
they should be diverted to use this method (though performance gains will be negligible)
*/
static int dvdnav_admap_find_by_sector(vobu_admap_t *admap, uint32_t entry_count, uint32_t sector_to_find, uint32_t *out_admap_idx) {
fprintf(MSG_OUT, " sector_to_find=%i", sector_to_find);
int32_t interval = entry_count;
int32_t multiplier = 1;
int32_t idx = 0;
int32_t tries = 0; // safeguard against infinite loop
int32_t sector_cur = 0;
while (1) {
interval = (interval + 1) / 2; // +1 to prevent fractional truncation; EX: 3 / 2 should be 2, not 1
if (interval == 0 || tries++ > 32) {
if (interval == 0)
fprintf(MSG_OUT, "\nlibdvdnav.time_search: WARN: binary search failed; zero interval; sector_to_find=%i", sector_to_find);
else
fprintf(MSG_OUT, "\nlibdvdnav.time_search: WARN: binary search failed; infinite loop; sector_to_find=%i", sector_to_find);
*out_admap_idx = idx;
fprintf(MSG_OUT, " admap_idx=%i", *out_admap_idx);
fprintf(MSG_OUT, " admap_find_tries=%i", tries);
return RESULT_FALSE;
}
idx += interval * multiplier;
sector_cur = admap->vobu_start_sectors[idx];
if (sector_cur == sector_to_find) {
*out_admap_idx = idx;
fprintf(MSG_OUT, " admap_idx=%i", *out_admap_idx);
fprintf(MSG_OUT, " admap_find_tries=%i", tries);
return RESULT_TRUE;
}
multiplier = sector_cur > sector_to_find ? -1 : 1;
}
}
/*
proc:
dvdnav_admap_interpolate_vobu
in:
@this: dvdnav instance
@sector_bgn: start sector of range
@sector_end: end sector of range
@fraction: fraction to interpolate
@vobu_adj: arbitrary value to add (fudge factor)
@out_sector: result of interpolation
out:
RESULT_TRUE if successful
RESULT_FALSE if not
notes:
this proc uses the admap to find the vobu that is the interpolated value of @sector_bgn, @sector_end and @fraction
again, an example may be easier to explain
@sector_bgn = 1000
@sector_end = 2000
@fraction = .25
@admap says @sector_bgn is vobu 10
@admap says @sector_end is vobu 14
there are 4 vobus between 14 and 10
so, requested vobu is 11 (1/4 of 4)
@admap says voub 11 is @sector 1125
*/
static int32_t dvdnav_admap_interpolate_vobu(dvdnav_t *this, uint32_t sector_bgn, uint32_t sector_end, double fraction, int32_t vobu_adj, uint32_t *out_sector) {
// get state
dvd_state_t *state = &(this->vm->state);
if (check_null(&state, "state")) return RESULT_FALSE;
// get domain
domain_t domain = state->domain;
fprintf(MSG_OUT, " domain=%i", domain);
// get admap
vobu_admap_t *admap = NULL;
switch(domain) {
case FP_DOMAIN:
case VMGM_DOMAIN:
admap = this->vm->vmgi->menu_vobu_admap;
break;
case VTSM_DOMAIN:
admap = this->vm->vtsi->menu_vobu_admap;
break;
case VTS_DOMAIN:
admap = this->vm->vtsi->vts_vobu_admap;
break;
}
if (check_null(&admap, "admap")) return RESULT_FALSE;
// get vobu_count
int32_t vobu_count = (admap->last_byte + 1 - VOBU_ADMAP_SIZE) / VOBU_ADMAP_SIZE;
fprintf(MSG_OUT, " vobu_count=%i", vobu_count);
if (vobu_count <= 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: vobu_count <= 0");
return RESULT_FALSE;
}
// get vobu_bgn
int32_t result;
uint32_t vobu_bgn;
result = dvdnav_admap_find_by_sector(admap, vobu_count, sector_bgn, &vobu_bgn);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: could not find sector_bgn");
return RESULT_FALSE;
}
// get vobu_end
uint32_t vobu_end;
result = dvdnav_admap_find_by_sector(admap, vobu_count, sector_end, &vobu_end);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: could not find sector_end");
return RESULT_FALSE;
}
// get vobu_dif
uint32_t vobu_dif = vobu_end - vobu_bgn;
fprintf(MSG_OUT, " vobu_dif=%i", vobu_dif);
// get vobu_off
uint32_t vobu_off = (fraction * ((double)vobu_dif + .5)); // +.5 to round up else .74 * 4 = 2
fprintf(MSG_OUT, " vobu_off=%i", vobu_off);
// adjust vobu_off
if (vobu_adj != 0) {
vobu_off += vobu_adj;
fprintf(MSG_OUT, " vobu_off + vobu_adj=%i", vobu_off);
}
// get sector
if (vobu_off >= vobu_count) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: vobu_off >= vobu_count");
return RESULT_FALSE;
}
*out_sector = admap->vobu_start_sectors[vobu_bgn + vobu_off];
fprintf(MSG_OUT, " vobu_out_sector=%i", *out_sector);
return RESULT_TRUE;
}
/*
proc:
dvdnav_find_sector_by_tmap
in:
@this: dvd_nav instance
@time_in_ms: time to find
@out_sector: sector found
out:
RESULT_TRUE if found
RESULT_FALSE if not
hacks:
+1 to vobu to adjust for time (is this a bounds issue?)
notes:
this proc finds the @sector from a given @time. Its approach is...
1. look at VTS_TMAPTI to find...
@time_interval: time precision of tmap (commonly 4 seconds?)
@sector_bgn: sector before @time
@sector_end: sector after @time
2. iterate over VTS_VOBU_ADMAP to find...
@vobu_bgn: vobu index corresponding to @sector_bgn
@vobu_end: vobu index corresponding to @sector_end
NOTE: VTS_VOBU_ADMAP will give precision to n seconds,
but n is only defined in the VOB PCI packet (vobu_s_ptm, vobu_e_ptm)
it will be .5 seconds (roughly) but it may vary
3. interpolate @sector using data from (1) and (2)
this proc will be accurate if...
1. VTS_TMAPTI is not corrupt
2. VTS_VOBU_ADMAP is not corrupt
NOTE: ifo_read.c reads VTS_TMAPTI and VTS_VOBU_ADMAP
- it will probably not be wrong, but look there for bugs
- if corrupt, the more likely source is either..
(a) original DVD may be mastered badly
(b) ifo may be altered manually by other programs
3. vobus are the same duration between two entries in VTS_TMAPTI
vobus seem to be the same time, though this is an assumption
even if false, then imprecision will be no more than @time_interval
EX:
find @sector for @time of 5 seconds
1. VTS_TMAPTI
@time_interval = 4
Entries = tmap_idx sector time
0 711 4
1 1776 8
2 2790 12
since 5 seconds is between 4 and 8, @time is between tmap_idx 0 and 1
@sector_bgn = 711
@sector_end = 1776
2. VTS_VOBU_ADMAP
Entries = vobu sector
0 0
1 43
etc.
9 711
10 831
11 948
12 1080
13 1213
14 1349
15 1493
16 1642
17 1776
@vobu_bgn = 9
@vobu_end = 17
3. interpolate
there are 8 vobus between 9 and 17
5 is 1/4 of the way between 4 and 8
1/4 of the way between 9 and 17 is 11
so, @sector = 948
*/
static int32_t dvdnav_find_sector_by_tmap(dvdnav_t *this, uint64_t time_in_ms, uint32_t *out_sector) {
// get tmap data
uint32_t tmap_idx; uint32_t time_interval;
uint32_t sector_bgn, sector_end;
int32_t result = dvdnav_tmap_find_by_time(this, time_in_ms, &tmap_idx, &time_interval, §or_bgn, §or_end);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: could not find time in tmap");
return RESULT_FALSE;
}
// get time_in_ms_bgn
// NOTE: +1 b/c tmap is 0 based array, with 1 * time_interval
// EX: idx sector time calc
// 0 711 4 (idx + 1) * time_interval -> 4
// 1 1776 8 (idx + 1) * time_interval -> 8
uint32_t time_in_ms_bgn = (tmap_idx + 1) * time_interval;
fprintf(MSG_OUT, " time_in_ms_bgn2=%i", time_in_ms_bgn);
// get time_in_ms_dif
int32_t time_in_ms_dif = time_in_ms - time_in_ms_bgn;
fprintf(MSG_OUT, " time_in_ms_dif=%i", time_in_ms_dif);
if (time_in_ms_dif < 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: time_in_ms_dif < 0");
return RESULT_FALSE;
}
// get time_pct
double time_pct = (double)time_in_ms_dif / time_interval; // time_interval checked previously to be > 0
fprintf(MSG_OUT, " time_pct=%f", time_pct);
// get sector
uint32_t sector = 0;
result = dvdnav_admap_interpolate_vobu(this, sector_bgn, sector_end, time_pct, 1, §or);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: find_by_tmap.interpolate");
return RESULT_FALSE;
}
fprintf(MSG_OUT, " sector=%i", sector);
// return
*out_sector = sector;
return RESULT_TRUE;
}
/*
proc:
dvdnav_find_sector_by_admap
in:
@this: dvd_nav instance
@time_in_ms: time to find
@out_sector: sector found
out:
RESULT_TRUE if found
RESULT_FALSE if not
hacks:
+1 to vobu to adjust for time (is this a bounds issue?)
assumes:
vmgi ifo only has one time map
notes:
this proc finds the @sector from a given @time. Its approach is...
1. look at cells to find...
@sector_bgn: start sector of cell
@sector_end: end sector of cell
@time_bgn: start time of cell
@time__end: end time of cell
2. iterate over VTS_VOBU_ADMAP to find...
@vobu_bgn: vobu index corresponding to @sector_bgn
@vobu_end: vobu index corresponding to @sector_end
3. interpolate @sector using data from (1) and (2)
refer to dvdnav_find_sector_by_tmap as logic is mostly the same
*/
static int32_t dvdnav_find_sector_by_admap(dvdnav_t *this, uint64_t time_in_ms, uint32_t *out_sector) {
// find cell_idx
int32_t cell_idx;
uint32_t sector_bgn, sector_end;
uint64_t time_bgn, time_end;
int32_t result;
result = dvdnav_cell_find(this, CELL_FIND_TIME, time_in_ms, &cell_idx, §or_bgn, §or_end, &time_bgn, &time_end);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: find_by_admap.cell_find");
return RESULT_FALSE;
}
// get time_in_ms_dif
uint64_t time_in_ms_dif = time_in_ms - time_bgn;
fprintf(MSG_OUT, " time_in_ms_dif=%"PRId64, time_in_ms_dif);
if (time_in_ms_dif < 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: time_in_ms_dif < 0");
return RESULT_FALSE;
}
// get time_len
uint64_t time_len = time_end - time_bgn;
fprintf(MSG_OUT, " time_len=%"PRId64, time_len);
if (time_len < 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: time_len < 0");
return RESULT_FALSE;
}
// get time_pct
double time_pct = (double)time_in_ms_dif / time_len;
fprintf(MSG_OUT, " time_pct=%f", time_pct);
// get sector
uint32_t sector = 0;
sector_end++; // +1: cell sector in VTS_PGC is last sector of cell which is NOT a VOBU
result = dvdnav_admap_interpolate_vobu(this, sector_bgn, sector_end, time_pct, 1, §or);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: find_by_admap.interpolate");
return RESULT_FALSE;
}
fprintf(MSG_OUT, " sector=%i", sector);
// return
*out_sector = sector;
return RESULT_TRUE;
}
/*
proc:
dvdnav_find_sector_by_cells
in:
@this: dvd_nav instance
@time_in_ms: time to find
@out_sector: sector found
out:
RESULT_TRUE if found
RESULT_FALSE if not
notes:
this proc finds the @sector from a given @time. Its approach is...
1. look at cells to find...
@sector_bgn: start sector of cell
@sector_end: end sector of cell
@time_bgn: start time of cell
@time__end: end time of cell
2. interpolate @sector using data from (1)
*/
static int32_t dvdnav_find_sector_by_cells(dvdnav_t *this, uint64_t time_in_ms, uint32_t *out_sector) {
// find cell_idx
int32_t cell_idx;
uint32_t sector_bgn, sector_end;
uint64_t time_bgn, time_end;
int32_t result;
result = dvdnav_cell_find(this, CELL_FIND_TIME, time_in_ms, &cell_idx, §or_bgn, §or_end, &time_bgn, &time_end);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: find_by_cells.cell_find");
return RESULT_FALSE;
}
// get time_in_ms_dif
uint64_t time_in_ms_dif = time_in_ms - time_bgn;
fprintf(MSG_OUT, " time_in_ms_dif=%"PRId64, time_in_ms_dif);
if (time_in_ms_dif < 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: time_in_ms_dif < 0");
return RESULT_FALSE;
}
// get time_dif
uint64_t time_dif = time_end - time_bgn;
fprintf(MSG_OUT, " time_dif=%"PRId64, time_dif);
if (time_dif < 0) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: time_dif < 0");
return RESULT_FALSE;
}
// get time_pct
double time_pct = (double)time_in_ms_dif / time_dif;
fprintf(MSG_OUT, " time_pct=%f", time_pct);
// get sector_dif
uint32_t sector_dif = sector_end - sector_bgn;
fprintf(MSG_OUT, " sector_dif=%i", sector_dif);
// get sector_off
uint32_t sector_off = sector_bgn + sector_off;
fprintf(MSG_OUT, " sector_off=%i", sector_off);
// return
*out_sector = sector_bgn + sector_off;
return RESULT_TRUE;
}
/*
proc:
dvdnav_jump_to_sector
in:
@this: dvd_nav instance
@sector: sector to jump to
out:
RESULT_TRUE if successful
RESULT_FALSE if not
notes:
this proc will cause the vm to go to the @sector
*/
static int32_t dvdnav_jump_to_sector(dvdnav_t *this, uint32_t sector) {
int32_t cell_idx;
uint32_t sector_bgn, sector_end;
uint64_t time_bgn, time_end;
int32_t result;
// find cell_idx
result = dvdnav_cell_find(this, CELL_FIND_SECTOR, sector, &cell_idx, §or_bgn, §or_end, &time_bgn, &time_end);
if (!result) {
fprintf(MSG_OUT, "\nlibdvdnav.time_search: ERR: jump_to_sector.cell_find");
return RESULT_FALSE;
}
// get sector_offset within cell; EX: sector=4100; cell begins at 4000; sector_offset = 100;
uint32_t sector_off = sector - sector_bgn;
fprintf(MSG_OUT, " sector_off=%i", sector_off);
// jump to cell/sector
this->cur_cell_time = 0;
if (vm_jump_cell_block(this->vm, cell_idx, sector_off)) {
pthread_mutex_lock(&this->vm_lock);
this->vm->hop_channel += HOP_SEEK;
pthread_mutex_unlock(&this->vm_lock);
return RESULT_TRUE;
}
return RESULT_FALSE;
}
/*
proc:
dvdnav_jump_to_sector_by_time
in:
@this: dvd_nav instance
@time_in_pts_ticks: time in presentation time stamp ticks
out:
RESULT_TRUE if successful
RESULT_FALSE if not
notes:
this proc tries to jump to a sector by time.
it tries to find a sector by calling each of these procs in succession
- dvdnav_find_sector_by_tmap
- dvdnav_find_sector_by_admap (if tmap fails)
- dvdnav_find_sector_by_cells (if tmap and admap fails)
if it gets a sector, it then calls dvdnav_jump_to_sector
*/
static int32_t dvdnav_jump_to_sector_by_time(dvdnav_t *this, uint32_t time_in_pts_ticks) {
int32_t result = RESULT_FALSE;
fprintf(MSG_OUT, "\n*** time_in_pts_ticks=%i", time_in_pts_ticks);
// get time_in_ms
uint64_t time_in_ms = time_in_pts_ticks / 90;
fprintf(MSG_OUT, " time_in_ms=%"PRId64, time_in_ms);
// get_sector
uint32_t sector = 0;
result = dvdnav_find_sector_by_tmap(this, time_in_ms, §or);
if (!result) {
result = dvdnav_find_sector_by_admap(this, time_in_ms, §or);
if (!result) {
result = dvdnav_find_sector_by_cells(this, time_in_ms, §or);
if (!result) {
goto exit;
}
}
}
// jump to sector
result = dvdnav_jump_to_sector(this, sector);
if (!result) goto exit;
result = RESULT_TRUE;
exit:
return result;
}
/* Scan the ADMAP for a particular block number. */
/* Return placed in vobu. */
/* Returns error status */
/* FIXME: Maybe need to handle seeking outside current cell. */
static dvdnav_status_t dvdnav_scan_admap(dvdnav_t *this, int32_t domain, uint32_t seekto_block, uint32_t *vobu) {
vobu_admap_t *admap = NULL;
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: Seeking to target %u ...\n", seekto_block);
#endif
*vobu = -1;
/* Search through the VOBU_ADMAP for the nearest VOBU
* to the target block */
switch(domain) {
case FP_DOMAIN:
case VMGM_DOMAIN:
admap = this->vm->vmgi->menu_vobu_admap;
break;
case VTSM_DOMAIN:
admap = this->vm->vtsi->menu_vobu_admap;
break;
case VTS_DOMAIN:
admap = this->vm->vtsi->vts_vobu_admap;
break;
default:
fprintf(MSG_OUT, "libdvdnav: Error: Unknown domain for seeking.\n");
}
if(admap) {
uint32_t address = 0;
uint32_t vobu_start, next_vobu;
int admap_entries = (admap->last_byte + 1 - VOBU_ADMAP_SIZE)/VOBU_ADMAP_SIZE;
/* Search through ADMAP for best sector */
vobu_start = SRI_END_OF_CELL;
/* FIXME: Implement a faster search algorithm */
while(address < admap_entries) {
next_vobu = admap->vobu_start_sectors[address];
/* fprintf(MSG_OUT, "libdvdnav: Found block %u\n", next_vobu); */
if(vobu_start <= seekto_block && next_vobu > seekto_block)
break;
vobu_start = next_vobu;
address++;
}
*vobu = vobu_start;
return DVDNAV_STATUS_OK;
}
fprintf(MSG_OUT, "libdvdnav: admap not located\n");
return DVDNAV_STATUS_ERR;
}
/* Searching API calls */
/* FIXME: right now, this function does not use the time tables but interpolates
only the cell times */
dvdnav_status_t dvdnav_time_search(dvdnav_t *this,
uint64_t time) {
uint64_t target = time;
uint64_t length = 0;
uint32_t first_cell_nr, last_cell_nr, cell_nr;
int32_t found;
cell_playback_t *cell;
dvd_state_t *state;
if(this->position_current.still != 0) {
printerr("Cannot seek in a still frame.");
return DVDNAV_STATUS_ERR;
}
pthread_mutex_lock(&this->vm_lock);
state = &(this->vm->state);
if(!state->pgc) {
printerr("No current PGC.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
this->cur_cell_time = 0;
if (this->pgc_based) {
first_cell_nr = 1;
last_cell_nr = state->pgc->nr_of_cells;
} else {
first_cell_nr = state->pgc->program_map[state->pgN-1];
if(state->pgN < state->pgc->nr_of_programs)
last_cell_nr = state->pgc->program_map[state->pgN] - 1;
else
last_cell_nr = state->pgc->nr_of_cells;
}
found = 0;
for(cell_nr = first_cell_nr; (cell_nr <= last_cell_nr) && !found; cell_nr ++) {
cell = &(state->pgc->cell_playback[cell_nr-1]);
if(cell->block_type == BLOCK_TYPE_ANGLE_BLOCK && cell->block_mode != BLOCK_MODE_FIRST_CELL)
continue;
length = dvdnav_convert_time(&cell->playback_time);
if (target >= length) {
target -= length;
} else {
target = target * (cell->last_sector - cell->first_sector + 1) / length;
target += cell->first_sector;
found = 1;
break;
}
}
if(found) {
uint32_t vobu;
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: Seeking to cell %i from choice of %i to %i\n",
cell_nr, first_cell_nr, last_cell_nr);
#endif
if (dvdnav_scan_admap(this, state->domain, target, &vobu) == DVDNAV_STATUS_OK) {
uint32_t start = state->pgc->cell_playback[cell_nr-1].first_sector;
if (vm_jump_cell_block(this->vm, cell_nr, vobu - start)) {
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: After cellN=%u blockN=%u target=%x vobu=%x start=%x\n" ,
state->cellN, state->blockN, target, vobu, start);
#endif
this->vm->hop_channel += HOP_SEEK;
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_OK;
}
}
}
fprintf(MSG_OUT, "libdvdnav: Error when seeking\n");
printerr("Error when seeking.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
dvdnav_status_t dvdnav_sector_search(dvdnav_t *this,
uint64_t offset, int32_t origin) {
uint32_t target = 0;
uint32_t length = 0;
uint32_t first_cell_nr, last_cell_nr, cell_nr;
int32_t found;
cell_playback_t *cell;
dvd_state_t *state;
dvdnav_status_t result;
if(this->position_current.still != 0) {
printerr("Cannot seek in a still frame.");
return DVDNAV_STATUS_ERR;
}
result = dvdnav_get_position(this, &target, &length);
if(!result) {
return DVDNAV_STATUS_ERR;
}
pthread_mutex_lock(&this->vm_lock);
state = &(this->vm->state);
if(!state->pgc) {
printerr("No current PGC.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: seeking to offset=%lu pos=%u length=%u\n", offset, target, length);
fprintf(MSG_OUT, "libdvdnav: Before cellN=%u blockN=%u\n", state->cellN, state->blockN);
#endif
switch(origin) {
case SEEK_SET:
if(offset >= length) {
printerr("Request to seek behind end.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
target = offset;
break;
case SEEK_CUR:
if(target + offset >= length) {
printerr("Request to seek behind end.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
target += offset;
break;
case SEEK_END:
if(length < offset) {
printerr("Request to seek before start.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
target = length - offset;
break;
default:
/* Error occured */
printerr("Illegal seek mode.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
this->cur_cell_time = 0;
if (this->pgc_based) {
first_cell_nr = 1;
last_cell_nr = state->pgc->nr_of_cells;
} else {
/* Find start cell of program. */
first_cell_nr = state->pgc->program_map[state->pgN-1];
/* Find end cell of program */
if(state->pgN < state->pgc->nr_of_programs)
last_cell_nr = state->pgc->program_map[state->pgN] - 1;
else
last_cell_nr = state->pgc->nr_of_cells;
}
found = 0;
for(cell_nr = first_cell_nr; (cell_nr <= last_cell_nr) && !found; cell_nr ++) {
cell = &(state->pgc->cell_playback[cell_nr-1]);
if(cell->block_type == BLOCK_TYPE_ANGLE_BLOCK && cell->block_mode != BLOCK_MODE_FIRST_CELL)
continue;
length = cell->last_sector - cell->first_sector + 1;
if (target >= length) {
target -= length;
} else {
/* convert the target sector from Cell-relative to absolute physical sector */
target += cell->first_sector;
found = 1;
break;
}
}
if(found) {
uint32_t vobu;
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: Seeking to cell %i from choice of %i to %i\n",
cell_nr, first_cell_nr, last_cell_nr);
#endif
if (dvdnav_scan_admap(this, state->domain, target, &vobu) == DVDNAV_STATUS_OK) {
int32_t start = state->pgc->cell_playback[cell_nr-1].first_sector;
if (vm_jump_cell_block(this->vm, cell_nr, vobu - start)) {
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: After cellN=%u blockN=%u target=%x vobu=%x start=%x\n" ,
state->cellN, state->blockN, target, vobu, start);
#endif
this->vm->hop_channel += HOP_SEEK;
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_OK;
}
}
}
fprintf(MSG_OUT, "libdvdnav: Error when seeking\n");
fprintf(MSG_OUT, "libdvdnav: FIXME: Implement seeking to location %u\n", target);
printerr("Error when seeking.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
dvdnav_status_t dvdnav_part_search(dvdnav_t *this, int32_t part) {
int32_t title, old_part;
if (dvdnav_current_title_info(this, &title, &old_part) == DVDNAV_STATUS_OK)
return dvdnav_part_play(this, title, part);
return DVDNAV_STATUS_ERR;
}
dvdnav_status_t dvdnav_prev_pg_search(dvdnav_t *this) {
pthread_mutex_lock(&this->vm_lock);
if(!this->vm->state.pgc) {
printerr("No current PGC.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: previous chapter\n");
#endif
if (!vm_jump_prev_pg(this->vm)) {
fprintf(MSG_OUT, "libdvdnav: previous chapter failed.\n");
printerr("Skip to previous chapter failed.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
this->cur_cell_time = 0;
this->position_current.still = 0;
this->vm->hop_channel++;
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: previous chapter done\n");
#endif
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_OK;
}
dvdnav_status_t dvdnav_top_pg_search(dvdnav_t *this) {
pthread_mutex_lock(&this->vm_lock);
if(!this->vm->state.pgc) {
printerr("No current PGC.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: top chapter\n");
#endif
if (!vm_jump_top_pg(this->vm)) {
fprintf(MSG_OUT, "libdvdnav: top chapter failed.\n");
printerr("Skip to top chapter failed.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
this->cur_cell_time = 0;
this->position_current.still = 0;
this->vm->hop_channel++;
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: top chapter done\n");
#endif
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_OK;
}
dvdnav_status_t dvdnav_next_pg_search(dvdnav_t *this) {
vm_t *try_vm;
pthread_mutex_lock(&this->vm_lock);
if(!this->vm->state.pgc) {
printerr("No current PGC.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: next chapter\n");
#endif
/* make a copy of current VM and try to navigate the copy to the next PG */
try_vm = vm_new_copy(this->vm);
if (!vm_jump_next_pg(try_vm) || try_vm->stopped) {
vm_free_copy(try_vm);
/* next_pg failed, try to jump at least to the next cell */
try_vm = vm_new_copy(this->vm);
vm_get_next_cell(try_vm);
if (try_vm->stopped) {
vm_free_copy(try_vm);
fprintf(MSG_OUT, "libdvdnav: next chapter failed.\n");
printerr("Skip to next chapter failed.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
}
this->cur_cell_time = 0;
/* merge changes on success */
vm_merge(this->vm, try_vm);
vm_free_copy(try_vm);
this->position_current.still = 0;
this->vm->hop_channel++;
#ifdef LOG_DEBUG
fprintf(MSG_OUT, "libdvdnav: next chapter done\n");
#endif
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_OK;
}
dvdnav_status_t dvdnav_menu_call(dvdnav_t *this, DVDMenuID_t menu) {
vm_t *try_vm;
pthread_mutex_lock(&this->vm_lock);
if(!this->vm->state.pgc) {
printerr("No current PGC.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
this->cur_cell_time = 0;
/* make a copy of current VM and try to navigate the copy to the menu */
try_vm = vm_new_copy(this->vm);
if ( (menu == DVD_MENU_Escape) && (this->vm->state.domain != VTS_DOMAIN)) {
/* Try resume */
if (vm_jump_resume(try_vm) && !try_vm->stopped) {
/* merge changes on success */
vm_merge(this->vm, try_vm);
vm_free_copy(try_vm);
this->position_current.still = 0;
this->vm->hop_channel++;
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_OK;
}
}
if (menu == DVD_MENU_Escape) menu = DVD_MENU_Root;
if (vm_jump_menu(try_vm, menu) && !try_vm->stopped) {
/* merge changes on success */
vm_merge(this->vm, try_vm);
vm_free_copy(try_vm);
this->position_current.still = 0;
this->vm->hop_channel++;
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_OK;
} else {
vm_free_copy(try_vm);
printerr("No such menu or menu not reachable.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
}
dvdnav_status_t dvdnav_get_position(dvdnav_t *this, uint32_t *pos,
uint32_t *len) {
uint32_t cur_sector;
int32_t cell_nr, first_cell_nr, last_cell_nr;
cell_playback_t *cell;
dvd_state_t *state;
if(!this->started) {
printerr("Virtual DVD machine not started.");
return DVDNAV_STATUS_ERR;
}
pthread_mutex_lock(&this->vm_lock);
state = &(this->vm->state);
if(!state->pgc || this->vm->stopped) {
printerr("No current PGC.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
if (this->position_current.hop_channel != this->vm->hop_channel ||
this->position_current.domain != state->domain ||
this->position_current.vts != state->vtsN ||
this->position_current.cell_restart != state->cell_restart) {
printerr("New position not yet determined.");
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_ERR;
}
/* Get current sector */
cur_sector = this->vobu.vobu_start + this->vobu.blockN;
if (this->pgc_based) {
first_cell_nr = 1;
last_cell_nr = state->pgc->nr_of_cells;
} else {
/* Find start cell of program. */
first_cell_nr = state->pgc->program_map[state->pgN-1];
/* Find end cell of program */
if(state->pgN < state->pgc->nr_of_programs)
last_cell_nr = state->pgc->program_map[state->pgN] - 1;
else
last_cell_nr = state->pgc->nr_of_cells;
}
*pos = -1;
*len = 0;
for (cell_nr = first_cell_nr; cell_nr <= last_cell_nr; cell_nr++) {
cell = &(state->pgc->cell_playback[cell_nr-1]);
if (cell_nr == state->cellN) {
/* the current sector is in this cell,
* pos is length of PG up to here + sector's offset in this cell */
*pos = *len + cur_sector - cell->first_sector;
}
*len += cell->last_sector - cell->first_sector + 1;
}
assert((signed)*pos != -1);
pthread_mutex_unlock(&this->vm_lock);
return DVDNAV_STATUS_OK;
}
dvdnav_status_t dvdnav_get_position_in_title(dvdnav_t *this,
uint32_t *pos,
uint32_t *len) {
uint32_t cur_sector;
uint32_t first_cell_nr;
uint32_t last_cell_nr;
cell_playback_t *first_cell;
cell_playback_t *last_cell;
dvd_state_t *state;
state = &(this->vm->state);
if(!state->pgc) {
printerr("No current PGC.");
return DVDNAV_STATUS_ERR;
}
/* Get current sector */
cur_sector = this->vobu.vobu_start + this->vobu.blockN;
/* Now find first and last cells in title. */
first_cell_nr = state->pgc->program_map[0];
first_cell = &(state->pgc->cell_playback[first_cell_nr-1]);
last_cell_nr = state->pgc->nr_of_cells;
last_cell = &(state->pgc->cell_playback[last_cell_nr-1]);
*pos = cur_sector - first_cell->first_sector;
*len = last_cell->last_sector - first_cell->first_sector;
return DVDNAV_STATUS_OK;
}
uint32_t dvdnav_describe_title_chapters(dvdnav_t *this, int32_t title, uint64_t **times, uint64_t *duration) {
int32_t retval=0;
uint16_t parts, i;
title_info_t *ptitle = NULL;
ptt_info_t *ptt = NULL;
ifo_handle_t *ifo;
pgc_t *pgc;
cell_playback_t *cell;
uint64_t length, *tmp=NULL;
*times = NULL;
*duration = 0;
pthread_mutex_lock(&this->vm_lock);
if(!this->vm->vmgi) {
printerr("Bad VM state or missing VTSI.");
goto fail;
}
if(!this->started) {
/* don't report an error but be nice */
vm_start(this->vm);
this->started = 1;
}
ifo = vm_get_title_ifo(this->vm, title);
if(!ifo || !ifo->vts_pgcit) {
printerr("Couldn't open IFO for chosen title, exit.");
goto fail;
}
ptitle = &this->vm->vmgi->tt_srpt->title[title-1];
parts = ptitle->nr_of_ptts;
ptt = ifo->vts_ptt_srpt->title[ptitle->vts_ttn-1].ptt;
tmp = calloc(1, sizeof(uint64_t)*parts);
if(!tmp)
goto fail;
length = 0;
for(i=0; i<parts; i++) {
uint32_t cellnr, endcellnr;
pgc = ifo->vts_pgcit->pgci_srp[ptt[i].pgcn-1].pgc;
if(ptt[i].pgn > pgc->nr_of_programs) {
printerr("WRONG part number.");
goto fail;
}
cellnr = pgc->program_map[ptt[i].pgn-1];
if(ptt[i].pgn < pgc->nr_of_programs)
endcellnr = pgc->program_map[ptt[i].pgn];
else
endcellnr = 0;
do {
cell = &pgc->cell_playback[cellnr-1];
if(!(cell->block_type == BLOCK_TYPE_ANGLE_BLOCK &&
cell->block_mode != BLOCK_MODE_FIRST_CELL
))
{
tmp[i] = length + dvdnav_convert_time(&cell->playback_time);
length = tmp[i];
}
cellnr++;
} while(cellnr < endcellnr);
}
*duration = length;
vm_ifo_close(ifo);
retval = parts;
*times = tmp;
fail:
pthread_mutex_unlock(&this->vm_lock);
if(!retval && tmp)
free(tmp);
return retval;
}