This source file includes following definitions.
- MergeFragment
- FixTrackID
- FixSDTPInTRAF
- gf_isom_parse_movie_boxes
- gf_isom_new_movie
- gf_isom_open_file
- gf_isom_get_mp4time
- gf_isom_delete_movie
- gf_isom_get_track_from_id
- gf_isom_get_track_from_original_id
- gf_isom_get_track_from_file
- GetMediaTime
- GetNextMediaTime
- GetPrevMediaTime
- gf_isom_insert_moov
- gf_isom_create_movie
- CreateEditEntry
- gf_isom_add_subsample_info
- gf_isom_sample_get_subsamples_count
- gf_isom_get_subsample_types
- gf_isom_sample_get_subsample_entry
#include <gpac/internal/isomedia_dev.h>
#include <gpac/network.h>
#ifndef GPAC_DISABLE_ISOM
GF_Err gf_isom_parse_root_box(GF_Box **outBox, GF_BitStream *bs, u64 *bytesExpected, Bool progressive_mode);
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
GF_Err MergeTrack(GF_TrackBox *trak, GF_TrackFragmentBox *traf, u64 moof_offset, Bool is_first_merge);
GF_Err MergeFragment(GF_MovieFragmentBox *moof, GF_ISOFile *mov)
{
GF_Err e;
u32 i, j;
u64 MaxDur;
GF_TrackFragmentBox *traf;
GF_TrackBox *trak;
MaxDur = 0;
if (!mov->moov || !mov->moov->mvex) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Error: %s not received before merging fragment\n", mov->moov ? "mvex" : "moov" ));
return GF_ISOM_INVALID_FILE;
}
if (mov->NextMoofNumber && (mov->NextMoofNumber >= moof->mfhd->sequence_number)) {
GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[iso file] Warning: wrong sequence number: got %d but last one was %d\n", moof->mfhd->sequence_number, mov->NextMoofNumber));
}
i=0;
while ((traf = (GF_TrackFragmentBox*)gf_list_enum(moof->TrackList, &i))) {
if (!traf->tfhd) {
trak = NULL;
traf->trex = NULL;
} else if (mov->is_smooth) {
trak = gf_list_get(mov->moov->trackList, 0);
traf->trex = (GF_TrackExtendsBox*)gf_list_get(mov->moov->mvex->TrackExList, 0);
assert(traf->trex);
traf->trex->trackID = trak->Header->trackID = traf->tfhd->trackID;
} else {
trak = gf_isom_get_track_from_id(mov->moov, traf->tfhd->trackID);
j=0;
while ((traf->trex = (GF_TrackExtendsBox*)gf_list_enum(mov->moov->mvex->TrackExList, &j))) {
if (traf->trex->trackID == traf->tfhd->trackID) break;
traf->trex = NULL;
}
}
if (!trak || !traf->trex) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Error: Cannot find fragment track with ID %d\n", traf->tfhd ? traf->tfhd->trackID : 0));
return GF_ISOM_INVALID_FILE;
}
e = MergeTrack(trak, traf, mov->current_top_box_start, !trak->first_traf_merged);
if (e) return e;
trak->present_in_scalable_segment = 1;
SetTrackDuration(trak);
if (trak->Header->duration > MaxDur)
MaxDur = trak->Header->duration;
trak->first_traf_merged = 1;
}
if (moof->other_boxes) {
GF_Box *a;
i = 0;
while ((a = (GF_Box *)gf_list_enum(moof->other_boxes, &i))) {
if (a->type == GF_ISOM_BOX_TYPE_PSSH) {
GF_ProtectionSystemHeaderBox *pssh = (GF_ProtectionSystemHeaderBox *)gf_isom_box_new(GF_ISOM_BOX_TYPE_PSSH);
memmove(pssh->SystemID, ((GF_ProtectionSystemHeaderBox *)a)->SystemID, 16);
pssh->KID_count = ((GF_ProtectionSystemHeaderBox *)a)->KID_count;
pssh->KIDs = (bin128 *)gf_malloc(pssh->KID_count*sizeof(bin128));
memmove(pssh->KIDs, ((GF_ProtectionSystemHeaderBox *)a)->KIDs, pssh->KID_count*sizeof(bin128));
pssh->private_data_size = ((GF_ProtectionSystemHeaderBox *)a)->private_data_size;
pssh->private_data = (u8 *)gf_malloc(pssh->private_data_size*sizeof(char));
memmove(pssh->private_data, ((GF_ProtectionSystemHeaderBox *)a)->private_data, pssh->private_data_size);
if (!mov->moov->other_boxes) mov->moov->other_boxes = gf_list_new();
gf_list_add(mov->moov->other_boxes, pssh);
}
}
}
mov->NextMoofNumber = moof->mfhd->sequence_number;
if (mov->moov->mvhd->duration < MaxDur) mov->moov->mvhd->duration = MaxDur;
return GF_OK;
}
static void FixTrackID(GF_ISOFile *mov)
{
if (!mov->moov) return;
if (gf_list_count(mov->moov->trackList) == 1 && gf_list_count(mov->moof->TrackList) == 1) {
GF_TrackFragmentBox *traf = (GF_TrackFragmentBox*)gf_list_get(mov->moof->TrackList, 0);
GF_TrackBox *trak = (GF_TrackBox*)gf_list_get(mov->moov->trackList, 0);
if ((traf->tfhd->trackID != trak->Header->trackID)) {
GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[iso file] Warning: trackID of MOOF/TRAF(%u) is not the same as MOOV/TRAK(%u). Trying to fix.\n", traf->tfhd->trackID, trak->Header->trackID));
traf->tfhd->trackID = trak->Header->trackID;
}
}
}
static void FixSDTPInTRAF(GF_MovieFragmentBox *moof)
{
u32 k;
if (!moof)
return;
for (k = 0; k < gf_list_count(moof->TrackList); k++) {
GF_TrackFragmentBox *traf = gf_list_get(moof->TrackList, k);
if (traf->sdtp) {
GF_TrackFragmentRunBox *trun;
u32 j = 0, sample_index = 0;
if (traf->sdtp->sampleCount == gf_list_count(traf->TrackRuns)) {
GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[iso file] Warning: TRAF box of track id=%u contains a SDTP. Converting to TRUN sample flags.\n", traf->tfhd->trackID));
}
while ((trun = (GF_TrackFragmentRunBox*)gf_list_enum(traf->TrackRuns, &j))) {
u32 i = 0;
GF_TrunEntry *entry;
trun->flags |= GF_ISOM_TRUN_FLAGS;
while ((entry = (GF_TrunEntry*)gf_list_enum(trun->entries, &i))) {
const u8 info = traf->sdtp->sample_info[sample_index];
entry->flags |= GF_ISOM_GET_FRAG_DEPEND_FLAGS(info >> 6, info >> 4, info >> 2, info);
sample_index++;
if (sample_index > traf->sdtp->sampleCount) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Error: TRAF box of track id=%u contained an inconsistent SDTP.\n", traf->tfhd->trackID));
return;
}
}
}
if (sample_index < traf->sdtp->sampleCount) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Error: TRAF box of track id=%u list less samples than SDTP.\n", traf->tfhd->trackID));
}
gf_isom_box_del((GF_Box*)traf->sdtp);
traf->sdtp = NULL;
}
}
}
#endif
GF_Err gf_isom_parse_movie_boxes(GF_ISOFile *mov, u64 *bytesMissing, Bool progressive_mode)
{
GF_Box *a;
u64 totSize;
GF_Err e = GF_OK;
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
if (mov->single_moof_mode && mov->single_moof_state == 2) {
return e;
}
totSize = mov->current_top_box_start;
gf_bs_seek(mov->movieFileMap->bs, mov->current_top_box_start);
#endif
while (gf_bs_available(mov->movieFileMap->bs)) {
*bytesMissing = 0;
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
mov->current_top_box_start = gf_bs_get_position(mov->movieFileMap->bs);
GF_LOG(GF_LOG_DEBUG, GF_LOG_CONTAINER, ("[iso file] Starting to parse a top-level box at position %d\n", mov->current_top_box_start));
#endif
e = gf_isom_parse_root_box(&a, mov->movieFileMap->bs, bytesMissing, progressive_mode);
if (e >= 0) {
} else if (e == GF_ISOM_INCOMPLETE_FILE) {
if (mov->openMode != GF_ISOM_OPEN_READ) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Incomplete MDAT while file is not read-only\n"));
return GF_ISOM_INVALID_FILE;
}
if (mov->openMode == GF_ISOM_OPEN_READ) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Incomplete file while reading for dump - aborting parsing\n"));
break;
}
return e;
} else {
return e;
}
switch (a->type) {
case GF_ISOM_BOX_TYPE_MOOV:
if (mov->moov) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Duplicate MOOV detected!\n"));
return GF_ISOM_INVALID_FILE;
}
mov->moov = (GF_MovieBox *)a;
mov->moov->mov = mov;
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
if (mov->moov->mvex) mov->moov->mvex->mov = mov;
#endif
e = gf_list_add(mov->TopBoxes, a);
if (e) return e;
totSize += a->size;
if (mov->dump_mode_alloc) {
u32 k;
for (k=0; k<gf_list_count(mov->moov->trackList); k++) {
GF_TrackBox *trak = (GF_TrackBox *)gf_list_get(mov->moov->trackList, k);
if (trak->sample_encryption) {
e = senc_Parse(mov->movieFileMap->bs, trak, NULL, trak->sample_encryption);
if (e) return e;
}
}
}
break;
case GF_ISOM_BOX_TYPE_META:
if (mov->meta) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Duplicate META detected!\n"));
return GF_ISOM_INVALID_FILE;
}
mov->meta = (GF_MetaBox *)a;
e = gf_list_add(mov->TopBoxes, a);
if (e) {
return e;
}
totSize += a->size;
break;
case GF_ISOM_BOX_TYPE_MDAT:
totSize += a->size;
if (mov->openMode == GF_ISOM_OPEN_READ) {
if (!mov->mdat) {
mov->mdat = (GF_MediaDataBox *) a;
e = gf_list_add(mov->TopBoxes, mov->mdat);
if (e) {
return e;
}
}
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
else if (mov->FragmentsFlags & GF_ISOM_FRAG_READ_DEBUG) gf_list_add(mov->TopBoxes, a);
#endif
else gf_isom_box_del(a);
}
else if (!mov->mdat && (mov->openMode != GF_ISOM_OPEN_READ) && (mov->openMode != GF_ISOM_OPEN_CAT_FRAGMENTS)) {
gf_isom_box_del(a);
mov->mdat = (GF_MediaDataBox *) gf_isom_box_new(GF_ISOM_BOX_TYPE_MDAT);
e = gf_list_add(mov->TopBoxes, mov->mdat);
if (e) {
return e;
}
} else {
gf_isom_box_del(a);
}
break;
case GF_ISOM_BOX_TYPE_FTYP:
if (mov->brand) {
gf_isom_box_del(a);
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Duplicate FTYP detected!\n"));
return GF_ISOM_INVALID_FILE;
}
mov->brand = (GF_FileTypeBox *)a;
totSize += a->size;
e = gf_list_add(mov->TopBoxes, a);
if (e) return e;
break;
case GF_ISOM_BOX_TYPE_PDIN:
if (mov->pdin) {
gf_isom_box_del(a);
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Duplicate PDIN detected!\n"));
return GF_ISOM_INVALID_FILE;
}
mov->pdin = (GF_ProgressiveDownloadBox *) a;
totSize += a->size;
e = gf_list_add(mov->TopBoxes, a);
if (e) return e;
break;
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
case GF_ISOM_BOX_TYPE_STYP:
{
u32 brand = ((GF_SegmentTypeBox *)a)->majorBrand;
switch (brand) {
case GF_4CC('s', 'i', 's', 'x'):
case GF_4CC('r', 'i', 's', 'x'):
case GF_4CC('s', 's', 's', 's'):
mov->is_index_segment = GF_TRUE;
break;
default:
break;
}
}
case GF_ISOM_BOX_TYPE_SIDX:
case GF_ISOM_BOX_TYPE_SSIX:
totSize += a->size;
if (mov->FragmentsFlags & GF_ISOM_FRAG_READ_DEBUG) {
e = gf_list_add(mov->TopBoxes, a);
if (e) return e;
} else {
gf_isom_box_del(a);
}
break;
case GF_ISOM_BOX_TYPE_MOOF:
if (!mov->moov) {
GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[iso file] Movie fragment but no moov (yet) - possibly broken parsing!\n"));
}
if (mov->single_moof_mode) {
mov->single_moof_state++;
if (mov->single_moof_state > 1) {
gf_isom_box_del(a);
return GF_OK;
}
}
((GF_MovieFragmentBox *)a)->mov = mov;
totSize += a->size;
mov->moof = (GF_MovieFragmentBox *) a;
FixTrackID(mov);
if (! (mov->FragmentsFlags & GF_ISOM_FRAG_READ_DEBUG)) {
FixSDTPInTRAF(mov->moof);
}
if (mov->FragmentsFlags & GF_ISOM_FRAG_READ_DEBUG) {
u32 k;
gf_list_add(mov->TopBoxes, a);
if (mov->moov) {
for (k=0; k<gf_list_count(mov->moof->TrackList); k++) {
GF_TrackFragmentBox *traf = gf_list_get(mov->moof->TrackList, k);
if (traf->tfhd) {
GF_TrackBox *trak = gf_isom_get_track_from_id(mov->moov, traf->tfhd->trackID);
u32 j=0;
while ((traf->trex = (GF_TrackExtendsBox*)gf_list_enum(mov->moov->mvex->TrackExList, &j))) {
if (traf->trex->trackID == traf->tfhd->trackID) {
if (!traf->trex->track) traf->trex->track = trak;
break;
}
traf->trex = NULL;
}
}
if (traf->trex && traf->trex->track && traf->sample_encryption) {
GF_TrackBox *trak = GetTrackbyID(mov->moov, traf->tfhd->trackID);
e = senc_Parse(mov->movieFileMap->bs, trak, traf, traf->sample_encryption);
if (e) return e;
}
}
}
} else if (mov->openMode==GF_ISOM_OPEN_CAT_FRAGMENTS) {
mov->NextMoofNumber = mov->moof->mfhd->sequence_number+1;
mov->moof = NULL;
gf_isom_box_del(a);
} else {
e = MergeFragment((GF_MovieFragmentBox *)a, mov);
if (e) return e;
gf_isom_box_del(a);
}
break;
#endif
case GF_ISOM_BOX_TYPE_UNKNOWN:
{
GF_UnknownBox *box = (GF_UnknownBox*)a;
if (box->original_4cc == GF_4CC('j','P',' ',' ')) {
u8 *c = (u8 *) box->data;
if ((box->dataSize==4) && (GF_4CC(c[0],c[1],c[2],c[3])==(u32)0x0D0A870A))
mov->is_jp2 = 1;
gf_isom_box_del(a);
} else {
e = gf_list_add(mov->TopBoxes, a);
if (e) return e;
}
}
break;
case GF_ISOM_BOX_TYPE_PRFT:
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
if (!(mov->FragmentsFlags & GF_ISOM_FRAG_READ_DEBUG)) {
if (mov->last_producer_ref_time)
gf_isom_box_del(a);
else
mov->last_producer_ref_time = (GF_ProducerReferenceTimeBox *)a;
break;
}
#endif
default:
totSize += a->size;
e = gf_list_add(mov->TopBoxes, a);
if (e) return e;
break;
}
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
mov->current_top_box_start = gf_bs_get_position(mov->movieFileMap->bs);
#endif
}
if (!mov->moov && !mov->meta
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
&& !mov->moof && !mov->is_index_segment
#endif
) {
return GF_ISOM_INCOMPLETE_FILE;
}
if (mov->moov && !mov->moov->mvhd) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Missing MVHD in MOOV!\n"));
return GF_ISOM_INVALID_FILE;
}
if (mov->meta && !mov->meta->handler) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[iso file] Missing handler in META!\n"));
return GF_ISOM_INVALID_FILE;
}
#ifndef GPAC_DISABLE_ISOM_WRITE
if (mov->moov) {
mov->interleavingTime = mov->moov->mvhd->timeScale;
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
if ((mov->openMode > GF_ISOM_OPEN_READ) && (mov->openMode != GF_ISOM_OPEN_CAT_FRAGMENTS) && mov->moov->mvex) {
gf_isom_box_del((GF_Box *)mov->moov->mvex);
mov->moov->mvex = NULL;
}
#endif
}
if (!mov->mdat && (mov->openMode != GF_ISOM_OPEN_READ) && (mov->openMode != GF_ISOM_OPEN_CAT_FRAGMENTS)) {
mov->mdat = (GF_MediaDataBox *) gf_isom_box_new(GF_ISOM_BOX_TYPE_MDAT);
e = gf_list_add(mov->TopBoxes, mov->mdat);
if (e) return e;
}
#endif
return GF_OK;
}
GF_ISOFile *gf_isom_new_movie()
{
GF_ISOFile *mov = (GF_ISOFile*)gf_malloc(sizeof(GF_ISOFile));
if (mov == NULL) {
gf_isom_set_last_error(NULL, GF_OUT_OF_MEM);
return NULL;
}
memset(mov, 0, sizeof(GF_ISOFile));
mov->TopBoxes = gf_list_new();
if (!mov->TopBoxes) {
gf_isom_set_last_error(NULL, GF_OUT_OF_MEM);
gf_free(mov);
return NULL;
}
mov->storageMode = GF_ISOM_STORE_FLAT;
return mov;
}
extern Bool use_dump_mode;
GF_ISOFile *gf_isom_open_file(const char *fileName, u32 OpenMode, const char *tmp_dir)
{
GF_Err e;
u64 bytes;
GF_ISOFile *mov = gf_isom_new_movie();
if (! mov) return NULL;
mov->fileName = gf_strdup(fileName);
mov->openMode = OpenMode;
if ( (OpenMode == GF_ISOM_OPEN_READ) || (OpenMode == GF_ISOM_OPEN_READ_DUMP) ) {
mov->openMode = GF_ISOM_OPEN_READ;
mov->es_id_default_sync = -1;
e = gf_isom_datamap_new(fileName, NULL, GF_ISOM_DATA_MAP_READ_ONLY, &mov->movieFileMap);
if (e) {
gf_isom_set_last_error(NULL, e);
gf_isom_delete_movie(mov);
return NULL;
}
if (OpenMode == GF_ISOM_OPEN_READ_DUMP) {
mov->dump_mode_alloc = GF_TRUE;
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
mov->FragmentsFlags |= GF_ISOM_FRAG_READ_DEBUG;
#endif
}
} else {
#ifdef GPAC_DISABLE_ISOM_WRITE
gf_isom_delete_movie(mov);
gf_isom_set_last_error(NULL, GF_ISOM_INVALID_MODE);
return NULL;
#else
mov->finalName = (char*)gf_malloc(strlen(fileName) + 5);
if (!mov->finalName) {
gf_isom_set_last_error(NULL, GF_OUT_OF_MEM);
gf_isom_delete_movie(mov);
return NULL;
}
strcpy(mov->finalName, "out_");
strcat(mov->finalName, fileName);
e = gf_isom_datamap_new(fileName, NULL, GF_ISOM_DATA_MAP_EDIT, &mov->movieFileMap);
if (e) {
gf_isom_set_last_error(NULL, e);
gf_isom_delete_movie(mov);
return NULL;
}
e = gf_isom_datamap_new("mp4_tmp_edit", tmp_dir, GF_ISOM_DATA_MAP_WRITE, & mov->editFileMap);
if (e) {
gf_isom_set_last_error(NULL, e);
gf_isom_delete_movie(mov);
return NULL;
}
mov->es_id_default_sync = -1;
#endif
}
use_dump_mode = mov->dump_mode_alloc;
mov->LastError = gf_isom_parse_movie_boxes(mov, &bytes, 0);
if (!mov->LastError && (OpenMode == GF_ISOM_OPEN_CAT_FRAGMENTS)) {
gf_isom_datamap_del(mov->movieFileMap);
mov->LastError = gf_isom_datamap_new(fileName, tmp_dir, GF_ISOM_DATA_MAP_CAT, & mov->movieFileMap);
}
if (mov->LastError) {
gf_isom_set_last_error(NULL, mov->LastError);
gf_isom_delete_movie(mov);
return NULL;
}
use_dump_mode = GF_FALSE;
return mov;
}
u64 gf_isom_get_mp4time()
{
u32 calctime, msec;
u64 ret;
gf_utc_time_since_1970(&calctime, &msec);
calctime += GF_ISOM_MAC_TIME_OFFSET;
ret = calctime;
return ret;
}
void gf_isom_delete_movie(GF_ISOFile *mov)
{
if (!mov) return;
use_dump_mode = mov->dump_mode_alloc;
if (mov->movieFileMap) gf_isom_datamap_del(mov->movieFileMap);
#ifndef GPAC_DISABLE_ISOM_WRITE
if (mov->editFileMap) {
gf_isom_datamap_del(mov->editFileMap);
}
if (mov->finalName) gf_free(mov->finalName);
#endif
gf_isom_box_array_del(mov->TopBoxes);
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
gf_isom_box_array_del(mov->moof_list);
#endif
if (mov->last_producer_ref_time)
gf_isom_box_del((GF_Box *) mov->last_producer_ref_time);
if (mov->fileName) gf_free(mov->fileName);
use_dump_mode = GF_FALSE;
gf_free(mov);
}
GF_TrackBox *gf_isom_get_track_from_id(GF_MovieBox *moov, u32 trackID)
{
u32 i, count;
GF_TrackBox *trak;
if (!moov || !trackID) return NULL;
count = gf_list_count(moov->trackList);
for (i = 0; i<count; i++) {
trak = (GF_TrackBox*)gf_list_get(moov->trackList, i);
if (trak->Header->trackID == trackID) return trak;
}
return NULL;
}
GF_TrackBox *gf_isom_get_track_from_original_id(GF_MovieBox *moov, u32 originalID, u32 originalFile)
{
u32 i, count;
GF_TrackBox *trak;
if (!moov || !originalID) return NULL;
count = gf_list_count(moov->trackList);
for (i = 0; i<count; i++) {
trak = (GF_TrackBox*)gf_list_get(moov->trackList, i);
if ((trak->originalFile == originalFile) && (trak->originalID == originalID)) return trak;
}
return NULL;
}
GF_TrackBox *gf_isom_get_track_from_file(GF_ISOFile *movie, u32 trackNumber)
{
GF_TrackBox *trak;
if (!movie) return NULL;
trak = gf_isom_get_track(movie->moov, trackNumber);
if (!trak) movie->LastError = GF_BAD_PARAM;
return trak;
}
GF_Err GetMediaTime(GF_TrackBox *trak, Bool force_non_empty, u64 movieTime, u64 *MediaTime, s64 *SegmentStartTime, s64 *MediaOffset, u8 *useEdit, u64 *next_edit_start_plus_one)
{
#if 0
GF_Err e;
u32 sampleNumber, prevSampleNumber;
u64 firstDTS;
#endif
u32 i, count;
Bool last_is_empty = 0;
u64 time, lastSampleTime;
s64 mtime;
GF_EdtsEntry *ent;
Double scale_ts;
GF_SampleTableBox *stbl = trak->Media->information->sampleTable;
if (next_edit_start_plus_one) *next_edit_start_plus_one = 0;
*useEdit = 1;
*MediaTime = 0;
*SegmentStartTime = -1;
*MediaOffset = -1;
if (!trak->moov->mvhd->timeScale || !trak->Media->mediaHeader->timeScale || !stbl->SampleSize) {
return GF_ISOM_INVALID_FILE;
}
if (!stbl->SampleSize->sampleCount) {
lastSampleTime = 0;
} else {
lastSampleTime = trak->Media->mediaHeader->duration;
}
if (! trak->editBox || !trak->editBox->editList) {
*MediaTime = movieTime;
if ((*MediaTime > lastSampleTime)
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
&& !trak->moov->mov->moof
#endif
) {
*MediaTime = lastSampleTime;
}
*useEdit = 0;
return GF_OK;
}
scale_ts = trak->Media->mediaHeader->timeScale;
scale_ts /= trak->moov->mvhd->timeScale;
time = 0;
ent = NULL;
count=gf_list_count(trak->editBox->editList->entryList);
for (i=0; i<count; i++) {
ent = (GF_EdtsEntry *)gf_list_get(trak->editBox->editList->entryList, i);
if ( (time + ent->segmentDuration) * scale_ts > movieTime) {
if (!force_non_empty || (ent->mediaTime >= 0)) {
if (next_edit_start_plus_one) *next_edit_start_plus_one = 1 + (u64) ((time + ent->segmentDuration) * scale_ts);
goto ent_found;
}
}
time += ent->segmentDuration;
last_is_empty = ent->segmentDuration ? 0 : 1;
}
if (last_is_empty) {
ent = (GF_EdtsEntry *)gf_list_last(trak->editBox->editList->entryList);
if (ent->mediaRate==1) {
*MediaTime = movieTime + ent->mediaTime;
} else {
ent = (GF_EdtsEntry *)gf_list_get(trak->editBox->editList->entryList, 0);
if (ent->mediaRate==-1) {
u64 dur = (u64) (ent->segmentDuration * scale_ts);
*MediaTime = (movieTime > dur) ? (movieTime-dur) : 0;
}
}
*useEdit = 0;
return GF_OK;
}
if (!ent) {
*MediaTime = movieTime;
if (*MediaTime > lastSampleTime) *MediaTime = lastSampleTime;
*useEdit = 0;
return GF_OK;
}
*MediaTime = lastSampleTime;
return GF_OK;
ent_found:
*SegmentStartTime = time;
if (ent->mediaTime < 0) {
*MediaTime = 0;
return GF_OK;
}
if (! ent->mediaRate) {
*MediaTime = ent->mediaTime;
*MediaOffset = 0;
*useEdit = 2;
return GF_OK;
}
mtime = ent->mediaTime + movieTime - (time * trak->Media->mediaHeader->timeScale / trak->moov->mvhd->timeScale);
if (mtime<0) mtime = 0;
*MediaTime = (u64) mtime;
*MediaOffset = ent->mediaTime;
#if 0
e = stbl_findEntryForTime(stbl, (u32) *MediaTime, 1, &sampleNumber, &prevSampleNumber);
if (e) return e;
if (!sampleNumber && !prevSampleNumber) {
*MediaTime = lastSampleTime;
return GF_OK;
}
if (!sampleNumber) sampleNumber = prevSampleNumber;
stbl_GetSampleDTS(stbl->TimeToSample, sampleNumber, &DTS);
CTS = 0;
if (stbl->CompositionOffset) stbl_GetSampleCTS(stbl->CompositionOffset, sampleNumber, &CTS);
e = stbl_findEntryForTime(stbl, (u32) ent->mediaTime, 0, &sampleNumber, &prevSampleNumber);
if (e) return e;
if (!sampleNumber && !prevSampleNumber) {
*MediaTime = lastSampleTime;
return GF_ISOM_INVALID_FILE;
}
if (!sampleNumber) sampleNumber = prevSampleNumber;
stbl_GetSampleDTS(stbl->TimeToSample, sampleNumber, &firstDTS);
*MediaOffset = firstDTS;
#endif
return GF_OK;
}
GF_Err GetNextMediaTime(GF_TrackBox *trak, u64 movieTime, u64 *OutMovieTime)
{
u32 i;
u64 time;
GF_EdtsEntry *ent;
*OutMovieTime = 0;
if (! trak->editBox || !trak->editBox->editList) return GF_BAD_PARAM;
time = 0;
ent = NULL;
i=0;
while ((ent = (GF_EdtsEntry *)gf_list_enum(trak->editBox->editList->entryList, &i))) {
if (time * trak->Media->mediaHeader->timeScale >= movieTime * trak->moov->mvhd->timeScale) {
if (ent->mediaTime >= 0) {
*OutMovieTime = time * trak->Media->mediaHeader->timeScale / trak->moov->mvhd->timeScale;
if (*OutMovieTime>0) *OutMovieTime -= 1;
return GF_OK;
}
}
time += ent->segmentDuration;
}
*OutMovieTime = trak->moov->mvhd->duration;
return GF_EOS;
}
GF_Err GetPrevMediaTime(GF_TrackBox *trak, u64 movieTime, u64 *OutMovieTime)
{
u32 i;
u64 time;
GF_EdtsEntry *ent;
*OutMovieTime = 0;
if (! trak->editBox || !trak->editBox->editList) return GF_BAD_PARAM;
time = 0;
ent = NULL;
i=0;
while ((ent = (GF_EdtsEntry *)gf_list_enum(trak->editBox->editList->entryList, &i))) {
if (ent->mediaTime == -1) {
if ( (time + ent->segmentDuration) * trak->Media->mediaHeader->timeScale >= movieTime * trak->moov->mvhd->timeScale) {
*OutMovieTime = time * trak->Media->mediaHeader->timeScale / trak->moov->mvhd->timeScale;
return GF_OK;
}
continue;
}
time += ent->segmentDuration;
if ( time * trak->Media->mediaHeader->timeScale >= movieTime * trak->moov->mvhd->timeScale) {
*OutMovieTime = time * trak->Media->mediaHeader->timeScale / trak->moov->mvhd->timeScale;
return GF_OK;
}
}
*OutMovieTime = 0;
return GF_OK;
}
#ifndef GPAC_DISABLE_ISOM_WRITE
void gf_isom_insert_moov(GF_ISOFile *file)
{
u64 now;
GF_MovieHeaderBox *mvhd;
if (file->moov) return;
file->moov = (GF_MovieBox *) gf_isom_box_new(GF_ISOM_BOX_TYPE_MOOV);
file->moov->mov = file;
now = gf_isom_get_mp4time();
mvhd = (GF_MovieHeaderBox *) gf_isom_box_new(GF_ISOM_BOX_TYPE_MVHD);
mvhd->creationTime = now;
if (!file->keep_utc)
mvhd->modificationTime = now;
mvhd->nextTrackID = 1;
mvhd->timeScale = 600;
file->interleavingTime = mvhd->timeScale;
moov_AddBox((GF_Box*)file->moov, (GF_Box *)mvhd);
gf_list_add(file->TopBoxes, file->moov);
}
GF_ISOFile *gf_isom_create_movie(const char *fileName, u32 OpenMode, const char *tmp_dir)
{
GF_Err e;
GF_ISOFile *mov = gf_isom_new_movie();
if (!mov) return NULL;
mov->openMode = OpenMode;
mov->movieFileMap = NULL;
if (OpenMode == GF_ISOM_OPEN_WRITE) {
mov->fileName = gf_strdup(fileName);
e = gf_isom_datamap_new(fileName, NULL, GF_ISOM_DATA_MAP_WRITE, & mov->editFileMap);
if (e) goto err_exit;
gf_isom_set_brand_info( (GF_ISOFile *) mov, GF_ISOM_BRAND_ISOM, 1);
} else {
mov->finalName = (char*)gf_malloc(strlen(fileName) + 1);
strcpy(mov->finalName, fileName);
e = gf_isom_datamap_new("mp4_tmp_edit", tmp_dir, GF_ISOM_DATA_MAP_WRITE, & mov->editFileMap);
if (e) {
gf_isom_set_last_error(NULL, e);
gf_isom_delete_movie(mov);
return NULL;
}
gf_isom_set_brand_info( (GF_ISOFile *) mov, GF_ISOM_BRAND_ISOM, 1);
}
mov->mdat = (GF_MediaDataBox *) gf_isom_box_new(GF_ISOM_BOX_TYPE_MDAT);
gf_list_add(mov->TopBoxes, mov->mdat);
mov->storageMode = GF_ISOM_STORE_FLAT;
return mov;
err_exit:
gf_isom_set_last_error(NULL, e);
if (mov) gf_isom_delete_movie(mov);
return NULL;
}
GF_EdtsEntry *CreateEditEntry(u64 EditDuration, u64 MediaTime, u8 EditMode)
{
GF_EdtsEntry *ent;
ent = (GF_EdtsEntry*)gf_malloc(sizeof(GF_EdtsEntry));
if (!ent) return NULL;
switch (EditMode) {
case GF_ISOM_EDIT_EMPTY:
ent->mediaRate = 1;
ent->mediaTime = -1;
break;
case GF_ISOM_EDIT_DWELL:
ent->mediaRate = 0;
ent->mediaTime = MediaTime;
break;
default:
ent->mediaRate = 1;
ent->mediaTime = MediaTime;
break;
}
ent->segmentDuration = EditDuration;
return ent;
}
GF_Err gf_isom_add_subsample_info(GF_SubSampleInformationBox *sub_samples, u32 sampleNumber, u32 subSampleSize, u8 priority, u32 reserved, Bool discardable)
{
u32 i, count, last_sample;
GF_SubSampleInfoEntry *pSamp;
GF_SubSampleEntry *pSubSamp;
pSamp = NULL;
last_sample = 0;
count = gf_list_count(sub_samples->Samples);
for (i=0; i<count; i++) {
pSamp = (GF_SubSampleInfoEntry*) gf_list_get(sub_samples->Samples, i);
if (last_sample + pSamp->sample_delta > sampleNumber) return GF_NOT_SUPPORTED;
if (last_sample + pSamp->sample_delta == sampleNumber) break;
last_sample += pSamp->sample_delta;
pSamp = NULL;
}
if (!pSamp) {
GF_SAFEALLOC(pSamp, GF_SubSampleInfoEntry);
if (!pSamp) return GF_OUT_OF_MEM;
pSamp->SubSamples = gf_list_new();
if (!pSamp->SubSamples ) {
gf_free(pSamp);
return GF_OUT_OF_MEM;
}
pSamp->sample_delta = sampleNumber - last_sample;
gf_list_add(sub_samples->Samples, pSamp);
}
if ((subSampleSize>0xFFFF) && !sub_samples->version) {
sub_samples->version = 1;
}
if (!subSampleSize) {
pSubSamp = gf_list_last(pSamp->SubSamples);
gf_list_rem_last(pSamp->SubSamples);
gf_free(pSubSamp);
if (!gf_list_count(pSamp->SubSamples)) {
gf_list_del_item(sub_samples->Samples, pSamp);
gf_list_del(pSamp->SubSamples);
gf_free(pSamp);
}
return GF_OK;
}
GF_SAFEALLOC(pSubSamp, GF_SubSampleEntry);
if (!pSubSamp) return GF_OUT_OF_MEM;
pSubSamp->subsample_size = subSampleSize;
pSubSamp->subsample_priority = priority;
pSubSamp->reserved = reserved;
pSubSamp->discardable = discardable;
return gf_list_add(pSamp->SubSamples, pSubSamp);
}
#endif
u32 gf_isom_sample_get_subsamples_count(GF_ISOFile *movie, u32 track)
{
GF_TrackBox *trak = gf_isom_get_track_from_file(movie, track);
if (!track) return 0;
if (!trak->Media || !trak->Media->information->sampleTable || !trak->Media->information->sampleTable->sub_samples) return 0;
return gf_list_count(trak->Media->information->sampleTable->sub_samples);
}
Bool gf_isom_get_subsample_types(GF_ISOFile *movie, u32 track, u32 subs_index, u32 *flags)
{
GF_SubSampleInformationBox *sub_samples=NULL;
GF_TrackBox *trak = gf_isom_get_track_from_file(movie, track);
if (!track || !subs_index) return GF_FALSE;
if (!trak->Media || !trak->Media->information->sampleTable || !trak->Media->information->sampleTable->sub_samples) return GF_FALSE;
sub_samples = gf_list_get(trak->Media->information->sampleTable->sub_samples, subs_index-1);
if (!sub_samples) return GF_FALSE;
*flags = sub_samples->flags;
return GF_TRUE;
}
u32 gf_isom_sample_get_subsample_entry(GF_ISOFile *movie, u32 track, u32 sampleNumber, u32 flags, GF_SubSampleInfoEntry **sub_sample)
{
u32 i, count, last_sample;
GF_SubSampleInformationBox *sub_samples=NULL;
GF_TrackBox *trak = gf_isom_get_track_from_file(movie, track);
if (sub_sample) *sub_sample = NULL;
if (!track) return 0;
if (!trak->Media || !trak->Media->information->sampleTable || !trak->Media->information->sampleTable->sub_samples) return 0;
count = gf_list_count(trak->Media->information->sampleTable->sub_samples);
for (i=0; i<count; i++) {
sub_samples = gf_list_get(trak->Media->information->sampleTable->sub_samples, i);
if (sub_samples->flags==flags) break;
sub_samples = NULL;
}
if (!sub_samples) return 0;
last_sample = 0;
count = gf_list_count(sub_samples->Samples);
for (i=0; i<count; i++) {
GF_SubSampleInfoEntry *pSamp = (GF_SubSampleInfoEntry *) gf_list_get(sub_samples->Samples, i);
if (last_sample + pSamp->sample_delta == sampleNumber) {
if (sub_sample) *sub_sample = pSamp;
return gf_list_count(pSamp->SubSamples);
}
last_sample += pSamp->sample_delta;
}
return 0;
}
#endif