27 #include "AampUtils.h"
37 if(buffer && bufferLen > 0)
39 for(
int i=0; i < bufferLen; i++)
56 uint64_t val = READ_U32(buf);
57 val = (val<<32) | (uint32_t)READ_U32(buf);
66 uint32_t msw = (uint32_t)(val>>32);
67 WRITE_U32(dst, msw); dst+=4;
74 Box::Box(uint32_t sz,
const char btype[4]) : offset(0), size(sz), type{}
132 if ((!IS_TYPE(
type, MOOV)) ||
133 (!IS_TYPE(
type, MDIA)) ||
134 (!IS_TYPE(
type, MOOF)) ||
135 (!IS_TYPE(
type, TRAF)) ||
136 (!IS_TYPE(
type, TFDT)) ||
137 (!IS_TYPE(
type, MVHD)) ||
138 (!IS_TYPE(
type, MDHD)) ||
139 (!IS_TYPE(
type, TFDT)) ||
140 (!IS_TYPE(
type, FTYP)) ||
141 (!IS_TYPE(
type, STYP)) ||
142 (!IS_TYPE(
type, SIDX)) ||
143 (!IS_TYPE(
type, PRFT)) ||
144 (!IS_TYPE(
type, MDAT)))
164 uint8_t *hdr_start = hdr;
169 AAMPLOG_TRACE(
"Box data < 4 bytes. Can't determine Size & Type");
170 return new Box(maxSz, (
const char *)
"UKWN");
172 else if(maxSz >= 4 && maxSz < 8)
174 AAMPLOG_TRACE(
"Box Size between >4 but <8 bytes. Can't determine Type");
176 return new Box(maxSz, (
const char *)
"UKWN");
180 size = READ_U32(hdr);
181 READ_U8(
type, hdr, 4);
190 AAMPLOG_WARN(
"Box[%s] fixing size error:size[%u] > maxSz[%u]",
type,
size, maxSz);
192 WRITE_U32(hdr,maxSz);
197 #ifdef AAMP_DEBUG_BOX_CONSTRUCT
198 AAMPLOG_WARN(
"Box[%s] Size error:size[%u] > maxSz[%u]",
type,
size, maxSz);
202 else if (IS_TYPE(
type, MOOV))
206 else if (IS_TYPE(
type, TRAK))
210 else if (IS_TYPE(
type, MDIA))
214 else if (IS_TYPE(
type, MOOF))
218 else if (IS_TYPE(
type, TRAF))
222 else if (IS_TYPE(
type, TFHD))
226 else if (IS_TYPE(
type, TFDT))
230 else if (IS_TYPE(
type, TRUN))
234 else if (IS_TYPE(
type, MVHD))
238 else if (IS_TYPE(
type, MDHD))
242 else if (IS_TYPE(
type, EMSG))
246 else if (IS_TYPE(
type, PRFT))
267 for (
unsigned int i = children.size(); i>0;)
270 SAFE_DELETE(children.at(i));
281 children.push_back(box);
313 uint32_t curOffset =
sizeof(uint32_t) +
sizeof(uint32_t);
314 while (curOffset < sz)
328 FullBox::FullBox(uint32_t sz,
const char btype[4], uint8_t ver, uint32_t f) :
Box(sz, btype), version(ver), flags(f)
370 uint8_t version = READ_VERSION(ptr);
371 uint32_t flags = READ_FLAGS(ptr);
374 uint32_t skip =
sizeof(uint32_t)*2;
378 skip =
sizeof(uint64_t)*2;
382 tScale = READ_U32(ptr);
384 FullBox fbox(sz, Box::MVHD, version, flags);
385 return new MvhdBox(fbox, tScale);
425 uint8_t version = READ_VERSION(ptr);
426 uint32_t flags = READ_FLAGS(ptr);
429 uint32_t skip =
sizeof(uint32_t)*2;
433 skip =
sizeof(uint64_t)*2;
437 tScale = READ_U32(ptr);
439 FullBox fbox(sz, Box::MDHD, version, flags);
440 return new MdhdBox(fbox, tScale);
480 uint8_t version = READ_VERSION(ptr);
481 uint32_t flags = READ_FLAGS(ptr);
486 mdt = READ_BMDT64(ptr);
490 mdt = (uint32_t)READ_U32(ptr);
492 FullBox fbox(sz, Box::TFDT, version, flags);
500 , timeScale(tScale), eventDuration(evtDur), id(_id)
501 , presentationTimeDelta(0), presentationTime(0)
502 , schemeIdUri(nullptr), value(nullptr), messageData(nullptr), messageLen(0)
511 , timeScale(tScale), eventDuration(evtDur), id(_id)
512 , presentationTimeDelta(presTimeDelta), presentationTime(presTime)
513 , schemeIdUri(nullptr), value(nullptr), messageData(nullptr), messageLen(0)
560 eventDuration = evtDur;
568 return eventDuration;
592 presentationTimeDelta = presTimeDelta;
600 return presentationTimeDelta;
608 presentationTime = presTime;
616 return presentationTime;
624 schemeIdUri = schemeIdURI;
640 value = schemeIdValue;
656 messageData = message;
681 uint8_t version = READ_VERSION(ptr);
682 uint32_t flags = READ_FLAGS(ptr);
685 uint32_t remainingSize = sz - ((
sizeof(uint32_t))+(
sizeof(uint64_t)));
687 uint64_t presTime = 0;
688 uint32_t presTimeDelta = 0;
693 uint8_t* schemeId =
nullptr;
694 uint8_t* schemeIdValue =
nullptr;
696 uint8_t* message =
nullptr;
697 FullBox fbox(sz, Box::EMSG, version, flags);
704 tScale = READ_U32(ptr);
706 presTime = READ_64(ptr);
707 evtDur = READ_U32(ptr);
708 boxId = READ_U32(ptr);
709 remainingSize -= ((
sizeof(uint32_t)*3) +
sizeof(uint64_t));
713 schemeId = (uint8_t*) malloc(
sizeof(uint8_t)*schemeIdLen);
714 READ_U8(schemeId, ptr, schemeIdLen);
715 remainingSize -= (
sizeof(uint8_t) * schemeIdLen);
717 if (schemeIdValueLen > 0)
719 schemeIdValue = (uint8_t*) malloc(
sizeof(uint8_t)*schemeIdValueLen);
720 READ_U8(schemeIdValue, ptr, schemeIdValueLen);
721 remainingSize -= (
sizeof(uint8_t) * schemeIdValueLen);
725 else if(0 == version)
730 schemeId = (uint8_t*) malloc(
sizeof(uint8_t)*schemeIdLen);
731 READ_U8(schemeId, ptr, schemeIdLen);
732 remainingSize -= (
sizeof(uint8_t) * schemeIdLen);
734 if (schemeIdValueLen)
736 schemeIdValue = (uint8_t*) malloc(
sizeof(uint8_t)*schemeIdValueLen);
737 READ_U8(schemeIdValue, ptr, schemeIdValueLen);
738 remainingSize -= (
sizeof(uint8_t) * schemeIdValueLen);
741 tScale = READ_U32(ptr);
742 presTimeDelta = READ_U32(ptr);
743 evtDur = READ_U32(ptr);
744 boxId = READ_U32(ptr);
745 remainingSize -= (
sizeof(uint32_t)*4);
749 AAMPLOG_WARN(
"Unsupported emsg box version");
750 return new EmsgBox(fbox, 0, 0, 0, 0, 0);
753 EmsgBox* retBox =
new EmsgBox(fbox, tScale, evtDur, boxId, presTime, presTimeDelta);
757 if(remainingSize > 0)
759 message = (uint8_t*) malloc(
sizeof(uint8_t)*remainingSize);
760 READ_U8(message, ptr, remainingSize);
817 duration = sampleDuration;
833 const uint32_t TRUN_FLAG_DATA_OFFSET_PRESENT = 0x0001;
834 const uint32_t TRUN_FLAG_FIRST_SAMPLE_FLAGS_PRESENT = 0x0004;
835 const uint32_t TRUN_FLAG_SAMPLE_DURATION_PRESENT = 0x0100;
836 const uint32_t TRUN_FLAG_SAMPLE_SIZE_PRESENT = 0x0200;
837 const uint32_t TRUN_FLAG_SAMPLE_FLAGS_PRESENT = 0x0400;
838 const uint32_t TRUN_FLAG_SAMPLE_COMPOSITION_TIME_OFFSET_PRESENT = 0x0800;
840 uint8_t version = READ_VERSION(ptr);
841 uint32_t flags = READ_FLAGS(ptr);
842 uint64_t sampleDuration = 0;
843 uint32_t sample_count = 0;
844 uint32_t sample_duration = 0;
845 uint32_t sample_size = 0;
846 uint32_t sample_flags = 0;
847 uint32_t sample_composition_time_offset = 0;
849 uint32_t totalSampleDuration = 0;
851 uint32_t record_fields_count = 0;
854 for (
unsigned int i=0; i<8; i++)
856 if (flags & (1<<(i+8))) ++record_fields_count;
859 sample_count = READ_U32(ptr);
861 discard = READ_U32(ptr);
863 for (
unsigned int i=0; i<sample_count; i++)
865 if (flags & TRUN_FLAG_SAMPLE_DURATION_PRESENT)
867 sample_duration = READ_U32(ptr);
868 totalSampleDuration += sample_duration;
870 if (flags & TRUN_FLAG_SAMPLE_SIZE_PRESENT)
872 sample_size = READ_U32(ptr);
874 if (flags & TRUN_FLAG_SAMPLE_FLAGS_PRESENT)
876 sample_flags = READ_U32(ptr);
878 if (flags & TRUN_FLAG_SAMPLE_COMPOSITION_TIME_OFFSET_PRESENT)
880 sample_composition_time_offset = READ_U32(ptr);
883 FullBox fbox(sz, Box::TRUN, version, flags);
884 return new TrunBox(fbox, totalSampleDuration);
908 duration = default_duration;
924 uint8_t version = READ_VERSION(ptr);
925 uint32_t flags = READ_FLAGS(ptr);
927 const uint32_t TFHD_FLAG_BASE_DATA_OFFSET_PRESENT = 0x00001;
928 const uint32_t TFHD_FLAG_SAMPLE_DESCRIPTION_INDEX_PRESENT = 0x00002;
929 const uint32_t TFHD_FLAG_DEFAULT_SAMPLE_DURATION_PRESENT = 0x00008;
930 const uint32_t TFHD_FLAG_DEFAULT_SAMPLE_SIZE_PRESENT = 0x00010;
931 const uint32_t TFHD_FLAG_DEFAULT_SAMPLE_FLAGS_PRESENT = 0x00020;
932 const uint32_t TFHD_FLAG_DURATION_IS_EMPTY = 0x10000;
933 const uint32_t TFHD_FLAG_DEFAULT_BASE_IS_MOOF = 0x20000;
936 uint32_t BaseDataOffset;
937 uint32_t SampleDescriptionIndex;
938 uint32_t DefaultSampleDuration;
939 uint32_t DefaultSampleSize;
940 uint32_t DefaultSampleFlags;
942 TrackId = READ_U32(ptr);
943 if (flags & TFHD_FLAG_BASE_DATA_OFFSET_PRESENT) {
944 BaseDataOffset = READ_64(ptr);
948 if (flags & TFHD_FLAG_SAMPLE_DESCRIPTION_INDEX_PRESENT) {
949 SampleDescriptionIndex = READ_U32(ptr);
951 SampleDescriptionIndex = 1;
953 if (flags & TFHD_FLAG_DEFAULT_SAMPLE_DURATION_PRESENT) {
954 DefaultSampleDuration = READ_U32(ptr);
956 DefaultSampleDuration = 0;
958 if (flags & TFHD_FLAG_DEFAULT_SAMPLE_SIZE_PRESENT) {
959 DefaultSampleSize = READ_U32(ptr);
961 DefaultSampleSize = 0;
963 if (flags & TFHD_FLAG_DEFAULT_SAMPLE_FLAGS_PRESENT) {
964 DefaultSampleFlags = READ_U32(ptr);
966 DefaultSampleFlags = 0;
976 FullBox fbox(sz, Box::TFHD, version, flags);
977 return new TfhdBox(fbox, DefaultSampleDuration);
983 PrftBox::PrftBox(uint32_t sz, uint32_t trackId, uint64_t ntpTs, uint64_t mediaTime) :
FullBox(sz,
Box::PRFT, 0, 0), track_id(trackId), ntp_ts(ntpTs), media_time(mediaTime)
1033 media_time = mediaTime;
1049 uint8_t version = READ_VERSION(ptr);
1050 uint32_t flags = READ_FLAGS(ptr);
1052 uint32_t track_id = 0;
1053 track_id = READ_U32(ptr);
1054 uint64_t ntp_ts = 0;
1055 ntp_ts = READ_64(ptr);
1061 FullBox fbox(sz, Box::PRFT, version, flags);
1063 return new PrftBox(fbox, track_id, ntp_ts, pts);
1076 uint32_t curOffset =
sizeof(uint32_t) +
sizeof(uint32_t);
1077 while (curOffset < sz)
1082 if (IS_TYPE(box->
getType(),TKHD))
1084 uint8_t *tkhd_start = ptr;
1086 uint8_t version = READ_VERSION(ptr);
1090 skip +=
sizeof(uint64_t) * 2;
1094 skip +=
sizeof(uint32_t) * 2;
1097 if(-1 != newTrackId)
1099 WRITE_U32(ptr, newTrackId);
1101 cbox->track_id = READ_U32(ptr);