1
0

mp4file.cpp 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is MPEG4IP.
  13. *
  14. * The Initial Developer of the Original Code is Cisco Systems Inc.
  15. * Portions created by Cisco Systems Inc. are
  16. * Copyright (C) Cisco Systems Inc. 2001 - 2005. All Rights Reserved.
  17. *
  18. * 3GPP features implementation is based on 3GPP's TS26.234-v5.60,
  19. * and was contributed by Ximpo Group Ltd.
  20. *
  21. * Portions created by Ximpo Group Ltd. are
  22. * Copyright (C) Ximpo Group Ltd. 2003, 2004. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Dave Mackie [email protected]
  26. * Alix Marchandise-Franquet [email protected]
  27. * Ximpo Group Ltd. [email protected]
  28. * Bill May [email protected]
  29. */
  30. #include "mp4common.h"
  31. MP4File::MP4File(u_int32_t verbosity)
  32. {
  33. m_fileName = NULL;
  34. m_pFile = NULL;
  35. m_virtual_IO = NULL;
  36. m_orgFileSize = 0;
  37. m_fileSize = 0;
  38. m_pRootAtom = NULL;
  39. m_odTrackId = MP4_INVALID_TRACK_ID;
  40. m_verbosity = verbosity;
  41. m_mode = 0;
  42. m_createFlags = 0;
  43. m_useIsma = false;
  44. m_pModificationProperty = NULL;
  45. m_pTimeScaleProperty = NULL;
  46. m_pDurationProperty = NULL;
  47. m_memoryBuffer = NULL;
  48. m_memoryBufferSize = 0;
  49. m_memoryBufferPosition = 0;
  50. m_numReadBits = 0;
  51. m_bufReadBits = 0;
  52. m_numWriteBits = 0;
  53. m_bufWriteBits = 0;
  54. m_editName = NULL;
  55. #ifndef _WIN32
  56. m_tempFileName[0] = '\0';
  57. #endif
  58. m_trakName[0] = '\0';
  59. m_tempFileName[0] = '\0';
  60. }
  61. MP4File::~MP4File()
  62. {
  63. MP4Free(m_fileName);
  64. if (m_pFile != NULL) {
  65. // not closed ?
  66. m_virtual_IO->Close(m_pFile);
  67. m_pFile = NULL;
  68. }
  69. delete m_pRootAtom;
  70. for (u_int32_t i = 0; i < m_pTracks.Size(); i++) {
  71. delete m_pTracks[i];
  72. }
  73. MP4Free(m_memoryBuffer); // just in case
  74. CHECK_AND_FREE(m_editName);
  75. }
  76. void MP4File::Read(const MP4_FILENAME_CHAR* fileName)
  77. {
  78. m_fileName = MP4Stralloc(fileName);
  79. m_mode = 'r';
  80. #ifdef _WIN32
  81. Open(L"rb");
  82. #else
  83. Open("rb");
  84. #endif
  85. ReadFromFile();
  86. CacheProperties();
  87. }
  88. // benski>
  89. void MP4File::ReadEx(const MP4_FILENAME_CHAR *fileName, void *user, Virtual_IO *virtual_IO)
  90. {
  91. m_fileName = MP4Stralloc(fileName);
  92. m_mode = 'r';
  93. m_pFile = user;
  94. m_virtual_IO = virtual_IO;
  95. ASSERT(m_pFile);
  96. ASSERT(m_virtual_IO)
  97. m_orgFileSize = m_fileSize = m_virtual_IO->GetFileLength(m_pFile);
  98. ReadFromFile();
  99. CacheProperties();
  100. }
  101. void MP4File::Create(const MP4_FILENAME_CHAR *fileName, u_int32_t flags,
  102. int add_ftyp, int add_iods,
  103. char* majorBrand, u_int32_t minorVersion,
  104. char** supportedBrands, u_int32_t supportedBrandsCount)
  105. {
  106. m_fileName = MP4Stralloc(fileName);
  107. m_mode = 'w';
  108. m_createFlags = flags;
  109. #ifdef _WIN32
  110. Open(L"wb+");
  111. #else
  112. Open("wb+");
  113. #endif
  114. // generate a skeletal atom tree
  115. m_pRootAtom = MP4Atom::CreateAtom(NULL);
  116. m_pRootAtom->SetFile(this);
  117. m_pRootAtom->Generate();
  118. if (add_ftyp != 0) {
  119. MakeFtypAtom(majorBrand, minorVersion,
  120. supportedBrands, supportedBrandsCount);
  121. }
  122. CacheProperties();
  123. // create mdat, and insert it after ftyp, and before moov
  124. (void)InsertChildAtom(m_pRootAtom, "mdat",
  125. add_ftyp != 0 ? 1 : 0);
  126. // start writing
  127. m_pRootAtom->BeginWrite();
  128. if (add_iods != 0) {
  129. (void)AddChildAtom("moov", "iods");
  130. }
  131. }
  132. bool MP4File::Use64Bits (const char *atomName)
  133. {
  134. uint32_t atomid = ATOMID(atomName);
  135. if (atomid == ATOMID("mdat") || atomid == ATOMID("stbl")) {
  136. return (m_createFlags & MP4_CREATE_64BIT_DATA) == MP4_CREATE_64BIT_DATA;
  137. }
  138. if (atomid == ATOMID("mvhd") ||
  139. atomid == ATOMID("tkhd") ||
  140. atomid == ATOMID("mdhd")) {
  141. return (m_createFlags & MP4_CREATE_64BIT_TIME) == MP4_CREATE_64BIT_TIME;
  142. }
  143. return false;
  144. }
  145. void MP4File::Check64BitStatus (const char *atomName)
  146. {
  147. uint32_t atomid = ATOMID(atomName);
  148. if (atomid == ATOMID("mdat") || atomid == ATOMID("stbl")) {
  149. m_createFlags |= MP4_CREATE_64BIT_DATA;
  150. } else if (atomid == ATOMID("mvhd") ||
  151. atomid == ATOMID("tkhd") ||
  152. atomid == ATOMID("mdhd")) {
  153. m_createFlags |= MP4_CREATE_64BIT_TIME;
  154. }
  155. }
  156. void MP4File::Modify(const MP4_FILENAME_CHAR* fileName)
  157. {
  158. m_fileName = MP4Stralloc(fileName);
  159. m_mode = 'r';
  160. #ifdef _WIN32
  161. Open(L"rb+");
  162. #else
  163. Open("rb+");
  164. #endif
  165. ReadFromFile();
  166. m_mode = 'w';
  167. // find the moov atom
  168. MP4Atom* pMoovAtom = m_pRootAtom->FindAtomMP4("moov");
  169. u_int32_t numAtoms;
  170. if (pMoovAtom == NULL) {
  171. // there isn't one, odd but we can still proceed
  172. pMoovAtom = AddChildAtom(m_pRootAtom, "moov");
  173. } else {
  174. numAtoms = m_pRootAtom->GetNumberOfChildAtoms();
  175. // work backwards thru the top level atoms
  176. int32_t i;
  177. bool lastAtomIsMoov = true;
  178. MP4Atom* pLastAtom = NULL;
  179. for (i = numAtoms - 1; i >= 0; i--) {
  180. MP4Atom* pAtom = m_pRootAtom->GetChildAtom(i);
  181. const char* type = pAtom->GetType();
  182. // get rid of any trailing free or skips
  183. if (!strcmp(type, "free") || !strcmp(type, "skip")) {
  184. m_pRootAtom->DeleteChildAtom(pAtom);
  185. continue;
  186. }
  187. if (strcmp(type, "moov")) {
  188. if (pLastAtom == NULL) {
  189. pLastAtom = pAtom;
  190. lastAtomIsMoov = false;
  191. }
  192. continue;
  193. }
  194. // now at moov atom
  195. // multiple moov atoms?!?
  196. if (pAtom != pMoovAtom) {
  197. throw new MP4Error(
  198. "Badly formed mp4 file, multiple moov atoms",
  199. "MP4Modify");
  200. }
  201. if (lastAtomIsMoov) {
  202. // position to start of moov atom,
  203. // effectively truncating file
  204. // prior to adding new mdat
  205. SetPosition(pMoovAtom->GetStart());
  206. } else { // last atom isn't moov
  207. // need to place a free atom
  208. MP4Atom* pFreeAtom = MP4Atom::CreateAtom("free");
  209. // in existing position of the moov atom
  210. m_pRootAtom->InsertChildAtom(pFreeAtom, i);
  211. m_pRootAtom->DeleteChildAtom(pMoovAtom);
  212. m_pRootAtom->AddChildAtom(pMoovAtom);
  213. // write free atom to disk
  214. SetPosition(pMoovAtom->GetStart());
  215. pFreeAtom->SetSize(pMoovAtom->GetSize());
  216. pFreeAtom->Write();
  217. // finally set our file position to the end of the last atom
  218. SetPosition(pLastAtom->GetEnd());
  219. }
  220. break;
  221. }
  222. ASSERT(i != -1);
  223. }
  224. CacheProperties(); // of moov atom
  225. numAtoms = m_pRootAtom->GetNumberOfChildAtoms();
  226. // insert another mdat prior to moov atom (the last atom)
  227. MP4Atom* pMdatAtom = InsertChildAtom(m_pRootAtom, "mdat", numAtoms - 1);
  228. // start writing new mdat
  229. pMdatAtom->BeginWrite(Use64Bits("mdat"));
  230. }
  231. void MP4File::Optimize(const MP4_FILENAME_CHAR* orgFileName, const MP4_FILENAME_CHAR* newFileName)
  232. {
  233. m_fileName = MP4Stralloc(orgFileName);
  234. m_mode = 'r';
  235. // first load meta-info into memory
  236. #ifdef _WIN32
  237. Open(L"rb");
  238. #else
  239. Open("rb");
  240. #endif
  241. ReadFromFile();
  242. CacheProperties(); // of moov atom
  243. // now switch over to writing the new file
  244. MP4Free(m_fileName);
  245. // create a temporary file if necessary
  246. if (newFileName == NULL) {
  247. m_fileName = MP4Stralloc(TempFileName());
  248. } else {
  249. m_fileName = MP4Stralloc(newFileName);
  250. }
  251. void* pReadFile = m_pFile;
  252. Virtual_IO *pReadIO = m_virtual_IO;
  253. m_pFile = NULL;
  254. m_mode = 'w';
  255. #ifdef _WIN32
  256. Open(L"wb");
  257. #else
  258. Open("wb");
  259. #endif
  260. SetIntegerProperty("moov.mvhd.modificationTime",
  261. MP4GetAbsTimestamp());
  262. // writing meta info in the optimal order
  263. ((MP4RootAtom*)m_pRootAtom)->BeginOptimalWrite();
  264. // write data in optimal order
  265. RewriteMdat(pReadFile, m_pFile, pReadIO, m_virtual_IO);
  266. // finish writing
  267. ((MP4RootAtom*)m_pRootAtom)->FinishOptimalWrite();
  268. // cleanup
  269. m_virtual_IO->Close(m_pFile);
  270. m_pFile = NULL;
  271. pReadIO->Close(pReadFile);
  272. // move temporary file into place
  273. if (newFileName == NULL) {
  274. Rename(m_fileName, orgFileName);
  275. }
  276. }
  277. void MP4File::RewriteMdat(void* pReadFile, void* pWriteFile,
  278. Virtual_IO *readIO, Virtual_IO *writeIO)
  279. {
  280. u_int32_t numTracks = m_pTracks.Size();
  281. MP4ChunkId* chunkIds = new MP4ChunkId[numTracks];
  282. MP4ChunkId* maxChunkIds = new MP4ChunkId[numTracks];
  283. MP4Timestamp* nextChunkTimes = new MP4Timestamp[numTracks];
  284. for (u_int32_t i = 0; i < numTracks; i++) {
  285. chunkIds[i] = 1;
  286. maxChunkIds[i] = m_pTracks[i]->GetNumberOfChunks();
  287. nextChunkTimes[i] = MP4_INVALID_TIMESTAMP;
  288. }
  289. while (true) {
  290. u_int32_t nextTrackIndex = (u_int32_t)-1;
  291. MP4Timestamp nextTime = MP4_INVALID_TIMESTAMP;
  292. for (u_int32_t i = 0; i < numTracks; i++) {
  293. if (chunkIds[i] > maxChunkIds[i]) {
  294. continue;
  295. }
  296. if (nextChunkTimes[i] == MP4_INVALID_TIMESTAMP) {
  297. MP4Timestamp chunkTime =
  298. m_pTracks[i]->GetChunkTime(chunkIds[i]);
  299. nextChunkTimes[i] = MP4ConvertTime(chunkTime,
  300. m_pTracks[i]->GetTimeScale(), GetTimeScale());
  301. }
  302. // time is not earliest so far
  303. if (nextChunkTimes[i] > nextTime) {
  304. continue;
  305. }
  306. // prefer hint tracks to media tracks if times are equal
  307. if (nextChunkTimes[i] == nextTime
  308. && strcmp(m_pTracks[i]->GetType(), MP4_HINT_TRACK_TYPE)) {
  309. continue;
  310. }
  311. // this is our current choice of tracks
  312. nextTime = nextChunkTimes[i];
  313. nextTrackIndex = i;
  314. }
  315. if (nextTrackIndex == (u_int32_t)-1) {
  316. break;
  317. }
  318. // point into original mp4 file for read chunk call
  319. m_pFile = pReadFile;
  320. m_virtual_IO = readIO;
  321. m_mode = 'r';
  322. u_int8_t* pChunk=0;
  323. u_int32_t chunkSize;
  324. m_pTracks[nextTrackIndex]->
  325. ReadChunk(chunkIds[nextTrackIndex], &pChunk, &chunkSize);
  326. // point back at the new mp4 file for write chunk
  327. m_pFile = pWriteFile;
  328. m_virtual_IO = writeIO;
  329. m_mode = 'w';
  330. m_pTracks[nextTrackIndex]->
  331. RewriteChunk(chunkIds[nextTrackIndex], pChunk, chunkSize);
  332. MP4Free(pChunk);
  333. chunkIds[nextTrackIndex]++;
  334. nextChunkTimes[nextTrackIndex] = MP4_INVALID_TIMESTAMP;
  335. }
  336. delete [] chunkIds;
  337. delete [] maxChunkIds;
  338. delete [] nextChunkTimes;
  339. }
  340. void MP4File::Open(const MP4_FILENAME_CHAR *fmode)
  341. {
  342. ASSERT(m_pFile == NULL);
  343. FILE *openFile = NULL;
  344. #ifdef O_LARGEFILE
  345. // UGH! fopen doesn't open a file in 64-bit mode, period.
  346. // So we need to use open() and then fdopen()
  347. int fd;
  348. int flags = O_LARGEFILE;
  349. if (strchr(fmode, '+')) {
  350. flags |= O_CREAT | O_RDWR;
  351. if (fmode[0] == 'w') {
  352. flags |= O_TRUNC;
  353. }
  354. } else {
  355. if (fmode[0] == 'w') {
  356. flags |= O_CREAT | O_TRUNC | O_WRONLY;
  357. } else {
  358. flags |= O_RDONLY;
  359. }
  360. }
  361. fd = open(m_fileName, flags, 0666);
  362. if (fd >= 0) {
  363. openFile = fdopen(fd, fmode);
  364. }
  365. #elif defined(_WIN32)
  366. openFile = _wfopen(m_fileName, fmode);
  367. #else
  368. openFile = fopen(m_fileName, fmode);
  369. #endif
  370. m_pFile = openFile;
  371. if (m_pFile == NULL) {
  372. throw new MP4Error(errno, "failed", "MP4Open");
  373. }
  374. m_virtual_IO = &FILE_virtual_IO;
  375. if (m_mode == 'r') {
  376. m_orgFileSize = m_fileSize = m_virtual_IO->GetFileLength(m_pFile); // benski
  377. } else {
  378. m_orgFileSize = m_fileSize = 0;
  379. }
  380. }
  381. void MP4File::ReadFromFile()
  382. {
  383. // ensure we start at beginning of file
  384. SetPosition(0);
  385. // create a new root atom
  386. ASSERT(m_pRootAtom == NULL);
  387. m_pRootAtom = MP4Atom::CreateAtom(NULL);
  388. u_int64_t fileSize = GetSize();
  389. m_pRootAtom->SetFile(this);
  390. m_pRootAtom->SetStart(0);
  391. m_pRootAtom->SetSize(fileSize);
  392. m_pRootAtom->SetEnd(fileSize);
  393. m_pRootAtom->Read();
  394. // create MP4Track's for any tracks in the file
  395. GenerateTracks();
  396. }
  397. void MP4File::GenerateTracks()
  398. {
  399. u_int32_t trackIndex = 0;
  400. while (true) {
  401. char trackName[32];
  402. snprintf(trackName, sizeof(trackName), "moov.trak[%u]", trackIndex);
  403. // find next trak atom
  404. MP4Atom* pTrakAtom = m_pRootAtom->FindAtomMP4(trackName);
  405. // done, no more trak atoms
  406. if (pTrakAtom == NULL) {
  407. break;
  408. }
  409. // find track id property
  410. MP4Integer32Property* pTrackIdProperty = NULL;
  411. (void)pTrakAtom->FindProperty(
  412. "trak.tkhd.trackId",
  413. (MP4Property**)&pTrackIdProperty);
  414. // find track type property
  415. MP4StringProperty* pTypeProperty = NULL;
  416. (void)pTrakAtom->FindProperty(
  417. "trak.mdia.hdlr.handlerType",
  418. (MP4Property**)&pTypeProperty);
  419. // ensure we have the basics properties
  420. if (pTrackIdProperty && pTypeProperty) {
  421. m_trakIds.Add(pTrackIdProperty->GetValue());
  422. MP4Track* pTrack = NULL;
  423. try {
  424. const char* value = pTypeProperty->GetValue();
  425. if (value && !strcmp(value, MP4_HINT_TRACK_TYPE)) {
  426. pTrack = new MP4RtpHintTrack(this, pTrakAtom);
  427. } else {
  428. pTrack = new MP4Track(this, pTrakAtom);
  429. }
  430. m_pTracks.Add(pTrack);
  431. }
  432. catch (MP4Error* e) {
  433. VERBOSE_ERROR(m_verbosity, e->Print());
  434. delete e;
  435. }
  436. // remember when we encounter the OD track
  437. const char* track = (pTrack ? pTrack->GetType() : 0);
  438. if (pTrack && !strcmp(track, MP4_OD_TRACK_TYPE)) {
  439. if (m_odTrackId == MP4_INVALID_TRACK_ID) {
  440. m_odTrackId = pTrackIdProperty->GetValue();
  441. } else {
  442. VERBOSE_READ(GetVerbosity(),
  443. printf("Warning: multiple OD tracks present\n"));
  444. }
  445. }
  446. } else {
  447. m_trakIds.Add(0);
  448. }
  449. trackIndex++;
  450. }
  451. }
  452. void MP4File::CacheProperties()
  453. {
  454. FindIntegerProperty("moov.mvhd.modificationTime",
  455. (MP4Property**)&m_pModificationProperty);
  456. FindIntegerProperty("moov.mvhd.timeScale",
  457. (MP4Property**)&m_pTimeScaleProperty);
  458. FindIntegerProperty("moov.mvhd.duration",
  459. (MP4Property**)&m_pDurationProperty);
  460. }
  461. void MP4File::BeginWrite()
  462. {
  463. m_pRootAtom->BeginWrite();
  464. }
  465. void MP4File::FinishWrite()
  466. {
  467. // for all tracks, flush chunking buffers
  468. for (u_int32_t i = 0; i < m_pTracks.Size(); i++) {
  469. ASSERT(m_pTracks[i]);
  470. m_pTracks[i]->FinishWrite();
  471. }
  472. // ask root atom to write
  473. m_pRootAtom->FinishWrite();
  474. // check if file shrunk, e.g. we deleted a track
  475. if (GetSize() < m_orgFileSize) {
  476. // just use a free atom to mark unused space
  477. // MP4Optimize() should be used to clean up this space
  478. MP4Atom* pFreeAtom = MP4Atom::CreateAtom("free");
  479. ASSERT(pFreeAtom);
  480. pFreeAtom->SetFile(this);
  481. int64_t size = m_orgFileSize - (m_fileSize + 8);
  482. if (size < 0) size = 0;
  483. pFreeAtom->SetSize(size);
  484. pFreeAtom->Write();
  485. delete pFreeAtom;
  486. }
  487. }
  488. void MP4File::UpdateDuration(MP4Duration duration)
  489. {
  490. MP4Duration currentDuration = GetDuration();
  491. if (duration > currentDuration) {
  492. SetDuration(duration);
  493. }
  494. }
  495. void MP4File::Close()
  496. {
  497. if (m_mode == 'w') {
  498. SetIntegerProperty("moov.mvhd.modificationTime",
  499. MP4GetAbsTimestamp());
  500. FinishWrite();
  501. }
  502. m_virtual_IO->Close(m_pFile);
  503. m_pFile = NULL;
  504. }
  505. const MP4_FILENAME_CHAR* MP4File::TempFileName()
  506. {
  507. // there are so many attempts in libc to get this right
  508. // that for portablity reasons, it's best just to roll our own
  509. #ifndef _WIN32
  510. u_int32_t i;
  511. for (i = getpid(); i < 0xFFFFFFFF; i++) {
  512. snprintf(m_tempFileName, sizeof(m_tempFileName),
  513. "./tmp%u.mp4", i);
  514. if (access(m_tempFileName, F_OK) != 0) {
  515. break;
  516. }
  517. }
  518. if (i == 0xFFFFFFFF) {
  519. throw new MP4Error("can't create temporary file", "TempFileName");
  520. }
  521. #else
  522. wchar_t tmppath[MAX_PATH-14];
  523. GetTempPathW(MAX_PATH-14,tmppath);
  524. GetTempFileNameW(tmppath, // dir. for temp. files
  525. L"mp4", // temp. filename prefix
  526. 0, // create unique name
  527. m_tempFileName); // buffer for name
  528. #endif
  529. return m_tempFileName;
  530. }
  531. void MP4File::Rename(const MP4_FILENAME_CHAR* oldFileName, const MP4_FILENAME_CHAR* newFileName)
  532. {
  533. int rc;
  534. #ifdef _WIN32
  535. rc=0;
  536. DeleteFileW(newFileName);
  537. if (MoveFileW(oldFileName,newFileName) == 0) // if the function fails
  538. {
  539. if (!CopyFileW(oldFileName,newFileName, FALSE))
  540. rc=1;
  541. else
  542. DeleteFileW(oldFileName);
  543. }
  544. /* benski> CUT:
  545. rc = remove(newFileName);
  546. if (rc == 0) {
  547. rc = rename(oldFileName, newFileName);
  548. }
  549. */
  550. #else
  551. rc = rename(oldFileName, newFileName);
  552. #endif
  553. if (rc != 0) {
  554. throw new MP4Error(errno, "can't overwrite existing file", "Rename");
  555. }
  556. }
  557. void MP4File::ProtectWriteOperation(char* where)
  558. {
  559. if (m_mode == 'r') {
  560. throw new MP4Error("operation not permitted in read mode", where);
  561. }
  562. }
  563. MP4Track* MP4File::GetTrack(MP4TrackId trackId)
  564. {
  565. return m_pTracks[FindTrackIndex(trackId)];
  566. }
  567. MP4Atom* MP4File::FindAtomMP4File(const char* name)
  568. {
  569. MP4Atom* pAtom = NULL;
  570. if (!name || !strcmp(name, "")) {
  571. pAtom = m_pRootAtom;
  572. } else {
  573. pAtom = m_pRootAtom->FindAtomMP4(name);
  574. }
  575. return pAtom;
  576. }
  577. MP4Atom* MP4File::AddChildAtom(
  578. const char* parentName,
  579. const char* childName)
  580. {
  581. return AddChildAtom(FindAtomMP4File(parentName), childName);
  582. }
  583. MP4Atom* MP4File::AddChildAtom(
  584. MP4Atom* pParentAtom,
  585. const char* childName)
  586. {
  587. return InsertChildAtom(pParentAtom, childName,
  588. pParentAtom->GetNumberOfChildAtoms());
  589. }
  590. MP4Atom* MP4File::InsertChildAtom(
  591. const char* parentName,
  592. const char* childName,
  593. u_int32_t index)
  594. {
  595. return InsertChildAtom(FindAtomMP4File(parentName), childName, index);
  596. }
  597. MP4Atom* MP4File::InsertChildAtom(
  598. MP4Atom* pParentAtom,
  599. const char* childName,
  600. u_int32_t index)
  601. {
  602. MP4Atom* pChildAtom = MP4Atom::CreateAtom(childName);
  603. ASSERT(pParentAtom);
  604. pParentAtom->InsertChildAtom(pChildAtom, index);
  605. pChildAtom->Generate();
  606. return pChildAtom;
  607. }
  608. MP4Atom* MP4File::AddDescendantAtoms(
  609. const char* ancestorName,
  610. const char* descendantNames)
  611. {
  612. return AddDescendantAtoms(FindAtomMP4File(ancestorName), descendantNames);
  613. }
  614. MP4Atom* MP4File::AddDescendantAtoms(
  615. MP4Atom* pAncestorAtom, const char* descendantNames)
  616. {
  617. ASSERT(pAncestorAtom);
  618. MP4Atom* pParentAtom = pAncestorAtom;
  619. MP4Atom* pChildAtom = NULL;
  620. while (true) {
  621. char* childName = MP4NameFirst(descendantNames);
  622. if (childName == NULL) {
  623. break;
  624. }
  625. descendantNames = MP4NameAfterFirst(descendantNames);
  626. pChildAtom = pParentAtom->FindChildAtom(childName);
  627. if (pChildAtom == NULL) {
  628. pChildAtom = AddChildAtom(pParentAtom, childName);
  629. }
  630. pParentAtom = pChildAtom;
  631. MP4Free(childName);
  632. }
  633. return pChildAtom;
  634. }
  635. bool MP4File::FindProperty(const char* name,
  636. MP4Property** ppProperty, u_int32_t* pIndex)
  637. {
  638. if (pIndex) {
  639. *pIndex = 0; // set the default answer for index
  640. }
  641. return m_pRootAtom->FindProperty(name, ppProperty, pIndex);
  642. }
  643. void MP4File::FindIntegerProperty(const char* name,
  644. MP4Property** ppProperty, u_int32_t* pIndex)
  645. {
  646. if (!FindProperty(name, ppProperty, pIndex)) {
  647. throw new MP4Error("no such property - %s", "MP4File::FindIntegerProperty", name);
  648. }
  649. switch ((*ppProperty)->GetType()) {
  650. case Integer8Property:
  651. case Integer16Property:
  652. case Integer24Property:
  653. case Integer32Property:
  654. case Integer64Property:
  655. break;
  656. default:
  657. throw new MP4Error("type mismatch - property %s type %d", "MP4File::FindIntegerProperty", name, (*ppProperty)->GetType());
  658. }
  659. }
  660. u_int64_t MP4File::GetIntegerProperty(const char* name)
  661. {
  662. MP4Property* pProperty;
  663. u_int32_t index;
  664. FindIntegerProperty(name, &pProperty, &index);
  665. return ((MP4IntegerProperty*)pProperty)->GetValue(index);
  666. }
  667. void MP4File::SetIntegerProperty(const char* name, u_int64_t value)
  668. {
  669. ProtectWriteOperation("SetIntegerProperty");
  670. MP4Property* pProperty = NULL;
  671. u_int32_t index = 0;
  672. FindIntegerProperty(name, &pProperty, &index);
  673. ((MP4IntegerProperty*)pProperty)->SetValue(value, index);
  674. }
  675. void MP4File::FindFloatProperty(const char* name,
  676. MP4Property** ppProperty, u_int32_t* pIndex)
  677. {
  678. if (!FindProperty(name, ppProperty, pIndex)) {
  679. throw new MP4Error("no such property - %s", "MP4File::FindFloatProperty", name);
  680. }
  681. if ((*ppProperty)->GetType() != Float32Property) {
  682. throw new MP4Error("type mismatch - property %s type %d",
  683. "MP4File::FindFloatProperty",
  684. name,
  685. (*ppProperty)->GetType());
  686. }
  687. }
  688. float MP4File::GetFloatProperty(const char* name)
  689. {
  690. MP4Property* pProperty;
  691. u_int32_t index;
  692. FindFloatProperty(name, &pProperty, &index);
  693. return ((MP4Float32Property*)pProperty)->GetValue(index);
  694. }
  695. void MP4File::SetFloatProperty(const char* name, float value)
  696. {
  697. ProtectWriteOperation("SetFloatProperty");
  698. MP4Property* pProperty;
  699. u_int32_t index;
  700. FindFloatProperty(name, &pProperty, &index);
  701. ((MP4Float32Property*)pProperty)->SetValue(value, index);
  702. }
  703. void MP4File::FindStringProperty(const char* name,
  704. MP4Property** ppProperty, u_int32_t* pIndex)
  705. {
  706. if (!FindProperty(name, ppProperty, pIndex)) {
  707. throw new MP4Error("no such property - %s", "MP4File::FindStringProperty", name);
  708. }
  709. if ((*ppProperty)->GetType() != StringProperty) {
  710. throw new MP4Error("type mismatch - property %s type %d", "MP4File::FindStringProperty",
  711. name, (*ppProperty)->GetType());
  712. }
  713. }
  714. const char* MP4File::GetStringProperty(const char* name)
  715. {
  716. MP4Property* pProperty;
  717. u_int32_t index;
  718. FindStringProperty(name, &pProperty, &index);
  719. return ((MP4StringProperty*)pProperty)->GetValue(index);
  720. }
  721. void MP4File::SetStringProperty(const char* name, const char* value)
  722. {
  723. ProtectWriteOperation("SetStringProperty");
  724. MP4Property* pProperty;
  725. u_int32_t index;
  726. FindStringProperty(name, &pProperty, &index);
  727. ((MP4StringProperty*)pProperty)->SetValue(value, index);
  728. }
  729. void MP4File::FindBytesProperty(const char* name,
  730. MP4Property** ppProperty, u_int32_t* pIndex)
  731. {
  732. if (!FindProperty(name, ppProperty, pIndex)) {
  733. throw new MP4Error("no such property %s", "MP4File::FindBytesProperty", name);
  734. }
  735. if ((*ppProperty)->GetType() != BytesProperty) {
  736. throw new MP4Error("type mismatch - property %s - type %d", "MP4File::FindBytesProperty", name, (*ppProperty)->GetType());
  737. }
  738. }
  739. void MP4File::GetBytesProperty(const char* name,
  740. u_int8_t** ppValue, u_int32_t* pValueSize)
  741. {
  742. MP4Property* pProperty;
  743. u_int32_t index;
  744. FindBytesProperty(name, &pProperty, &index);
  745. ((MP4BytesProperty*)pProperty)->GetValue(ppValue, pValueSize, index);
  746. }
  747. void MP4File::SetBytesProperty(const char* name,
  748. const u_int8_t* pValue, u_int32_t valueSize)
  749. {
  750. ProtectWriteOperation("SetBytesProperty");
  751. MP4Property* pProperty;
  752. u_int32_t index;
  753. FindBytesProperty(name, &pProperty, &index);
  754. ((MP4BytesProperty*)pProperty)->SetValue(pValue, valueSize, index);
  755. }
  756. // track functions
  757. MP4TrackId MP4File::AddTrack(const char* type, u_int32_t timeScale)
  758. {
  759. ProtectWriteOperation("AddTrack");
  760. // create and add new trak atom
  761. MP4Atom* pTrakAtom = AddChildAtom("moov", "trak");
  762. // allocate a new track id
  763. MP4TrackId trackId = AllocTrackId();
  764. m_trakIds.Add(trackId);
  765. // set track id
  766. MP4Integer32Property* pInteger32Property = NULL;
  767. (void)pTrakAtom->FindProperty("trak.tkhd.trackId",
  768. (MP4Property**)&pInteger32Property);
  769. ASSERT(pInteger32Property);
  770. pInteger32Property->SetValue(trackId);
  771. // set track type
  772. const char* normType = MP4NormalizeTrackType(type, m_verbosity);
  773. // sanity check for user defined types
  774. if (strlen(normType) > 4) {
  775. VERBOSE_WARNING(m_verbosity,
  776. printf("AddTrack: type truncated to four characters\n"));
  777. // StringProperty::SetValue() will do the actual truncation
  778. }
  779. MP4StringProperty* pStringProperty = NULL;
  780. (void)pTrakAtom->FindProperty("trak.mdia.hdlr.handlerType",
  781. (MP4Property**)&pStringProperty);
  782. ASSERT(pStringProperty);
  783. pStringProperty->SetValue(normType);
  784. // set track time scale
  785. pInteger32Property = NULL;
  786. (void)pTrakAtom->FindProperty("trak.mdia.mdhd.timeScale",
  787. (MP4Property**)&pInteger32Property);
  788. ASSERT(pInteger32Property);
  789. pInteger32Property->SetValue(timeScale ? timeScale : 1000);
  790. // now have enough to create MP4Track object
  791. MP4Track* pTrack = NULL;
  792. if (!strcmp(normType, MP4_HINT_TRACK_TYPE)) {
  793. pTrack = new MP4RtpHintTrack(this, pTrakAtom);
  794. } else {
  795. pTrack = new MP4Track(this, pTrakAtom);
  796. }
  797. m_pTracks.Add(pTrack);
  798. // mark non-hint tracks as enabled
  799. if (strcmp(normType, MP4_HINT_TRACK_TYPE)) {
  800. SetTrackIntegerProperty(trackId, "tkhd.flags", 1);
  801. }
  802. // mark track as contained in this file
  803. // LATER will provide option for external data references
  804. AddDataReference(trackId, NULL);
  805. return trackId;
  806. }
  807. void MP4File::AddTrackToIod(MP4TrackId trackId)
  808. {
  809. MP4DescriptorProperty* pDescriptorProperty = NULL;
  810. (void)m_pRootAtom->FindProperty("moov.iods.esIds",
  811. (MP4Property**)&pDescriptorProperty);
  812. ASSERT(pDescriptorProperty);
  813. MP4Descriptor* pDescriptor =
  814. pDescriptorProperty->AddDescriptor(MP4ESIDIncDescrTag);
  815. ASSERT(pDescriptor);
  816. MP4Integer32Property* pIdProperty = NULL;
  817. (void)pDescriptor->FindProperty("id",
  818. (MP4Property**)&pIdProperty);
  819. ASSERT(pIdProperty);
  820. pIdProperty->SetValue(trackId);
  821. }
  822. void MP4File::RemoveTrackFromIod(MP4TrackId trackId, bool shallHaveIods)
  823. {
  824. MP4DescriptorProperty* pDescriptorProperty = NULL;
  825. if (!m_pRootAtom->FindProperty("moov.iods.esIds",
  826. (MP4Property**)&pDescriptorProperty))
  827. return;
  828. #if 0
  829. // we may not have iods
  830. if (shallHaveIods) {
  831. ASSERT(pDescriptorProperty);
  832. } else {
  833. if (!pDescriptorProperty) {
  834. return;
  835. }
  836. }
  837. #else
  838. if (pDescriptorProperty == NULL) {
  839. return;
  840. }
  841. #endif
  842. for (u_int32_t i = 0; i < pDescriptorProperty->GetCount(); i++) {
  843. /* static */char name[32];
  844. snprintf(name, sizeof(name), "esIds[%u].id", i);
  845. MP4Integer32Property* pIdProperty = NULL;
  846. (void)pDescriptorProperty->FindProperty(name,
  847. (MP4Property**)&pIdProperty);
  848. // wmay ASSERT(pIdProperty);
  849. if (pIdProperty != NULL &&
  850. pIdProperty->GetValue() == trackId) {
  851. pDescriptorProperty->DeleteDescriptor(i);
  852. break;
  853. }
  854. }
  855. }
  856. void MP4File::AddTrackToOd(MP4TrackId trackId)
  857. {
  858. if (!m_odTrackId) {
  859. return;
  860. }
  861. AddTrackReference(MakeTrackName(m_odTrackId, "tref.mpod"), trackId);
  862. }
  863. void MP4File::RemoveTrackFromOd(MP4TrackId trackId)
  864. {
  865. if (!m_odTrackId) {
  866. return;
  867. }
  868. RemoveTrackReference(MakeTrackName(m_odTrackId, "tref.mpod"), trackId);
  869. }
  870. void MP4File::GetTrackReferenceProperties(const char* trefName,
  871. MP4Property** ppCountProperty, MP4Property** ppTrackIdProperty)
  872. {
  873. char propName[1024];
  874. snprintf(propName, sizeof(propName), "%s.%s", trefName, "entryCount");
  875. (void)m_pRootAtom->FindProperty(propName, ppCountProperty);
  876. ASSERT(*ppCountProperty);
  877. snprintf(propName, sizeof(propName), "%s.%s", trefName, "entries.trackId");
  878. (void)m_pRootAtom->FindProperty(propName, ppTrackIdProperty);
  879. ASSERT(*ppTrackIdProperty);
  880. }
  881. void MP4File::AddTrackReference(const char* trefName, MP4TrackId refTrackId)
  882. {
  883. MP4Integer32Property* pCountProperty = NULL;
  884. MP4Integer32Property* pTrackIdProperty = NULL;
  885. GetTrackReferenceProperties(trefName,
  886. (MP4Property**)&pCountProperty,
  887. (MP4Property**)&pTrackIdProperty);
  888. pTrackIdProperty->AddValue(refTrackId);
  889. pCountProperty->IncrementValue();
  890. }
  891. u_int32_t MP4File::FindTrackReference(const char* trefName,
  892. MP4TrackId refTrackId)
  893. {
  894. MP4Integer32Property* pCountProperty = NULL;
  895. MP4Integer32Property* pTrackIdProperty = NULL;
  896. GetTrackReferenceProperties(trefName,
  897. (MP4Property**)&pCountProperty,
  898. (MP4Property**)&pTrackIdProperty);
  899. for (u_int32_t i = 0; i < pCountProperty->GetValue(); i++) {
  900. if (refTrackId == pTrackIdProperty->GetValue(i)) {
  901. return i + 1; // N.B. 1 not 0 based index
  902. }
  903. }
  904. return 0;
  905. }
  906. void MP4File::RemoveTrackReference(const char* trefName, MP4TrackId refTrackId)
  907. {
  908. MP4Integer32Property* pCountProperty = NULL;
  909. MP4Integer32Property* pTrackIdProperty = NULL;
  910. GetTrackReferenceProperties(trefName,
  911. (MP4Property**)&pCountProperty,
  912. (MP4Property**)&pTrackIdProperty);
  913. for (u_int32_t i = 0; i < pCountProperty->GetValue(); i++) {
  914. if (refTrackId == pTrackIdProperty->GetValue(i)) {
  915. pTrackIdProperty->DeleteValue(i);
  916. pCountProperty->IncrementValue(-1);
  917. }
  918. }
  919. }
  920. void MP4File::AddDataReference(MP4TrackId trackId, const char* url)
  921. {
  922. MP4Atom* pDrefAtom =
  923. FindAtomMP4File(MakeTrackName(trackId, "mdia.minf.dinf.dref"));
  924. ASSERT(pDrefAtom);
  925. MP4Integer32Property* pCountProperty = NULL;
  926. (void)pDrefAtom->FindProperty("dref.entryCount",
  927. (MP4Property**)&pCountProperty);
  928. ASSERT(pCountProperty);
  929. pCountProperty->IncrementValue();
  930. MP4Atom* pUrlAtom = AddChildAtom(pDrefAtom, "url ");
  931. if (url && url[0] != '\0') {
  932. pUrlAtom->SetFlags(pUrlAtom->GetFlags() & 0xFFFFFE);
  933. MP4StringProperty* pUrlProperty = NULL;
  934. (void)pUrlAtom->FindProperty("url .location",
  935. (MP4Property**)&pUrlProperty);
  936. ASSERT(pUrlProperty);
  937. pUrlProperty->SetValue(url);
  938. } else {
  939. pUrlAtom->SetFlags(pUrlAtom->GetFlags() | 1);
  940. }
  941. }
  942. MP4TrackId MP4File::AddSystemsTrack(const char* type)
  943. {
  944. const char* normType = MP4NormalizeTrackType(type, m_verbosity);
  945. // TBD if user type, fix name to four chars, and warn
  946. MP4TrackId trackId = AddTrack(type, MP4_MSECS_TIME_SCALE);
  947. (void)InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "nmhd", 0);
  948. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "mp4s");
  949. // stsd is a unique beast in that it has a count of the number
  950. // of child atoms that needs to be incremented after we add the mp4s atom
  951. MP4Integer32Property* pStsdCountProperty;
  952. FindIntegerProperty(
  953. MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"),
  954. (MP4Property**)&pStsdCountProperty);
  955. pStsdCountProperty->IncrementValue();
  956. SetTrackIntegerProperty(trackId,
  957. "mdia.minf.stbl.stsd.mp4s.esds.ESID",
  958. #if 0
  959. // note - for a file, these values need to
  960. // be 0 - wmay - 04/16/2003
  961. trackId
  962. #else
  963. 0
  964. #endif
  965. );
  966. SetTrackIntegerProperty(trackId,
  967. "mdia.minf.stbl.stsd.mp4s.esds.decConfigDescr.objectTypeId",
  968. MP4SystemsV1ObjectType);
  969. SetTrackIntegerProperty(trackId,
  970. "mdia.minf.stbl.stsd.mp4s.esds.decConfigDescr.streamType",
  971. ConvertTrackTypeToStreamType(normType));
  972. return trackId;
  973. }
  974. MP4TrackId MP4File::AddODTrack()
  975. {
  976. // until a demonstrated need emerges
  977. // we limit ourselves to one object description track
  978. if (m_odTrackId != MP4_INVALID_TRACK_ID) {
  979. throw new MP4Error("object description track already exists",
  980. "AddObjectDescriptionTrack");
  981. }
  982. m_odTrackId = AddSystemsTrack(MP4_OD_TRACK_TYPE);
  983. AddTrackToIod(m_odTrackId);
  984. (void)AddDescendantAtoms(MakeTrackName(m_odTrackId, NULL), "tref.mpod");
  985. return m_odTrackId;
  986. }
  987. MP4TrackId MP4File::AddSceneTrack()
  988. {
  989. MP4TrackId trackId = AddSystemsTrack(MP4_SCENE_TRACK_TYPE);
  990. AddTrackToIod(trackId);
  991. AddTrackToOd(trackId);
  992. return trackId;
  993. }
  994. // NULL terminated list of brands which require the IODS atom
  995. char *brandsWithIods[] = { "mp42",
  996. "isom",
  997. NULL};
  998. bool MP4File::ShallHaveIods()
  999. {
  1000. u_int32_t compatibleBrandsCount;
  1001. MP4StringProperty *pMajorBrandProperty;
  1002. MP4Atom* ftypAtom = m_pRootAtom->FindAtomMP4("ftyp");
  1003. if (ftypAtom == NULL) return false;
  1004. // Check the major brand
  1005. (void)ftypAtom->FindProperty(
  1006. "ftyp.majorBrand",
  1007. (MP4Property**)&pMajorBrandProperty);
  1008. ASSERT(pMajorBrandProperty);
  1009. for(u_int32_t j = 0 ; brandsWithIods[j] != NULL ; j++) {
  1010. if (!strcasecmp( ((MP4StringProperty*)pMajorBrandProperty)->GetValue(),
  1011. brandsWithIods[j]))
  1012. return true;
  1013. }
  1014. // Check the compatible brands
  1015. MP4Integer32Property* pCompatibleBrandsCountProperty;
  1016. (void)ftypAtom->FindProperty(
  1017. "ftyp.compatibleBrandsCount",
  1018. (MP4Property**)&pCompatibleBrandsCountProperty);
  1019. ASSERT(pCompatibleBrandsCountProperty);
  1020. compatibleBrandsCount = pCompatibleBrandsCountProperty->GetValue();
  1021. MP4TableProperty* pCompatibleBrandsProperty;
  1022. (void)ftypAtom->FindProperty(
  1023. "ftyp.compatibleBrands",
  1024. (MP4Property**)&pCompatibleBrandsProperty);
  1025. MP4StringProperty* pBrandProperty = (MP4StringProperty*)pCompatibleBrandsProperty->GetProperty(0);
  1026. ASSERT(pBrandProperty);
  1027. for(u_int32_t i = 0 ; i < compatibleBrandsCount ; i++) {
  1028. for(u_int32_t j = 0 ; brandsWithIods[j] != NULL ; j++) {
  1029. if (!strcasecmp(pBrandProperty->GetValue(i), brandsWithIods[j]))
  1030. return true;
  1031. }
  1032. }
  1033. return false;
  1034. }
  1035. void MP4File::SetAmrVendor(
  1036. MP4TrackId trackId,
  1037. u_int32_t vendor)
  1038. {
  1039. SetTrackIntegerProperty(trackId,
  1040. "mdia.minf.stbl.stsd.*.damr.vendor",
  1041. vendor);
  1042. }
  1043. void MP4File::SetAmrDecoderVersion(
  1044. MP4TrackId trackId,
  1045. u_int8_t decoderVersion)
  1046. {
  1047. SetTrackIntegerProperty(trackId,
  1048. "mdia.minf.stbl.stsd.*.damr.decoderVersion",
  1049. decoderVersion);
  1050. }
  1051. void MP4File::SetAmrModeSet(
  1052. MP4TrackId trackId,
  1053. u_int16_t modeSet)
  1054. {
  1055. SetTrackIntegerProperty(trackId,
  1056. "mdia.minf.stbl.stsd.*.damr.modeSet",
  1057. modeSet);
  1058. }
  1059. uint16_t MP4File::GetAmrModeSet(MP4TrackId trackId)
  1060. {
  1061. return GetTrackIntegerProperty(trackId,
  1062. "mdia.minf.stbl.stsd.*.damr.modeSet");
  1063. }
  1064. MP4TrackId MP4File::AddAmrAudioTrack(
  1065. u_int32_t timeScale,
  1066. u_int16_t modeSet,
  1067. u_int8_t modeChangePeriod,
  1068. u_int8_t framesPerSample,
  1069. bool isAmrWB)
  1070. {
  1071. u_int32_t fixedSampleDuration = (timeScale * 20)/1000; // 20mSec/Sample
  1072. MP4TrackId trackId = AddTrack(MP4_AUDIO_TRACK_TYPE, timeScale);
  1073. AddTrackToOd(trackId);
  1074. SetTrackFloatProperty(trackId, "tkhd.volume", 1.0);
  1075. (void)InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "smhd", 0);
  1076. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), isAmrWB ? "sawb" : "samr");
  1077. // stsd is a unique beast in that it has a count of the number
  1078. // of child atoms that needs to be incremented after we add the mp4a atom
  1079. MP4Integer32Property* pStsdCountProperty;
  1080. FindIntegerProperty(
  1081. MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"),
  1082. (MP4Property**)&pStsdCountProperty);
  1083. pStsdCountProperty->IncrementValue();
  1084. SetTrackIntegerProperty(trackId,
  1085. "mdia.minf.stbl.stsd.*.timeScale",
  1086. timeScale);
  1087. SetTrackIntegerProperty(trackId,
  1088. "mdia.minf.stbl.stsd.*.damr.modeSet",
  1089. modeSet);
  1090. SetTrackIntegerProperty(trackId,
  1091. "mdia.minf.stbl.stsd.*.damr.modeChangePeriod",
  1092. modeChangePeriod);
  1093. SetTrackIntegerProperty(trackId,
  1094. "mdia.minf.stbl.stsd.*.damr.framesPerSample",
  1095. framesPerSample);
  1096. m_pTracks[FindTrackIndex(trackId)]->
  1097. SetFixedSampleDuration(fixedSampleDuration);
  1098. return trackId;
  1099. }
  1100. MP4TrackId MP4File::AddAudioTrack(
  1101. u_int32_t timeScale,
  1102. MP4Duration sampleDuration,
  1103. u_int8_t audioType)
  1104. {
  1105. MP4TrackId trackId = AddTrack(MP4_AUDIO_TRACK_TYPE, timeScale);
  1106. AddTrackToOd(trackId);
  1107. SetTrackFloatProperty(trackId, "tkhd.volume", 1.0);
  1108. (void)InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "smhd", 0);
  1109. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "mp4a");
  1110. // stsd is a unique beast in that it has a count of the number
  1111. // of child atoms that needs to be incremented after we add the mp4a atom
  1112. MP4Integer32Property* pStsdCountProperty;
  1113. FindIntegerProperty(
  1114. MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"),
  1115. (MP4Property**)&pStsdCountProperty);
  1116. pStsdCountProperty->IncrementValue();
  1117. SetTrackIntegerProperty(trackId,
  1118. "mdia.minf.stbl.stsd.mp4a.timeScale", timeScale);
  1119. SetTrackIntegerProperty(trackId,
  1120. "mdia.minf.stbl.stsd.mp4a.esds.ESID",
  1121. #if 0
  1122. // note - for a file, these values need to
  1123. // be 0 - wmay - 04/16/2003
  1124. trackId
  1125. #else
  1126. 0
  1127. #endif
  1128. );
  1129. SetTrackIntegerProperty(trackId,
  1130. "mdia.minf.stbl.stsd.mp4a.esds.decConfigDescr.objectTypeId",
  1131. audioType);
  1132. SetTrackIntegerProperty(trackId,
  1133. "mdia.minf.stbl.stsd.mp4a.esds.decConfigDescr.streamType",
  1134. MP4AudioStreamType);
  1135. m_pTracks[FindTrackIndex(trackId)]->
  1136. SetFixedSampleDuration(sampleDuration);
  1137. return trackId;
  1138. }
  1139. MP4TrackId MP4File::AddEncAudioTrack(u_int32_t timeScale,
  1140. MP4Duration sampleDuration,
  1141. u_int8_t audioType,
  1142. u_int32_t scheme_type,
  1143. u_int16_t scheme_version,
  1144. u_int8_t key_ind_len,
  1145. u_int8_t iv_len,
  1146. bool selective_enc,
  1147. const char *kms_uri,
  1148. bool use_ismacryp
  1149. )
  1150. {
  1151. u_int32_t original_fmt = 0;
  1152. MP4TrackId trackId = AddTrack(MP4_AUDIO_TRACK_TYPE, timeScale);
  1153. AddTrackToOd(trackId);
  1154. SetTrackFloatProperty(trackId, "tkhd.volume", 1.0);
  1155. (void)InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "smhd", 0);
  1156. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "enca");
  1157. // stsd is a unique beast in that it has a count of the number
  1158. // of child atoms that needs to be incremented after we add the enca atom
  1159. MP4Integer32Property* pStsdCountProperty;
  1160. FindIntegerProperty(MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"),
  1161. (MP4Property**)&pStsdCountProperty);
  1162. pStsdCountProperty->IncrementValue();
  1163. /* set all the ismacryp-specific values */
  1164. // original format is mp4a
  1165. if (use_ismacryp) {
  1166. original_fmt = ATOMID("mp4a");
  1167. SetTrackIntegerProperty(trackId,
  1168. "mdia.minf.stbl.stsd.enca.sinf.frma.data-format",
  1169. original_fmt);
  1170. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.enca.sinf"),
  1171. "schm");
  1172. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.enca.sinf"),
  1173. "schi");
  1174. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.enca.sinf.schi"),
  1175. "iKMS");
  1176. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.enca.sinf.schi"),
  1177. "iSFM");
  1178. SetTrackIntegerProperty(trackId,
  1179. "mdia.minf.stbl.stsd.enca.sinf.schm.scheme_type",
  1180. scheme_type);
  1181. SetTrackIntegerProperty(trackId,
  1182. "mdia.minf.stbl.stsd.enca.sinf.schm.scheme_version",
  1183. scheme_version);
  1184. SetTrackStringProperty(trackId,
  1185. "mdia.minf.stbl.stsd.enca.sinf.schi.iKMS.kms_URI",
  1186. kms_uri);
  1187. #if 0
  1188. if (kms_uri != NULL) {
  1189. free((void *)kms_uri);
  1190. }
  1191. #endif
  1192. SetTrackIntegerProperty(trackId,
  1193. "mdia.minf.stbl.stsd.enca.sinf.schi.iSFM.selective-encryption",
  1194. selective_enc);
  1195. SetTrackIntegerProperty(trackId,
  1196. "mdia.minf.stbl.stsd.enca.sinf.schi.iSFM.key-indicator-length",
  1197. key_ind_len);
  1198. SetTrackIntegerProperty(trackId,
  1199. "mdia.minf.stbl.stsd.enca.sinf.schi.iSFM.IV-length",
  1200. iv_len);
  1201. /* end ismacryp */
  1202. }
  1203. SetTrackIntegerProperty(trackId,
  1204. "mdia.minf.stbl.stsd.enca.timeScale", timeScale);
  1205. SetTrackIntegerProperty(trackId,
  1206. "mdia.minf.stbl.stsd.enca.esds.ESID",
  1207. #if 0
  1208. // note - for a file, these values need to
  1209. // be 0 - wmay - 04/16/2003
  1210. trackId
  1211. #else
  1212. 0
  1213. #endif
  1214. );
  1215. SetTrackIntegerProperty(trackId,
  1216. "mdia.minf.stbl.stsd.enca.esds.decConfigDescr.objectTypeId",
  1217. audioType);
  1218. SetTrackIntegerProperty(trackId,
  1219. "mdia.minf.stbl.stsd.enca.esds.decConfigDescr.streamType",
  1220. MP4AudioStreamType);
  1221. m_pTracks[FindTrackIndex(trackId)]->
  1222. SetFixedSampleDuration(sampleDuration);
  1223. return trackId;
  1224. }
  1225. MP4TrackId MP4File::AddCntlTrackDefault (uint32_t timeScale,
  1226. MP4Duration sampleDuration,
  1227. const char *type)
  1228. {
  1229. MP4TrackId trackId = AddTrack(MP4_CNTL_TRACK_TYPE, timeScale);
  1230. (void)InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "nmhd", 0);
  1231. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), type);
  1232. // stsd is a unique beast in that it has a count of the number
  1233. // of child atoms that needs to be incremented after we add the mp4v atom
  1234. MP4Integer32Property* pStsdCountProperty;
  1235. FindIntegerProperty(
  1236. MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"),
  1237. (MP4Property**)&pStsdCountProperty);
  1238. pStsdCountProperty->IncrementValue();
  1239. SetTrackIntegerProperty(trackId,
  1240. "mdia.minf.stbl.stsz.sampleSize", sampleDuration);
  1241. m_pTracks[FindTrackIndex(trackId)]->
  1242. SetFixedSampleDuration(sampleDuration);
  1243. return trackId;
  1244. }
  1245. MP4TrackId MP4File::AddHrefTrack (uint32_t timeScale,
  1246. MP4Duration sampleDuration,
  1247. const char *base_url)
  1248. {
  1249. MP4TrackId trackId = AddCntlTrackDefault(timeScale, sampleDuration, "href");
  1250. if (base_url != NULL) {
  1251. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.href"),
  1252. "burl");
  1253. SetTrackStringProperty(trackId, "mdia.minf.stbl.stsd.href.burl.base_url",
  1254. base_url);
  1255. }
  1256. return trackId;
  1257. }
  1258. MP4TrackId MP4File::AddVideoTrackDefault(
  1259. u_int32_t timeScale,
  1260. MP4Duration sampleDuration,
  1261. u_int16_t width,
  1262. u_int16_t height,
  1263. const char *videoType)
  1264. {
  1265. MP4TrackId trackId = AddTrack(MP4_VIDEO_TRACK_TYPE, timeScale);
  1266. AddTrackToOd(trackId);
  1267. SetTrackFloatProperty(trackId, "tkhd.width", width);
  1268. SetTrackFloatProperty(trackId, "tkhd.height", height);
  1269. (void)InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "vmhd", 0);
  1270. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), videoType);
  1271. // stsd is a unique beast in that it has a count of the number
  1272. // of child atoms that needs to be incremented after we add the mp4v atom
  1273. MP4Integer32Property* pStsdCountProperty;
  1274. FindIntegerProperty(
  1275. MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"),
  1276. (MP4Property**)&pStsdCountProperty);
  1277. pStsdCountProperty->IncrementValue();
  1278. SetTrackIntegerProperty(trackId,
  1279. "mdia.minf.stbl.stsz.sampleSize", sampleDuration);
  1280. m_pTracks[FindTrackIndex(trackId)]->
  1281. SetFixedSampleDuration(sampleDuration);
  1282. return trackId;
  1283. }
  1284. MP4TrackId MP4File::AddMP4VideoTrack(
  1285. u_int32_t timeScale,
  1286. MP4Duration sampleDuration,
  1287. u_int16_t width,
  1288. u_int16_t height,
  1289. u_int8_t videoType)
  1290. {
  1291. MP4TrackId trackId = AddVideoTrackDefault(timeScale,
  1292. sampleDuration,
  1293. width,
  1294. height,
  1295. "mp4v");
  1296. SetTrackIntegerProperty(trackId,
  1297. "mdia.minf.stbl.stsd.mp4v.width", width);
  1298. SetTrackIntegerProperty(trackId,
  1299. "mdia.minf.stbl.stsd.mp4v.height", height);
  1300. SetTrackIntegerProperty(trackId,
  1301. "mdia.minf.stbl.stsd.mp4v.esds.ESID",
  1302. #if 0
  1303. // note - for a file, these values need to
  1304. // be 0 - wmay - 04/16/2003
  1305. trackId
  1306. #else
  1307. 0
  1308. #endif
  1309. );
  1310. SetTrackIntegerProperty(trackId,
  1311. "mdia.minf.stbl.stsd.mp4v.esds.decConfigDescr.objectTypeId",
  1312. videoType);
  1313. SetTrackIntegerProperty(trackId,
  1314. "mdia.minf.stbl.stsd.mp4v.esds.decConfigDescr.streamType",
  1315. MP4VisualStreamType);
  1316. return trackId;
  1317. }
  1318. // ismacrypted
  1319. MP4TrackId MP4File::AddEncVideoTrack(u_int32_t timeScale,
  1320. MP4Duration sampleDuration,
  1321. u_int16_t width,
  1322. u_int16_t height,
  1323. u_int8_t videoType,
  1324. mp4v2_ismacrypParams *icPp,
  1325. const char *oFormat
  1326. )
  1327. {
  1328. u_int32_t original_fmt = 0;
  1329. MP4TrackId trackId = AddVideoTrackDefault(timeScale,
  1330. sampleDuration,
  1331. width,
  1332. height,
  1333. "encv");
  1334. SetTrackIntegerProperty(trackId,
  1335. "mdia.minf.stbl.stsd.encv.width", width);
  1336. SetTrackIntegerProperty(trackId,
  1337. "mdia.minf.stbl.stsd.encv.height", height);
  1338. /* set all the ismacryp-specific values */
  1339. original_fmt = ATOMID(oFormat);
  1340. SetTrackIntegerProperty(trackId,
  1341. "mdia.minf.stbl.stsd.encv.sinf.frma.data-format",
  1342. original_fmt);
  1343. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.sinf"),
  1344. "schm");
  1345. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.sinf"),
  1346. "schi");
  1347. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.sinf.schi"),
  1348. "iKMS");
  1349. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.sinf.schi"),
  1350. "iSFM");
  1351. SetTrackIntegerProperty(trackId,
  1352. "mdia.minf.stbl.stsd.encv.sinf.schm.scheme_type",
  1353. icPp->scheme_type);
  1354. SetTrackIntegerProperty(trackId,
  1355. "mdia.minf.stbl.stsd.encv.sinf.schm.scheme_version",
  1356. icPp->scheme_version);
  1357. SetTrackStringProperty(trackId,
  1358. "mdia.minf.stbl.stsd.encv.sinf.schi.iKMS.kms_URI",
  1359. icPp->kms_uri);
  1360. SetTrackIntegerProperty(trackId,
  1361. "mdia.minf.stbl.stsd.encv.sinf.schi.iSFM.selective-encryption",
  1362. icPp->selective_enc);
  1363. SetTrackIntegerProperty(trackId,
  1364. "mdia.minf.stbl.stsd.encv.sinf.schi.iSFM.key-indicator-length",
  1365. icPp->key_ind_len);
  1366. SetTrackIntegerProperty(trackId,
  1367. "mdia.minf.stbl.stsd.encv.sinf.schi.iSFM.IV-length",
  1368. icPp->iv_len);
  1369. #if 0
  1370. if (icPp->kms_uri != NULL) {
  1371. free(icPp->kms_uri);
  1372. }
  1373. #endif
  1374. SetTrackIntegerProperty(trackId,
  1375. "mdia.minf.stbl.stsd.encv.esds.ESID",
  1376. #if 0
  1377. // note - for a file, these values need to
  1378. // be 0 - wmay - 04/16/2003
  1379. trackId
  1380. #else
  1381. 0
  1382. #endif
  1383. );
  1384. SetTrackIntegerProperty(trackId,
  1385. "mdia.minf.stbl.stsd.encv.esds.decConfigDescr.objectTypeId",
  1386. videoType);
  1387. SetTrackIntegerProperty(trackId,
  1388. "mdia.minf.stbl.stsd.encv.esds.decConfigDescr.streamType",
  1389. MP4VisualStreamType);
  1390. return trackId;
  1391. }
  1392. MP4TrackId MP4File::AddH264VideoTrack(
  1393. u_int32_t timeScale,
  1394. MP4Duration sampleDuration,
  1395. u_int16_t width,
  1396. u_int16_t height,
  1397. uint8_t AVCProfileIndication,
  1398. uint8_t profile_compat,
  1399. uint8_t AVCLevelIndication,
  1400. uint8_t sampleLenFieldSizeMinusOne)
  1401. {
  1402. MP4TrackId trackId = AddVideoTrackDefault(timeScale,
  1403. sampleDuration,
  1404. width,
  1405. height,
  1406. "avc1");
  1407. SetTrackIntegerProperty(trackId,
  1408. "mdia.minf.stbl.stsd.avc1.width", width);
  1409. SetTrackIntegerProperty(trackId,
  1410. "mdia.minf.stbl.stsd.avc1.height", height);
  1411. //FIXME - check this
  1412. // shouldn't need this
  1413. #if 0
  1414. AddChildAtom(MakeTrackName(trackId,
  1415. "mdia.minf.stbl.stsd.avc1"),
  1416. "avcC");
  1417. #endif
  1418. SetTrackIntegerProperty(trackId,
  1419. "mdia.minf.stbl.stsd.avc1.avcC.AVCProfileIndication",
  1420. AVCProfileIndication);
  1421. SetTrackIntegerProperty(trackId,
  1422. "mdia.minf.stbl.stsd.avc1.avcC.profile_compatibility",
  1423. profile_compat);
  1424. SetTrackIntegerProperty(trackId,
  1425. "mdia.minf.stbl.stsd.avc1.avcC.AVCLevelIndication",
  1426. AVCLevelIndication);
  1427. SetTrackIntegerProperty(trackId,
  1428. "mdia.minf.stbl.stsd.avc1.avcC.lengthSizeMinusOne",
  1429. sampleLenFieldSizeMinusOne);
  1430. return trackId;
  1431. }
  1432. MP4TrackId MP4File::AddEncH264VideoTrack(
  1433. u_int32_t timeScale,
  1434. MP4Duration sampleDuration,
  1435. u_int16_t width,
  1436. u_int16_t height,
  1437. MP4Atom *srcAtom,
  1438. mp4v2_ismacrypParams *icPp)
  1439. {
  1440. u_int32_t original_fmt = 0;
  1441. MP4Atom *avcCAtom;
  1442. MP4TrackId trackId = AddVideoTrackDefault(timeScale,
  1443. sampleDuration,
  1444. width,
  1445. height,
  1446. "encv");
  1447. SetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.encv.width", width);
  1448. SetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.encv.height", height);
  1449. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv"), "avcC");
  1450. // create default values
  1451. avcCAtom = FindAtomMP4File(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.avcC"));
  1452. // export source atom
  1453. ((MP4AvcCAtom *) srcAtom)->Clone((MP4AvcCAtom *)avcCAtom);
  1454. /* set all the ismacryp-specific values */
  1455. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.sinf"), "schm");
  1456. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.sinf"), "schi");
  1457. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.sinf.schi"), "iKMS");
  1458. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.sinf.schi"), "iSFM");
  1459. // per ismacrypt E&A V1.1 section 9.1.2.1 'avc1' is renamed '264b'
  1460. // avc1 must not appear as a sample entry name or original format name
  1461. original_fmt = ATOMID("264b");
  1462. SetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.encv.sinf.frma.data-format",
  1463. original_fmt);
  1464. SetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.encv.sinf.schm.scheme_type",
  1465. icPp->scheme_type);
  1466. SetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.encv.sinf.schm.scheme_version",
  1467. icPp->scheme_version);
  1468. SetTrackStringProperty(trackId, "mdia.minf.stbl.stsd.encv.sinf.schi.iKMS.kms_URI",
  1469. icPp->kms_uri);
  1470. SetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.encv.sinf.schi.iSFM.selective-encryption",
  1471. icPp->selective_enc);
  1472. SetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.encv.sinf.schi.iSFM.key-indicator-length",
  1473. icPp->key_ind_len);
  1474. SetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.encv.sinf.schi.iSFM.IV-length",
  1475. icPp->iv_len);
  1476. return trackId;
  1477. }
  1478. void MP4File::AddH264SequenceParameterSet (MP4TrackId trackId,
  1479. const uint8_t *pSequence,
  1480. uint16_t sequenceLen)
  1481. {
  1482. const char *format;
  1483. MP4Atom *avcCAtom;
  1484. // get 4cc media format - can be avc1 or encv for ismacrypted track
  1485. format = GetTrackMediaDataName(trackId);
  1486. if (!strcasecmp(format, "avc1"))
  1487. avcCAtom = FindAtomMP4File(MakeTrackName(trackId, "mdia.minf.stbl.stsd.avc1.avcC"));
  1488. else if (!strcasecmp(format, "encv"))
  1489. avcCAtom = FindAtomMP4File(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.avcC"));
  1490. else
  1491. // huh? unknown track format
  1492. return;
  1493. MP4BitfieldProperty *pCount;
  1494. MP4Integer16Property *pLength;
  1495. MP4BytesProperty *pUnit;
  1496. if ((avcCAtom->FindProperty("avcC.numOfSequenceParameterSets",
  1497. (MP4Property **)&pCount) == false) ||
  1498. (avcCAtom->FindProperty("avcC.sequenceEntries.sequenceParameterSetLength",
  1499. (MP4Property **)&pLength) == false) ||
  1500. (avcCAtom->FindProperty("avcC.sequenceEntries.sequenceParameterSetNALUnit",
  1501. (MP4Property **)&pUnit) == false)) {
  1502. VERBOSE_ERROR(m_verbosity, WARNING("Could not find avcC properties"));
  1503. return;
  1504. }
  1505. uint32_t count = pCount->GetValue();
  1506. if (count > 0) {
  1507. // see if we already exist
  1508. for (uint32_t index = 0; index < count; index++) {
  1509. if (pLength->GetValue(index) == sequenceLen) {
  1510. uint8_t *seq;
  1511. uint32_t seqlen;
  1512. pUnit->GetValue(&seq, &seqlen, index);
  1513. if (memcmp(seq, pSequence, sequenceLen) == 0) {
  1514. free(seq);
  1515. return;
  1516. }
  1517. free(seq);
  1518. }
  1519. }
  1520. }
  1521. pLength->AddValue(sequenceLen);
  1522. pUnit->AddValue(pSequence, sequenceLen);
  1523. pCount->IncrementValue();
  1524. return;
  1525. }
  1526. void MP4File::AddH264PictureParameterSet (MP4TrackId trackId,
  1527. const uint8_t *pPict,
  1528. uint16_t pictLen)
  1529. {
  1530. MP4Atom *avcCAtom =
  1531. FindAtomMP4File(MakeTrackName(trackId,
  1532. "mdia.minf.stbl.stsd.avc1.avcC"));
  1533. MP4Integer8Property *pCount;
  1534. MP4Integer16Property *pLength;
  1535. MP4BytesProperty *pUnit;
  1536. if ((avcCAtom->FindProperty("avcC.numOfPictureParameterSets",
  1537. (MP4Property **)&pCount) == false) ||
  1538. (avcCAtom->FindProperty("avcC.pictureEntries.pictureParameterSetLength",
  1539. (MP4Property **)&pLength) == false) ||
  1540. (avcCAtom->FindProperty("avcC.pictureEntries.pictureParameterSetNALUnit",
  1541. (MP4Property **)&pUnit) == false)) {
  1542. VERBOSE_ERROR(m_verbosity,
  1543. WARNING("Could not find avcC picture table properties"));
  1544. return;
  1545. }
  1546. uint32_t count = pCount->GetValue();
  1547. if (count > 0) {
  1548. // see if we already exist
  1549. for (uint32_t index = 0; index < count; index++) {
  1550. if (pLength->GetValue(index) == pictLen) {
  1551. uint8_t *seq;
  1552. uint32_t seqlen;
  1553. pUnit->GetValue(&seq, &seqlen, index);
  1554. if (memcmp(seq, pPict, pictLen) == 0) {
  1555. VERBOSE_WRITE(m_verbosity,
  1556. fprintf(stderr, "picture matches %d\n", index));
  1557. free(seq);
  1558. return;
  1559. }
  1560. free(seq);
  1561. }
  1562. }
  1563. }
  1564. pLength->AddValue(pictLen);
  1565. pUnit->AddValue(pPict, pictLen);
  1566. pCount->IncrementValue();
  1567. VERBOSE_WRITE(m_verbosity,
  1568. fprintf(stderr, "new picture added %d\n", pCount->GetValue()));
  1569. return;
  1570. }
  1571. void MP4File::SetH263Vendor(
  1572. MP4TrackId trackId,
  1573. u_int32_t vendor)
  1574. {
  1575. SetTrackIntegerProperty(trackId,
  1576. "mdia.minf.stbl.stsd.s263.d263.vendor",
  1577. vendor);
  1578. }
  1579. void MP4File::SetH263DecoderVersion(
  1580. MP4TrackId trackId,
  1581. u_int8_t decoderVersion)
  1582. {
  1583. SetTrackIntegerProperty(trackId,
  1584. "mdia.minf.stbl.stsd.s263.d263.decoderVersion",
  1585. decoderVersion);
  1586. }
  1587. void MP4File::SetH263Bitrates(
  1588. MP4TrackId trackId,
  1589. u_int32_t avgBitrate,
  1590. u_int32_t maxBitrate)
  1591. {
  1592. SetTrackIntegerProperty(trackId,
  1593. "mdia.minf.stbl.stsd.s263.d263.bitr.avgBitrate",
  1594. avgBitrate);
  1595. SetTrackIntegerProperty(trackId,
  1596. "mdia.minf.stbl.stsd.s263.d263.bitr.maxBitrate",
  1597. maxBitrate);
  1598. }
  1599. MP4TrackId MP4File::AddH263VideoTrack(
  1600. u_int32_t timeScale,
  1601. MP4Duration sampleDuration,
  1602. u_int16_t width,
  1603. u_int16_t height,
  1604. u_int8_t h263Level,
  1605. u_int8_t h263Profile,
  1606. u_int32_t avgBitrate,
  1607. u_int32_t maxBitrate)
  1608. {
  1609. MP4TrackId trackId = AddVideoTrackDefault(timeScale,
  1610. sampleDuration,
  1611. width,
  1612. height,
  1613. "s263");
  1614. SetTrackIntegerProperty(trackId,
  1615. "mdia.minf.stbl.stsd.s263.width", width);
  1616. SetTrackIntegerProperty(trackId,
  1617. "mdia.minf.stbl.stsd.s263.height", height);
  1618. SetTrackIntegerProperty(trackId,
  1619. "mdia.minf.stbl.stsd.s263.d263.h263Level", h263Level);
  1620. SetTrackIntegerProperty(trackId,
  1621. "mdia.minf.stbl.stsd.s263.d263.h263Profile", h263Profile);
  1622. // Add the bitr atom
  1623. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.s263.d263"),
  1624. "bitr");
  1625. SetTrackIntegerProperty(trackId,
  1626. "mdia.minf.stbl.stsd.s263.d263.bitr.avgBitrate", avgBitrate);
  1627. SetTrackIntegerProperty(trackId,
  1628. "mdia.minf.stbl.stsd.s263.d263.bitr.maxBitrate", maxBitrate);
  1629. SetTrackIntegerProperty(trackId,
  1630. "mdia.minf.stbl.stsz.sampleSize", sampleDuration);
  1631. return trackId;
  1632. }
  1633. MP4TrackId MP4File::AddHintTrack(MP4TrackId refTrackId)
  1634. {
  1635. // validate reference track id
  1636. (void)FindTrackIndex(refTrackId);
  1637. MP4TrackId trackId =
  1638. AddTrack(MP4_HINT_TRACK_TYPE, GetTrackTimeScale(refTrackId));
  1639. (void)InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "hmhd", 0);
  1640. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "rtp ");
  1641. // stsd is a unique beast in that it has a count of the number
  1642. // of child atoms that needs to be incremented after we add the rtp atom
  1643. MP4Integer32Property* pStsdCountProperty;
  1644. FindIntegerProperty(
  1645. MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"),
  1646. (MP4Property**)&pStsdCountProperty);
  1647. pStsdCountProperty->IncrementValue();
  1648. SetTrackIntegerProperty(trackId,
  1649. "mdia.minf.stbl.stsd.rtp .tims.timeScale",
  1650. GetTrackTimeScale(trackId));
  1651. (void)AddDescendantAtoms(MakeTrackName(trackId, NULL), "tref.hint");
  1652. AddTrackReference(MakeTrackName(trackId, "tref.hint"), refTrackId);
  1653. (void)AddDescendantAtoms(MakeTrackName(trackId, NULL), "udta.hnti.sdp ");
  1654. (void)AddDescendantAtoms(MakeTrackName(trackId, NULL), "udta.hinf");
  1655. return trackId;
  1656. }
  1657. MP4TrackId MP4File::AddTextTrack(MP4TrackId refTrackId)
  1658. {
  1659. // validate reference track id
  1660. (void)FindTrackIndex(refTrackId);
  1661. MP4TrackId trackId =
  1662. AddTrack(MP4_TEXT_TRACK_TYPE, GetTrackTimeScale(refTrackId));
  1663. (void)InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "gmhd", 0);
  1664. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "text");
  1665. // stsd is a unique beast in that it has a count of the number
  1666. // of child atoms that needs to be incremented after we add the text atom
  1667. MP4Integer32Property* pStsdCountProperty;
  1668. FindIntegerProperty(
  1669. MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"),
  1670. (MP4Property**)&pStsdCountProperty);
  1671. pStsdCountProperty->IncrementValue();
  1672. return trackId;
  1673. }
  1674. MP4TrackId MP4File::AddChapterTextTrack(MP4TrackId refTrackId, u_int32_t timescale)
  1675. {
  1676. // validate reference track id
  1677. (void)FindTrackIndex(refTrackId);
  1678. if (0 == timescale) {
  1679. timescale = GetTrackTimeScale(refTrackId);
  1680. }
  1681. MP4TrackId trackId = AddTrack(MP4_TEXT_TRACK_TYPE, timescale);
  1682. (void)InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "gmhd", 0);
  1683. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "text");
  1684. // stsd is a unique beast in that it has a count of the number
  1685. // of child atoms that needs to be incremented after we add the text atom
  1686. MP4Integer32Property* pStsdCountProperty;
  1687. FindIntegerProperty(
  1688. MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"),
  1689. (MP4Property**)&pStsdCountProperty);
  1690. pStsdCountProperty->IncrementValue();
  1691. // add a "text" atom to the generic media header
  1692. // this is different to the stsd "text" atom added above
  1693. // truth be told, it's not clear what this second "text" atom does,
  1694. // but all iTunes Store movies (with chapter markers) have it,
  1695. // as do all movies with chapter tracks made by hand in QuickTime Pro
  1696. (void)AddChildAtom(MakeTrackName(trackId, "mdia.minf.gmhd"), "text");
  1697. // disable the chapter text track
  1698. // it won't display anyway, as it has zero display size,
  1699. // but nonetheless it's good to disable it
  1700. // the track still operates as a chapter track when disabled
  1701. MP4Atom *pTkhdAtom = FindAtomMP4File(MakeTrackName(trackId, "tkhd"));
  1702. if (pTkhdAtom) {
  1703. pTkhdAtom->SetFlags(0xE);
  1704. }
  1705. // add a "chapter" track reference to our reference track,
  1706. // pointing to this new chapter track
  1707. (void)AddDescendantAtoms(MakeTrackName(refTrackId, NULL), "tref.chap");
  1708. AddTrackReference(MakeTrackName(refTrackId, "tref.chap"), trackId);
  1709. return trackId;
  1710. }
  1711. void MP4File::AddChapter(MP4TrackId chapterTrackId, MP4Duration chapterDuration, u_int32_t chapterNr, const char * chapterTitle)
  1712. {
  1713. if (0 == chapterTrackId) {
  1714. throw new MP4Error("No chapter track given","AddChapter");
  1715. }
  1716. uint32_t sampleLength = 0;
  1717. uint8_t sample[1040] = {0};
  1718. int stringLen = 0;
  1719. char *string = (char *)&(sample[2]);
  1720. if( chapterTitle != NULL )
  1721. {
  1722. stringLen = (int)strlen(chapterTitle);
  1723. strncpy( string, chapterTitle, MIN(stringLen, 1023) );
  1724. }
  1725. if( stringLen == 0 || stringLen >= 1024 )
  1726. {
  1727. snprintf( string, 1023, "Chapter %03i", chapterNr );
  1728. stringLen = (int)strlen(string);
  1729. }
  1730. sampleLength = stringLen + 2 + 12; // Account for text length code and other marker
  1731. // 2-byte length marker
  1732. sample[0] = (stringLen >> 8) & 0xff;
  1733. sample[1] = stringLen & 0xff;
  1734. int x = 2 + stringLen;
  1735. // Modifier Length Marker
  1736. sample[x] = 0x00;
  1737. sample[x+1] = 0x00;
  1738. sample[x+2] = 0x00;
  1739. sample[x+3] = 0x0C;
  1740. // Modifier Type Code
  1741. sample[x+4] = 'e';
  1742. sample[x+5] = 'n';
  1743. sample[x+6] = 'c';
  1744. sample[x+7] = 'd';
  1745. // Modifier Value
  1746. sample[x+8] = 0x00;
  1747. sample[x+9] = 0x00;
  1748. sample[x+10] = (256 >> 8) & 0xff;
  1749. sample[x+11] = 256 & 0xff;
  1750. WriteSample(chapterTrackId, sample, sampleLength, chapterDuration);
  1751. }
  1752. void MP4File::AddChapter(MP4Timestamp chapterStart, const char * chapterTitle)
  1753. {
  1754. MP4Atom * pChpl = FindAtomMP4File("moov.udta.chpl");
  1755. if (!pChpl) {
  1756. pChpl = AddDescendantAtoms("", "moov.udta.chpl");
  1757. }
  1758. char buffer[256];
  1759. int bufferLen = 0;
  1760. MP4Integer32Property * pCount = (MP4Integer32Property*)pChpl->GetProperty(3);
  1761. pCount->IncrementValue();
  1762. u_int32_t count = pCount->GetValue();
  1763. if (0 == chapterTitle) {
  1764. snprintf( buffer, 255, "Chapter %03i", count );
  1765. } else {
  1766. int len = MIN(255, (int)strlen(chapterTitle));
  1767. strncpy( buffer, chapterTitle, len );
  1768. buffer[len] = 0;
  1769. }
  1770. bufferLen = (int)strlen(buffer);
  1771. MP4TableProperty * pTable;
  1772. if (pChpl->FindProperty("chpl.chapters", (MP4Property **)&pTable)) {
  1773. MP4Integer64Property * pStartTime = (MP4Integer64Property *) pTable->GetProperty(0);
  1774. MP4StringProperty * pName = (MP4StringProperty *) pTable->GetProperty(1);
  1775. if (pStartTime && pTable) {
  1776. pStartTime->AddValue(chapterStart);
  1777. pName->AddValue(buffer);
  1778. }
  1779. }
  1780. }
  1781. void MP4File::ConvertChapters(boolean toQT)
  1782. {
  1783. if (toQT) {
  1784. MP4Chapters_t * chapters = 0;
  1785. u_int32_t chapterCount = 0;
  1786. const char * name = 0;
  1787. MP4Duration chapterDurationSum = 0;
  1788. GetChaptersList(&chapters, &chapterCount, false);
  1789. if (0 == chapterCount) {
  1790. throw new MP4Error("Could not find chapter markers", "ConvertChapters");
  1791. }
  1792. // remove chapter track if there is an existing one
  1793. DeleteChapters();
  1794. // create the chapter track
  1795. MP4TrackId refTrack = FindTrackId(0, MP4_AUDIO_TRACK_TYPE);
  1796. MP4TrackId chapterTrack = AddChapterTextTrack(refTrack, MP4_MILLISECONDS_TIME_SCALE);
  1797. // calculate the duration of the chapter track
  1798. MP4Duration chapterTrackDuration = MP4ConvertTime(GetTrackDuration(refTrack),
  1799. GetTrackTimeScale(refTrack),
  1800. MP4_MILLISECONDS_TIME_SCALE);
  1801. for (u_int32_t chapterIndex = 0 ; chapterIndex < chapterCount; ++chapterIndex) {
  1802. // calculate the duration
  1803. MP4Duration duration = chapters[chapterIndex].duration;
  1804. // sum up the chapter duration
  1805. chapterDurationSum += duration;
  1806. // create and write the chapter track sample for the previous chapter
  1807. AddChapter( chapterTrack, duration, chapterIndex+1, chapters[chapterIndex].title );
  1808. }
  1809. MP4Free(chapters);
  1810. } else {
  1811. MP4Chapters_t * chapters = 0;
  1812. u_int32_t chapterCount = 0;
  1813. GetChaptersList(&chapters, &chapterCount);
  1814. if (0 == chapterCount) {
  1815. throw new MP4Error("Could not find chapter markers", "ConvertChapters");
  1816. }
  1817. // remove existing chapters
  1818. DeleteChapters(0, false);
  1819. MP4Duration startTime = 0;
  1820. for (u_int32_t i = 0; i < chapterCount; ++i) {
  1821. const char * title = chapters[i].title;
  1822. MP4Duration duration = chapters[i].duration;
  1823. AddChapter(startTime, title);
  1824. startTime += duration * MILLI2HUNDREDNANO;
  1825. }
  1826. MP4Free(chapters);
  1827. }
  1828. }
  1829. void MP4File::DeleteChapters(MP4TrackId chapterTrackId, boolean deleteQT)
  1830. {
  1831. if (!deleteQT) {
  1832. MP4Atom * pChpl = FindAtomMP4File("moov.udta.chpl");
  1833. if (pChpl) {
  1834. MP4Atom * pParent = pChpl->GetParentAtom();
  1835. pParent->DeleteChildAtom(pChpl);
  1836. }
  1837. return;
  1838. }
  1839. char trackName[128] = {0};
  1840. // no text track given, find a suitable
  1841. if (0 == chapterTrackId) {
  1842. chapterTrackId = FindChapterTrack(trackName, 127);
  1843. } else {
  1844. FindChapterReferenceTrack(chapterTrackId, trackName, 127);
  1845. }
  1846. if (0 != chapterTrackId && 0 != trackName[0]) {
  1847. // remove the reference
  1848. RemoveTrackReference(trackName, chapterTrackId);
  1849. // remove the chapter track
  1850. DeleteTrack(chapterTrackId);
  1851. }
  1852. }
  1853. void MP4File::GetChaptersList(MP4Chapters_t ** chapterList,
  1854. u_int32_t * chapterCount,
  1855. boolean getQT)
  1856. {
  1857. *chapterList = 0;
  1858. *chapterCount = 0;
  1859. if (!getQT) {
  1860. MP4Atom * pChpl = FindAtomMP4File("moov.udta.chpl");
  1861. if (!pChpl) {
  1862. throw new MP4Error("Atom moov.udta.chpl does not exist ", "GetChaptersList");
  1863. }
  1864. MP4Integer32Property * pCounter = 0;
  1865. MP4TableProperty * pTable = 0;
  1866. MP4Integer64Property * pStartTime = 0;
  1867. MP4StringProperty * pName = 0;
  1868. MP4Duration chapterDurationSum = 0;
  1869. const char * name = 0;
  1870. if (!pChpl->FindProperty("chpl.chaptercount", (MP4Property **)&pCounter)) {
  1871. throw new MP4Error("Chapter count does not exist ", "GetChaptersList");
  1872. }
  1873. u_int32_t counter = pCounter->GetValue();
  1874. if (0 == counter) {
  1875. return;
  1876. }
  1877. if (!pChpl->FindProperty("chpl.chapters", (MP4Property **)&pTable)) {
  1878. throw new MP4Error("Chapter list does not exist ", "GetChaptersList");
  1879. }
  1880. if (0 == (pStartTime = (MP4Integer64Property *) pTable->GetProperty(0))) {
  1881. throw new MP4Error("List of Chapter starttimes does not exist ", "GetChaptersList");
  1882. }
  1883. if (0 == (pName = (MP4StringProperty *) pTable->GetProperty(1))) {
  1884. throw new MP4Error("List of Chapter titles does not exist ", "GetChaptersList");
  1885. }
  1886. MP4Chapters_t * chapters = (MP4Chapters_t*)MP4Malloc(sizeof(MP4Chapters_t) * counter);
  1887. // get the name of the first chapter
  1888. name = pName->GetValue();
  1889. // process remaining chapters
  1890. u_int32_t i, j;
  1891. for (i = 0, j = 1; i < counter; ++i, ++j) {
  1892. // insert the chapter title
  1893. u_int32_t len = MIN((u_int32_t)strlen(name), CHAPTERTITLELEN);
  1894. strncpy(chapters[i].title, name, len);
  1895. chapters[i].title[len] = 0;
  1896. // calculate the duration
  1897. MP4Duration duration = 0;
  1898. if (j < counter) {
  1899. duration = MP4ConvertTime(pStartTime->GetValue(j),
  1900. (MP4_NANOSECONDS_TIME_SCALE / 100),
  1901. MP4_MILLISECONDS_TIME_SCALE) - chapterDurationSum;
  1902. // now get the name of the chapter (to be written next)
  1903. name = pName->GetValue(j);
  1904. } else {
  1905. // last chapter
  1906. duration = MP4ConvertTime(GetDuration(), GetTimeScale(), MP4_MILLISECONDS_TIME_SCALE) - chapterDurationSum;
  1907. }
  1908. // sum up the chapter duration
  1909. chapterDurationSum += duration;
  1910. // insert the chapter duration
  1911. chapters[i].duration = duration;
  1912. }
  1913. *chapterList = chapters;
  1914. *chapterCount = counter;
  1915. // ok, we're done
  1916. return;
  1917. }
  1918. u_int8_t * sample = 0;
  1919. u_int32_t sampleSize = 0;
  1920. MP4Timestamp startTime = 0;
  1921. MP4Duration duration = 0;
  1922. // get the chapter track
  1923. MP4TrackId chapterTrackId = FindChapterTrack();
  1924. if (0 == chapterTrackId) {
  1925. throw new MP4Error("Could not find a chapter track", "GetChaptersList");
  1926. }
  1927. // get infos about the chapters
  1928. MP4Track * pChapterTrack = GetTrack(chapterTrackId);
  1929. u_int32_t counter = pChapterTrack->GetNumberOfSamples();
  1930. u_int32_t timescale = pChapterTrack->GetTimeScale();
  1931. MP4Chapters_t * chapters = (MP4Chapters_t*)MP4Malloc(sizeof(MP4Chapters_t) * counter);
  1932. // process all chapter sample
  1933. for (u_int32_t i = 0; i < counter; ++i) {
  1934. // get the sample corresponding to the starttime
  1935. MP4SampleId sampleId = pChapterTrack->GetSampleIdFromTime(startTime + duration, true);
  1936. pChapterTrack->ReadSample(sampleId, &sample, &sampleSize);
  1937. // get the starttime and duration
  1938. pChapterTrack->GetSampleTimes(sampleId, &startTime, &duration);
  1939. // we know that sample+2 contains the title
  1940. const char * title = (const char *)&(sample[2]);
  1941. int len = MIN((int)strlen(title), CHAPTERTITLELEN);
  1942. strncpy(chapters[i].title, title, len);
  1943. chapters[i].title[len] = 0;
  1944. // write the duration (in milliseconds)
  1945. chapters[i].duration = MP4ConvertTime(duration, timescale, MP4_MILLISECONDS_TIME_SCALE);
  1946. // we're done with this sample
  1947. MP4Free(sample);
  1948. sample = 0;
  1949. }
  1950. *chapterList = chapters;
  1951. *chapterCount = counter;
  1952. }
  1953. MP4TrackId MP4File::FindChapterTrack(char * trackName, int trackNameSize)
  1954. {
  1955. for (u_int32_t i = 0; i < m_pTracks.Size(); i++) {
  1956. if (!strcmp(MP4_TEXT_TRACK_TYPE, m_pTracks[i]->GetType())) {
  1957. MP4TrackId refTrackId = FindChapterReferenceTrack(m_pTracks[i]->GetId(), trackName, trackNameSize);
  1958. if (0 != refTrackId) {
  1959. return m_pTracks[i]->GetId();
  1960. }
  1961. }
  1962. }
  1963. return 0;
  1964. }
  1965. MP4TrackId MP4File::FindChapterReferenceTrack(MP4TrackId chapterTrackId, char * trackName, size_t trackNameSize)
  1966. {
  1967. for (u_int32_t i = 0; i < m_pTracks.Size(); i++) {
  1968. if (!strcmp(MP4_AUDIO_TRACK_TYPE, m_pTracks[i]->GetType())) {
  1969. MP4TrackId refTrackId = m_pTracks[i]->GetId();
  1970. char * name = MakeTrackName(refTrackId, "tref.chap");
  1971. if (FindTrackReference(name, chapterTrackId)) {
  1972. if (0 != trackName) {
  1973. strncpy(trackName, name, MIN(strlen(name),trackNameSize));
  1974. }
  1975. return m_pTracks[i]->GetId();
  1976. }
  1977. }
  1978. }
  1979. return 0;
  1980. }
  1981. void MP4File::DeleteTrack(MP4TrackId trackId)
  1982. {
  1983. ProtectWriteOperation("MP4DeleteTrack");
  1984. u_int32_t trakIndex = FindTrakAtomIndex(trackId);
  1985. u_int16_t trackIndex = FindTrackIndex(trackId);
  1986. MP4Track* pTrack = m_pTracks[trackIndex];
  1987. MP4Atom* pTrakAtom = pTrack->GetTrakAtom();
  1988. ASSERT(pTrakAtom);
  1989. MP4Atom* pMoovAtom = FindAtomMP4File("moov");
  1990. ASSERT(pMoovAtom);
  1991. RemoveTrackFromIod(trackId, ShallHaveIods());
  1992. RemoveTrackFromOd(trackId);
  1993. if (trackId == m_odTrackId) {
  1994. m_odTrackId = 0;
  1995. }
  1996. pMoovAtom->DeleteChildAtom(pTrakAtom);
  1997. m_trakIds.Delete(trakIndex);
  1998. m_pTracks.Delete(trackIndex);
  1999. delete pTrack;
  2000. delete pTrakAtom;
  2001. }
  2002. u_int32_t MP4File::GetNumberOfTracks(const char* type, u_int8_t subType)
  2003. {
  2004. if (type == NULL) {
  2005. return m_pTracks.Size();
  2006. }
  2007. u_int32_t typeSeen = 0;
  2008. const char* normType = MP4NormalizeTrackType(type, m_verbosity);
  2009. for (u_int32_t i = 0; i < m_pTracks.Size(); i++) {
  2010. if (!strcmp(normType, m_pTracks[i]->GetType())) {
  2011. if (subType) {
  2012. if (normType == MP4_AUDIO_TRACK_TYPE) {
  2013. if (subType != GetTrackEsdsObjectTypeId(m_pTracks[i]->GetId())) {
  2014. continue;
  2015. }
  2016. } else if (normType == MP4_VIDEO_TRACK_TYPE) {
  2017. if (subType != GetTrackEsdsObjectTypeId(m_pTracks[i]->GetId())) {
  2018. continue;
  2019. }
  2020. }
  2021. // else unknown subtype, ignore it
  2022. }
  2023. typeSeen++;
  2024. }
  2025. }
  2026. return typeSeen;
  2027. }
  2028. MP4TrackId MP4File::AllocTrackId()
  2029. {
  2030. MP4TrackId trackId =
  2031. GetIntegerProperty("moov.mvhd.nextTrackId");
  2032. if (trackId <= 0xFFFF) {
  2033. // check that nextTrackid is correct
  2034. try {
  2035. (void)FindTrackIndex(trackId);
  2036. // ERROR, this trackId is in use
  2037. }
  2038. catch (MP4Error* e) {
  2039. // OK, this trackId is not in use, proceed
  2040. delete e;
  2041. SetIntegerProperty("moov.mvhd.nextTrackId", trackId + 1);
  2042. return trackId;
  2043. }
  2044. }
  2045. // we need to search for a track id
  2046. for (trackId = 1; trackId <= 0xFFFF; trackId++) {
  2047. try {
  2048. (void)FindTrackIndex(trackId);
  2049. // KEEP LOOKING, this trackId is in use
  2050. }
  2051. catch (MP4Error* e) {
  2052. // OK, this trackId is not in use, proceed
  2053. delete e;
  2054. return trackId;
  2055. }
  2056. }
  2057. // extreme case where mp4 file has 2^16 tracks in it
  2058. throw new MP4Error("too many existing tracks", "AddTrack");
  2059. return MP4_INVALID_TRACK_ID; // to keep MSVC happy
  2060. }
  2061. MP4TrackId MP4File::FindTrackId(u_int16_t trackIndex,
  2062. const char* type, u_int8_t subType)
  2063. {
  2064. if (type == NULL) {
  2065. return m_pTracks[trackIndex]->GetId();
  2066. }
  2067. u_int32_t typeSeen = 0;
  2068. const char* normType = MP4NormalizeTrackType(type, m_verbosity);
  2069. for (u_int32_t i = 0; i < m_pTracks.Size(); i++) {
  2070. if (!strcmp(normType, m_pTracks[i]->GetType())) {
  2071. if (subType) {
  2072. if (normType == MP4_AUDIO_TRACK_TYPE) {
  2073. if (subType != GetTrackEsdsObjectTypeId(m_pTracks[i]->GetId())) {
  2074. continue;
  2075. }
  2076. } else if (normType == MP4_VIDEO_TRACK_TYPE) {
  2077. if (subType != GetTrackEsdsObjectTypeId(m_pTracks[i]->GetId())) {
  2078. continue;
  2079. }
  2080. }
  2081. // else unknown subtype, ignore it
  2082. }
  2083. if (trackIndex == typeSeen) {
  2084. return m_pTracks[i]->GetId();
  2085. }
  2086. typeSeen++;
  2087. }
  2088. }
  2089. throw new MP4Error("Track index doesn't exist - track %d type %s",
  2090. "FindTrackId",
  2091. trackIndex, type);
  2092. return MP4_INVALID_TRACK_ID; // satisfy MS compiler
  2093. }
  2094. u_int16_t MP4File::FindTrackIndex(MP4TrackId trackId)
  2095. {
  2096. for (u_int32_t i = 0; i < m_pTracks.Size() && i <= 0xFFFF; i++) {
  2097. if (m_pTracks[i]->GetId() == trackId) {
  2098. return (u_int16_t)i;
  2099. }
  2100. }
  2101. throw new MP4Error("Track id %d doesn't exist", "FindTrackIndex", trackId);
  2102. return (u_int16_t)-1; // satisfy MS compiler
  2103. }
  2104. u_int16_t MP4File::FindTrakAtomIndex(MP4TrackId trackId)
  2105. {
  2106. if (trackId) {
  2107. for (u_int32_t i = 0; i < m_trakIds.Size(); i++) {
  2108. if (m_trakIds[i] == trackId) {
  2109. return i;
  2110. }
  2111. }
  2112. }
  2113. throw new MP4Error("Track id %d doesn't exist", "FindTrakAtomIndex",
  2114. trackId);
  2115. return (u_int16_t)-1; // satisfy MS compiler
  2116. }
  2117. u_int32_t MP4File::GetSampleSize(MP4TrackId trackId, MP4SampleId sampleId)
  2118. {
  2119. return m_pTracks[FindTrackIndex(trackId)]->GetSampleSize(sampleId);
  2120. }
  2121. u_int32_t MP4File::GetTrackMaxSampleSize(MP4TrackId trackId)
  2122. {
  2123. return m_pTracks[FindTrackIndex(trackId)]->GetMaxSampleSize();
  2124. }
  2125. MP4SampleId MP4File::GetSampleIdFromTime(MP4TrackId trackId,
  2126. MP4Timestamp when, bool wantSyncSample, bool rewind)
  2127. {
  2128. return m_pTracks[FindTrackIndex(trackId)]->
  2129. GetSampleIdFromTime(when, wantSyncSample, rewind);
  2130. }
  2131. MP4ChunkId MP4File::GetChunkIdFromTime(MP4TrackId trackId, MP4Timestamp when)
  2132. {
  2133. return m_pTracks[FindTrackIndex(trackId)]->
  2134. GetChunkIdFromTime(when);
  2135. }
  2136. MP4Timestamp MP4File::GetSampleTime(
  2137. MP4TrackId trackId, MP4SampleId sampleId)
  2138. {
  2139. MP4Timestamp timestamp;
  2140. m_pTracks[FindTrackIndex(trackId)]->
  2141. GetSampleTimes(sampleId, &timestamp, NULL);
  2142. return timestamp;
  2143. }
  2144. MP4Duration MP4File::GetSampleDuration(
  2145. MP4TrackId trackId, MP4SampleId sampleId)
  2146. {
  2147. MP4Duration duration;
  2148. m_pTracks[FindTrackIndex(trackId)]->
  2149. GetSampleTimes(sampleId, NULL, &duration);
  2150. return duration;
  2151. }
  2152. MP4Duration MP4File::GetSampleRenderingOffset(
  2153. MP4TrackId trackId, MP4SampleId sampleId)
  2154. {
  2155. return m_pTracks[FindTrackIndex(trackId)]->
  2156. GetSampleRenderingOffset(sampleId);
  2157. }
  2158. bool MP4File::GetSampleSync(MP4TrackId trackId, MP4SampleId sampleId)
  2159. {
  2160. return m_pTracks[FindTrackIndex(trackId)]->IsSyncSample(sampleId);
  2161. }
  2162. void MP4File::ReadSample(MP4TrackId trackId, MP4SampleId sampleId,
  2163. u_int8_t** ppBytes, u_int32_t* pNumBytes,
  2164. MP4Timestamp* pStartTime, MP4Duration* pDuration,
  2165. MP4Duration* pRenderingOffset, bool* pIsSyncSample)
  2166. {
  2167. m_pTracks[FindTrackIndex(trackId)]->
  2168. ReadSample(sampleId, ppBytes, pNumBytes,
  2169. pStartTime, pDuration, pRenderingOffset, pIsSyncSample);
  2170. }
  2171. void MP4File::ReadChunk(MP4TrackId trackId, MP4ChunkId sampleId,
  2172. u_int8_t** ppBytes, u_int32_t* pNumBytes,
  2173. MP4Timestamp* pStartTime, MP4Duration* pDuration)
  2174. {
  2175. m_pTracks[FindTrackIndex(trackId)]->
  2176. ReadChunk(sampleId, ppBytes, pNumBytes, pStartTime, pDuration);
  2177. }
  2178. void MP4File::WriteSample(MP4TrackId trackId,
  2179. const u_int8_t* pBytes, u_int32_t numBytes,
  2180. MP4Duration duration, MP4Duration renderingOffset, bool isSyncSample)
  2181. {
  2182. ProtectWriteOperation("MP4WriteSample");
  2183. m_pTracks[FindTrackIndex(trackId)]->
  2184. WriteSample(pBytes, numBytes, duration, renderingOffset, isSyncSample);
  2185. m_pModificationProperty->SetValue(MP4GetAbsTimestamp());
  2186. }
  2187. void MP4File::SetSampleRenderingOffset(MP4TrackId trackId,
  2188. MP4SampleId sampleId, MP4Duration renderingOffset)
  2189. {
  2190. ProtectWriteOperation("MP4SetSampleRenderingOffset");
  2191. m_pTracks[FindTrackIndex(trackId)]->
  2192. SetSampleRenderingOffset(sampleId, renderingOffset);
  2193. m_pModificationProperty->SetValue(MP4GetAbsTimestamp());
  2194. }
  2195. char* MP4File::MakeTrackName(MP4TrackId trackId, const char* name)
  2196. {
  2197. u_int16_t trakIndex = FindTrakAtomIndex(trackId);
  2198. if (name == NULL || name[0] == '\0') {
  2199. snprintf(m_trakName, sizeof(m_trakName),
  2200. "moov.trak[%u]", trakIndex);
  2201. } else {
  2202. snprintf(m_trakName, sizeof(m_trakName),
  2203. "moov.trak[%u].%s", trakIndex, name);
  2204. }
  2205. return m_trakName;
  2206. }
  2207. MP4Atom *MP4File::FindTrackAtom (MP4TrackId trackId, const char *name)
  2208. {
  2209. return FindAtomMP4File(MakeTrackName(trackId, name));
  2210. }
  2211. u_int64_t MP4File::GetTrackIntegerProperty(MP4TrackId trackId, const char* name)
  2212. {
  2213. return GetIntegerProperty(MakeTrackName(trackId, name));
  2214. }
  2215. void MP4File::SetTrackIntegerProperty(MP4TrackId trackId, const char* name,
  2216. int64_t value)
  2217. {
  2218. SetIntegerProperty(MakeTrackName(trackId, name), value);
  2219. }
  2220. float MP4File::GetTrackFloatProperty(MP4TrackId trackId, const char* name)
  2221. {
  2222. return GetFloatProperty(MakeTrackName(trackId, name));
  2223. }
  2224. void MP4File::SetTrackFloatProperty(MP4TrackId trackId, const char* name,
  2225. float value)
  2226. {
  2227. SetFloatProperty(MakeTrackName(trackId, name), value);
  2228. }
  2229. const char* MP4File::GetTrackStringProperty(MP4TrackId trackId, const char* name)
  2230. {
  2231. return GetStringProperty(MakeTrackName(trackId, name));
  2232. }
  2233. void MP4File::SetTrackStringProperty(MP4TrackId trackId, const char* name,
  2234. const char* value)
  2235. {
  2236. SetStringProperty(MakeTrackName(trackId, name), value);
  2237. }
  2238. void MP4File::GetTrackBytesProperty(MP4TrackId trackId, const char* name,
  2239. u_int8_t** ppValue, u_int32_t* pValueSize)
  2240. {
  2241. GetBytesProperty(MakeTrackName(trackId, name), ppValue, pValueSize);
  2242. }
  2243. void MP4File::SetTrackBytesProperty(MP4TrackId trackId, const char* name,
  2244. const u_int8_t* pValue, u_int32_t valueSize)
  2245. {
  2246. SetBytesProperty(MakeTrackName(trackId, name), pValue, valueSize);
  2247. }
  2248. // file level convenience functions
  2249. MP4Duration MP4File::GetDuration()
  2250. {
  2251. return m_pDurationProperty->GetValue();
  2252. }
  2253. void MP4File::SetDuration(MP4Duration value)
  2254. {
  2255. m_pDurationProperty->SetValue(value);
  2256. }
  2257. u_int32_t MP4File::GetTimeScale()
  2258. {
  2259. return m_pTimeScaleProperty->GetValue();
  2260. }
  2261. void MP4File::SetTimeScale(u_int32_t value)
  2262. {
  2263. if (value == 0) {
  2264. throw new MP4Error("invalid value", "SetTimeScale");
  2265. }
  2266. m_pTimeScaleProperty->SetValue(value);
  2267. }
  2268. u_int8_t MP4File::GetODProfileLevel()
  2269. {
  2270. return GetIntegerProperty("moov.iods.ODProfileLevelId");
  2271. }
  2272. void MP4File::SetODProfileLevel(u_int8_t value)
  2273. {
  2274. SetIntegerProperty("moov.iods.ODProfileLevelId", value);
  2275. }
  2276. u_int8_t MP4File::GetSceneProfileLevel()
  2277. {
  2278. return GetIntegerProperty("moov.iods.sceneProfileLevelId");
  2279. }
  2280. void MP4File::SetSceneProfileLevel(u_int8_t value)
  2281. {
  2282. SetIntegerProperty("moov.iods.sceneProfileLevelId", value);
  2283. }
  2284. u_int8_t MP4File::GetVideoProfileLevel()
  2285. {
  2286. return GetIntegerProperty("moov.iods.visualProfileLevelId");
  2287. }
  2288. void MP4File::SetVideoProfileLevel(u_int8_t value)
  2289. {
  2290. SetIntegerProperty("moov.iods.visualProfileLevelId", value);
  2291. }
  2292. u_int8_t MP4File::GetAudioProfileLevel()
  2293. {
  2294. return GetIntegerProperty("moov.iods.audioProfileLevelId");
  2295. }
  2296. void MP4File::SetAudioProfileLevel(u_int8_t value)
  2297. {
  2298. SetIntegerProperty("moov.iods.audioProfileLevelId", value);
  2299. }
  2300. u_int8_t MP4File::GetGraphicsProfileLevel()
  2301. {
  2302. return GetIntegerProperty("moov.iods.graphicsProfileLevelId");
  2303. }
  2304. void MP4File::SetGraphicsProfileLevel(u_int8_t value)
  2305. {
  2306. SetIntegerProperty("moov.iods.graphicsProfileLevelId", value);
  2307. }
  2308. const char* MP4File::GetSessionSdp()
  2309. {
  2310. return GetStringProperty("moov.udta.hnti.rtp .sdpText");
  2311. }
  2312. void MP4File::SetSessionSdp(const char* sdpString)
  2313. {
  2314. (void)AddDescendantAtoms("moov", "udta.hnti.rtp ");
  2315. SetStringProperty("moov.udta.hnti.rtp .sdpText", sdpString);
  2316. }
  2317. void MP4File::AppendSessionSdp(const char* sdpFragment)
  2318. {
  2319. const char* oldSdpString = NULL;
  2320. try {
  2321. oldSdpString = GetSessionSdp();
  2322. }
  2323. catch (MP4Error* e) {
  2324. delete e;
  2325. SetSessionSdp(sdpFragment);
  2326. return;
  2327. }
  2328. char* newSdpString =
  2329. (char*)MP4Malloc(strlen(oldSdpString) + strlen(sdpFragment) + 1);
  2330. strcpy(newSdpString, oldSdpString);
  2331. strcat(newSdpString, sdpFragment);
  2332. SetSessionSdp(newSdpString);
  2333. MP4Free(newSdpString);
  2334. }
  2335. //
  2336. // ismacrypt API - retrieve OriginalFormatBox
  2337. //
  2338. // parameters are assumed to have been sanity tested in mp4.cpp
  2339. // don't call this unless media data name is 'encv',
  2340. // results may otherwise be unpredictable.
  2341. //
  2342. // input:
  2343. // trackID - valid encv track ID for this file
  2344. // buflen - length of oFormat, minimum is 5 (4cc plus null terminator)
  2345. //
  2346. // output:
  2347. // oFormat - buffer to return null terminated string containing
  2348. // track original format
  2349. // return:
  2350. // 0 - original format returned OK
  2351. // 1 - buffer length error or problem retrieving track property
  2352. //
  2353. //
  2354. bool MP4File::GetTrackMediaDataOriginalFormat(MP4TrackId trackId,
  2355. char *originalFormat, u_int32_t buflen)
  2356. {
  2357. u_int32_t format;
  2358. if (buflen < 5)
  2359. return false;
  2360. format = GetTrackIntegerProperty(trackId,
  2361. "mdia.minf.stbl.stsd.*.sinf.frma.data-format");
  2362. IDATOM(format, originalFormat);
  2363. return true;
  2364. }
  2365. // track level convenience functions
  2366. MP4SampleId MP4File::GetTrackNumberOfSamples(MP4TrackId trackId)
  2367. {
  2368. return m_pTracks[FindTrackIndex(trackId)]->GetNumberOfSamples();
  2369. }
  2370. MP4ChunkId MP4File::GetTrackNumberOfChunks(MP4TrackId trackId)
  2371. {
  2372. return m_pTracks[FindTrackIndex(trackId)]->GetNumberOfChunks();
  2373. }
  2374. const char* MP4File::GetTrackType(MP4TrackId trackId)
  2375. {
  2376. return m_pTracks[FindTrackIndex(trackId)]->GetType();
  2377. }
  2378. const char *MP4File::GetTrackMediaDataName (MP4TrackId trackId)
  2379. {
  2380. MP4Atom *pChild;
  2381. MP4Atom *pAtom =
  2382. FindAtomMP4File(MakeTrackName(trackId,
  2383. "mdia.minf.stbl.stsd"));
  2384. if (!pAtom || pAtom->GetNumberOfChildAtoms() != 1) {
  2385. VERBOSE_ERROR(m_verbosity,
  2386. fprintf(stderr, "track %d has more than 1 child atoms in stsd\n", trackId));
  2387. return NULL;
  2388. }
  2389. pChild = pAtom->GetChildAtom(0);
  2390. return pChild->GetType();
  2391. }
  2392. u_int32_t MP4File::GetTrackTimeScale(MP4TrackId trackId)
  2393. {
  2394. return m_pTracks[FindTrackIndex(trackId)]->GetTimeScale();
  2395. }
  2396. void MP4File::SetTrackTimeScale(MP4TrackId trackId, u_int32_t value)
  2397. {
  2398. if (value == 0) {
  2399. throw new MP4Error("invalid value", "SetTrackTimeScale");
  2400. }
  2401. SetTrackIntegerProperty(trackId, "mdia.mdhd.timeScale", value);
  2402. }
  2403. MP4Duration MP4File::GetTrackDuration(MP4TrackId trackId)
  2404. {
  2405. return GetTrackIntegerProperty(trackId, "mdia.mdhd.duration");
  2406. }
  2407. u_int8_t MP4File::GetTrackEsdsObjectTypeId(MP4TrackId trackId)
  2408. {
  2409. // changed mp4a to * to handle enca case
  2410. try {
  2411. return GetTrackIntegerProperty(trackId,
  2412. "mdia.minf.stbl.stsd.*.esds.decConfigDescr.objectTypeId");
  2413. } catch (MP4Error *e) {
  2414. delete e;
  2415. return GetTrackIntegerProperty(trackId,
  2416. "mdia.minf.stbl.stsd.*.*.esds.decConfigDescr.objectTypeId");
  2417. }
  2418. }
  2419. u_int8_t MP4File::GetTrackAudioMpeg4Type(MP4TrackId trackId)
  2420. {
  2421. // verify that track is an MPEG-4 audio track
  2422. if (GetTrackEsdsObjectTypeId(trackId) != MP4_MPEG4_AUDIO_TYPE) {
  2423. return MP4_MPEG4_INVALID_AUDIO_TYPE;
  2424. }
  2425. u_int8_t* pEsConfig = NULL;
  2426. u_int32_t esConfigSize;
  2427. // The Mpeg4 audio type (AAC, CELP, HXVC, ...)
  2428. // is the first 5 bits of the ES configuration
  2429. GetTrackESConfiguration(trackId, &pEsConfig, &esConfigSize);
  2430. if (esConfigSize < 1) {
  2431. free(pEsConfig);
  2432. return MP4_MPEG4_INVALID_AUDIO_TYPE;
  2433. }
  2434. u_int8_t mpeg4Type = ((pEsConfig[0] >> 3) & 0x1f);
  2435. // TTTT TXXX XXX potentially 6 bits of extension.
  2436. if (mpeg4Type == 0x1f) {
  2437. if (esConfigSize < 2) {
  2438. free(pEsConfig);
  2439. return MP4_MPEG4_INVALID_AUDIO_TYPE;
  2440. }
  2441. mpeg4Type = 32 +
  2442. (((pEsConfig[0] & 0x7) << 3) | ((pEsConfig[1] >> 5) & 0x7));
  2443. }
  2444. free(pEsConfig);
  2445. return mpeg4Type;
  2446. }
  2447. MP4Duration MP4File::GetTrackFixedSampleDuration(MP4TrackId trackId)
  2448. {
  2449. return m_pTracks[FindTrackIndex(trackId)]->GetFixedSampleDuration();
  2450. }
  2451. double MP4File::GetTrackVideoFrameRate(MP4TrackId trackId)
  2452. {
  2453. MP4SampleId numSamples =
  2454. GetTrackNumberOfSamples(trackId);
  2455. u_int64_t
  2456. msDuration =
  2457. ConvertFromTrackDuration(trackId,
  2458. GetTrackDuration(trackId), MP4_MSECS_TIME_SCALE);
  2459. if (msDuration == 0) {
  2460. return 0.0;
  2461. }
  2462. return ((double)numSamples / UINT64_TO_DOUBLE(msDuration)) * MP4_MSECS_TIME_SCALE;
  2463. }
  2464. int MP4File::GetTrackAudioChannels (MP4TrackId trackId)
  2465. {
  2466. return GetTrackIntegerProperty(trackId,
  2467. "mdia.minf.stbl.stsd.*[0].channels");
  2468. }
  2469. // true if media track encrypted according to ismacryp
  2470. bool MP4File::IsIsmaCrypMediaTrack(MP4TrackId trackId)
  2471. {
  2472. if (GetTrackIntegerProperty(trackId,
  2473. "mdia.minf.stbl.stsd.*.sinf.frma.data-format")
  2474. != (u_int64_t)-1) {
  2475. return true;
  2476. }
  2477. return false;
  2478. }
  2479. void MP4File::GetTrackESConfiguration(MP4TrackId trackId,
  2480. u_int8_t** ppConfig, u_int32_t* pConfigSize)
  2481. {
  2482. try {
  2483. GetTrackBytesProperty(trackId,
  2484. "mdia.minf.stbl.stsd.*[0].esds.decConfigDescr.decSpecificInfo[0].info",
  2485. ppConfig, pConfigSize);
  2486. } catch (MP4Error *e) {
  2487. delete e;
  2488. GetTrackBytesProperty(trackId,
  2489. "mdia.minf.stbl.stsd.*[0].*.esds.decConfigDescr.decSpecificInfo[0].info",
  2490. ppConfig, pConfigSize);
  2491. }
  2492. }
  2493. void MP4File::GetTrackVideoMetadata(MP4TrackId trackId,
  2494. u_int8_t** ppConfig, u_int32_t* pConfigSize)
  2495. {
  2496. GetTrackBytesProperty(trackId,
  2497. "mdia.minf.stbl.stsd.*[0].*.metadata",
  2498. ppConfig, pConfigSize);
  2499. }
  2500. void MP4File::SetTrackESConfiguration(MP4TrackId trackId,
  2501. const u_int8_t* pConfig, u_int32_t configSize)
  2502. {
  2503. // get a handle on the track decoder config descriptor
  2504. MP4DescriptorProperty* pConfigDescrProperty = NULL;
  2505. if (FindProperty(MakeTrackName(trackId,
  2506. "mdia.minf.stbl.stsd.*[0].esds.decConfigDescr.decSpecificInfo"),
  2507. (MP4Property**)&pConfigDescrProperty) == false ||
  2508. pConfigDescrProperty == NULL) {
  2509. // probably trackId refers to a hint track
  2510. throw new MP4Error("no such property", "MP4SetTrackESConfiguration");
  2511. }
  2512. // lookup the property to store the configuration
  2513. MP4BytesProperty* pInfoProperty = NULL;
  2514. (void)pConfigDescrProperty->FindProperty("decSpecificInfo[0].info",
  2515. (MP4Property**)&pInfoProperty);
  2516. // configuration being set for the first time
  2517. if (pInfoProperty == NULL) {
  2518. // need to create a new descriptor to hold it
  2519. MP4Descriptor* pConfigDescr =
  2520. pConfigDescrProperty->AddDescriptor(MP4DecSpecificDescrTag);
  2521. pConfigDescr->Generate();
  2522. (void)pConfigDescrProperty->FindProperty(
  2523. "decSpecificInfo[0].info",
  2524. (MP4Property**)&pInfoProperty);
  2525. ASSERT(pInfoProperty);
  2526. }
  2527. // set the value
  2528. pInfoProperty->SetValue(pConfig, configSize);
  2529. }
  2530. void MP4File::GetTrackH264SeqPictHeaders (MP4TrackId trackId,
  2531. uint8_t ***pppSeqHeader,
  2532. uint32_t **ppSeqHeaderSize,
  2533. uint8_t ***pppPictHeader,
  2534. uint32_t **ppPictHeaderSize)
  2535. {
  2536. uint32_t count;
  2537. const char *format;
  2538. MP4Atom *avcCAtom;
  2539. *pppSeqHeader = NULL; *pppPictHeader = NULL;
  2540. *ppSeqHeaderSize = NULL; *ppPictHeaderSize = NULL;
  2541. // get 4cc media format - can be avc1 or encv for ismacrypted track
  2542. format = GetTrackMediaDataName (trackId);
  2543. if (!strcasecmp(format, "avc1"))
  2544. avcCAtom = FindAtomMP4File(MakeTrackName(trackId, "mdia.minf.stbl.stsd.avc1.avcC"));
  2545. else if (!strcasecmp(format, "encv"))
  2546. avcCAtom = FindAtomMP4File(MakeTrackName(trackId, "mdia.minf.stbl.stsd.encv.avcC"));
  2547. else
  2548. // huh? unknown track format
  2549. return;
  2550. MP4BitfieldProperty *pSeqCount;
  2551. MP4IntegerProperty *pSeqLen, *pPictCount, *pPictLen;
  2552. MP4BytesProperty *pSeqVal, *pPictVal;
  2553. if ((avcCAtom->FindProperty("avcC.numOfSequenceParameterSets",
  2554. (MP4Property **)&pSeqCount) == false) ||
  2555. (avcCAtom->FindProperty("avcC.sequenceEntries.sequenceParameterSetLength",
  2556. (MP4Property **)&pSeqLen) == false) ||
  2557. (avcCAtom->FindProperty("avcC.sequenceEntries.sequenceParameterSetNALUnit",
  2558. (MP4Property **)&pSeqVal) == false)) {
  2559. VERBOSE_ERROR(m_verbosity, WARNING("Could not find avcC properties"));
  2560. return ;
  2561. }
  2562. uint8_t **ppSeqHeader =
  2563. (uint8_t **)malloc((pSeqCount->GetValue() + 1) * sizeof(uint8_t *));
  2564. if (ppSeqHeader == NULL) return;
  2565. *pppSeqHeader = ppSeqHeader;
  2566. uint32_t *pSeqHeaderSize =
  2567. (uint32_t *)malloc((pSeqCount->GetValue() + 1) * sizeof(uint32_t *));
  2568. if (pSeqHeaderSize == NULL) return;
  2569. *ppSeqHeaderSize = pSeqHeaderSize;
  2570. for (count = 0; count < pSeqCount->GetValue(); count++) {
  2571. pSeqVal->GetValue(&(ppSeqHeader[count]), &(pSeqHeaderSize[count]),
  2572. count);
  2573. }
  2574. ppSeqHeader[count] = NULL;
  2575. pSeqHeaderSize[count] = 0;
  2576. if ((avcCAtom->FindProperty("avcC.numOfPictureParameterSets",
  2577. (MP4Property **)&pPictCount) == false) ||
  2578. (avcCAtom->FindProperty("avcC.pictureEntries.pictureParameterSetLength",
  2579. (MP4Property **)&pPictLen) == false) ||
  2580. (avcCAtom->FindProperty("avcC.pictureEntries.pictureParameterSetNALUnit",
  2581. (MP4Property **)&pPictVal) == false)) {
  2582. VERBOSE_ERROR(m_verbosity,
  2583. WARNING("Could not find avcC picture table properties"));
  2584. return ;
  2585. }
  2586. uint8_t
  2587. **ppPictHeader =
  2588. (uint8_t **)malloc((pPictCount->GetValue() + 1) * sizeof(uint8_t *));
  2589. if (ppPictHeader == NULL) return;
  2590. uint32_t *pPictHeaderSize =
  2591. (uint32_t *)malloc((pPictCount->GetValue() + 1)* sizeof(uint32_t *));
  2592. if (pPictHeaderSize == NULL) {
  2593. free(ppPictHeader);
  2594. return;
  2595. }
  2596. *pppPictHeader = ppPictHeader;
  2597. *ppPictHeaderSize = pPictHeaderSize;
  2598. for (count = 0; count < pPictCount->GetValue(); count++) {
  2599. pPictVal->GetValue(&(ppPictHeader[count]), &(pPictHeaderSize[count]),
  2600. count);
  2601. }
  2602. ppPictHeader[count] = NULL;
  2603. pPictHeaderSize[count] = 0;
  2604. return ;
  2605. }
  2606. const char* MP4File::GetHintTrackSdp(MP4TrackId hintTrackId)
  2607. {
  2608. return GetTrackStringProperty(hintTrackId, "udta.hnti.sdp .sdpText");
  2609. }
  2610. void MP4File::SetHintTrackSdp(MP4TrackId hintTrackId, const char* sdpString)
  2611. {
  2612. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2613. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2614. throw new MP4Error("track is not a hint track",
  2615. "MP4SetHintTrackSdp");
  2616. }
  2617. (void)AddDescendantAtoms(
  2618. MakeTrackName(hintTrackId, NULL), "udta.hnti.sdp ");
  2619. SetTrackStringProperty(hintTrackId, "udta.hnti.sdp .sdpText", sdpString);
  2620. }
  2621. void MP4File::AppendHintTrackSdp(MP4TrackId hintTrackId,
  2622. const char* sdpFragment)
  2623. {
  2624. const char* oldSdpString = NULL;
  2625. try {
  2626. oldSdpString = GetHintTrackSdp(hintTrackId);
  2627. }
  2628. catch (MP4Error* e) {
  2629. delete e;
  2630. SetHintTrackSdp(hintTrackId, sdpFragment);
  2631. return;
  2632. }
  2633. char* newSdpString =
  2634. (char*)MP4Malloc(strlen(oldSdpString) + strlen(sdpFragment) + 1);
  2635. strcpy(newSdpString, oldSdpString);
  2636. strcat(newSdpString, sdpFragment);
  2637. SetHintTrackSdp(hintTrackId, newSdpString);
  2638. MP4Free(newSdpString);
  2639. }
  2640. void MP4File::GetHintTrackRtpPayload(
  2641. MP4TrackId hintTrackId,
  2642. char** ppPayloadName,
  2643. u_int8_t* pPayloadNumber,
  2644. u_int16_t* pMaxPayloadSize,
  2645. char **ppEncodingParams)
  2646. {
  2647. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2648. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2649. throw new MP4Error("track is not a hint track",
  2650. "MP4GetHintTrackRtpPayload");
  2651. }
  2652. ((MP4RtpHintTrack*)pTrack)->GetPayload(
  2653. ppPayloadName, pPayloadNumber, pMaxPayloadSize, ppEncodingParams);
  2654. }
  2655. void MP4File::SetHintTrackRtpPayload(MP4TrackId hintTrackId,
  2656. const char* payloadName, u_int8_t* pPayloadNumber, u_int16_t maxPayloadSize,
  2657. const char *encoding_params,
  2658. bool include_rtp_map,
  2659. bool include_mpeg4_esid)
  2660. {
  2661. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2662. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2663. throw new MP4Error("track is not a hint track",
  2664. "MP4SetHintTrackRtpPayload");
  2665. }
  2666. u_int8_t payloadNumber;
  2667. if (pPayloadNumber && *pPayloadNumber != MP4_SET_DYNAMIC_PAYLOAD) {
  2668. payloadNumber = *pPayloadNumber;
  2669. } else {
  2670. payloadNumber = AllocRtpPayloadNumber();
  2671. if (pPayloadNumber) {
  2672. *pPayloadNumber = payloadNumber;
  2673. }
  2674. }
  2675. ((MP4RtpHintTrack*)pTrack)->SetPayload(
  2676. payloadName, payloadNumber, maxPayloadSize, encoding_params,
  2677. include_rtp_map, include_mpeg4_esid);
  2678. }
  2679. u_int8_t MP4File::AllocRtpPayloadNumber()
  2680. {
  2681. MP4Integer32Array usedPayloads;
  2682. u_int32_t i;
  2683. // collect rtp payload numbers in use by existing tracks
  2684. for (i = 0; i < m_pTracks.Size(); i++) {
  2685. MP4Atom* pTrakAtom = m_pTracks[i]->GetTrakAtom();
  2686. MP4Integer32Property* pPayloadProperty = NULL;
  2687. if (pTrakAtom->FindProperty("trak.udta.hinf.payt.payloadNumber",
  2688. (MP4Property**)&pPayloadProperty) &&
  2689. pPayloadProperty) {
  2690. usedPayloads.Add(pPayloadProperty->GetValue());
  2691. }
  2692. }
  2693. // search dynamic payload range for an available slot
  2694. u_int8_t payload;
  2695. for (payload = 96; payload < 128; payload++) {
  2696. for (i = 0; i < usedPayloads.Size(); i++) {
  2697. if (payload == usedPayloads[i]) {
  2698. break;
  2699. }
  2700. }
  2701. if (i == usedPayloads.Size()) {
  2702. break;
  2703. }
  2704. }
  2705. if (payload >= 128) {
  2706. throw new MP4Error("no more available rtp payload numbers",
  2707. "AllocRtpPayloadNumber");
  2708. }
  2709. return payload;
  2710. }
  2711. MP4TrackId MP4File::GetHintTrackReferenceTrackId(
  2712. MP4TrackId hintTrackId)
  2713. {
  2714. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2715. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2716. throw new MP4Error("track is not a hint track",
  2717. "MP4GetHintTrackReferenceTrackId");
  2718. }
  2719. MP4Track* pRefTrack = ((MP4RtpHintTrack*)pTrack)->GetRefTrack();
  2720. if (pRefTrack == NULL) {
  2721. return MP4_INVALID_TRACK_ID;
  2722. }
  2723. return pRefTrack->GetId();
  2724. }
  2725. void MP4File::ReadRtpHint(
  2726. MP4TrackId hintTrackId,
  2727. MP4SampleId hintSampleId,
  2728. u_int16_t* pNumPackets)
  2729. {
  2730. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2731. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2732. throw new MP4Error("track is not a hint track", "MP4ReadRtpHint");
  2733. }
  2734. ((MP4RtpHintTrack*)pTrack)->
  2735. ReadHint(hintSampleId, pNumPackets);
  2736. }
  2737. u_int16_t MP4File::GetRtpHintNumberOfPackets(
  2738. MP4TrackId hintTrackId)
  2739. {
  2740. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2741. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2742. throw new MP4Error("track is not a hint track",
  2743. "MP4GetRtpHintNumberOfPackets");
  2744. }
  2745. return ((MP4RtpHintTrack*)pTrack)->GetHintNumberOfPackets();
  2746. }
  2747. int8_t MP4File::GetRtpPacketBFrame(
  2748. MP4TrackId hintTrackId,
  2749. u_int16_t packetIndex)
  2750. {
  2751. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2752. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2753. throw new MP4Error("track is not a hint track",
  2754. "MP4GetRtpHintBFrame");
  2755. }
  2756. return ((MP4RtpHintTrack*)pTrack)->GetPacketBFrame(packetIndex);
  2757. }
  2758. int32_t MP4File::GetRtpPacketTransmitOffset(
  2759. MP4TrackId hintTrackId,
  2760. u_int16_t packetIndex)
  2761. {
  2762. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2763. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2764. throw new MP4Error("track is not a hint track",
  2765. "MP4GetRtpPacketTransmitOffset");
  2766. }
  2767. return ((MP4RtpHintTrack*)pTrack)->GetPacketTransmitOffset(packetIndex);
  2768. }
  2769. void MP4File::ReadRtpPacket(
  2770. MP4TrackId hintTrackId,
  2771. u_int16_t packetIndex,
  2772. u_int8_t** ppBytes,
  2773. u_int32_t* pNumBytes,
  2774. u_int32_t ssrc,
  2775. bool includeHeader,
  2776. bool includePayload)
  2777. {
  2778. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2779. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2780. throw new MP4Error("track is not a hint track", "MP4ReadPacket");
  2781. }
  2782. ((MP4RtpHintTrack*)pTrack)->ReadPacket(
  2783. packetIndex, ppBytes, pNumBytes,
  2784. ssrc, includeHeader, includePayload);
  2785. }
  2786. MP4Timestamp MP4File::GetRtpTimestampStart(
  2787. MP4TrackId hintTrackId)
  2788. {
  2789. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2790. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2791. throw new MP4Error("track is not a hint track",
  2792. "MP4GetRtpTimestampStart");
  2793. }
  2794. return ((MP4RtpHintTrack*)pTrack)->GetRtpTimestampStart();
  2795. }
  2796. void MP4File::SetRtpTimestampStart(
  2797. MP4TrackId hintTrackId,
  2798. MP4Timestamp rtpStart)
  2799. {
  2800. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2801. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2802. throw new MP4Error("track is not a hint track",
  2803. "MP4SetRtpTimestampStart");
  2804. }
  2805. ((MP4RtpHintTrack*)pTrack)->SetRtpTimestampStart(rtpStart);
  2806. }
  2807. void MP4File::AddRtpHint(MP4TrackId hintTrackId,
  2808. bool isBframe, u_int32_t timestampOffset)
  2809. {
  2810. ProtectWriteOperation("MP4AddRtpHint");
  2811. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2812. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2813. throw new MP4Error("track is not a hint track", "MP4AddRtpHint");
  2814. }
  2815. ((MP4RtpHintTrack*)pTrack)->AddHint(isBframe, timestampOffset);
  2816. }
  2817. void MP4File::AddRtpPacket(
  2818. MP4TrackId hintTrackId, bool setMbit, int32_t transmitOffset)
  2819. {
  2820. ProtectWriteOperation("MP4AddRtpPacket");
  2821. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2822. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2823. throw new MP4Error("track is not a hint track", "MP4AddRtpPacket");
  2824. }
  2825. ((MP4RtpHintTrack*)pTrack)->AddPacket(setMbit, transmitOffset);
  2826. }
  2827. void MP4File::AddRtpImmediateData(MP4TrackId hintTrackId,
  2828. const u_int8_t* pBytes, u_int32_t numBytes)
  2829. {
  2830. ProtectWriteOperation("MP4AddRtpImmediateData");
  2831. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2832. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2833. throw new MP4Error("track is not a hint track",
  2834. "MP4AddRtpImmediateData");
  2835. }
  2836. ((MP4RtpHintTrack*)pTrack)->AddImmediateData(pBytes, numBytes);
  2837. }
  2838. void MP4File::AddRtpSampleData(MP4TrackId hintTrackId,
  2839. MP4SampleId sampleId, u_int32_t dataOffset, u_int32_t dataLength)
  2840. {
  2841. ProtectWriteOperation("MP4AddRtpSampleData");
  2842. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2843. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2844. throw new MP4Error("track is not a hint track",
  2845. "MP4AddRtpSampleData");
  2846. }
  2847. ((MP4RtpHintTrack*)pTrack)->AddSampleData(
  2848. sampleId, dataOffset, dataLength);
  2849. }
  2850. void MP4File::AddRtpESConfigurationPacket(MP4TrackId hintTrackId)
  2851. {
  2852. ProtectWriteOperation("MP4AddRtpESConfigurationPacket");
  2853. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2854. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2855. throw new MP4Error("track is not a hint track",
  2856. "MP4AddRtpESConfigurationPacket");
  2857. }
  2858. ((MP4RtpHintTrack*)pTrack)->AddESConfigurationPacket();
  2859. }
  2860. void MP4File::WriteRtpHint(MP4TrackId hintTrackId,
  2861. MP4Duration duration, bool isSyncSample)
  2862. {
  2863. ProtectWriteOperation("MP4WriteRtpHint");
  2864. MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)];
  2865. if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) {
  2866. throw new MP4Error("track is not a hint track",
  2867. "MP4WriteRtpHint");
  2868. }
  2869. ((MP4RtpHintTrack*)pTrack)->WriteHint(duration, isSyncSample);
  2870. }
  2871. u_int64_t MP4File::ConvertFromMovieDuration(
  2872. MP4Duration duration,
  2873. u_int32_t timeScale)
  2874. {
  2875. return MP4ConvertTime((u_int64_t)duration,
  2876. GetTimeScale(), timeScale);
  2877. }
  2878. u_int64_t MP4File::ConvertFromTrackTimestamp(
  2879. MP4TrackId trackId,
  2880. MP4Timestamp timeStamp,
  2881. u_int32_t timeScale)
  2882. {
  2883. return MP4ConvertTime(timeStamp,
  2884. GetTrackTimeScale(trackId), timeScale);
  2885. }
  2886. MP4Timestamp MP4File::ConvertToTrackTimestamp(
  2887. MP4TrackId trackId,
  2888. u_int64_t timeStamp,
  2889. u_int32_t timeScale)
  2890. {
  2891. return (MP4Timestamp)MP4ConvertTime(timeStamp,
  2892. timeScale, GetTrackTimeScale(trackId));
  2893. }
  2894. u_int64_t MP4File::ConvertFromTrackDuration(
  2895. MP4TrackId trackId,
  2896. MP4Duration duration,
  2897. u_int32_t timeScale)
  2898. {
  2899. return MP4ConvertTime((u_int64_t)duration,
  2900. GetTrackTimeScale(trackId), timeScale);
  2901. }
  2902. MP4Duration MP4File::ConvertToTrackDuration(
  2903. MP4TrackId trackId,
  2904. u_int64_t duration,
  2905. u_int32_t timeScale)
  2906. {
  2907. return (MP4Duration)MP4ConvertTime(duration,
  2908. timeScale, GetTrackTimeScale(trackId));
  2909. }
  2910. u_int8_t MP4File::ConvertTrackTypeToStreamType(const char* trackType)
  2911. {
  2912. u_int8_t streamType;
  2913. if (!strcmp(trackType, MP4_OD_TRACK_TYPE)) {
  2914. streamType = MP4ObjectDescriptionStreamType;
  2915. } else if (!strcmp(trackType, MP4_SCENE_TRACK_TYPE)) {
  2916. streamType = MP4SceneDescriptionStreamType;
  2917. } else if (!strcmp(trackType, MP4_CLOCK_TRACK_TYPE)) {
  2918. streamType = MP4ClockReferenceStreamType;
  2919. } else if (!strcmp(trackType, MP4_MPEG7_TRACK_TYPE)) {
  2920. streamType = MP4Mpeg7StreamType;
  2921. } else if (!strcmp(trackType, MP4_OCI_TRACK_TYPE)) {
  2922. streamType = MP4OCIStreamType;
  2923. } else if (!strcmp(trackType, MP4_IPMP_TRACK_TYPE)) {
  2924. streamType = MP4IPMPStreamType;
  2925. } else if (!strcmp(trackType, MP4_MPEGJ_TRACK_TYPE)) {
  2926. streamType = MP4MPEGJStreamType;
  2927. } else {
  2928. streamType = MP4UserPrivateStreamType;
  2929. }
  2930. return streamType;
  2931. }
  2932. // edit list
  2933. char* MP4File::MakeTrackEditName(
  2934. MP4TrackId trackId,
  2935. MP4EditId editId,
  2936. const char* name)
  2937. {
  2938. char* trakName = MakeTrackName(trackId, NULL);
  2939. if (m_editName == NULL) {
  2940. m_editName = (char *)malloc(1024);
  2941. if (m_editName == NULL) return NULL;
  2942. }
  2943. snprintf(m_editName, 1024,
  2944. "%s.edts.elst.entries[%u].%s",
  2945. trakName, editId - 1, name);
  2946. return m_editName;
  2947. }
  2948. MP4EditId MP4File::AddTrackEdit(
  2949. MP4TrackId trackId,
  2950. MP4EditId editId)
  2951. {
  2952. ProtectWriteOperation("AddTrackEdit");
  2953. return m_pTracks[FindTrackIndex(trackId)]->AddEdit(editId);
  2954. }
  2955. void MP4File::DeleteTrackEdit(
  2956. MP4TrackId trackId,
  2957. MP4EditId editId)
  2958. {
  2959. ProtectWriteOperation("DeleteTrackEdit");
  2960. m_pTracks[FindTrackIndex(trackId)]->DeleteEdit(editId);
  2961. }
  2962. u_int32_t MP4File::GetTrackNumberOfEdits(
  2963. MP4TrackId trackId)
  2964. {
  2965. return GetTrackIntegerProperty(trackId, "edts.elst.entryCount");
  2966. }
  2967. MP4Duration MP4File::GetTrackEditTotalDuration(
  2968. MP4TrackId trackId,
  2969. MP4EditId editId)
  2970. {
  2971. return m_pTracks[FindTrackIndex(trackId)]->GetEditTotalDuration(editId);
  2972. }
  2973. MP4Timestamp MP4File::GetTrackEditStart(
  2974. MP4TrackId trackId,
  2975. MP4EditId editId)
  2976. {
  2977. return m_pTracks[FindTrackIndex(trackId)]->GetEditStart(editId);
  2978. }
  2979. MP4Timestamp MP4File::GetTrackEditMediaStart(
  2980. MP4TrackId trackId,
  2981. MP4EditId editId)
  2982. {
  2983. return GetIntegerProperty(
  2984. MakeTrackEditName(trackId, editId, "mediaTime"));
  2985. }
  2986. void MP4File::SetTrackEditMediaStart(
  2987. MP4TrackId trackId,
  2988. MP4EditId editId,
  2989. MP4Timestamp startTime)
  2990. {
  2991. SetIntegerProperty(
  2992. MakeTrackEditName(trackId, editId, "mediaTime"),
  2993. startTime);
  2994. }
  2995. MP4Duration MP4File::GetTrackEditDuration(
  2996. MP4TrackId trackId,
  2997. MP4EditId editId)
  2998. {
  2999. return GetIntegerProperty(
  3000. MakeTrackEditName(trackId, editId, "segmentDuration"));
  3001. }
  3002. void MP4File::SetTrackEditDuration(
  3003. MP4TrackId trackId,
  3004. MP4EditId editId,
  3005. MP4Duration duration)
  3006. {
  3007. SetIntegerProperty(
  3008. MakeTrackEditName(trackId, editId, "segmentDuration"),
  3009. duration);
  3010. }
  3011. bool MP4File::GetTrackEditDwell(
  3012. MP4TrackId trackId,
  3013. MP4EditId editId)
  3014. {
  3015. return (GetIntegerProperty(
  3016. MakeTrackEditName(trackId, editId, "mediaRate")) == 0);
  3017. }
  3018. void MP4File::SetTrackEditDwell(
  3019. MP4TrackId trackId,
  3020. MP4EditId editId,
  3021. bool dwell)
  3022. {
  3023. SetIntegerProperty(
  3024. MakeTrackEditName(trackId, editId, "mediaRate"),
  3025. (dwell ? 0 : 1));
  3026. }
  3027. MP4SampleId MP4File::GetSampleIdFromEditTime(
  3028. MP4TrackId trackId,
  3029. MP4Timestamp when,
  3030. MP4Timestamp* pStartTime,
  3031. MP4Duration* pDuration)
  3032. {
  3033. return m_pTracks[FindTrackIndex(trackId)]->GetSampleIdFromEditTime(
  3034. when, pStartTime, pDuration);
  3035. }