Code: Select all
#include <dvdread/ifo_read.h>
#define RESULT_FALSE 0
#define RESULT_TRUE 1
#define CELL_FIND_SECTOR 1
#define CELL_FIND_TIME 2
/* Function prototypes */
int32_t dvdnav_jump_to_sector_by_time(dvdnav_t *this
, uint32_t time_in_pts_ticks);
/* Find functions */
int32_t dvdnav_find_sector_by_tmap(dvdnav_t *this, uint64_t time_in_ms
, uint32_t *out_sector);
int32_t dvdnav_find_sector_by_admap(dvdnav_t *this, uint64_t time_in_ms
, uint32_t *out_sector);
int32_t dvdnav_find_sector_by_cells(dvdnav_t *this, uint64_t time_in_ms
, uint32_t *out_sector);
/* Sector functions */
int32_t dvdnav_jump_to_sector(dvdnav_t *this, uint32_t sector);
/* PG Cell functions */
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);
/* Time Map functions */
int32_t dvdnav_tmap_is_valid(dvdnav_t *this, vts_tmap_t *tmap
, uint32_t entry_count, uint32_t time_interval);
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);
/* Address Map functions */
int dvdnav_admap_find_by_sector(vobu_admap_t *admap, uint32_t entry_count
, uint32_t sector_to_find, uint32_t *out_admap_idx);
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);
Code: Select all
/*
This proc tries to jump to a specific sector
by calling these procs in the following order:
- dvdnav_find_sector_by_tmap
- dvdnav_find_sector_by_admap (if tmap fails)
- dvdnav_find_sector_by_cells (if tmap and admap fail)
If it gets a sector, it then calls dvdnav_jump_to_sector
*/
dvdnav_status_t dvdnav_jump_to_sector_by_time(dvdnav_t *this
, uint32_t time_in_pts_ticks) {
dvdnav_status_t return_value = DVDNAV_STATUS_ERR;
// get sector
uint64_t time_in_ms = time_in_pts_ticks / 90;
uint32_t sector = 0;
int32_t 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) {
return_value = DVDNAV_STATUS_ERR;
goto exit;
}
}
}
// jump to sector
result = dvdnav_jump_to_sector(this, sector);
return_value = result ? DVDNAV_STATUS_OK : DVDNAV_STATUS_ERR;
exit:
return return_value;
}
/*
This proc uses VTS_TMAPTI to find the @sector from a given @time.
1. uses VTS_TMAPTI to find:
@time_interval: time precision of tmap (commonly 4 seconds)
@sector_bgn: sector before @time
@sector_end: sector after @time
2. uses 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)
*/
int32_t dvdnav_find_sector_by_tmap(dvdnav_t *this, uint64_t time_in_ms
, uint32_t *out_sector) {
// get VTS_TMAPTI 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) {
printerr("could not find time in tmap");
return RESULT_FALSE;
}
uint32_t time_in_ms_bgn
= (tmap_idx + 1) * time_interval; // +1 b/c tmap is 0-based
int32_t time_in_ms_dif = time_in_ms - time_in_ms_bgn;
if (time_in_ms_dif < 0) {
printerr("time_in_ms_dif cannot be < 0");
return RESULT_FALSE;
}
double time_pct = (double)time_in_ms_dif / time_interval;
// get sector
uint32_t sector = 0;
result = dvdnav_admap_interpolate_vobu(this, sector_bgn, sector_end
, time_pct, 1, §or);
if (!result) {
printerr("could not find time in admap");
return RESULT_FALSE;
}
*out_sector = sector;
return RESULT_TRUE;
}
/*
this proc VTS_VOBU_ADMAP to find the @sector from a given @time
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)
*/
int32_t dvdnav_find_sector_by_admap(dvdnav_t *this, uint64_t time_in_ms
, uint32_t *out_sector) {
// get VTS_VOBU_ADMAP data
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) {
printerr("could not find time in admap");
return RESULT_FALSE;
}
uint64_t time_in_ms_dif = time_in_ms - time_bgn;
if (time_in_ms_dif < 0) {
printerr("time_in_ms_dif cannot be < 0");
return RESULT_FALSE;
}
uint64_t time_len = time_end - time_bgn;
if (time_len < 0) {
printerr("time_len cannot be < 0");
return RESULT_FALSE;
}
double time_pct = (double)time_in_ms_dif / time_len;
// get sector
uint32_t sector = 0;
sector_end++; // +1: @sector_end 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) {
printerr("could not find time in admap");
return RESULT_FALSE;
}
*out_sector = sector;
return RESULT_TRUE;
}
/*
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)
*/
int32_t dvdnav_find_sector_by_cells(dvdnav_t *this, uint64_t time_in_ms
, uint32_t *out_sector) {
// get cell data
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) {
printerr("find_by_cells.cell_find");
return RESULT_FALSE;
}
uint64_t time_in_ms_dif = time_in_ms - time_bgn;
if (time_in_ms_dif < 0) {
printerr("time_in_ms_dif < 0");
return RESULT_FALSE;
}
uint64_t time_dif = time_end - time_bgn;
if (time_dif < 0) {
printerr("time_dif < 0");
return RESULT_FALSE;
}
// get sector
uint32_t sector_dif = sector_end - sector_bgn;
uint32_t sector_off = sector_bgn + sector_dif;
*out_sector = sector_bgn + sector_off;
return RESULT_TRUE;
}
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/sector_offset
result = dvdnav_cell_find(this, CELL_FIND_SECTOR, sector, &cell_idx
, §or_bgn, §or_end, &time_bgn, &time_end);
if (!result) {
printerr("could not jump_to_sector");
return RESULT_FALSE;
}
uint32_t sector_off = sector - sector_bgn;
// 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;
}
/*
this proc...
- does bounds checking
- gets a sector from a tmap
- unsets discontinuity bit (if present)
*/
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) {
// printerr("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) {
// printerr("sector < 0; discontinuity?");
return RESULT_FALSE;
}
*out_sector = sector;
return RESULT_TRUE;
}
/*
this proc finds a cell, according to the find_args
*/
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) {
// get state
dvd_state_t *state = &(this->vm->state);
pgc_t *pgc = state->pgc;
uint32_t cell_count = pgc->nr_of_cells;
if (cell_count == 0) {
printerr("cell_count == 0");
return RESULT_FALSE;
}
// get cell_bgn, cell_end
uint32_t cell_bgn, cell_end;
if (this->pgc_based) {
cell_bgn = 1;
cell_end = cell_count;
}
else {
int pgN = state->pgN;
cell_bgn = pgc->program_map[pgN - 1]; // -1 b/c pgN is 1 based?
int program_count = pgc->nr_of_programs;
if (pgN < program_count) {
cell_end = pgc->program_map[pgN] - 1;
}
else {
cell_end = cell_count;
}
}
// 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;
// 90: pts to ms
time_end += (dvdnav_convert_time(&cell->playback_time) / 90);
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) {
*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
printerr("cell not found");
return located;
}
/*
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 not valid
*/
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) {
printerr("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) {
printerr("could not find cell in tmap");
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) {
printerr("could not find sector in tmap");
return RESULT_FALSE;
}
// check entry0_sector is between corresponding cell_sectors
if (entry0_sector >= sector_bgn && entry0_sector <= sector_end) {
return RESULT_TRUE;
}
else {
return RESULT_FALSE;
}
}
/*
this proc scans the time_map to find the entry for @time_in_ms
*/
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) {
// get state
dvd_state_t *state = &(this->vm->state);
domain_t domain = state->domain;
int32_t vts_idx = state->vtsN; // int
// get ifo
ifo_handle_t *ifo = NULL;
switch(domain) {
case FP_DOMAIN:
case VTSM_DOMAIN:
case VMGM_DOMAIN: {
// NOTE: ifo = this->vm->vmgi did not work
ifo = ifoOpenVMGI(this->vm->dvd);
break;
}
case VTS_DOMAIN: {
// NOTE: ifo = this->vm->vtsi did not work
ifo = ifoOpen(this->vm->dvd, vts_idx);
break;
}
default: {// NOTE: will not check out-of-range domain again
printerr("unknown domain");
return RESULT_FALSE;
}
}
// get tmapt
vts_tmapt_t *tmapt = ifo->vts_tmapt;
uint16_t tmap_count = tmapt->nr_of_tmaps;
// get pgcN; -1 b/c pgcN is base1
int32_t pgcN = (state->pgcN) - 1; // int
if (pgcN < 0) {
printerr("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) {
printerr("tmap_count == 0");
return RESULT_FALSE;
}
tmap = &(tmapt->tmap[0]); // ASSUME: vmgi only has one time map
break;
}
case VTS_DOMAIN: {
if (pgcN >= tmap_count) {
return RESULT_FALSE;
}
tmap = &(tmapt->tmap[pgcN]);
break;
}
}
// get time_interval; tmap->tmu is in seconds
uint32_t time_interval = (tmap->tmu) * 1000; // * 1000 converts to millisecs
if (time_interval == 0) {
printerr(" time_interval == 0");
return RESULT_FALSE;
}
// check tmap is valid
if (!dvdnav_tmap_is_valid(this, tmap, time_in_ms, time_interval)) {
printerr("tmap not valid");
return RESULT_FALSE;
}
uint16_t entry_count = tmap->nr_of_entries;
// 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
int32_t tmap_idx_end = tmap_idx_bgn + 1;
// 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);
if (!result) return result;
result = dvdnav_tmap_get_entry(tmap, entry_count, tmap_idx_end, §or_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;
return RESULT_TRUE;
}
/*
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 procs in searching.c iterates over the admap one at a time
they can use this proc (though performance gains will be negligible)
*/
int dvdnav_admap_find_by_sector(vobu_admap_t *admap, uint32_t entry_count
, uint32_t sector_to_find, uint32_t *out_admap_idx) {
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) {
// +1 to prevent fractional truncation; EX: 3 / 2 should be 2, not 1
interval = (interval + 1) / 2;
if (interval == 0 || tries++ > 32) {
if (interval == 0)
fprintf(MSG_OUT, "binary search failed; zero interval");
else
fprintf(MSG_OUT, "binary search failed; infinite loop");
*out_admap_idx = idx;
return RESULT_FALSE;
}
idx += interval * multiplier;
sector_cur = admap->vobu_start_sectors[idx];
if (sector_cur == sector_to_find) {
*out_admap_idx = idx;
return RESULT_TRUE;
}
multiplier = sector_cur > sector_to_find ? -1 : 1;
}
}
/*
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
*/
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) {
dvd_state_t *state = &(this->vm->state);
domain_t domain = state->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;
}
// get vobu_count
int32_t vobu_count = (admap->last_byte + 1 - VOBU_ADMAP_SIZE)
/ VOBU_ADMAP_SIZE;
if (vobu_count <= 0) {
printerr("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) {
printerr("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) {
printerr("could not find sector_end");
return RESULT_FALSE;
}
// get vobu_dif
uint32_t vobu_dif = vobu_end - vobu_bgn;
// get vobu_off; +.5 to round up else .74 * 4 = 2
uint32_t vobu_off = (fraction * ((double)vobu_dif + .5));
// adjust vobu_off
if (vobu_adj != 0) {
vobu_off += vobu_adj;
}
// get sector
if (vobu_off >= vobu_count) {
printerr("vobu_off >= vobu_count");
return RESULT_FALSE;
}
*out_sector = admap->vobu_start_sectors[vobu_bgn + vobu_off];
return RESULT_TRUE;
}
Code: Select all
int32_t dvdnav_jump_to_sector_by_time(dvdnav_t *this, uint32_t time_in_pts_ticks);
Code: Select all
case DEMUX_SET_POSITION:
case DEMUX_GET_POSITION:
case DEMUX_GET_TIME:
case DEMUX_GET_LENGTH:
{
int64_t timeInNavUnits, timeInSeconds, titleInSeconds;
titleInSeconds = p_sys->i_pgc_length;
timeInNavUnits = dvdnav_get_current_time( p_sys->dvdnav );
timeInSeconds = (timeInNavUnits / 90) * 1000; // see dvdnav_convert_time in dvdnav.c
if ( titleInSeconds == 0 )
return VLC_EGENERIC;
switch( i_query )
{
case DEMUX_GET_POSITION:
*va_arg( args, double* ) = (double)timeInSeconds / (double)titleInSeconds;
return VLC_SUCCESS;
case DEMUX_SET_POSITION: {
double rate = va_arg( args, double );
timeInSeconds = rate * titleInSeconds;
timeInNavUnits = (timeInSeconds / 1000) * 90;
dvdnav_jump_to_sector_by_time( p_sys->dvdnav, timeInNavUnits );
break;
}
case DEMUX_GET_TIME:
if( p_sys->i_pgc_length > 0 )
{
*va_arg( args, int64_t * ) = timeInSeconds;
return VLC_SUCCESS;
}
break;
case DEMUX_GET_LENGTH:
if( p_sys->i_pgc_length > 0 )
{
*va_arg( args, int64_t * ) = titleInSeconds;
return VLC_SUCCESS;
}
break;
}
return VLC_EGENERIC;
}
There are a few DVD wrappers, out there...Yep.Out of curiosity, did you want me to try dvd vs dvdsimple because one uses dvdnav and the other uses dvdplay?
Okay. I will give them another try later this week/next week. I didn't want to come across as pushy, especially in a project for which I have had no involvement. My other thoughts are...So it would not hurt to try to push harder on the dvdnav mailing list.
If there's sufficient documentation they might be all right with it.
Okay. I will give them another try later this week/next week. I didn't want to come across as pushy, especially in a project for which I have had no involvement. My other thoughts are...
I will post some status later on. If any libdvdnav developers happen to read this, please feel free to provide other pointers/suggestions.
- searching.c is currently 18.9KB. The proposed change would make it 37.0KB. Granted, I put in a lot of comments and logging, but even if I strip them away, it would be something like +9KB. That would be a code growth of 50% in a file with 12 or so functions -- just to handle one new function. I can see why any developer would balk at integrating the new "fix" from an unknown developer.
Unfortunately I believe this complexity is necessary. Time seek is not an easy task and I think that is why the original developer left it as a FIXME.- My approach does not handle all DVDs. As I detail earlier, it uses the .IFO files only. I think the "ideal" approach would be to look at the info in the .VOB files (the VOB PCI pak). However, this is not a trivial project. I tried to do as much afterwards, and the project rapidly became even more complicated than my +9KB solution.
That said, I think the current fix will work for 90+% of DVDs. The remaining % will fall back to a less precise variation of seek-time-by-sector-implementation, which may be close enough for most users.
Please re-send. The project is now alive again.I emailed the DVDNav mailing list because 90% of the change needs to be made in libdvdnav.
Okay. Sorry for the delay, but I ended up rewriting a good part of the code. The new version now handles those outlier dvds I've encountered (strange time maps: I won't bore anyone with the details).Please re-send. The project is now alive again.
Thanks for the link. I looked at the code, and it looks like we basically have the same approach. Kovensky's code is much shorter than mine: 60 lines versus 600. As an example, Kovensky gets a time_sector with this:Also of note to you might be https://github.com/Kovensky/mplayer-kov ... dvd.c#L551 (how mplayer does stream seeking)
Code: Select all
tmap_sector = vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff;
Code: Select all
static int32_t dvdnav_tmap_get_entry(vts_tmap_t *tmap, uint16_t tmap_len, uint32_t idx, uint32_t *out_sector) {
if (idx < 0 || idx >= tmap_len)
return RESULT_FALSE;
int32_t sector = tmap->map_ent[idx];
if ((sector & (1 << 31)) != 0) { // if discontinuity bit is set
sector &= ~(1 << 31); // unset bit
}
if (sector < 0)
return RESULT_FALSE;
*out_sector = sector;
return RESULT_TRUE;
}
I looked at the link, but couldn't get to the source file (http://www.ratdvd.dk/downloads/libdvdnav.zip). It's probably the same variation as abovehttp://sourceforge.net/tracker/?func=de ... tid=402476
Code: Select all
Hi all. I've written a patch for jumping to a time in searching.c. I emailed something similar to this group a year ago, but there didn't seem to be much interest then. I am emailing again because there is some revived interest in the VLC community. (See http://forum.videolan.org/viewtopic.php?f=32&t=76308&start=20#p315221)
Currently, dvdnav_time_search is marked "FIXME: right now, this function does not use the time tables but interpolates only the cell times". This method is usually inaccurate: it interpolates using sectors, but one sector does not equal one fixed unit of time. For example, if there is a 10:00 cell and the cell is 1000 sectors long, it assumes that 5:00 is at sector 500. Jumping to sector 500 may actually jump instead to 5:12.
My patch jumps to a time using the time map and the vobu address map. I placed it in a new function called "dvdnav_jump_to_sector" because I wanted to keep backward compatibility with the existing "dvdnav_time_search" -- in case anyone wanted to keep using this method. I provide more details on my approach below. I will include the patch itself in the next email.
Please note that most of my work is based on reverse-engineering the IFO files. I am not a DVD application developer, so my approach may be flawed or may not follow DVD specification.
Also, please note that Java/C# is my primary language. I learned C for this task, and have not used it for anything else since then. My apologies in advance if I do something terribly stupid.
Finally, please note that my patch does not change or delete any existing code. However, it does add a lot of code: 650+ lines. I realize that such length makes it harder and less desirable to review. I do think the approach needs to have this complexity, especially to handle some outlier DVDs I've encountered.
I appreciate any feedback or review. At the very least, I hope others will find this useful.
Thanks.
-------
Details
-------
My patch introduces a new function called "dvdnav_jump_to_sector". It finds a sector for a given time and jumps to it.
In libdvdnav/src/dvdnav/dvdnav.h, it adds 1 line. (see dvdnav.h.patch)
In libdvdnav/src/searching.c, it adds 650+ lines. (see searching.c.patch.zip)
No other lines are modified or deleted.
From a broad perspective, it works like this:
(1) Find the two time map entries around a given time
(2) Get the vobus of these two time map entries
(3) Get the sector of the jump_time through interpolation of the vobus
For clarity's sake, I will repeat the above with fabricated data
(1) Find the two time map entries around a given time
Given time 56...
Time map index 10 is identified as 55 seconds
56 seconds must be between entries 10 and 11
(2) Get the vobus of these two time map entries
Time entry 10 is at sector 193 which is vobu 405
Time entry 11 is at sector 303 which is vobu 455
(3) Get the sector of the jump_time through interpolation of the vobus
56 is 20% of the way between 55 and 60
415 is 20% of the way between 405 and 455
320 is the corresponding sector of vobu 415
Note that step (1) actually requires more sub-steps
(1a) Find the cell for a given time
(1b) Find the tmap entries around the cell's start sector
(1c) Find the vobus for these tmap entries and cell start
(1d) Interpolate the time of earlier tmap entry
(1e) Find the tmap entries around jump_time
(2) Get the vobus of these two time map entries
(3) Get the sector of the jump_time through interpolation of the vobus
Again, here is the above with some fabricated data.
I try to represent the tables visually below. It was written with a fixed-width font.
My apologies if it does not show up formatted in the email.
(1a) Find the cell for a given time
Given time 56...
---- -------- -------- ---------- ----------
cell bgn_time end_time bgn_sector end_sector
---- -------- -------- ---------- ----------
1 0 28 0 99
---> 2 29 600 100 1000
3 601 900 1001 1400
...
**** Cell 2 encloses 56.
(1b) Find the tmap entries around the cell's start sector
---- ------
tmap sector
---- ------
...
3 71
---> 4 88
---> 5 108
6 121
...
**** tmap entries 4 and 5 enclose cell start sector of 100.
(1c) Find the vobus for these tmap entries and cell start
---- ------
vobu sector
---- ------
...
---> 200 88
...
---> 208 100
209 102
---> 210 108
...
**** 200, 208 and 210 are the relevant vobus
(1d) Interpolate the time of earlier tmap entry
208 is 80% of the way between 200 and 210
4 secs is 80% of 5 secs
25 is the time-point when subtracting 4 from 29 (cell_bgn)
vobu 200 is at 25 seconds
**** 25 secs is the calculated time of tmap idx 4
(1e) Find the tmap entries around jump_time
---- ---- ------
tmap time sector
---- ---- ------
4 25
5 30
6 35
7 40
8 45
9 50
---> 10 55 193
---> 11 60 303
...
**** 56 must be between tmap entries 10 and 11
(2) Get the vobus of these two time map entries
---- ------
vobu sector
---- ------
...
---> 405 193
...
415 320
...
---> 455 503
...
**** 405 and 445 are the relevant vobus
(3) Get the sector of the jump_time through interpolation of the vobus
56 is 20% of the way between 55 and 60
415 is 20% of the way between 405 and 455
320 is the corresponding sector of vobu 415
**** 320 is the closest vobu sector for 56 secs
-----------
Other notes
-----------
(1b) has a hack to handle cells that start at discontinuity entries
- Basically if a cell starts in a discontinuity entry, then the sector/vobu data may not be good for interpolation
For example, using the same data above
---- ---- ------ ----
tmap time sector vobu
---- ---- ------ ----
4 25 88 200 <- flagged with discontinuity bit
5 30 108 210
6 35 140 218
Since there is a discontinuity occurring at tmap entry 4, the sectors between 88 and 108 may not represent 5 seconds of data (It usually does, but not always)
To handle this, the proc looks forward to the next tmap entry and uses the vobu difference there to infer the vobu of the lo tmap
With data, this means
Look at tmap entry 6. It has a vobu of 218
There are 8 vobus between 218 and 210
Assume that tmap entry 10 starts at 202 (not 200)
This works in practice on a handful of DVDs I've encountered, but the logic will probably not hold for all.
There is a small percentage of DVDs that have issues
- Time map was missing (no entries)
For these DVDs, I wrote dvdnav_find_vobu_by_cell_boundaries. This function uses the same logic as tmap, but uses the cell_boundaries only. It is not as accurate as a tmap, but it is close. It is more accurate than raw sector interpolation. Roughly speaking, it is accurate 50% of the time, and the other other 50% of the time, it is off by 1 to 3 seconds.
- Time map has non-consecutive entries. For example, a DVD with a time interval of 5 seconds had entries at tmap index 99 that was not 500 seconds in playback, but 521 seconds (or some other non-intuitive number).
For these DVDs, steps (1a) - (1d) were written. If we could assume that tmap index 99 was always at time 500 seconds, then we could skip directly to (1e).
----------------
Function summary
----------------
Entry function:
- dvdnav_jump_to_sector_by_time
Find the nearest vobu and jump to it
Core function:
- dvdnav_find_vobu_by_tmap
Find the nearest vobu by using the tmap
Fallback function:
- dvdnav_find_vobu_by_cell_boundaries
Find the nearest vobu by using the cell boundaries
Utility functions:
- dvdnav_tmap_get_entries_for_sector
Find the tmap entries on either side of a given sector
- dvdnav_tmap_calc_time_for_tmap_entry
Given two tmap entries and a known time, calc the time for the lo tmap entry
- dvdnav_admap_interpolate_vobu
Given two sectors and a fraction, calc the corresponding vobu
- dvdnav_cell_find
Given a sector/time/idx find the cell that encloses it
- dvdnav_tmap_search
Do binary search for the earlier tmap entry near find_sector
- dvdnav_admap_search
Do a binary search for earlier admap index near find_sector
- dvdnav_tmap_get_entry
Get a sector from a tmap
Misc functions (separated for readability):
- dvdnav_tmap_get
Get a tmap, tmap_len and tmap_interval
- dvdnav_admap_get
Get an admap and admap_len
Log/Debug functions
- is_null
Check if pointer is null, and log it if it is
Code: Select all
Index: src/searching.c
===================================================================
--- src/searching.c (revision 1243)
+++ src/searching.c (working copy)
@@ -36,6 +36,14 @@
#include "vm/decoder.h"
#include "vm/vm.h"
#include "dvdnav_internal.h"
+#include <dvdread/ifo_read.h>
+#define RESULT_FALSE 0
+#define RESULT_TRUE 1
+#define CELL_FIND_SECTOR 1
+#define CELL_FIND_TIME 2
+#define CELL_FIND_INDEX 3
+#define TMAP_IDX_EDGE_BGN -1
+#define TMAP_IDX_EDGE_END -2
/*
#define LOG_DEBUG
@@ -654,3 +662,674 @@
free(tmp);
return retval;
}
+/*
+Check if pointer is null, and log it if it is
+*/
+int32_t is_null(void *val, const char *val_name) {
+ if (val == NULL) {
+ fprintf(MSG_OUT, "ERR:NULL REF: %s\n", val_name);
+ return RESULT_TRUE;
+ }
+ else {
+ return RESULT_FALSE;
+ }
+}
+/*
+Get an admap and admap_len
+*/
+vobu_admap_t* dvdnav_admap_get(dvdnav_t *this, dvd_state_t *state
+ , int32_t *out_admap_len) {
+ vobu_admap_t *admap = NULL;
+ domain_t domain = state->domain;
+ 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, "ERR:Unknown domain\n");
+ return NULL;
+ }
+ }
+ if (is_null(admap, "admap")) return NULL;
+
+ *out_admap_len = (admap->last_byte + 1 - VOBU_ADMAP_SIZE) / VOBU_ADMAP_SIZE;
+ if (*out_admap_len <= 0) {
+ fprintf(MSG_OUT, "ERR:admap_len <= 0\n");
+ return NULL;
+ }
+ return admap;
+}
+/*
+Get a tmap, tmap_len and tmap_interval
+*/
+vts_tmap_t* dvdnav_tmap_get(dvdnav_t *this, dvd_state_t *state
+ , int32_t *out_tmap_len, int32_t *out_tmap_interval) {
+ int32_t vts_idx = state->vtsN;
+
+ domain_t domain = state->domain;
+ ifo_handle_t *ifo = NULL;
+ switch(domain) {
+ case FP_DOMAIN:
+ case VTSM_DOMAIN:
+ case VMGM_DOMAIN: {
+ // NOTE: ifo = this->vm->vmgi seems to be always null
+ ifo = this->vm->vmgi;
+ break;
+ }
+ case VTS_DOMAIN: {
+ // NOTE: ifo = this->vm->vtsi doesn't work
+ ifo = ifoOpen(this->vm->dvd, vts_idx);
+ break;
+ }
+ default: {
+ fprintf(MSG_OUT, "ERR:unknown domain for tmap\n");
+ return NULL;
+ }
+ }
+ if (is_null(ifo, "ifo")) return NULL;
+
+ vts_tmapt_t *tmapt = ifo->vts_tmapt;
+ if (is_null(tmapt, "tmapt")) return NULL;
+
+ uint16_t tmap_count = tmapt->nr_of_tmaps;
+
+ // get pgcN; -1 b/c pgcN is base1
+ int32_t pgcN = state->pgcN - 1;
+ if (pgcN < 0) {
+ fprintf(MSG_OUT, "ERR:pgcN < 0\n");
+ return NULL;
+ }
+
+ // 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, "ERR:tmap_count == 0\n");
+ return NULL;
+ }
+ tmap = &tmapt->tmap[0]; // ASSUME: vmgi only has one time map
+ break;
+ }
+ case VTS_DOMAIN: {
+ if (pgcN >= tmap_count) {
+ fprintf(MSG_OUT, "ERR:pgcN >= tmap_count; pgcN=%i tmap_count=%i\n"
+ , pgcN, tmap_count);
+ return NULL;
+ }
+ tmap = &tmapt->tmap[pgcN];
+ break;
+ }
+ }
+ if (is_null(tmap, "tmap")) return NULL;
+
+ // get tmap_interval; tmap->tmu is in seconds; convert to millisecs
+ *out_tmap_interval = tmap->tmu * 1000;
+ if (*out_tmap_interval == 0) {
+ fprintf(MSG_OUT, "ERR:tmap_interval == 0\n");
+ return NULL;
+ }
+ // get tmap_len
+ *out_tmap_len = tmap->nr_of_entries;
+ if (*out_tmap_len == 0) {
+ fprintf(MSG_OUT, "ERR:tmap_len == 0\n");
+ return NULL;
+ }
+ return tmap;
+}
+/*
+Get a sector from a tmap
+*/
+int32_t dvdnav_tmap_get_entry(vts_tmap_t *tmap, uint16_t tmap_len
+ , int32_t idx, uint32_t *out_sector) {
+ // tmaps start at idx 0 which represents a sector at time 1 * tmap_interval
+ // this creates a "fake" tmap index at idx -1 for sector 0
+ if (idx == TMAP_IDX_EDGE_BGN) {
+ *out_sector = 0;
+ return RESULT_TRUE;
+ }
+ if (idx < TMAP_IDX_EDGE_BGN || idx >= tmap_len) {
+ fprintf(MSG_OUT, "ERR:idx out of bounds idx=%i %i\n", idx, tmap_len);
+ return RESULT_FALSE;
+ }
+ // 0x7fffffff unsets discontinuity bit if present
+ *out_sector = tmap->map_ent[idx] & 0x7fffffff;
+ return RESULT_TRUE;
+}
+/*
+Do a binary search for earlier admap index near find_sector
+*/
+int32_t dvdnav_admap_search(vobu_admap_t *admap, uint32_t admap_len
+ , uint32_t find_sector, uint32_t *out_vobu) {
+ int32_t adj = 1, prv_pos = 0, prv_len = admap_len;
+ while (1) {
+ int32_t cur_len = prv_len / 2;
+ // need to add 1 when prv_len == 3 (cur_len shoud go to 2, not 1)
+ if (prv_len % 2 == 1) ++cur_len;
+ int32_t cur_idx = prv_pos + (cur_len * adj);
+ if (cur_idx < 0) cur_idx = 0;
+ else if (cur_idx >= admap_len) cur_idx = admap_len - 1;
+
+ uint32_t cur_sector = admap->vobu_start_sectors[cur_idx];
+ if (find_sector < cur_sector) adj = -1;
+ else if (find_sector > cur_sector) adj = 1;
+ else if (find_sector == cur_sector) {
+ *out_vobu = cur_idx;
+ return RESULT_TRUE;
+ }
+ if (cur_len == 1) {// no smaller intervals left
+ if (adj == -1) {// last comparison was greater; take lesser
+ cur_idx -= 1;
+ cur_sector = admap->vobu_start_sectors[cur_idx];
+ }
+ *out_vobu = cur_idx;
+ return RESULT_TRUE;
+ }
+ prv_len = cur_len;
+ prv_pos = cur_idx;
+ }
+}
+/*
+Do a binary search for the earlier tmap entry near find_sector
+*/
+int32_t dvdnav_tmap_search(vts_tmap_t *tmap, uint32_t tmap_len
+ , uint32_t find_sector, int32_t *out_tmap, uint32_t *out_sector) {
+ int32_t adj = 1; int32_t prv_pos = 0; int32_t prv_len = tmap_len;
+ int32_t result = RESULT_FALSE;
+ while (1) {
+ int32_t cur_len = prv_len / 2;
+ // need to add 1 when prv_len == 3 (cur_len shoud go to 2, not 1)
+ if (prv_len % 2 == 1) ++cur_len;
+ int32_t cur_idx = prv_pos + (cur_len * adj);
+ if (cur_idx < 0) cur_idx = 0;
+ else if (cur_idx >= tmap_len) cur_idx = tmap_len - 1;
+ uint32_t cur_sector = 0;
+ result = dvdnav_tmap_get_entry(tmap, tmap_len, cur_idx, &cur_sector);
+ if (!result) return RESULT_FALSE;
+ if (find_sector < cur_sector) adj = -1;
+ else if (find_sector > cur_sector) adj = 1;
+ else if (find_sector == cur_sector) {
+ *out_tmap = cur_idx;
+ *out_sector = cur_sector;
+ return RESULT_TRUE;
+ }
+ if (cur_len == 1) {// no smaller intervals left
+ if (adj == -1) {// last comparison was greater; take lesser
+ if (cur_idx == 0) { // fake tmap index for sector 0
+ cur_idx = TMAP_IDX_EDGE_BGN;
+ cur_sector = 0;
+ }
+ else {
+ cur_idx -= 1;
+ result = dvdnav_tmap_get_entry(tmap, tmap_len, cur_idx, &cur_sector);
+ if (!result) return RESULT_FALSE;
+ }
+ }
+ *out_tmap = cur_idx;
+ *out_sector = cur_sector;
+ return RESULT_TRUE;
+ }
+ prv_len = cur_len;
+ prv_pos = cur_idx;
+ }
+}
+
+/*
+Given a sector/time/idx find the cell that encloses it
+*/
+int32_t dvdnav_cell_find(dvdnav_t *this, dvd_state_t *state
+ , int32_t find_mode, uint64_t find_val
+ , int32_t *out_cell_idx
+ , uint64_t *out_bgn_time, uint32_t *out_bgn_sector
+ , uint64_t *out_end_time, uint32_t *out_end_sector) {
+ pgc_t *pgc = state->pgc;
+ if (is_null(pgc, "pgc")) return RESULT_FALSE;
+
+ // get cells_len
+ uint32_t cells_len = pgc->nr_of_cells;
+ if (cells_len == 0) {
+ fprintf(MSG_OUT, "ERR:cells_len == 0\n");
+ return RESULT_FALSE;
+ }
+
+ // get cells_bgn, cells_end
+ uint32_t cells_bgn, cells_end;
+ if (this->pgc_based) {
+ cells_bgn = 1;
+ cells_end = cells_len;
+ }
+ else {
+ int pgN = state->pgN;
+ cells_bgn = pgc->program_map[pgN - 1]; // -1 b/c pgN is 1 based?
+ int program_count = pgc->nr_of_programs;
+ if (pgN < program_count) {
+ cells_end = pgc->program_map[pgN] - 1;
+ }
+ else {
+ cells_end = cells_len;
+ }
+ }
+
+ // 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 found = RESULT_FALSE;
+ for (cell_idx = cells_bgn; cell_idx <= cells_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) {
+ found = RESULT_TRUE;
+ break;
+ }
+ }
+ else if (find_mode == CELL_FIND_TIME) {
+ if (find_val >= time_bgn && find_val <= time_end) {
+ found = RESULT_TRUE;
+ break;
+ }
+ }
+ else if (find_mode == CELL_FIND_INDEX) {
+ if (find_val == cell_idx) {
+ found = RESULT_TRUE;
+ break;
+ }
+ }
+ time_bgn = time_end;
+ }
+
+ // found cell: set *out vars
+ if (found) {
+ *out_cell_idx = cell_idx;
+ *out_bgn_sector = sector_bgn;
+ *out_end_sector = sector_end;
+ *out_bgn_time = time_bgn;
+ *out_end_time = time_end;
+ }
+ else
+ fprintf(MSG_OUT, "ERR:cell not found\n");
+ return found;
+}
+/*
+Given two sectors and a fraction, calc the corresponding vobu
+*/
+int32_t dvdnav_admap_interpolate_vobu(dvdnav_t *this
+ , vobu_admap_t *admap, int32_t admap_len
+ , uint32_t sector_bgn, uint32_t sector_end, double fraction
+ , uint32_t *out_jump_sector) {
+
+ // get vobu_bgn
+ int32_t result;
+ uint32_t vobu_bgn;
+ result = dvdnav_admap_search(admap, admap_len, sector_bgn, &vobu_bgn);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR admap_interpolate: could not find sector_bgn");
+ return RESULT_FALSE;
+ }
+
+ // get vobu_end
+ uint32_t vobu_end;
+ result = dvdnav_admap_search(admap, admap_len, sector_end, &vobu_end);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR admap_interpolate: could not find sector_end");
+ return RESULT_FALSE;
+ }
+
+ // get vobu_dif
+ uint32_t vobu_dif = vobu_end - vobu_bgn;
+
+ // get vobu_off; +.5 to round up else 74% of a 4 sec interval = 2 sec
+ uint32_t vobu_off = (fraction * ((double)vobu_dif + .5));
+ // HACK: need to add +1, or else will land too soon (not sure why)
+ vobu_off++;
+ // get sector
+ if (vobu_off >= admap_len) {
+ fprintf(MSG_OUT, "ERR admap_interpolate: vobu_off >= admap_len");
+ return RESULT_FALSE;
+ }
+
+ int32_t vobu_idx = vobu_bgn + vobu_off;
+ *out_jump_sector = admap->vobu_start_sectors[vobu_idx];
+ return RESULT_TRUE;
+}
+/*
+Given two tmap entries and a known time, calc the time for the lo tmap entry
+*/
+int32_t dvdnav_tmap_calc_time_for_tmap_entry(vobu_admap_t *admap
+ , uint32_t admap_len, int32_t tmap_interval
+ , uint32_t cell_sector, uint32_t lo_sector, uint32_t hi_sector
+ , uint64_t cell_time, uint64_t *out_lo_time
+ ) {
+ if (lo_sector == hi_sector) {
+ fprintf(MSG_OUT, "ERR:lo_sector == hi_sector: %i\n", lo_sector);
+ return RESULT_FALSE;
+ }
+
+ // get vobus corresponding to lo, hi, cell
+ int32_t result = RESULT_FALSE;
+ uint32_t lo_vobu;
+ result = dvdnav_admap_search(admap, admap_len, lo_sector, &lo_vobu);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR lo_vobu: lo_sector=%i", lo_sector);
+ return RESULT_FALSE;
+ }
+ uint32_t hi_vobu;
+ result = dvdnav_admap_search(admap, admap_len, hi_sector, &hi_vobu);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR hi_vobu: hi_sector=%i", hi_sector);
+ return RESULT_FALSE;
+ }
+ uint32_t cell_vobu;
+ result = dvdnav_admap_search(admap, admap_len, cell_sector, &cell_vobu);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR cell_vobu: cell_sector=%i", cell_sector);
+ return RESULT_FALSE;
+ }
+
+ // calc position of cell relative to lo
+ double vobu_pct = (double)(cell_vobu - lo_vobu)
+ / (double)(hi_vobu - lo_vobu);
+ if (vobu_pct < 0 || vobu_pct > 1) {
+ fprintf(MSG_OUT, "ERR vobu_pct must be between 0 and 1");
+ return RESULT_FALSE;
+ }
+
+ // calc time of lo
+ uint64_t time_adj = (uint64_t)(tmap_interval * vobu_pct);
+ *out_lo_time = cell_time - time_adj;
+ return RESULT_TRUE;
+}
+/*
+Find the tmap entries on either side of a given sector
+*/
+int32_t dvdnav_tmap_get_entries_for_sector(dvdnav_t *this, dvd_state_t *state
+ , vobu_admap_t *admap, int32_t admap_len, vts_tmap_t *tmap, uint32_t tmap_len
+ , int32_t cell_idx, uint32_t cell_end_sector, uint32_t find_sector
+ , int32_t *lo_tmap, uint32_t *lo_sector
+ , int32_t *hi_tmap, uint32_t *hi_sector
+ ) {
+ int32_t result = RESULT_FALSE;
+ result = dvdnav_tmap_search(tmap, tmap_len, find_sector, lo_tmap, lo_sector);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR:could not find lo idx: %i\n", find_sector);
+ return RESULT_FALSE;
+ }
+
+ uint32_t out_sector = 0;
+ // lo is last tmap entry; "fake" entry for one beyond
+ // and mark it with cell_end_sector
+ if (*lo_tmap == tmap_len - 1) {
+ *hi_tmap = TMAP_IDX_EDGE_END;
+ *hi_sector = cell_end_sector;
+ }
+ else {
+ *hi_tmap = *lo_tmap + 1;
+ result = dvdnav_tmap_get_entry(tmap, tmap_len, *hi_tmap, &out_sector);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR:could not find hi sector: %i\n", *hi_tmap);
+ return RESULT_FALSE;
+ }
+ *hi_sector = out_sector;
+ }
+ // HACK: handle cells that start at discontinuity entries
+ // first check if discontinuity applies
+ int32_t discontinuity = RESULT_FALSE;
+ if (*lo_tmap == TMAP_IDX_EDGE_BGN) {
+ int32_t out_cell_idx;
+ // get cell 1
+ uint64_t cell_1_bgn_time; uint32_t cell_1_bgn_sector;
+ uint64_t cell_1_end_time; uint32_t cell_1_end_sector;
+ result = dvdnav_cell_find(this, state, CELL_FIND_INDEX
+ , 1, &out_cell_idx, &cell_1_bgn_time, &cell_1_bgn_sector
+ , &cell_1_end_time, &cell_1_end_sector
+ );
+ if (!result) {
+ fprintf(MSG_OUT, "ERR:could not retrieve cell 1\n");
+ return RESULT_FALSE;
+ }
+ // if cell 1 does not start at sector 0 then assume discontinuity
+ // NOTE: most DVDs should start at 0
+ if (cell_1_bgn_sector != 0) {
+ discontinuity = RESULT_TRUE;
+ }
+ }
+ else {
+ if ((tmap->map_ent[*lo_tmap] & (1 << 31)) != 0) {
+ discontinuity = RESULT_TRUE;
+ }
+ }
+ if (discontinuity) {
+ // HACK: since there is either
+ // (a) a non-zero sector start at cell_1
+ // (b) a discontinuity entry
+ // the vobu at lo_tmap is suspect. In order to find a vobu, assume that
+ // lo_tmap to hi_tmap is separated by the same number of vobus as
+ // hi_tmap to hi_tmap + 1
+ // This is a hack. It works in practice but there must be a better way....
+
+ // first get the vobu for tmap_2
+ uint32_t tmap_2_sector = 0, tmap_2_vobu;
+ result = dvdnav_tmap_get_entry(tmap, tmap_len
+ , *hi_tmap + 1, &tmap_2_sector);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR:no tmap_2_sector: %i\n"
+ , *hi_tmap + 1);
+ return RESULT_FALSE;
+ }
+ result = dvdnav_admap_search(admap, admap_len
+ , tmap_2_sector, &tmap_2_vobu);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR:no tmap_2_vobu: %i\n", tmap_2_vobu);
+ return RESULT_FALSE;
+ }
+
+ // now get the vobu for tmap_1
+ uint32_t tmap_1_vobu;
+ result = dvdnav_admap_search(admap, admap_len, *hi_sector, &tmap_1_vobu);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR:no find tmap_1_vobu: %i\n", tmap_1_vobu);
+ return RESULT_FALSE;
+ }
+
+ // now calc the vobu for lo_tmap
+ uint32_t vobu_diff = tmap_2_vobu - tmap_1_vobu;
+ uint32_t tmap_neg1_vobu = tmap_1_vobu - vobu_diff;
+ if (tmap_neg1_vobu < 0 || tmap_neg1_vobu >= admap_len) {
+ fprintf(MSG_OUT, "ERR:tmap_neg1_vobu: %i\n", tmap_neg1_vobu);
+ return RESULT_FALSE;
+ }
+ *lo_sector = admap->vobu_start_sectors[tmap_neg1_vobu];
+ }
+ if (!(find_sector >= *lo_sector && find_sector <= *hi_sector)) {
+ fprintf(MSG_OUT, "ERR:find_sector not between lo/hi\n");
+ return RESULT_FALSE;
+ }
+ return RESULT_TRUE;
+}
+
+/*
+Find the nearest vobu by using the tmap
+*/
+int32_t dvdnav_find_vobu_by_tmap(dvdnav_t *this, dvd_state_t *state
+ , vobu_admap_t *admap, int32_t admap_len
+ , int32_t cell_idx
+ , uint64_t cell_bgn_time, uint32_t cell_bgn_sector
+ , uint32_t cell_end_sector
+ , uint64_t time_in_ms, uint32_t *out_jump_sector) {
+ // get tmap, tmap_len, tmap_interval
+ int32_t tmap_len, tmap_interval;
+ vts_tmap_t *tmap = dvdnav_tmap_get(this, state
+ , &tmap_len, &tmap_interval);
+ if (is_null(tmap, "tmap")) return RESULT_FALSE;
+
+ // get tmap entries on either side of cell_bgn
+ int32_t cell_bgn_lo_tmap; uint32_t cell_bgn_lo_sector;
+ int32_t cell_bgn_hi_tmap; uint32_t cell_bgn_hi_sector;
+ int32_t result = RESULT_FALSE;
+ result = dvdnav_tmap_get_entries_for_sector(this, state, admap, admap_len
+ , tmap, tmap_len, cell_idx, cell_end_sector, cell_bgn_sector
+ , &cell_bgn_lo_tmap, &cell_bgn_lo_sector
+ , &cell_bgn_hi_tmap, &cell_bgn_hi_sector
+ );
+ if (!result) return RESULT_FALSE;
+
+ // calc time of cell_bgn_lo
+ uint64_t cell_bgn_lo_time;
+ result = dvdnav_tmap_calc_time_for_tmap_entry(admap, admap_len, tmap_interval
+ , cell_bgn_sector, cell_bgn_lo_sector, cell_bgn_hi_sector
+ , cell_bgn_time, &cell_bgn_lo_time
+ );
+ if (!result) return RESULT_FALSE;
+
+ // calc tmap_time of jump_time relative to cell_bgn_lo
+ uint64_t seek_offset = time_in_ms - cell_bgn_lo_time;
+ uint32_t seek_idx, jump_lo_sector, jump_hi_sector;
+ seek_idx = (uint32_t)(seek_offset / tmap_interval);
+ uint32_t seek_remainder = seek_offset - (seek_idx * tmap_interval);
+ double seek_pct = (double)seek_remainder / (double)tmap_interval;
+
+ // get tmap entries on either side of jump_time
+ uint32_t jump_lo_idx = (uint32_t)(cell_bgn_lo_tmap + seek_idx);
+ result = dvdnav_tmap_get_entry(tmap, tmap_len, jump_lo_idx, &jump_lo_sector);
+ if (!result) return RESULT_FALSE;
+
+ uint32_t jump_hi_idx = jump_lo_idx + 1; // +1 handled by get_entry
+ result = dvdnav_tmap_get_entry(tmap, tmap_len, jump_hi_idx, &jump_hi_sector);
+ if (!result) return RESULT_FALSE;
+
+ // interpolate sector
+ result = dvdnav_admap_interpolate_vobu(this, admap, admap_len
+ , jump_lo_sector, jump_hi_sector, seek_pct, out_jump_sector);
+
+ if (!result) return RESULT_FALSE;
+ return RESULT_TRUE;
+}
+/*
+Find the nearest vobu by using the cell boundaries
+*/
+int32_t dvdnav_find_vobu_by_cell_boundaries(dvdnav_t *this
+ , vobu_admap_t *admap, int32_t admap_len
+ , uint64_t cell_bgn_time, uint32_t cell_bgn_sector
+ , uint64_t cell_end_time, uint32_t cell_end_sector
+ , uint64_t time_in_ms
+ , uint32_t *out_jump_sector
+ ) {
+ // get jump_offset
+ uint64_t jump_offset = time_in_ms - cell_bgn_time;
+ if (jump_offset < 0) {
+ fprintf(MSG_OUT, "ERR:jump_offset < 0\n");
+ return RESULT_FALSE;
+ }
+
+ // get cell_len
+ uint64_t cell_len = cell_end_time - cell_bgn_time;
+ if (cell_len < 0) {
+ fprintf(MSG_OUT, "ERR:cell_len < 0\n");
+ return RESULT_FALSE;
+ }
+
+ // get jump_pct
+ double jump_pct = (double)jump_offset / cell_len;
+
+ // get sector
+ // NOTE: end cell sector in VTS_PGC is last sector of cell
+ // this last sector is not the start of a VOBU
+ // +1 to get sector that is the start of a VOBU
+ cell_end_sector += 1;
+ int32_t result = RESULT_FALSE;
+ result = dvdnav_admap_interpolate_vobu(this, admap, admap_len
+ , cell_bgn_sector, cell_end_sector, jump_pct, out_jump_sector);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR:find_by_admap.interpolate\n");
+ return RESULT_FALSE;
+ }
+ return RESULT_TRUE;
+}
+/*
+Find the nearest vobu and jump to it
+*/
+int32_t dvdnav_jump_to_sector_by_time(dvdnav_t *this
+ , uint32_t time_in_pts_ticks) {
+ int32_t result = RESULT_FALSE;
+
+ // convert time to milliseconds
+ uint64_t time_in_ms = time_in_pts_ticks / 90;
+
+ // get variables that will be used across both functions
+ dvd_state_t *state = &(this->vm->state);
+ if (is_null(state, "state")) goto exit;
+
+ // get cell info
+ int32_t cell_idx;
+ uint64_t cell_bgn_time; uint32_t cell_bgn_sector;
+ uint64_t cell_end_time; uint32_t cell_end_sector;
+ result = dvdnav_cell_find(this, state, CELL_FIND_TIME, time_in_ms
+ , &cell_idx
+ , &cell_bgn_time, &cell_bgn_sector
+ , &cell_end_time, &cell_end_sector);
+ if (!result) goto exit;
+
+ // get admap
+ int32_t admap_len;
+ vobu_admap_t *admap = dvdnav_admap_get(this, state, &admap_len);
+ if (is_null(admap, "admap")) goto exit;
+
+ // find sector
+ uint32_t jump_sector;
+ result = dvdnav_find_vobu_by_tmap(this, state, admap, admap_len
+ , cell_idx, cell_bgn_time, cell_bgn_sector, cell_end_sector
+ , time_in_ms, &jump_sector);
+ if (!result) {// bad tmap; interpolate over cell
+ result = dvdnav_find_vobu_by_cell_boundaries(this, admap, admap_len
+ , cell_bgn_time, cell_bgn_sector
+ , cell_end_time, cell_end_sector
+ , time_in_ms, &jump_sector);
+ if (!result) {
+ goto exit;
+ }
+ }
+
+ // may need to reget time when jump goes to diff cell (occurs near time 0)
+ if ( jump_sector < cell_bgn_sector
+ || jump_sector > cell_end_sector) {
+ result = dvdnav_cell_find(this, state, CELL_FIND_SECTOR, jump_sector
+ , &cell_idx
+ , &cell_bgn_time, &cell_bgn_sector
+ , &cell_end_time, &cell_end_sector);
+ if (!result) {
+ fprintf(MSG_OUT, "ERR:unable to find cell for %i\n", jump_sector);
+ goto exit;
+ }
+ }
+
+ // jump to sector
+ uint32_t sector_off = jump_sector - cell_bgn_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);
+ result = RESULT_TRUE;
+ }
+
+exit:
+ return result;
+}
Thanks for the gentle reminder.Now, we need a full patch and merge
Return to “Development around libVLC”
Users browsing this forum: No registered users and 7 guests