mistserver/lib/mp4.cpp
2013-03-04 22:02:10 +01:00

2083 lines
60 KiB
C++

#include <stdlib.h> //for malloc and free
#include <string.h> //for memcpy
#include <arpa/inet.h> //for htonl and friends
#include "mp4.h"
#include "json.h"
#define Int64 uint64_t
/// Contains all MP4 format related code.
namespace MP4 {
/// Creates a new box, optionally using the indicated pointer for storage.
/// If manage is set to true, the pointer will be realloc'ed when the box needs to be resized.
/// If the datapointer is NULL, manage is assumed to be true even if explicitly given as false.
/// If managed, the pointer will be free'd upon destruction.
Box::Box(char * datapointer, bool manage){
data = datapointer;
managed = manage;
payloadOffset = 8;
if (data == 0){
clear();
}else{
data_size = ntohl(((int*)data)[0]);
}
}
/// If managed, this will free the data pointer.
Box::~Box(){
if (managed && data != 0){
free(data);
data = 0;
}
}
/// Returns the values at byte positions 4 through 7.
std::string Box::getType(){
return std::string(data + 4, 4);
}
/// Returns true if the given 4-byte boxtype is equal to the values at byte positions 4 through 7.
bool Box::isType(const char* boxType){
return !memcmp(boxType, data + 4, 4);
}
/// Reads out a whole box (if possible) from newData, copying to the internal data storage and removing from the input string.
/// \returns True on success, false otherwise.
bool Box::read(std::string & newData){
if ( !managed){
return false;
}
if (newData.size() > 4){
payloadOffset = 8;
uint64_t size = ntohl(((int*)newData.c_str())[0]);
if (size == 1){
if (newData.size() > 16){
size = 0 + ntohl(((int*)newData.c_str())[2]);
size <<= 32;
size += ntohl(((int*)newData.c_str())[3]);
payloadOffset = 16;
}else{
return false;
}
}
if (newData.size() >= size){
void * ret = malloc(size);
if ( !ret){
return false;
}
free(data);
data = (char*)ret;
memcpy(data, newData.c_str(), size);
newData.erase(0, size);
return true;
}
}
return false;
}
/// Returns the total boxed size of this box, including the header.
uint64_t Box::boxedSize(){
if (payloadOffset == 16){
return ((uint64_t)ntohl(((int*)data)[2]) << 32) + ntohl(((int*)data)[3]);
}
return ntohl(((int*)data)[0]);
}
/// Retruns the size of the payload of thix box, excluding the header.
/// This value is defined as boxedSize() - 8.
uint64_t Box::payloadSize(){
return boxedSize() - payloadOffset;
}
/// Returns a copy of the data pointer.
char * Box::asBox(){
return data;
}
char * Box::payload(){
return data + payloadOffset;
}
/// Makes this box managed if it wasn't already, resetting the internal storage to 8 bytes (the minimum).
/// If this box wasn't managed, the original data is left intact - otherwise it is free'd.
/// If it was somehow impossible to allocate 8 bytes (should never happen), this will cause segfaults later.
void Box::clear(){
if (data && managed){
free(data);
}
managed = true;
payloadOffset = 8;
data = (char*)malloc(8);
if (data){
data_size = 8;
((int*)data)[0] = htonl(data_size);
}else{
data_size = 0;
}
}
/// Attempts to typecast this Box to a more specific type and call the toPrettyString() function of that type.
/// If this failed, it will print out a message saying pretty-printing is not implemented for <boxtype>.
std::string Box::toPrettyString(int indent){
switch (ntohl( *((int*)(data + 4)))){ //type is at this address
case 0x6D666864:
return ((MFHD*)this)->toPrettyString(indent);
break;
case 0x6D6F6F66:
return ((MOOF*)this)->toPrettyString(indent);
break;
case 0x61627374:
return ((ABST*)this)->toPrettyString(indent);
break;
case 0x61667274:
return ((AFRT*)this)->toPrettyString(indent);
break;
case 0x61667261:
return ((AFRA*)this)->toPrettyString(indent);
break;
case 0x61737274:
return ((ASRT*)this)->toPrettyString(indent);
break;
case 0x7472756E:
return ((TRUN*)this)->toPrettyString(indent);
break;
case 0x74726166:
return ((TRAF*)this)->toPrettyString(indent);
break;
case 0x74666864:
return ((TFHD*)this)->toPrettyString(indent);
break;
case 0x61766343:
return ((AVCC*)this)->toPrettyString(indent);
break;
default:
break;
}
return std::string(indent, ' ') + "Unimplemented pretty-printing for box " + std::string(data + 4, 4) + "\n";
}
/// Sets the 8 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Fails silently if resizing failed.
void Box::setInt8(char newData, size_t index){
index += payloadOffset;
if (index >= boxedSize()){
if ( !reserve(index, 0, 1)){
return;
}
}
data[index] = newData;
}
/// Gets the 8 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Returns zero if resizing failed.
char Box::getInt8(size_t index){
index += payloadOffset;
if (index >= boxedSize()){
if ( !reserve(index, 0, 1)){
return 0;
}
setInt8(0, index - payloadOffset);
}
return data[index];
}
/// Sets the 16 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Fails silently if resizing failed.
void Box::setInt16(short newData, size_t index){
index += payloadOffset;
if (index + 1 >= boxedSize()){
if ( !reserve(index, 0, 2)){
return;
}
}
newData = htons(newData);
memcpy(data + index, (char*) &newData, 2);
}
/// Gets the 16 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Returns zero if resizing failed.
short Box::getInt16(size_t index){
index += payloadOffset;
if (index + 1 >= boxedSize()){
if ( !reserve(index, 0, 2)){
return 0;
}
setInt16(0, index - payloadOffset);
}
short result;
memcpy((char*) &result, data + index, 2);
return ntohs(result);
}
/// Sets the 24 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Fails silently if resizing failed.
void Box::setInt24(uint32_t newData, size_t index){
index += payloadOffset;
if (index + 2 >= boxedSize()){
if ( !reserve(index, 0, 3)){
return;
}
}
data[index] = (newData & 0x00FF0000) >> 16;
data[index + 1] = (newData & 0x0000FF00) >> 8;
data[index + 2] = (newData & 0x000000FF);
}
/// Gets the 24 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Returns zero if resizing failed.
uint32_t Box::getInt24(size_t index){
index += payloadOffset;
if (index + 2 >= boxedSize()){
if ( !reserve(index, 0, 3)){
return 0;
}
setInt24(0, index - payloadOffset);
}
uint32_t result = data[index];
result <<= 8;
result += data[index + 1];
result <<= 8;
result += data[index + 2];
return result;
}
/// Sets the 32 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Fails silently if resizing failed.
void Box::setInt32(uint32_t newData, size_t index){
index += payloadOffset;
if (index + 3 >= boxedSize()){
if ( !reserve(index, 0, 4)){
return;
}
}
newData = htonl(newData);
memcpy(data + index, (char*) &newData, 4);
}
/// Gets the 32 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Returns zero if resizing failed.
uint32_t Box::getInt32(size_t index){
index += payloadOffset;
if (index + 3 >= boxedSize()){
if ( !reserve(index, 0, 4)){
return 0;
}
setInt32(0, index - payloadOffset);
}
uint32_t result;
memcpy((char*) &result, data + index, 4);
return ntohl(result);
}
/// Sets the 64 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Fails silently if resizing failed.
void Box::setInt64(Int64 newData, size_t index){
index += payloadOffset;
if (index + 7 >= boxedSize()){
if ( !reserve(index, 0, 8)){
return;
}
}
((int*)(data + index))[0] = htonl((int)(newData >> 32));
((int*)(data + index))[1] = htonl((int)(newData & 0xFFFFFFFF));
}
/// Gets the 64 bits integer at the given index.
/// Attempts to resize the data pointer if the index is out of range.
/// Returns zero if resizing failed.
Int64 Box::getInt64(size_t index){
index += payloadOffset;
if (index + 7 >= boxedSize()){
if ( !reserve(index, 0, 8)){
return 0;
}
setInt64(0, index - payloadOffset);
}
Int64 result = ntohl(((int*)(data + index))[0]);
result <<= 32;
result += ntohl(((int*)(data + index))[1]);
return result;
}
/// Sets the NULL-terminated string at the given index.
/// Will attempt to resize if the string doesn't fit.
/// Fails silently if resizing failed.
void Box::setString(std::string newData, size_t index){
setString((char*)newData.c_str(), newData.size(), index);
}
/// Sets the NULL-terminated string at the given index.
/// Will attempt to resize if the string doesn't fit.
/// Fails silently if resizing failed.
void Box::setString(char* newData, size_t size, size_t index){
index += payloadOffset;
if (index >= boxedSize()){
if ( !reserve(index, 0, 1)){
return;
}
data[index] = 0;
}
if (getStringLen(index) != size){
if ( !reserve(index, getStringLen(index) + 1, size + 1)){
return;
}
}
memcpy(data + index, newData, size + 1);
}
/// Gets the NULL-terminated string at the given index.
/// Will attempt to resize if the string is out of range.
/// Returns null if resizing failed.
char * Box::getString(size_t index){
index += payloadOffset;
if (index >= boxedSize()){
if ( !reserve(index, 0, 1)){
return 0;
}
data[index] = 0;
}
return data + index;
}
/// Returns the length of the NULL-terminated string at the given index.
/// Returns 0 if out of range.
size_t Box::getStringLen(size_t index){
index += payloadOffset;
if (index >= boxedSize()){
return 0;
}
return strlen(data + index);
}
/// Gets a reference to the box at the given index.
/// Do not store or copy this reference, for there will be raptors.
/// Will attempt to resize if out of range.
/// Returns an 8-byte error box if resizing failed.
Box & Box::getBox(size_t index){
static Box retbox;
index += payloadOffset;
if (index + 8 > boxedSize()){
if ( !reserve(index, 0, 8)){
retbox = Box((char*)"\000\000\000\010erro", false);
return retbox;
}
memcpy(data + index, "\000\000\000\010erro", 8);
}
retbox = Box(data + index, false);
return retbox;
}
/// Returns the size of the box at the given position.
/// Returns undefined values if there is no box at the given position.
/// Returns 0 if out of range.
size_t Box::getBoxLen(size_t index){
if (index + payloadOffset + 8 > boxedSize()){
return 0;
}
return getBox(index).boxedSize();
}
/// Replaces the existing box at the given index by the new box newEntry.
/// Will resize if needed, will reserve new space if out of range.
void Box::setBox(Box & newEntry, size_t index){
int oldlen = getBoxLen(index);
int newlen = newEntry.boxedSize();
if (oldlen != newlen && !reserve(index + payloadOffset, oldlen, newlen)){
return;
}
memcpy(data + index + payloadOffset, newEntry.asBox(), newlen);
}
/// Attempts to reserve enough space for wanted bytes of data at given position, where current bytes of data is now reserved.
/// This will move any existing data behind the currently reserved space to the proper location after reserving.
/// \returns True on success, false otherwise.
bool Box::reserve(size_t position, size_t current, size_t wanted){
if (current == wanted){
return true;
}
if (position > boxedSize()){
wanted += position - boxedSize();
}
if (current < wanted){
//make bigger
if (boxedSize() + (wanted - current) > data_size){
//realloc if managed, otherwise fail
if ( !managed){
return false;
}
void * ret = realloc(data, boxedSize() + (wanted - current));
if ( !ret){
return false;
}
data = (char*)ret;
memset(data + boxedSize(), 0, wanted - current); //initialize to 0
data_size = boxedSize() + (wanted - current);
}
}
//move data behind, if any
if (boxedSize() > (position + current)){
memmove(data + position + wanted, data + position + current, boxedSize() - (position + current));
}
//calculate and set new size
if (payloadOffset != 16){
int newSize = boxedSize() + (wanted - current);
((int*)data)[0] = htonl(newSize);
}
return true;
}
ABST::ABST(){
memcpy(data + 4, "abst", 4);
setVersion(0);
setFlags(0);
setBootstrapinfoVersion(0);
setProfile(0);
setLive(1);
setUpdate(0);
setTimeScale(1000);
setCurrentMediaTime(0);
setSmpteTimeCodeOffset(0);
std::string empty;
setMovieIdentifier(empty);
setInt8(0, 30); //set serverentrycount to 0
setInt8(0, 31); //set qualityentrycount to 0
setDrmData(empty);
setMetaData(empty);
}
void ABST::setVersion(char newVersion){
setInt8(newVersion, 0);
}
char ABST::getVersion(){
return getInt8(0);
}
void ABST::setFlags(uint32_t newFlags){
setInt24(newFlags, 1);
}
uint32_t ABST::getFlags(){
return getInt24(1);
}
void ABST::setBootstrapinfoVersion(uint32_t newVersion){
setInt32(newVersion, 4);
}
uint32_t ABST::getBootstrapinfoVersion(){
return getInt32(4);
}
void ABST::setProfile(char newProfile){
//profile = bit 1 and 2 of byte 8.
setInt8((getInt8(8) & 0x3F) + ((newProfile & 0x03) << 6), 8);
}
char ABST::getProfile(){
return (getInt8(8) & 0xC0);
}
;
void ABST::setLive(bool newLive){
//live = bit 4 of byte 8.
setInt8((getInt8(8) & 0xDF) + (newLive ? 0x10 : 0), 8);
}
bool ABST::getLive(){
return (getInt8(8) & 0x10);
}
void ABST::setUpdate(bool newUpdate){
//update = bit 5 of byte 8.
setInt8((getInt8(8) & 0xEF) + (newUpdate ? 0x08 : 0), 8);
}
bool ABST::getUpdate(){
return (getInt8(8) & 0x08);
}
void ABST::setTimeScale(uint32_t newScale){
setInt32(newScale, 9);
}
uint32_t ABST::getTimeScale(){
return getInt32(9);
}
void ABST::setCurrentMediaTime(Int64 newTime){
setInt64(newTime, 13);
}
Int64 ABST::getCurrentMediaTime(){
return getInt64(13);
}
void ABST::setSmpteTimeCodeOffset(Int64 newTime){
setInt64(newTime, 21);
}
Int64 ABST::getSmpteTimeCodeOffset(){
return getInt64(21);
}
void ABST::setMovieIdentifier(std::string & newIdentifier){
setString(newIdentifier, 29);
}
char* ABST::getMovieIdentifier(){
return getString(29);
}
uint32_t ABST::getServerEntryCount(){
int countLoc = 29 + getStringLen(29) + 1;
return getInt8(countLoc);
}
void ABST::setServerEntry(std::string & newEntry, uint32_t no){
int countLoc = 29 + getStringLen(29) + 1;
int tempLoc = countLoc + 1;
//attempt to reach the wanted position
int i;
for (i = 0; i < getInt8(countLoc) && i < no; ++i){
tempLoc += getStringLen(tempLoc) + 1;
}
//we are now either at the end, or at the right position
//let's reserve any unreserved space...
if (no + 1 > getInt8(countLoc)){
int amount = no + 1 - getInt8(countLoc);
if ( !reserve(payloadOffset + tempLoc, 0, amount)){
return;
};
memset(data + payloadOffset + tempLoc, 0, amount);
setInt8(no + 1, countLoc); //set new qualityEntryCount
tempLoc += no - i;
}
//now, tempLoc is at position for string number no, and we have at least 1 byte reserved.
setString(newEntry, tempLoc);
}
///\return Empty string if no > serverEntryCount(), serverEntry[no] otherwise.
const char* ABST::getServerEntry(uint32_t no){
if (no + 1 > getServerEntryCount()){
return "";
}
int tempLoc = 29 + getStringLen(29) + 1 + 1; //position of first entry
for (int i = 0; i < no; i++){
tempLoc += getStringLen(tempLoc) + 1;
}
return getString(tempLoc);
}
uint32_t ABST::getQualityEntryCount(){
int countLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
countLoc += getStringLen(countLoc) + 1;
}
return getInt8(countLoc);
}
void ABST::setQualityEntry(std::string & newEntry, uint32_t no){
int countLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
countLoc += getStringLen(countLoc) + 1;
}
int tempLoc = countLoc + 1;
//attempt to reach the wanted position
int i;
for (i = 0; i < getInt8(countLoc) && i < no; ++i){
tempLoc += getStringLen(tempLoc) + 1;
}
//we are now either at the end, or at the right position
//let's reserve any unreserved space...
if (no + 1 > getInt8(countLoc)){
int amount = no + 1 - getInt8(countLoc);
if ( !reserve(payloadOffset + tempLoc, 0, amount)){
return;
};
memset(data + payloadOffset + tempLoc, 0, amount);
setInt8(no + 1, countLoc); //set new qualityEntryCount
tempLoc += no - i;
}
//now, tempLoc is at position for string number no, and we have at least 1 byte reserved.
setString(newEntry, tempLoc);
}
const char* ABST::getQualityEntry(uint32_t no){
if (no > getQualityEntryCount()){
return "";
}
int tempLoc = 29 + getStringLen(29) + 1 + 1; //position of serverentries;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc += 1; //first qualityentry
for (int i = 0; i < no; i++){
tempLoc += getStringLen(tempLoc) + 1;
}
return getString(tempLoc);
}
void ABST::setDrmData(std::string newDrm){
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
setString(newDrm, tempLoc);
}
char* ABST::getDrmData(){
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
return getString(tempLoc);
}
void ABST::setMetaData(std::string newMetaData){
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc += getStringLen(tempLoc) + 1;
setString(newMetaData, tempLoc);
}
char* ABST::getMetaData(){
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc += getStringLen(tempLoc) + 1;
return getString(tempLoc);
}
uint32_t ABST::getSegmentRunTableCount(){
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc += getStringLen(tempLoc) + 1; //DrmData
tempLoc += getStringLen(tempLoc) + 1; //MetaData
return getInt8(tempLoc);
}
void ABST::setSegmentRunTable(ASRT & newSegment, uint32_t no){
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc += getStringLen(tempLoc) + 1; //DrmData
tempLoc += getStringLen(tempLoc) + 1; //MetaData
int countLoc = tempLoc;
tempLoc++; //skip segmentRuntableCount
//attempt to reach the wanted position
int i;
for (i = 0; i < getInt8(countLoc) && i < no; ++i){
tempLoc += getBoxLen(tempLoc);
}
//we are now either at the end, or at the right position
//let's reserve any unreserved space...
if (no + 1 > getInt8(countLoc)){
int amount = no + 1 - getInt8(countLoc);
if ( !reserve(payloadOffset + tempLoc, 0, amount * 8)){
return;
};
//set empty erro boxes as contents
for (int j = 0; j < amount; ++j){
memcpy(data + payloadOffset + tempLoc + j * 8, "\000\000\000\010erro", 8);
}
setInt8(no + 1, countLoc); //set new count
tempLoc += (no - i) * 8;
}
//now, tempLoc is at position for string number no, and we have at least an erro box reserved.
setBox(newSegment, tempLoc);
}
ASRT & ABST::getSegmentRunTable(uint32_t no){
static Box result;
if (no > getSegmentRunTableCount()){
static Box res;
return (ASRT&)res;
}
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc += getStringLen(tempLoc) + 1; //DrmData
tempLoc += getStringLen(tempLoc) + 1; //MetaData
int countLoc = tempLoc;
tempLoc++; //segmentRuntableCount
for (int i = 0; i < no; ++i){
tempLoc += getBoxLen(tempLoc);
}
return (ASRT&)getBox(tempLoc);
}
uint32_t ABST::getFragmentRunTableCount(){
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc += getStringLen(tempLoc) + 1; //DrmData
tempLoc += getStringLen(tempLoc) + 1; //MetaData
for (int i = getInt8(tempLoc++); i != 0; --i){
tempLoc += getBoxLen(tempLoc);
}
return getInt8(tempLoc);
}
void ABST::setFragmentRunTable(AFRT & newFragment, uint32_t no){
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc += getStringLen(tempLoc) + 1; //DrmData
tempLoc += getStringLen(tempLoc) + 1; //MetaData
for (int i = getInt8(tempLoc++); i != 0; --i){
tempLoc += getBoxLen(tempLoc);
}
int countLoc = tempLoc;
tempLoc++;
//attempt to reach the wanted position
int i;
for (i = 0; i < getInt8(countLoc) && i < no; ++i){
tempLoc += getBoxLen(tempLoc);
}
//we are now either at the end, or at the right position
//let's reserve any unreserved space...
if (no + 1 > getInt8(countLoc)){
int amount = no + 1 - getInt8(countLoc);
if ( !reserve(payloadOffset + tempLoc, 0, amount * 8)){
return;
};
//set empty erro boxes as contents
for (int j = 0; j < amount; ++j){
memcpy(data + payloadOffset + tempLoc + j * 8, "\000\000\000\010erro", 8);
}
setInt8(no + 1, countLoc); //set new count
tempLoc += (no - i) * 8;
}
//now, tempLoc is at position for string number no, and we have at least 1 byte reserved.
setBox(newFragment, tempLoc);
}
AFRT & ABST::getFragmentRunTable(uint32_t no){
static Box result;
if (no >= getFragmentRunTableCount()){
static Box res;
return (AFRT&)res;
}
uint32_t tempLoc = 29 + getStringLen(29) + 1 + 1;
for (int i = 0; i < getServerEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc++;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
tempLoc += getStringLen(tempLoc) + 1; //DrmData
tempLoc += getStringLen(tempLoc) + 1; //MetaData
for (int i = getInt8(tempLoc++); i != 0; --i){
tempLoc += getBoxLen(tempLoc);
}
int countLoc = tempLoc;
tempLoc++;
for (int i = 0; i < no; i++){
tempLoc += getBoxLen(tempLoc);
}
return (AFRT&)getBox(tempLoc);
}
std::string ABST::toPrettyString(uint32_t indent){
std::stringstream r;
r << std::string(indent, ' ') << "[abst] Bootstrap Info (" << boxedSize() << ")" << std::endl;
r << std::string(indent + 1, ' ') << "Version " << (int)getVersion() << std::endl;
r << std::string(indent + 1, ' ') << "BootstrapinfoVersion " << getBootstrapinfoVersion() << std::endl;
r << std::string(indent + 1, ' ') << "Profile " << (int)getProfile() << std::endl;
if (getLive()){
r << std::string(indent + 1, ' ') << "Live" << std::endl;
}else{
r << std::string(indent + 1, ' ') << "Recorded" << std::endl;
}
if (getUpdate()){
r << std::string(indent + 1, ' ') << "Update" << std::endl;
}else{
r << std::string(indent + 1, ' ') << "Replacement or new table" << std::endl;
}
r << std::string(indent + 1, ' ') << "Timescale " << getTimeScale() << std::endl;
r << std::string(indent + 1, ' ') << "CurrMediaTime " << getCurrentMediaTime() << std::endl;
r << std::string(indent + 1, ' ') << "SmpteTimeCodeOffset " << getSmpteTimeCodeOffset() << std::endl;
r << std::string(indent + 1, ' ') << "MovieIdentifier " << getMovieIdentifier() << std::endl;
r << std::string(indent + 1, ' ') << "ServerEntryTable (" << getServerEntryCount() << ")" << std::endl;
for (int i = 0; i < getServerEntryCount(); i++){
r << std::string(indent + 2, ' ') << i << ": " << getServerEntry(i) << std::endl;
}
r << std::string(indent + 1, ' ') << "QualityEntryTable (" << getQualityEntryCount() << ")" << std::endl;
for (int i = 0; i < getQualityEntryCount(); i++){
r << std::string(indent + 2, ' ') << i << ": " << getQualityEntry(i) << std::endl;
}
r << std::string(indent + 1, ' ') << "DrmData " << getDrmData() << std::endl;
r << std::string(indent + 1, ' ') << "MetaData " << getMetaData() << std::endl;
r << std::string(indent + 1, ' ') << "SegmentRunTableEntries (" << getSegmentRunTableCount() << ")" << std::endl;
for (uint32_t i = 0; i < getSegmentRunTableCount(); i++){
r << ((Box)getSegmentRunTable(i)).toPrettyString(indent + 2);
}
r << std::string(indent + 1, ' ') + "FragmentRunTableEntries (" << getFragmentRunTableCount() << ")" << std::endl;
for (uint32_t i = 0; i < getFragmentRunTableCount(); i++){
r << ((Box)getFragmentRunTable(i)).toPrettyString(indent + 2);
}
return r.str();
}
AFRT::AFRT(){
memcpy(data + 4, "afrt", 4);
setVersion(0);
setUpdate(0);
setTimeScale(1000);
}
void AFRT::setVersion(char newVersion){
setInt8(newVersion, 0);
}
uint32_t AFRT::getVersion(){
return getInt8(0);
}
void AFRT::setUpdate(uint32_t newUpdate){
setInt24(newUpdate, 1);
}
uint32_t AFRT::getUpdate(){
return getInt24(1);
}
void AFRT::setTimeScale(uint32_t newScale){
setInt32(newScale, 4);
}
uint32_t AFRT::getTimeScale(){
return getInt32(4);
}
uint32_t AFRT::getQualityEntryCount(){
return getInt8(8);
}
void AFRT::setQualityEntry(std::string & newEntry, uint32_t no){
int countLoc = 8;
int tempLoc = countLoc + 1;
//attempt to reach the wanted position
int i;
for (i = 0; i < getQualityEntryCount() && i < no; ++i){
tempLoc += getStringLen(tempLoc) + 1;
}
//we are now either at the end, or at the right position
//let's reserve any unreserved space...
if (no + 1 > getQualityEntryCount()){
int amount = no + 1 - getQualityEntryCount();
if ( !reserve(payloadOffset + tempLoc, 0, amount)){
return;
};
memset(data + payloadOffset + tempLoc, 0, amount);
setInt8(no + 1, countLoc); //set new qualityEntryCount
tempLoc += no - i;
}
//now, tempLoc is at position for string number no, and we have at least 1 byte reserved.
setString(newEntry, tempLoc);
}
const char* AFRT::getQualityEntry(uint32_t no){
if (no + 1 > getQualityEntryCount()){
return "";
}
int tempLoc = 9; //position of first quality entry
for (int i = 0; i < no; i++){
tempLoc += getStringLen(tempLoc) + 1;
}
return getString(tempLoc);
}
uint32_t AFRT::getFragmentRunCount(){
int tempLoc = 9;
for (int i = 0; i < getQualityEntryCount(); ++i){
tempLoc += getStringLen(tempLoc) + 1;
}
return getInt32(tempLoc);
}
void AFRT::setFragmentRun(afrt_runtable newRun, uint32_t no){
int tempLoc = 9;
for (int i = 0; i < getQualityEntryCount(); ++i){
tempLoc += getStringLen(tempLoc) + 1;
}
int countLoc = tempLoc;
tempLoc += 4;
for (int i = 0; i < no; i++){
if (i + 1 > getInt32(countLoc)){
setInt32(0, tempLoc);
setInt64(0, tempLoc + 4);
setInt32(1, tempLoc + 12);
}
if (getInt32(tempLoc + 12) == 0){
tempLoc += 17;
}else{
tempLoc += 16;
}
}
setInt32(newRun.firstFragment, tempLoc);
setInt64(newRun.firstTimestamp, tempLoc + 4);
setInt32(newRun.duration, tempLoc + 12);
if (newRun.duration == 0){
setInt8(newRun.discontinuity, tempLoc + 16);
}
if (getInt32(countLoc) < no + 1){
setInt32(no + 1, countLoc);
}
}
afrt_runtable AFRT::getFragmentRun(uint32_t no){
afrt_runtable res;
if (no > getFragmentRunCount()){
return res;
}
int tempLoc = 9;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
int countLoc = tempLoc;
tempLoc += 4;
for (int i = 0; i < no; i++){
if (getInt32(tempLoc + 12) == 0){
tempLoc += 17;
}else{
tempLoc += 16;
}
}
res.firstFragment = getInt32(tempLoc);
res.firstTimestamp = getInt64(tempLoc + 4);
res.duration = getInt32(tempLoc + 12);
if (res.duration){
res.discontinuity = getInt8(tempLoc + 16);
}else{
res.discontinuity = 0;
}
return res;
}
std::string AFRT::toPrettyString(int indent){
std::stringstream r;
r << std::string(indent, ' ') << "[afrt] Fragment Run Table (" << boxedSize() << ")" << std::endl;
r << std::string(indent + 1, ' ') << "Version " << (int)getVersion() << std::endl;
if (getUpdate()){
r << std::string(indent + 1, ' ') << "Update" << std::endl;
}else{
r << std::string(indent + 1, ' ') << "Replacement or new table" << std::endl;
}
r << std::string(indent + 1, ' ') << "Timescale " << getTimeScale() << std::endl;
r << std::string(indent + 1, ' ') << "QualitySegmentUrlModifiers (" << getQualityEntryCount() << ")" << std::endl;
for (int i = 0; i < getQualityEntryCount(); i++){
r << std::string(indent + 2, ' ') << i << ": " << getQualityEntry(i) << std::endl;
}
r << std::string(indent + 1, ' ') << "FragmentRunEntryTable (" << getFragmentRunCount() << ")" << std::endl;
for (int i = 0; i < getFragmentRunCount(); i++){
afrt_runtable myRun = getFragmentRun(i);
if (myRun.duration){
r << std::string(indent + 2, ' ') << i << ": " << myRun.firstFragment << " is at " << ((double)myRun.firstTimestamp / (double)getTimeScale())
<< "s, " << ((double)myRun.duration / (double)getTimeScale()) << "s per fragment." << std::endl;
}else{
r << std::string(indent + 2, ' ') << i << ": " << myRun.firstFragment << " is at " << ((double)myRun.firstTimestamp / (double)getTimeScale())
<< "s, discontinuity type " << myRun.discontinuity << std::endl;
}
}
return r.str();
}
ASRT::ASRT(){
memcpy(data + 4, "asrt", 4);
setVersion(0);
setUpdate(0);
}
void ASRT::setVersion(char newVersion){
setInt8(newVersion, 0);
}
uint32_t ASRT::getVersion(){
return getInt8(0);
}
void ASRT::setUpdate(uint32_t newUpdate){
setInt24(newUpdate, 1);
}
uint32_t ASRT::getUpdate(){
return getInt24(1);
}
uint32_t ASRT::getQualityEntryCount(){
return getInt8(4);
}
void ASRT::setQualityEntry(std::string & newEntry, uint32_t no){
int countLoc = 4;
int tempLoc = countLoc + 1;
//attempt to reach the wanted position
int i;
for (i = 0; i < getQualityEntryCount() && i < no; ++i){
tempLoc += getStringLen(tempLoc) + 1;
}
//we are now either at the end, or at the right position
//let's reserve any unreserved space...
if (no + 1 > getQualityEntryCount()){
int amount = no + 1 - getQualityEntryCount();
if ( !reserve(payloadOffset + tempLoc, 0, amount)){
return;
};
memset(data + payloadOffset + tempLoc, 0, amount);
setInt8(no + 1, countLoc); //set new qualityEntryCount
tempLoc += no - i;
}
//now, tempLoc is at position for string number no, and we have at least 1 byte reserved.
setString(newEntry, tempLoc);
}
const char* ASRT::getQualityEntry(uint32_t no){
if (no > getQualityEntryCount()){
return "";
}
int tempLoc = 5; //position of qualityentry count;
for (int i = 0; i < no; i++){
tempLoc += getStringLen(tempLoc) + 1;
}
return getString(tempLoc);
}
uint32_t ASRT::getSegmentRunEntryCount(){
int tempLoc = 5; //position of qualityentry count;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
return getInt32(tempLoc);
}
void ASRT::setSegmentRun(uint32_t firstSegment, uint32_t fragmentsPerSegment, uint32_t no){
int tempLoc = 5; //position of qualityentry count;
for (int i = 0; i < getQualityEntryCount(); i++){
tempLoc += getStringLen(tempLoc) + 1;
}
int countLoc = tempLoc;
tempLoc += 4 + no * 8;
if (no + 1 > getInt32(countLoc)){
setInt32(no + 1, countLoc); //set new qualityEntryCount
}
setInt32(firstSegment, tempLoc);
setInt32(fragmentsPerSegment, tempLoc + 4);
}
asrt_runtable ASRT::getSegmentRun(uint32_t no){
asrt_runtable res;
if (no >= getSegmentRunEntryCount()){
return res;
}
int tempLoc = 5; //position of qualityentry count;
for (int i = 0; i < getQualityEntryCount(); ++i){
tempLoc += getStringLen(tempLoc) + 1;
}
int countLoc = tempLoc;
tempLoc += 4 + 8 * no;
res.firstSegment = getInt32(tempLoc);
res.fragmentsPerSegment = getInt32(tempLoc + 4);
return res;
}
std::string ASRT::toPrettyString(int indent){
std::stringstream r;
r << std::string(indent, ' ') << "[asrt] Segment Run Table (" << boxedSize() << ")" << std::endl;
r << std::string(indent + 1, ' ') << "Version " << getVersion() << std::endl;
if (getUpdate()){
r << std::string(indent + 1, ' ') << "Update" << std::endl;
}else{
r << std::string(indent + 1, ' ') << "Replacement or new table" << std::endl;
}
r << std::string(indent + 1, ' ') << "QualityEntryTable (" << getQualityEntryCount() << ")" << std::endl;
for (int i = 0; i < getQualityEntryCount(); i++){
r << std::string(indent + 2, ' ') << i << ": " << getQualityEntry(i) << std::endl;
}
r << std::string(indent + 1, ' ') << "SegmentRunEntryTable (" << getSegmentRunEntryCount() << ")" << std::endl;
for (int i = 0; i < getSegmentRunEntryCount(); i++){
r << std::string(indent + 2, ' ') << i << ": First=" << getSegmentRun(i).firstSegment << ", FragmentsPerSegment="
<< getSegmentRun(i).fragmentsPerSegment << std::endl;
}
return r.str();
}
MFHD::MFHD(){
memcpy(data + 4, "mfhd", 4);
setInt32(0, 0);
}
void MFHD::setSequenceNumber(uint32_t newSequenceNumber){
setInt32(newSequenceNumber, 4);
}
uint32_t MFHD::getSequenceNumber(){
return getInt32(4);
}
std::string MFHD::toPrettyString(int indent){
std::stringstream r;
r << std::string(indent, ' ') << "[mfhd] Movie Fragment Header (" << boxedSize() << ")" << std::endl;
r << std::string(indent + 1, ' ') << "SequenceNumber " << getSequenceNumber() << std::endl;
return r.str();
}
MOOF::MOOF(){
memcpy(data + 4, "moof", 4);
}
uint32_t MOOF::getContentCount(){
int res = 0;
int tempLoc = 0;
while (tempLoc < boxedSize() - 8){
res++;
tempLoc += getBoxLen(tempLoc);
}
return res;
}
void MOOF::setContent(Box & newContent, uint32_t no){
int tempLoc = 0;
int contentCount = getContentCount();
for (int i = 0; i < no; i++){
if (i < contentCount){
tempLoc += getBoxLen(tempLoc);
}else{
if ( !reserve(tempLoc, 0, (no - contentCount) * 8)){
return;
};
memset(data + tempLoc, 0, (no - contentCount) * 8);
tempLoc += (no - contentCount) * 8;
break;
}
}
setBox(newContent, tempLoc);
}
Box & MOOF::getContent(uint32_t no){
static Box ret = Box((char*)"\000\000\000\010erro", false);
if (no > getContentCount()){
return ret;
}
int i = 0;
int tempLoc = 0;
while (i < no){
tempLoc += getBoxLen(tempLoc);
i++;
}
return getBox(tempLoc);
}
std::string MOOF::toPrettyString(int indent){
std::stringstream r;
r << std::string(indent, ' ') << "[moof] Movie Fragment Box (" << boxedSize() << ")" << std::endl;
Box curBox;
int tempLoc = 0;
int contentCount = getContentCount();
for (int i = 0; i < contentCount; i++){
curBox = getContent(i);
r << curBox.toPrettyString(indent + 1);
tempLoc += getBoxLen(tempLoc);
}
return r.str();
}
TRAF::TRAF(){
memcpy(data + 4, "traf", 4);
}
uint32_t TRAF::getContentCount(){
int res = 0;
int tempLoc = 0;
while (tempLoc < boxedSize() - 8){
res++;
tempLoc += getBoxLen(tempLoc);
}
return res;
}
void TRAF::setContent(Box & newContent, uint32_t no){
int tempLoc = 0;
int contentCount = getContentCount();
for (int i = 0; i < no; i++){
if (i < contentCount){
tempLoc += getBoxLen(tempLoc);
}else{
if ( !reserve(tempLoc, 0, (no - contentCount) * 8)){
return;
};
memset(data + tempLoc, 0, (no - contentCount) * 8);
tempLoc += (no - contentCount) * 8;
break;
}
}
setBox(newContent, tempLoc);
}
Box & TRAF::getContent(uint32_t no){
static Box ret = Box((char*)"\000\000\000\010erro", false);
if (no > getContentCount()){
return ret;
}
int i = 0;
int tempLoc = 0;
while (i < no){
tempLoc += getBoxLen(tempLoc);
i++;
}
return getBox(tempLoc);
}
std::string TRAF::toPrettyString(int indent){
std::stringstream r;
r << std::string(indent, ' ') << "[traf] Track Fragment Box (" << boxedSize() << ")" << std::endl;
Box curBox;
int tempLoc = 0;
int contentCount = getContentCount();
for (int i = 0; i < contentCount; i++){
curBox = getContent(i);
r << curBox.toPrettyString(indent + 1);
tempLoc += curBox.boxedSize();
}
return r.str();
}
TRUN::TRUN(){
memcpy(data + 4, "trun", 4);
}
void TRUN::setFlags(uint32_t newFlags){
setInt24(newFlags, 1);
}
uint32_t TRUN::getFlags(){
return getInt24(1);
}
void TRUN::setDataOffset(uint32_t newOffset){
if (getFlags() & trundataOffset){
setInt32(newOffset, 8);
}
}
uint32_t TRUN::getDataOffset(){
if (getFlags() & trundataOffset){
return getInt32(8);
}else{
return 0;
}
}
void TRUN::setFirstSampleFlags(uint32_t newSampleFlags){
if ( !(getFlags() & trunfirstSampleFlags)){
return;
}
if (getFlags() & trundataOffset){
setInt32(newSampleFlags, 12);
}else{
setInt32(newSampleFlags, 8);
}
}
uint32_t TRUN::getFirstSampleFlags(){
if ( !(getFlags() & trunfirstSampleFlags)){
return 0;
}
if (getFlags() & trundataOffset){
return getInt32(12);
}else{
return getInt32(8);
}
}
uint32_t TRUN::getSampleInformationCount(){
return getInt32(4);
}
void TRUN::setSampleInformation(trunSampleInformation newSample, uint32_t no){
uint32_t flags = getFlags();
uint32_t sampInfoSize = 0;
if (flags & trunsampleDuration){
sampInfoSize += 4;
}
if (flags & trunsampleSize){
sampInfoSize += 4;
}
if (flags & trunsampleFlags){
sampInfoSize += 4;
}
if (flags & trunsampleOffsets){
sampInfoSize += 4;
}
uint32_t offset = 8;
if (flags & trundataOffset){
offset += 4;
}
if (flags & trunfirstSampleFlags){
offset += 4;
}
uint32_t innerOffset = 0;
if (flags & trunsampleDuration){
setInt32(newSample.sampleDuration, offset + no * sampInfoSize + innerOffset);
innerOffset += 4;
}
if (flags & trunsampleSize){
setInt32(newSample.sampleSize, offset + no * sampInfoSize + innerOffset);
innerOffset += 4;
}
if (flags & trunsampleFlags){
setInt32(newSample.sampleFlags, offset + no * sampInfoSize + innerOffset);
innerOffset += 4;
}
if (flags & trunsampleOffsets){
setInt32(newSample.sampleOffset, offset + no * sampInfoSize + innerOffset);
innerOffset += 4;
}
if (getSampleInformationCount() < no + 1){
setInt32(no + 1, 4);
}
}
trunSampleInformation TRUN::getSampleInformation(uint32_t no){
trunSampleInformation ret;
ret.sampleDuration = 0;
ret.sampleSize = 0;
ret.sampleFlags = 0;
ret.sampleOffset = 0;
if (getSampleInformationCount() < no + 1){
return ret;
}
uint32_t flags = getFlags();
uint32_t sampInfoSize = 0;
if (flags & trunsampleDuration){
sampInfoSize += 4;
}
if (flags & trunsampleSize){
sampInfoSize += 4;
}
if (flags & trunsampleFlags){
sampInfoSize += 4;
}
if (flags & trunsampleOffsets){
sampInfoSize += 4;
}
uint32_t offset = 8;
if (flags & trundataOffset){
offset += 4;
}
if (flags & trunfirstSampleFlags){
offset += 4;
}
uint32_t innerOffset = 0;
if (flags & trunsampleDuration){
ret.sampleDuration = getInt32(offset + no * sampInfoSize + innerOffset);
innerOffset += 4;
}
if (flags & trunsampleSize){
ret.sampleSize = getInt32(offset + no * sampInfoSize + innerOffset);
innerOffset += 4;
}
if (flags & trunsampleFlags){
ret.sampleFlags = getInt32(offset + no * sampInfoSize + innerOffset);
innerOffset += 4;
}
if (flags & trunsampleOffsets){
ret.sampleOffset = getInt32(offset + no * sampInfoSize + innerOffset);
innerOffset += 4;
}
return ret;
}
std::string TRUN::toPrettyString(uint32_t indent){
std::stringstream r;
r << std::string(indent, ' ') << "[trun] Track Fragment Run (" << boxedSize() << ")" << std::endl;
r << std::string(indent + 1, ' ') << "Version " << (int)getInt8(0) << std::endl;
uint32_t flags = getFlags();
r << std::string(indent + 1, ' ') << "Flags";
if (flags & trundataOffset){
r << " dataOffset";
}
if (flags & trunfirstSampleFlags){
r << " firstSampleFlags";
}
if (flags & trunsampleDuration){
r << " sampleDuration";
}
if (flags & trunsampleSize){
r << " sampleSize";
}
if (flags & trunsampleFlags){
r << " sampleFlags";
}
if (flags & trunsampleOffsets){
r << " sampleOffsets";
}
r << std::endl;
if (flags & trundataOffset){
r << std::string(indent + 1, ' ') << "Data Offset " << getDataOffset() << std::endl;
}
if (flags & trundataOffset){
r << std::string(indent + 1, ' ') << "Sample Flags" << prettySampleFlags(getFirstSampleFlags()) << std::endl;
}
r << std::string(indent + 1, ' ') << "SampleInformation (" << getSampleInformationCount() << "):" << std::endl;
for (int i = 0; i < getSampleInformationCount(); ++i){
r << std::string(indent + 2, ' ') << "[" << i << "]" << std::endl;
trunSampleInformation samp = getSampleInformation(i);
if (flags & trunsampleDuration){
r << std::string(indent + 2, ' ') << "Duration " << samp.sampleDuration << std::endl;
}
if (flags & trunsampleSize){
r << std::string(indent + 2, ' ') << "Size " << samp.sampleSize << std::endl;
}
if (flags & trunsampleFlags){
r << std::string(indent + 2, ' ') << "Flags " << prettySampleFlags(samp.sampleFlags) << std::endl;
}
if (flags & trunsampleOffsets){
r << std::string(indent + 2, ' ') << "Offset " << samp.sampleOffset << std::endl;
}
}
return r.str();
}
std::string prettySampleFlags(uint32_t flag){
std::stringstream r;
if (flag & noIPicture){
r << " noIPicture";
}
if (flag & isIPicture){
r << " isIPicture";
}
if (flag & noDisposable){
r << " noDisposable";
}
if (flag & isDisposable){
r << " isDisposable";
}
if (flag & isRedundant){
r << " isRedundant";
}
if (flag & noRedundant){
r << " noRedundant";
}
if (flag & noKeySample){
r << " noKeySample";
}else{
r << " isKeySample";
}
return r.str();
}
TFHD::TFHD(){
memcpy(data + 4, "tfhd", 4);
}
void TFHD::setFlags(uint32_t newFlags){
setInt24(newFlags, 1);
}
uint32_t TFHD::getFlags(){
return getInt24(1);
}
void TFHD::setTrackID(uint32_t newID){
setInt32(newID, 4);
}
uint32_t TFHD::getTrackID(){
return getInt32(4);
}
void TFHD::setBaseDataOffset(uint64_t newOffset){
if (getFlags() & tfhdBaseOffset){
setInt64(newOffset, 8);
}
}
uint64_t TFHD::getBaseDataOffset(){
if (getFlags() & tfhdBaseOffset){
return getInt64(8);
}else{
return 0;
}
}
void TFHD::setSampleDescriptionIndex(uint32_t newIndex){
if ( !(getFlags() & tfhdSampleDesc)){
return;
}
int offset = 8;
if (getFlags() & tfhdBaseOffset){
offset += 8;
}
setInt32(newIndex, offset);
}
uint32_t TFHD::getSampleDescriptionIndex(){
if ( !(getFlags() & tfhdSampleDesc)){
return 0;
}
int offset = 8;
if (getFlags() & tfhdBaseOffset){
offset += 8;
}
return getInt32(offset);
}
void TFHD::setDefaultSampleDuration(uint32_t newDuration){
if ( !(getFlags() & tfhdSampleDura)){
return;
}
int offset = 8;
if (getFlags() & tfhdBaseOffset){
offset += 8;
}
if (getFlags() & tfhdSampleDesc){
offset += 4;
}
setInt32(newDuration, offset);
}
uint32_t TFHD::getDefaultSampleDuration(){
if ( !(getFlags() & tfhdSampleDura)){
return 0;
}
int offset = 8;
if (getFlags() & tfhdBaseOffset){
offset += 8;
}
if (getFlags() & tfhdSampleDesc){
offset += 4;
}
return getInt32(offset);
}
void TFHD::setDefaultSampleSize(uint32_t newSize){
if ( !(getFlags() & tfhdSampleSize)){
return;
}
int offset = 8;
if (getFlags() & tfhdBaseOffset){
offset += 8;
}
if (getFlags() & tfhdSampleDesc){
offset += 4;
}
if (getFlags() & tfhdSampleDura){
offset += 4;
}
setInt32(newSize, offset);
}
uint32_t TFHD::getDefaultSampleSize(){
if ( !(getFlags() & tfhdSampleSize)){
return 0;
}
int offset = 8;
if (getFlags() & tfhdBaseOffset){
offset += 8;
}
if (getFlags() & tfhdSampleDesc){
offset += 4;
}
if (getFlags() & tfhdSampleDura){
offset += 4;
}
return getInt32(offset);
}
void TFHD::setDefaultSampleFlags(uint32_t newFlags){
if ( !(getFlags() & tfhdSampleFlag)){
return;
}
int offset = 8;
if (getFlags() & tfhdBaseOffset){
offset += 8;
}
if (getFlags() & tfhdSampleDesc){
offset += 4;
}
if (getFlags() & tfhdSampleDura){
offset += 4;
}
if (getFlags() & tfhdSampleSize){
offset += 4;
}
setInt32(newFlags, offset);
}
uint32_t TFHD::getDefaultSampleFlags(){
if ( !(getFlags() & tfhdSampleFlag)){
return 0;
}
int offset = 8;
if (getFlags() & tfhdBaseOffset){
offset += 8;
}
if (getFlags() & tfhdSampleDesc){
offset += 4;
}
if (getFlags() & tfhdSampleDura){
offset += 4;
}
if (getFlags() & tfhdSampleSize){
offset += 4;
}
return getInt32(offset);
}
std::string TFHD::toPrettyString(uint32_t indent){
std::stringstream r;
r << std::string(indent, ' ') << "[tfhd] Track Fragment Header (" << boxedSize() << ")" << std::endl;
r << std::string(indent + 1, ' ') << "Version " << (int)getInt8(0) << std::endl;
uint32_t flags = getFlags();
r << std::string(indent + 1, ' ') << "Flags";
if (flags & tfhdBaseOffset){
r << " BaseOffset";
}
if (flags & tfhdSampleDesc){
r << " SampleDesc";
}
if (flags & tfhdSampleDura){
r << " SampleDura";
}
if (flags & tfhdSampleSize){
r << " SampleSize";
}
if (flags & tfhdSampleFlag){
r << " SampleFlag";
}
if (flags & tfhdNoDuration){
r << " NoDuration";
}
r << std::endl;
r << std::string(indent + 1, ' ') << "TrackID " << getTrackID() << std::endl;
if (flags & tfhdBaseOffset){
r << std::string(indent + 1, ' ') << "Base Offset " << getBaseDataOffset() << std::endl;
}
if (flags & tfhdSampleDesc){
r << std::string(indent + 1, ' ') << "Sample Description Index " << getSampleDescriptionIndex() << std::endl;
}
if (flags & tfhdSampleDura){
r << std::string(indent + 1, ' ') << "Default Sample Duration " << getDefaultSampleDuration() << std::endl;
}
if (flags & tfhdSampleSize){
r << std::string(indent + 1, ' ') << "Default Same Size " << getDefaultSampleSize() << std::endl;
}
if (flags & tfhdSampleFlag){
r << std::string(indent + 1, ' ') << "Default Sample Flags " << prettySampleFlags(getDefaultSampleFlags()) << std::endl;
}
return r.str();
}
AFRA::AFRA(){
memcpy(data + 4, "afra", 4);
setInt32(0, 9); //entrycount = 0
setFlags(0);
}
void AFRA::setVersion(uint32_t newVersion){
setInt8(newVersion, 0);
}
uint32_t AFRA::getVersion(){
return getInt8(0);
}
void AFRA::setFlags(uint32_t newFlags){
setInt24(newFlags, 1);
}
uint32_t AFRA::getFlags(){
return getInt24(1);
}
void AFRA::setLongIDs(bool newVal){
if (newVal){
setInt8((getInt8(4) & 0x7F) + 0x80, 4);
}else{
setInt8((getInt8(4) & 0x7F), 4);
}
}
bool AFRA::getLongIDs(){
return getInt8(4) & 0x80;
}
void AFRA::setLongOffsets(bool newVal){
if (newVal){
setInt8((getInt8(4) & 0xBF) + 0x40, 4);
}else{
setInt8((getInt8(4) & 0xBF), 4);
}
}
bool AFRA::getLongOffsets(){
return getInt8(4) & 0x40;
}
void AFRA::setGlobalEntries(bool newVal){
if (newVal){
setInt8((getInt8(4) & 0xDF) + 0x20, 4);
}else{
setInt8((getInt8(4) & 0xDF), 4);
}
}
bool AFRA::getGlobalEntries(){
return getInt8(4) & 0x20;
}
void AFRA::setTimeScale(uint32_t newVal){
setInt32(newVal, 5);
}
uint32_t AFRA::getTimeScale(){
return getInt32(5);
}
uint32_t AFRA::getEntryCount(){
return getInt32(9);
}
void AFRA::setEntry(afraentry newEntry, uint32_t no){
int entrysize = 12;
if (getLongOffsets()){
entrysize = 16;
}
setInt64(newEntry.time, 13 + entrysize * no);
if (getLongOffsets()){
setInt64(newEntry.offset, 21 + entrysize * no);
}else{
setInt32(newEntry.offset, 21 + entrysize * no);
}
if (no + 1 > getEntryCount()){
setInt32(no + 1, 9);
}
}
afraentry AFRA::getEntry(uint32_t no){
afraentry ret;
int entrysize = 12;
if (getLongOffsets()){
entrysize = 16;
}
ret.time = getInt64(13 + entrysize * no);
if (getLongOffsets()){
ret.offset = getInt64(21 + entrysize * no);
}else{
ret.offset = getInt32(21 + entrysize * no);
}
return ret;
}
uint32_t AFRA::getGlobalEntryCount(){
if ( !getGlobalEntries()){
return 0;
}
int entrysize = 12;
if (getLongOffsets()){
entrysize = 16;
}
return getInt32(13 + entrysize * getEntryCount());
}
void AFRA::setGlobalEntry(globalafraentry newEntry, uint32_t no){
int offset = 13 + 12 * getEntryCount() + 4;
if (getLongOffsets()){
offset = 13 + 16 * getEntryCount() + 4;
}
int entrysize = 20;
if (getLongIDs()){
entrysize += 4;
}
if (getLongOffsets()){
entrysize += 8;
}
setInt64(newEntry.time, offset + entrysize * no);
if (getLongIDs()){
setInt32(newEntry.segment, offset + entrysize * no + 8);
setInt32(newEntry.fragment, offset + entrysize * no + 12);
}else{
setInt16(newEntry.segment, offset + entrysize * no + 8);
setInt16(newEntry.fragment, offset + entrysize * no + 10);
}
if (getLongOffsets()){
setInt64(newEntry.afraoffset, offset + entrysize * no + entrysize - 16);
setInt64(newEntry.offsetfromafra, offset + entrysize * no + entrysize - 8);
}else{
setInt32(newEntry.afraoffset, offset + entrysize * no + entrysize - 8);
setInt32(newEntry.offsetfromafra, offset + entrysize * no + entrysize - 4);
}
if (getInt32(offset - 4) < no + 1){
setInt32(no + 1, offset - 4);
}
}
globalafraentry AFRA::getGlobalEntry(uint32_t no){
globalafraentry ret;
int offset = 13 + 12 * getEntryCount() + 4;
if (getLongOffsets()){
offset = 13 + 16 * getEntryCount() + 4;
}
int entrysize = 20;
if (getLongIDs()){
entrysize += 4;
}
if (getLongOffsets()){
entrysize += 8;
}
ret.time = getInt64(offset + entrysize * no);
if (getLongIDs()){
ret.segment = getInt32(offset + entrysize * no + 8);
ret.fragment = getInt32(offset + entrysize * no + 12);
}else{
ret.segment = getInt16(offset + entrysize * no + 8);
ret.fragment = getInt16(offset + entrysize * no + 10);
}
if (getLongOffsets()){
ret.afraoffset = getInt64(offset + entrysize * no + entrysize - 16);
ret.offsetfromafra = getInt64(offset + entrysize * no + entrysize - 8);
}else{
ret.afraoffset = getInt32(offset + entrysize * no + entrysize - 8);
ret.offsetfromafra = getInt32(offset + entrysize * no + entrysize - 4);
}
return ret;
}
std::string AFRA::toPrettyString(uint32_t indent){
std::stringstream r;
r << std::string(indent, ' ') << "[afra] Fragment Random Access (" << boxedSize() << ")" << std::endl;
r << std::string(indent + 1, ' ') << "Version " << getVersion() << std::endl;
r << std::string(indent + 1, ' ') << "Flags " << getFlags() << std::endl;
r << std::string(indent + 1, ' ') << "Long IDs " << getLongIDs() << std::endl;
r << std::string(indent + 1, ' ') << "Long Offsets " << getLongOffsets() << std::endl;
r << std::string(indent + 1, ' ') << "Global Entries " << getGlobalEntries() << std::endl;
r << std::string(indent + 1, ' ') << "TimeScale " << getTimeScale() << std::endl;
uint32_t count = getEntryCount();
r << std::string(indent + 1, ' ') << "Entries (" << count << ") " << std::endl;
for (uint32_t i = 0; i < count; ++i){
afraentry tmpent = getEntry(i);
r << std::string(indent + 1, ' ') << i << ": Time " << tmpent.time << ", Offset " << tmpent.offset << std::endl;
}
if (getGlobalEntries()){
count = getGlobalEntryCount();
r << std::string(indent + 1, ' ') << "Global Entries (" << count << ") " << std::endl;
for (uint32_t i = 0; i < count; ++i){
globalafraentry tmpent = getGlobalEntry(i);
r << std::string(indent + 1, ' ') << i << ": T " << tmpent.time << ", S" << tmpent.segment << "F" << tmpent.fragment << ", "
<< tmpent.afraoffset << "/" << tmpent.offsetfromafra << std::endl;
}
}
return r.str();
}
AVCC::AVCC(){
memcpy(data + 4, "avcC", 4);
setInt8(0xFF, 4); //reserved + 4-bytes NAL length
}
void AVCC::setVersion(uint32_t newVersion){
setInt8(newVersion, 0);
}
uint32_t AVCC::getVersion(){
return getInt8(0);
}
void AVCC::setProfile(uint32_t newProfile){
setInt8(newProfile, 1);
}
uint32_t AVCC::getProfile(){
return getInt8(1);
}
void AVCC::setCompatibleProfiles(uint32_t newCompatibleProfiles){
setInt8(newCompatibleProfiles, 2);
}
uint32_t AVCC::getCompatibleProfiles(){
return getInt8(2);
}
void AVCC::setLevel(uint32_t newLevel){
setInt8(newLevel, 3);
}
uint32_t AVCC::getLevel(){
return getInt8(3);
}
void AVCC::setSPSNumber(uint32_t newSPSNumber){
setInt8(newSPSNumber, 5);
}
uint32_t AVCC::getSPSNumber(){
return getInt8(5);
}
void AVCC::setSPS(std::string newSPS){
setInt16(newSPS.size(), 6);
for (int i = 0; i < newSPS.size(); i++){
setInt8(newSPS[i], 8 + i);
} //not null-terminated
}
uint32_t AVCC::getSPSLen(){
return getInt16(6);
}
char* AVCC::getSPS(){
return payload() + 8;
}
void AVCC::setPPSNumber(uint32_t newPPSNumber){
int offset = 8 + getSPSLen();
setInt8(newPPSNumber, offset);
}
uint32_t AVCC::getPPSNumber(){
int offset = 8 + getSPSLen();
return getInt8(offset);
}
void AVCC::setPPS(std::string newPPS){
int offset = 8 + getSPSLen() + 1;
setInt16(newPPS.size(), offset);
for (int i = 0; i < newPPS.size(); i++){
setInt8(newPPS[i], offset + 2 + i);
} //not null-terminated
}
uint32_t AVCC::getPPSLen(){
int offset = 8 + getSPSLen() + 1;
return getInt16(offset);
}
char* AVCC::getPPS(){
int offset = 8 + getSPSLen() + 3;
return payload() + offset;
}
std::string AVCC::toPrettyString(uint32_t indent){
std::stringstream r;
r << std::string(indent, ' ') << "[avcC] H.264 Init Data (" << boxedSize() << ")" << std::endl;
r << std::string(indent + 1, ' ') << "Version: " << getVersion() << std::endl;
r << std::string(indent + 1, ' ') << "Profile: " << getProfile() << std::endl;
r << std::string(indent + 1, ' ') << "Compatible Profiles: " << getCompatibleProfiles() << std::endl;
r << std::string(indent + 1, ' ') << "Level: " << getLevel() << std::endl;
r << std::string(indent + 1, ' ') << "SPS Number: " << getSPSNumber() << std::endl;
r << std::string(indent + 2, ' ') << getSPSLen() << " of SPS data" << std::endl;
r << std::string(indent + 1, ' ') << "PPS Number: " << getPPSNumber() << std::endl;
r << std::string(indent + 2, ' ') << getPPSLen() << " of PPS data" << std::endl;
return r.str();
}
std::string AVCC::asAnnexB(){
std::stringstream r;
r << (char)0x00 << (char)0x00 << (char)0x00 << (char)0x01;
r.write(getSPS(), getSPSLen());
r << (char)0x00 << (char)0x00 << (char)0x00 << (char)0x01;
r.write(getPPS(), getPPSLen());
return r.str();
}
void AVCC::setPayload(std::string newPayload){
if ( !reserve(0, payloadSize(), newPayload.size())){
std::cerr << "Cannot allocate enough memory for payload" << std::endl;
return;
}
memcpy((char*)payload(), (char*)newPayload.c_str(), newPayload.size());
}
SDTP::SDTP(){
memcpy(data + 4, "sdtp", 4);
}
void SDTP::setVersion(uint32_t newVersion){
setInt8(newVersion, 0);
}
uint32_t SDTP::getVersion(){
return getInt8(0);
}
void SDTP::setValue(uint32_t newValue, size_t index){
setInt8(newValue, index);
}
uint32_t SDTP::getValue(size_t index){
getInt8(index);
}
}