minimp4.h 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502
  1. #ifndef MINIMP4_H
  2. #define MINIMP4_H
  3. /*
  4. https://github.com/aspt/mp4
  5. https://github.com/lieff/minimp4
  6. To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide.
  7. This software is distributed without any warranty.
  8. See <http://creativecommons.org/publicdomain/zero/1.0/>.
  9. */
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <limits.h>
  15. #include <assert.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define MINIMP4_MIN(x, y) ((x) < (y) ? (x) : (y))
  20. /************************************************************************/
  21. /* Build configuration */
  22. /************************************************************************/
  23. #define FIX_BAD_ANDROID_META_BOX 1
  24. #define MAX_CHUNKS_DEPTH 64 // Max chunks nesting level
  25. #define MINIMP4_MAX_SPS 32
  26. #define MINIMP4_MAX_PPS 256
  27. #define MINIMP4_TRANSCODE_SPS_ID 1
  28. // Support indexing of MP4 files over 4 GB.
  29. // If disabled, files with 64-bit offset fields is still supported,
  30. // but error signaled if such field contains too big offset
  31. // This switch affect return type of MP4D_frame_offset() function
  32. #define MINIMP4_ALLOW_64BIT 1
  33. #define MP4D_TRACE_SUPPORTED 0 // Debug trace
  34. #define MP4D_TRACE_TIMESTAMPS 1
  35. // Support parsing of supplementary information, not necessary for decoding:
  36. // duration, language, bitrate, metadata tags, etc
  37. #define MP4D_INFO_SUPPORTED 1
  38. // Enable code, which prints to stdout supplementary MP4 information:
  39. #define MP4D_PRINT_INFO_SUPPORTED 0
  40. #define MP4D_AVC_SUPPORTED 1
  41. #define MP4D_HEVC_SUPPORTED 1
  42. #define MP4D_TIMESTAMPS_SUPPORTED 1
  43. // Enable TrackFragmentBaseMediaDecodeTimeBox support
  44. #define MP4D_TFDT_SUPPORT 0
  45. /************************************************************************/
  46. /* Some values of MP4(E/D)_track_t->object_type_indication */
  47. /************************************************************************/
  48. // MPEG-4 AAC (all profiles)
  49. #define MP4_OBJECT_TYPE_AUDIO_ISO_IEC_14496_3 0x40
  50. // MPEG-2 AAC, Main profile
  51. #define MP4_OBJECT_TYPE_AUDIO_ISO_IEC_13818_7_MAIN_PROFILE 0x66
  52. // MPEG-2 AAC, LC profile
  53. #define MP4_OBJECT_TYPE_AUDIO_ISO_IEC_13818_7_LC_PROFILE 0x67
  54. // MPEG-2 AAC, SSR profile
  55. #define MP4_OBJECT_TYPE_AUDIO_ISO_IEC_13818_7_SSR_PROFILE 0x68
  56. // H.264 (AVC) video
  57. #define MP4_OBJECT_TYPE_AVC 0x21
  58. // H.265 (HEVC) video
  59. #define MP4_OBJECT_TYPE_HEVC 0x23
  60. // http://www.mp4ra.org/object.html 0xC0-E0 && 0xE2 - 0xFE are specified as "user private"
  61. #define MP4_OBJECT_TYPE_USER_PRIVATE 0xC0
  62. /************************************************************************/
  63. /* API error codes */
  64. /************************************************************************/
  65. #define MP4E_STATUS_OK 0
  66. #define MP4E_STATUS_BAD_ARGUMENTS -1
  67. #define MP4E_STATUS_NO_MEMORY -2
  68. #define MP4E_STATUS_FILE_WRITE_ERROR -3
  69. #define MP4E_STATUS_ONLY_ONE_DSI_ALLOWED -4
  70. /************************************************************************/
  71. /* Sample kind for MP4E_put_sample() */
  72. /************************************************************************/
  73. #define MP4E_SAMPLE_DEFAULT 0 // (beginning of) audio or video frame
  74. #define MP4E_SAMPLE_RANDOM_ACCESS 1 // mark sample as random access point (key frame)
  75. #define MP4E_SAMPLE_CONTINUATION 2 // Not a sample, but continuation of previous sample (new slice)
  76. /************************************************************************/
  77. /* Portable 64-bit type definition */
  78. /************************************************************************/
  79. #if MINIMP4_ALLOW_64BIT
  80. typedef uint64_t boxsize_t;
  81. #else
  82. typedef unsigned int boxsize_t;
  83. #endif
  84. typedef boxsize_t MP4D_file_offset_t;
  85. /************************************************************************/
  86. /* Some values of MP4D_track_t->handler_type */
  87. /************************************************************************/
  88. // Video track : 'vide'
  89. #define MP4D_HANDLER_TYPE_VIDE 0x76696465
  90. // Audio track : 'soun'
  91. #define MP4D_HANDLER_TYPE_SOUN 0x736F756E
  92. // General MPEG-4 systems streams (without specific handler).
  93. // Used for private stream, as suggested in http://www.mp4ra.org/handler.html
  94. #define MP4E_HANDLER_TYPE_GESM 0x6765736D
  95. #define HEVC_NAL_VPS 32
  96. #define HEVC_NAL_SPS 33
  97. #define HEVC_NAL_PPS 34
  98. #define HEVC_NAL_BLA_W_LP 16
  99. #define HEVC_NAL_CRA_NUT 21
  100. /************************************************************************/
  101. /* Data structures */
  102. /************************************************************************/
  103. typedef struct MP4E_mux_tag MP4E_mux_t;
  104. typedef enum
  105. {
  106. e_audio,
  107. e_video,
  108. e_private
  109. } track_media_kind_t;
  110. typedef struct
  111. {
  112. // MP4 object type code, which defined codec class for the track.
  113. // See MP4E_OBJECT_TYPE_* values for some codecs
  114. unsigned object_type_indication;
  115. // Track language: 3-char ISO 639-2T code: "und", "eng", "rus", "jpn" etc...
  116. unsigned char language[4];
  117. track_media_kind_t track_media_kind;
  118. // 90000 for video, sample rate for audio
  119. unsigned time_scale;
  120. unsigned default_duration;
  121. union
  122. {
  123. struct
  124. {
  125. // number of channels in the audio track.
  126. unsigned channelcount;
  127. } a;
  128. struct
  129. {
  130. int width;
  131. int height;
  132. } v;
  133. } u;
  134. } MP4E_track_t;
  135. typedef struct MP4D_sample_to_chunk_t_tag MP4D_sample_to_chunk_t;
  136. typedef struct
  137. {
  138. /************************************************************************/
  139. /* mandatory public data */
  140. /************************************************************************/
  141. // How many 'samples' in the track
  142. // The 'sample' is MP4 term, denoting audio or video frame
  143. unsigned sample_count;
  144. // Decoder-specific info (DSI) data
  145. unsigned char *dsi;
  146. // DSI data size
  147. unsigned dsi_bytes;
  148. // MP4 object type code
  149. // case 0x00: return "Forbidden";
  150. // case 0x01: return "Systems ISO/IEC 14496-1";
  151. // case 0x02: return "Systems ISO/IEC 14496-1";
  152. // case 0x20: return "Visual ISO/IEC 14496-2";
  153. // case 0x40: return "Audio ISO/IEC 14496-3";
  154. // case 0x60: return "Visual ISO/IEC 13818-2 Simple Profile";
  155. // case 0x61: return "Visual ISO/IEC 13818-2 Main Profile";
  156. // case 0x62: return "Visual ISO/IEC 13818-2 SNR Profile";
  157. // case 0x63: return "Visual ISO/IEC 13818-2 Spatial Profile";
  158. // case 0x64: return "Visual ISO/IEC 13818-2 High Profile";
  159. // case 0x65: return "Visual ISO/IEC 13818-2 422 Profile";
  160. // case 0x66: return "Audio ISO/IEC 13818-7 Main Profile";
  161. // case 0x67: return "Audio ISO/IEC 13818-7 LC Profile";
  162. // case 0x68: return "Audio ISO/IEC 13818-7 SSR Profile";
  163. // case 0x69: return "Audio ISO/IEC 13818-3";
  164. // case 0x6A: return "Visual ISO/IEC 11172-2";
  165. // case 0x6B: return "Audio ISO/IEC 11172-3";
  166. // case 0x6C: return "Visual ISO/IEC 10918-1";
  167. unsigned object_type_indication;
  168. #if MP4D_INFO_SUPPORTED
  169. /************************************************************************/
  170. /* informational public data */
  171. /************************************************************************/
  172. // handler_type when present in a media box, is an integer containing one of
  173. // the following values, or a value from a derived specification:
  174. // 'vide' Video track
  175. // 'soun' Audio track
  176. // 'hint' Hint track
  177. unsigned handler_type;
  178. // Track duration: 64-bit value split into 2 variables
  179. unsigned duration_hi;
  180. unsigned duration_lo;
  181. // duration scale: duration = timescale*seconds
  182. unsigned timescale;
  183. // Average bitrate, bits per second
  184. unsigned avg_bitrate_bps;
  185. // Track language: 3-char ISO 639-2T code: "und", "eng", "rus", "jpn" etc...
  186. unsigned char language[4];
  187. // MP4 stream type
  188. // case 0x00: return "Forbidden";
  189. // case 0x01: return "ObjectDescriptorStream";
  190. // case 0x02: return "ClockReferenceStream";
  191. // case 0x03: return "SceneDescriptionStream";
  192. // case 0x04: return "VisualStream";
  193. // case 0x05: return "AudioStream";
  194. // case 0x06: return "MPEG7Stream";
  195. // case 0x07: return "IPMPStream";
  196. // case 0x08: return "ObjectContentInfoStream";
  197. // case 0x09: return "MPEGJStream";
  198. unsigned stream_type;
  199. union
  200. {
  201. // for handler_type == 'soun' tracks
  202. struct
  203. {
  204. unsigned channelcount;
  205. unsigned samplerate_hz;
  206. } audio;
  207. // for handler_type == 'vide' tracks
  208. struct
  209. {
  210. unsigned width;
  211. unsigned height;
  212. } video;
  213. } SampleDescription;
  214. #endif
  215. /************************************************************************/
  216. /* private data: MP4 indexes */
  217. /************************************************************************/
  218. unsigned *entry_size;
  219. unsigned sample_to_chunk_count;
  220. struct MP4D_sample_to_chunk_t_tag *sample_to_chunk;
  221. unsigned chunk_count;
  222. MP4D_file_offset_t *chunk_offset;
  223. #if MP4D_TIMESTAMPS_SUPPORTED
  224. unsigned *timestamp;
  225. unsigned *duration;
  226. #endif
  227. } MP4D_track_t;
  228. typedef struct MP4D_demux_tag
  229. {
  230. /************************************************************************/
  231. /* mandatory public data */
  232. /************************************************************************/
  233. int64_t read_pos;
  234. int64_t read_size;
  235. MP4D_track_t *track;
  236. int (*read_callback)(int64_t offset, void *buffer, size_t size, void *token);
  237. void *token;
  238. unsigned track_count; // number of tracks in the movie
  239. #if MP4D_INFO_SUPPORTED
  240. /************************************************************************/
  241. /* informational public data */
  242. /************************************************************************/
  243. // Movie duration: 64-bit value split into 2 variables
  244. unsigned duration_hi;
  245. unsigned duration_lo;
  246. // duration scale: duration = timescale*seconds
  247. unsigned timescale;
  248. // Metadata tag (optional)
  249. // Tags provided 'as-is', without any re-encoding
  250. struct
  251. {
  252. unsigned char *title;
  253. unsigned char *artist;
  254. unsigned char *album;
  255. unsigned char *year;
  256. unsigned char *comment;
  257. unsigned char *genre;
  258. } tag;
  259. #endif
  260. } MP4D_demux_t;
  261. struct MP4D_sample_to_chunk_t_tag
  262. {
  263. unsigned first_chunk;
  264. unsigned samples_per_chunk;
  265. };
  266. typedef struct
  267. {
  268. void *sps_cache[MINIMP4_MAX_SPS];
  269. void *pps_cache[MINIMP4_MAX_PPS];
  270. int sps_bytes[MINIMP4_MAX_SPS];
  271. int pps_bytes[MINIMP4_MAX_PPS];
  272. int map_sps[MINIMP4_MAX_SPS];
  273. int map_pps[MINIMP4_MAX_PPS];
  274. } h264_sps_id_patcher_t;
  275. typedef struct mp4_h26x_writer_tag
  276. {
  277. #if MINIMP4_TRANSCODE_SPS_ID
  278. h264_sps_id_patcher_t sps_patcher;
  279. #endif
  280. MP4E_mux_t *mux;
  281. int mux_track_id, is_hevc, need_vps, need_sps, need_pps, need_idr;
  282. } mp4_h26x_writer_t;
  283. int mp4_h26x_write_init(mp4_h26x_writer_t *h, MP4E_mux_t *mux, int width, int height, int is_hevc);
  284. void mp4_h26x_write_close(mp4_h26x_writer_t *h);
  285. int mp4_h26x_write_nal(mp4_h26x_writer_t *h, const unsigned char *nal, int length, unsigned timeStamp90kHz_next);
  286. /************************************************************************/
  287. /* API */
  288. /************************************************************************/
  289. /**
  290. * Parse given input stream as MP4 file. Allocate and store data indexes.
  291. * return 1 on success, 0 on failure
  292. * The MP4 indexes may be stored at the end of stream, so this
  293. * function may parse all stream.
  294. * It is guaranteed that function will read/seek sequentially,
  295. * and will never jump back.
  296. */
  297. int MP4D_open(MP4D_demux_t *mp4, int (*read_callback)(int64_t offset, void *buffer, size_t size, void *token), void *token, int64_t file_size);
  298. /**
  299. * Return position and size for given sample from given track. The 'sample' is a
  300. * MP4 term for 'frame'
  301. *
  302. * frame_bytes [OUT] - return coded frame size in bytes
  303. * timestamp [OUT] - return frame timestamp (in mp4->timescale units)
  304. * duration [OUT] - return frame duration (in mp4->timescale units)
  305. *
  306. * function return offset for the frame
  307. */
  308. MP4D_file_offset_t MP4D_frame_offset(const MP4D_demux_t *mp4, unsigned int ntrack, unsigned int nsample, unsigned int *frame_bytes, unsigned *timestamp, unsigned *duration);
  309. /**
  310. * De-allocated memory
  311. */
  312. void MP4D_close(MP4D_demux_t *mp4);
  313. /**
  314. * Helper functions to parse mp4.track[ntrack].dsi for H.264 SPS/PPS
  315. * Return pointer to internal mp4 memory, it must not be free()-ed
  316. *
  317. * Example: process all SPS in MP4 file:
  318. * while (sps = MP4D_read_sps(mp4, num_of_avc_track, sps_count, &sps_bytes))
  319. * {
  320. * process(sps, sps_bytes);
  321. * sps_count++;
  322. * }
  323. */
  324. const void *MP4D_read_sps(const MP4D_demux_t *mp4, unsigned int ntrack, int nsps, int *sps_bytes);
  325. const void *MP4D_read_pps(const MP4D_demux_t *mp4, unsigned int ntrack, int npps, int *pps_bytes);
  326. #if MP4D_PRINT_INFO_SUPPORTED
  327. /**
  328. * Print MP4 information to stdout.
  329. * Uses printf() as well as floating-point functions
  330. * Given as implementation example and for test purposes
  331. */
  332. void MP4D_printf_info(const MP4D_demux_t *mp4);
  333. #endif
  334. /**
  335. * Allocates and initialize mp4 multiplexor
  336. * Given file handler is transparent to the MP4 library, and used only as
  337. * argument for given fwrite_callback() function. By appropriate definition
  338. * of callback function application may use any other file output API (for
  339. * example C++ streams, or Win32 file functions)
  340. *
  341. * return multiplexor handle on success; NULL on failure
  342. */
  343. MP4E_mux_t *MP4E_open(int sequential_mode_flag, int enable_fragmentation, void *token,
  344. int (*write_callback)(int64_t offset, const void *buffer, size_t size, void *token));
  345. /**
  346. * Add new track
  347. * The track_data parameter does not referred by the multiplexer after function
  348. * return, and may be allocated in short-time memory. The dsi member of
  349. * track_data parameter is mandatory.
  350. *
  351. * return ID of added track, or error code MP4E_STATUS_*
  352. */
  353. int MP4E_add_track(MP4E_mux_t *mux, const MP4E_track_t *track_data);
  354. /**
  355. * Add new sample to specified track
  356. * The tracks numbered starting with 0, according to order of MP4E_add_track() calls
  357. * 'kind' is one of MP4E_SAMPLE_... defines
  358. *
  359. * return error code MP4E_STATUS_*
  360. *
  361. * Example:
  362. * MP4E_put_sample(mux, 0, data, data_bytes, duration, MP4E_SAMPLE_DEFAULT);
  363. */
  364. int MP4E_put_sample(MP4E_mux_t *mux, int track_num, const void *data, int data_bytes, int duration, int kind);
  365. /**
  366. * Finalize MP4 file, de-allocated memory, and closes MP4 multiplexer.
  367. * The close operation takes a time and disk space, since it writes MP4 file
  368. * indexes. Please note that this function does not closes file handle,
  369. * which was passed to open function.
  370. *
  371. * return error code MP4E_STATUS_*
  372. */
  373. int MP4E_close(MP4E_mux_t *mux);
  374. /**
  375. * Set Decoder Specific Info (DSI)
  376. * Can be used for audio and private tracks.
  377. * MUST be used for AAC track.
  378. * Only one DSI can be set. It is an error to set DSI again
  379. *
  380. * return error code MP4E_STATUS_*
  381. */
  382. int MP4E_set_dsi(MP4E_mux_t *mux, int track_id, const void *dsi, int bytes);
  383. /**
  384. * Set VPS data. MUST be used for HEVC (H.265) track.
  385. *
  386. * return error code MP4E_STATUS_*
  387. */
  388. int MP4E_set_vps(MP4E_mux_t *mux, int track_id, const void *vps, int bytes);
  389. /**
  390. * Set SPS data. MUST be used for AVC (H.264) track. Up to 32 different SPS can be used in one track.
  391. *
  392. * return error code MP4E_STATUS_*
  393. */
  394. int MP4E_set_sps(MP4E_mux_t *mux, int track_id, const void *sps, int bytes);
  395. /**
  396. * Set PPS data. MUST be used for AVC (H.264) track. Up to 256 different PPS can be used in one track.
  397. *
  398. * return error code MP4E_STATUS_*
  399. */
  400. int MP4E_set_pps(MP4E_mux_t *mux, int track_id, const void *pps, int bytes);
  401. /**
  402. * Set or replace ASCII test comment for the file. Set comment to NULL to remove comment.
  403. *
  404. * return error code MP4E_STATUS_*
  405. */
  406. int MP4E_set_text_comment(MP4E_mux_t *mux, const char *comment);
  407. #ifdef __cplusplus
  408. }
  409. #endif
  410. #endif //MINIMP4_H
  411. #if defined(MINIMP4_IMPLEMENTATION) && !defined(MINIMP4_IMPLEMENTATION_GUARD)
  412. #define MINIMP4_IMPLEMENTATION_GUARD
  413. #define FOUR_CHAR_INT(a, b, c, d) (((uint32_t)(a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  414. enum
  415. {
  416. BOX_co64 = FOUR_CHAR_INT( 'c', 'o', '6', '4' ),//ChunkLargeOffsetAtomType
  417. BOX_stco = FOUR_CHAR_INT( 's', 't', 'c', 'o' ),//ChunkOffsetAtomType
  418. BOX_crhd = FOUR_CHAR_INT( 'c', 'r', 'h', 'd' ),//ClockReferenceMediaHeaderAtomType
  419. BOX_ctts = FOUR_CHAR_INT( 'c', 't', 't', 's' ),//CompositionOffsetAtomType
  420. BOX_cprt = FOUR_CHAR_INT( 'c', 'p', 'r', 't' ),//CopyrightAtomType
  421. BOX_url_ = FOUR_CHAR_INT( 'u', 'r', 'l', ' ' ),//DataEntryURLAtomType
  422. BOX_urn_ = FOUR_CHAR_INT( 'u', 'r', 'n', ' ' ),//DataEntryURNAtomType
  423. BOX_dinf = FOUR_CHAR_INT( 'd', 'i', 'n', 'f' ),//DataInformationAtomType
  424. BOX_dref = FOUR_CHAR_INT( 'd', 'r', 'e', 'f' ),//DataReferenceAtomType
  425. BOX_stdp = FOUR_CHAR_INT( 's', 't', 'd', 'p' ),//DegradationPriorityAtomType
  426. BOX_edts = FOUR_CHAR_INT( 'e', 'd', 't', 's' ),//EditAtomType
  427. BOX_elst = FOUR_CHAR_INT( 'e', 'l', 's', 't' ),//EditListAtomType
  428. BOX_uuid = FOUR_CHAR_INT( 'u', 'u', 'i', 'd' ),//ExtendedAtomType
  429. BOX_free = FOUR_CHAR_INT( 'f', 'r', 'e', 'e' ),//FreeSpaceAtomType
  430. BOX_hdlr = FOUR_CHAR_INT( 'h', 'd', 'l', 'r' ),//HandlerAtomType
  431. BOX_hmhd = FOUR_CHAR_INT( 'h', 'm', 'h', 'd' ),//HintMediaHeaderAtomType
  432. BOX_hint = FOUR_CHAR_INT( 'h', 'i', 'n', 't' ),//HintTrackReferenceAtomType
  433. BOX_mdia = FOUR_CHAR_INT( 'm', 'd', 'i', 'a' ),//MediaAtomType
  434. BOX_mdat = FOUR_CHAR_INT( 'm', 'd', 'a', 't' ),//MediaDataAtomType
  435. BOX_mdhd = FOUR_CHAR_INT( 'm', 'd', 'h', 'd' ),//MediaHeaderAtomType
  436. BOX_minf = FOUR_CHAR_INT( 'm', 'i', 'n', 'f' ),//MediaInformationAtomType
  437. BOX_moov = FOUR_CHAR_INT( 'm', 'o', 'o', 'v' ),//MovieAtomType
  438. BOX_mvhd = FOUR_CHAR_INT( 'm', 'v', 'h', 'd' ),//MovieHeaderAtomType
  439. BOX_stsd = FOUR_CHAR_INT( 's', 't', 's', 'd' ),//SampleDescriptionAtomType
  440. BOX_stsz = FOUR_CHAR_INT( 's', 't', 's', 'z' ),//SampleSizeAtomType
  441. BOX_stz2 = FOUR_CHAR_INT( 's', 't', 'z', '2' ),//CompactSampleSizeAtomType
  442. BOX_stbl = FOUR_CHAR_INT( 's', 't', 'b', 'l' ),//SampleTableAtomType
  443. BOX_stsc = FOUR_CHAR_INT( 's', 't', 's', 'c' ),//SampleToChunkAtomType
  444. BOX_stsh = FOUR_CHAR_INT( 's', 't', 's', 'h' ),//ShadowSyncAtomType
  445. BOX_skip = FOUR_CHAR_INT( 's', 'k', 'i', 'p' ),//SkipAtomType
  446. BOX_smhd = FOUR_CHAR_INT( 's', 'm', 'h', 'd' ),//SoundMediaHeaderAtomType
  447. BOX_stss = FOUR_CHAR_INT( 's', 't', 's', 's' ),//SyncSampleAtomType
  448. BOX_stts = FOUR_CHAR_INT( 's', 't', 't', 's' ),//TimeToSampleAtomType
  449. BOX_trak = FOUR_CHAR_INT( 't', 'r', 'a', 'k' ),//TrackAtomType
  450. BOX_tkhd = FOUR_CHAR_INT( 't', 'k', 'h', 'd' ),//TrackHeaderAtomType
  451. BOX_tref = FOUR_CHAR_INT( 't', 'r', 'e', 'f' ),//TrackReferenceAtomType
  452. BOX_udta = FOUR_CHAR_INT( 'u', 'd', 't', 'a' ),//UserDataAtomType
  453. BOX_vmhd = FOUR_CHAR_INT( 'v', 'm', 'h', 'd' ),//VideoMediaHeaderAtomType
  454. BOX_url = FOUR_CHAR_INT( 'u', 'r', 'l', ' ' ),
  455. BOX_urn = FOUR_CHAR_INT( 'u', 'r', 'n', ' ' ),
  456. BOX_gnrv = FOUR_CHAR_INT( 'g', 'n', 'r', 'v' ),//GenericVisualSampleEntryAtomType
  457. BOX_gnra = FOUR_CHAR_INT( 'g', 'n', 'r', 'a' ),//GenericAudioSampleEntryAtomType
  458. //V2 atoms
  459. BOX_ftyp = FOUR_CHAR_INT( 'f', 't', 'y', 'p' ),//FileTypeAtomType
  460. BOX_padb = FOUR_CHAR_INT( 'p', 'a', 'd', 'b' ),//PaddingBitsAtomType
  461. //MP4 Atoms
  462. BOX_sdhd = FOUR_CHAR_INT( 's', 'd', 'h', 'd' ),//SceneDescriptionMediaHeaderAtomType
  463. BOX_dpnd = FOUR_CHAR_INT( 'd', 'p', 'n', 'd' ),//StreamDependenceAtomType
  464. BOX_iods = FOUR_CHAR_INT( 'i', 'o', 'd', 's' ),//ObjectDescriptorAtomType
  465. BOX_odhd = FOUR_CHAR_INT( 'o', 'd', 'h', 'd' ),//ObjectDescriptorMediaHeaderAtomType
  466. BOX_mpod = FOUR_CHAR_INT( 'm', 'p', 'o', 'd' ),//ODTrackReferenceAtomType
  467. BOX_nmhd = FOUR_CHAR_INT( 'n', 'm', 'h', 'd' ),//MPEGMediaHeaderAtomType
  468. BOX_esds = FOUR_CHAR_INT( 'e', 's', 'd', 's' ),//ESDAtomType
  469. BOX_sync = FOUR_CHAR_INT( 's', 'y', 'n', 'c' ),//OCRReferenceAtomType
  470. BOX_ipir = FOUR_CHAR_INT( 'i', 'p', 'i', 'r' ),//IPIReferenceAtomType
  471. BOX_mp4s = FOUR_CHAR_INT( 'm', 'p', '4', 's' ),//MPEGSampleEntryAtomType
  472. BOX_mp4a = FOUR_CHAR_INT( 'm', 'p', '4', 'a' ),//MPEGAudioSampleEntryAtomType
  473. BOX_mp4v = FOUR_CHAR_INT( 'm', 'p', '4', 'v' ),//MPEGVisualSampleEntryAtomType
  474. // http://www.itscj.ipsj.or.jp/sc29/open/29view/29n7644t.doc
  475. BOX_avc1 = FOUR_CHAR_INT( 'a', 'v', 'c', '1' ),
  476. BOX_avc2 = FOUR_CHAR_INT( 'a', 'v', 'c', '2' ),
  477. BOX_svc1 = FOUR_CHAR_INT( 's', 'v', 'c', '1' ),
  478. BOX_avcC = FOUR_CHAR_INT( 'a', 'v', 'c', 'C' ),
  479. BOX_svcC = FOUR_CHAR_INT( 's', 'v', 'c', 'C' ),
  480. BOX_btrt = FOUR_CHAR_INT( 'b', 't', 'r', 't' ),
  481. BOX_m4ds = FOUR_CHAR_INT( 'm', '4', 'd', 's' ),
  482. BOX_seib = FOUR_CHAR_INT( 's', 'e', 'i', 'b' ),
  483. // H264/HEVC
  484. BOX_hev1 = FOUR_CHAR_INT( 'h', 'e', 'v', '1' ),
  485. BOX_hvc1 = FOUR_CHAR_INT( 'h', 'v', 'c', '1' ),
  486. BOX_hvcC = FOUR_CHAR_INT( 'h', 'v', 'c', 'C' ),
  487. //3GPP atoms
  488. BOX_samr = FOUR_CHAR_INT( 's', 'a', 'm', 'r' ),//AMRSampleEntryAtomType
  489. BOX_sawb = FOUR_CHAR_INT( 's', 'a', 'w', 'b' ),//WB_AMRSampleEntryAtomType
  490. BOX_damr = FOUR_CHAR_INT( 'd', 'a', 'm', 'r' ),//AMRConfigAtomType
  491. BOX_s263 = FOUR_CHAR_INT( 's', '2', '6', '3' ),//H263SampleEntryAtomType
  492. BOX_d263 = FOUR_CHAR_INT( 'd', '2', '6', '3' ),//H263ConfigAtomType
  493. //V2 atoms - Movie Fragments
  494. BOX_mvex = FOUR_CHAR_INT( 'm', 'v', 'e', 'x' ),//MovieExtendsAtomType
  495. BOX_trex = FOUR_CHAR_INT( 't', 'r', 'e', 'x' ),//TrackExtendsAtomType
  496. BOX_moof = FOUR_CHAR_INT( 'm', 'o', 'o', 'f' ),//MovieFragmentAtomType
  497. BOX_mfhd = FOUR_CHAR_INT( 'm', 'f', 'h', 'd' ),//MovieFragmentHeaderAtomType
  498. BOX_traf = FOUR_CHAR_INT( 't', 'r', 'a', 'f' ),//TrackFragmentAtomType
  499. BOX_tfhd = FOUR_CHAR_INT( 't', 'f', 'h', 'd' ),//TrackFragmentHeaderAtomType
  500. BOX_tfdt = FOUR_CHAR_INT( 't', 'f', 'd', 't' ),//TrackFragmentBaseMediaDecodeTimeBox
  501. BOX_trun = FOUR_CHAR_INT( 't', 'r', 'u', 'n' ),//TrackFragmentRunAtomType
  502. BOX_mehd = FOUR_CHAR_INT( 'm', 'e', 'h', 'd' ),//MovieExtendsHeaderBox
  503. // Object Descriptors (OD) data coding
  504. // These takes only 1 byte; this implementation translate <od_tag> to
  505. // <od_tag> + OD_BASE to keep API uniform and safe for string functions
  506. OD_BASE = FOUR_CHAR_INT( '$', '$', '$', '0' ),//
  507. OD_ESD = FOUR_CHAR_INT( '$', '$', '$', '3' ),//SDescriptor_Tag
  508. OD_DCD = FOUR_CHAR_INT( '$', '$', '$', '4' ),//DecoderConfigDescriptor_Tag
  509. OD_DSI = FOUR_CHAR_INT( '$', '$', '$', '5' ),//DecoderSpecificInfo_Tag
  510. OD_SLC = FOUR_CHAR_INT( '$', '$', '$', '6' ),//SLConfigDescriptor_Tag
  511. BOX_meta = FOUR_CHAR_INT( 'm', 'e', 't', 'a' ),
  512. BOX_ilst = FOUR_CHAR_INT( 'i', 'l', 's', 't' ),
  513. // Metagata tags, see http://atomicparsley.sourceforge.net/mpeg-4files.html
  514. BOX_calb = FOUR_CHAR_INT( '\xa9', 'a', 'l', 'b'), // album
  515. BOX_cart = FOUR_CHAR_INT( '\xa9', 'a', 'r', 't'), // artist
  516. BOX_aART = FOUR_CHAR_INT( 'a', 'A', 'R', 'T' ), // album artist
  517. BOX_ccmt = FOUR_CHAR_INT( '\xa9', 'c', 'm', 't'), // comment
  518. BOX_cday = FOUR_CHAR_INT( '\xa9', 'd', 'a', 'y'), // year (as string)
  519. BOX_cnam = FOUR_CHAR_INT( '\xa9', 'n', 'a', 'm'), // title
  520. BOX_cgen = FOUR_CHAR_INT( '\xa9', 'g', 'e', 'n'), // custom genre (as string or as byte!)
  521. BOX_trkn = FOUR_CHAR_INT( 't', 'r', 'k', 'n'), // track number (byte)
  522. BOX_disk = FOUR_CHAR_INT( 'd', 'i', 's', 'k'), // disk number (byte)
  523. BOX_cwrt = FOUR_CHAR_INT( '\xa9', 'w', 'r', 't'), // composer
  524. BOX_ctoo = FOUR_CHAR_INT( '\xa9', 't', 'o', 'o'), // encoder
  525. BOX_tmpo = FOUR_CHAR_INT( 't', 'm', 'p', 'o'), // bpm (byte)
  526. BOX_cpil = FOUR_CHAR_INT( 'c', 'p', 'i', 'l'), // compilation (byte)
  527. BOX_covr = FOUR_CHAR_INT( 'c', 'o', 'v', 'r'), // cover art (JPEG/PNG)
  528. BOX_rtng = FOUR_CHAR_INT( 'r', 't', 'n', 'g'), // rating/advisory (byte)
  529. BOX_cgrp = FOUR_CHAR_INT( '\xa9', 'g', 'r', 'p'), // grouping
  530. BOX_stik = FOUR_CHAR_INT( 's', 't', 'i', 'k'), // stik (byte) 0 = Movie 1 = Normal 2 = Audiobook 5 = Whacked Bookmark 6 = Music Video 9 = Short Film 10 = TV Show 11 = Booklet 14 = Ringtone
  531. BOX_pcst = FOUR_CHAR_INT( 'p', 'c', 's', 't'), // podcast (byte)
  532. BOX_catg = FOUR_CHAR_INT( 'c', 'a', 't', 'g'), // category
  533. BOX_keyw = FOUR_CHAR_INT( 'k', 'e', 'y', 'w'), // keyword
  534. BOX_purl = FOUR_CHAR_INT( 'p', 'u', 'r', 'l'), // podcast URL (byte)
  535. BOX_egid = FOUR_CHAR_INT( 'e', 'g', 'i', 'd'), // episode global unique ID (byte)
  536. BOX_desc = FOUR_CHAR_INT( 'd', 'e', 's', 'c'), // description
  537. BOX_clyr = FOUR_CHAR_INT( '\xa9', 'l', 'y', 'r'), // lyrics (may be > 255 bytes)
  538. BOX_tven = FOUR_CHAR_INT( 't', 'v', 'e', 'n'), // tv episode number
  539. BOX_tves = FOUR_CHAR_INT( 't', 'v', 'e', 's'), // tv episode (byte)
  540. BOX_tvnn = FOUR_CHAR_INT( 't', 'v', 'n', 'n'), // tv network name
  541. BOX_tvsh = FOUR_CHAR_INT( 't', 'v', 's', 'h'), // tv show name
  542. BOX_tvsn = FOUR_CHAR_INT( 't', 'v', 's', 'n'), // tv season (byte)
  543. BOX_purd = FOUR_CHAR_INT( 'p', 'u', 'r', 'd'), // purchase date
  544. BOX_pgap = FOUR_CHAR_INT( 'p', 'g', 'a', 'p'), // Gapless Playback (byte)
  545. //BOX_aart = FOUR_CHAR_INT( 'a', 'a', 'r', 't' ), // Album artist
  546. BOX_cART = FOUR_CHAR_INT( '\xa9', 'A', 'R', 'T'), // artist
  547. BOX_gnre = FOUR_CHAR_INT( 'g', 'n', 'r', 'e'),
  548. // 3GPP metatags (http://cpansearch.perl.org/src/JHAR/MP4-Info-1.12/Info.pm)
  549. BOX_auth = FOUR_CHAR_INT( 'a', 'u', 't', 'h'), // author
  550. BOX_titl = FOUR_CHAR_INT( 't', 'i', 't', 'l'), // title
  551. BOX_dscp = FOUR_CHAR_INT( 'd', 's', 'c', 'p'), // description
  552. BOX_perf = FOUR_CHAR_INT( 'p', 'e', 'r', 'f'), // performer
  553. BOX_mean = FOUR_CHAR_INT( 'm', 'e', 'a', 'n'), //
  554. BOX_name = FOUR_CHAR_INT( 'n', 'a', 'm', 'e'), //
  555. BOX_data = FOUR_CHAR_INT( 'd', 'a', 't', 'a'), //
  556. // these from http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2008-September/053151.html
  557. BOX_albm = FOUR_CHAR_INT( 'a', 'l', 'b', 'm'), // album
  558. BOX_yrrc = FOUR_CHAR_INT( 'y', 'r', 'r', 'c') // album
  559. };
  560. // Video track : 'vide'
  561. #define MP4E_HANDLER_TYPE_VIDE 0x76696465
  562. // Audio track : 'soun'
  563. #define MP4E_HANDLER_TYPE_SOUN 0x736F756E
  564. // General MPEG-4 systems streams (without specific handler).
  565. // Used for private stream, as suggested in http://www.mp4ra.org/handler.html
  566. #define MP4E_HANDLER_TYPE_GESM 0x6765736D
  567. typedef struct
  568. {
  569. boxsize_t size;
  570. boxsize_t offset;
  571. unsigned duration;
  572. unsigned flag_random_access;
  573. } sample_t;
  574. typedef struct {
  575. unsigned char *data;
  576. int bytes;
  577. int capacity;
  578. } minimp4_vector_t;
  579. typedef struct
  580. {
  581. MP4E_track_t info;
  582. minimp4_vector_t smpl; // sample descriptor
  583. minimp4_vector_t pending_sample;
  584. minimp4_vector_t vsps; // or dsi for audio
  585. minimp4_vector_t vpps; // not used for audio
  586. minimp4_vector_t vvps; // used for HEVC
  587. } track_t;
  588. typedef struct MP4E_mux_tag
  589. {
  590. minimp4_vector_t tracks;
  591. int64_t write_pos;
  592. int (*write_callback)(int64_t offset, const void *buffer, size_t size, void *token);
  593. void *token;
  594. char *text_comment;
  595. int sequential_mode_flag;
  596. int enable_fragmentation; // flag, indicating streaming-friendly 'fragmentation' mode
  597. int fragments_count; // # of fragments in 'fragmentation' mode
  598. } MP4E_mux_t;
  599. static const unsigned char box_ftyp[] = {
  600. #if 1
  601. 0,0,0,0x18,'f','t','y','p',
  602. 'm','p','4','2',0,0,0,0,
  603. 'm','p','4','2','i','s','o','m',
  604. #else
  605. // as in ffmpeg
  606. 0,0,0,0x20,'f','t','y','p',
  607. 'i','s','o','m',0,0,2,0,
  608. 'm','p','4','1','i','s','o','m',
  609. 'i','s','o','2','a','v','c','1',
  610. #endif
  611. };
  612. /**
  613. * Endian-independent byte-write macros
  614. */
  615. #define WR(x, n) *p++ = (unsigned char)((x) >> 8*n)
  616. #define WRITE_1(x) WR(x, 0);
  617. #define WRITE_2(x) WR(x, 1); WR(x, 0);
  618. #define WRITE_3(x) WR(x, 2); WR(x, 1); WR(x, 0);
  619. #define WRITE_4(x) WR(x, 3); WR(x, 2); WR(x, 1); WR(x, 0);
  620. #define WR4(p, x) (p)[0] = (char)((x) >> 8*3); (p)[1] = (char)((x) >> 8*2); (p)[2] = (char)((x) >> 8*1); (p)[3] = (char)((x));
  621. // Finish atom: update atom size field
  622. #define END_ATOM --stack; WR4((unsigned char*)*stack, p - *stack);
  623. // Initiate atom: save position of size field on stack
  624. #define ATOM(x) *stack++ = p; p += 4; WRITE_4(x);
  625. // Atom with 'FullAtomVersionFlags' field
  626. #define ATOM_FULL(x, flag) ATOM(x); WRITE_4(flag);
  627. #define ERR(func) { int err = func; if (err) return err; }
  628. /**
  629. Allocate vector with given size, return 1 on success, 0 on fail
  630. */
  631. static int minimp4_vector_init(minimp4_vector_t *h, int capacity)
  632. {
  633. h->bytes = 0;
  634. h->capacity = capacity;
  635. h->data = capacity ? (unsigned char *)malloc(capacity) : NULL;
  636. return !capacity || !!h->data;
  637. }
  638. /**
  639. Deallocates vector memory
  640. */
  641. static void minimp4_vector_reset(minimp4_vector_t *h)
  642. {
  643. if (h->data)
  644. free(h->data);
  645. memset(h, 0, sizeof(minimp4_vector_t));
  646. }
  647. /**
  648. Reallocate vector memory to the given size
  649. */
  650. static int minimp4_vector_grow(minimp4_vector_t *h, int bytes)
  651. {
  652. void *p;
  653. int new_size = h->capacity*2 + 1024;
  654. if (new_size < h->capacity + bytes)
  655. new_size = h->capacity + bytes + 1024;
  656. p = realloc(h->data, new_size);
  657. if (!p)
  658. return 0;
  659. h->data = (unsigned char*)p;
  660. h->capacity = new_size;
  661. return 1;
  662. }
  663. /**
  664. Allocates given number of bytes at the end of vector data, increasing
  665. vector memory if necessary.
  666. Return allocated memory.
  667. */
  668. static unsigned char *minimp4_vector_alloc_tail(minimp4_vector_t *h, int bytes)
  669. {
  670. unsigned char *p;
  671. if (!h->data && !minimp4_vector_init(h, 2*bytes + 1024))
  672. return NULL;
  673. if ((h->capacity - h->bytes) < bytes && !minimp4_vector_grow(h, bytes))
  674. return NULL;
  675. assert(h->data);
  676. assert((h->capacity - h->bytes) >= bytes);
  677. p = h->data + h->bytes;
  678. h->bytes += bytes;
  679. return p;
  680. }
  681. /**
  682. Append data to the end of the vector (accumulate ot enqueue)
  683. */
  684. static unsigned char *minimp4_vector_put(minimp4_vector_t *h, const void *buf, int bytes)
  685. {
  686. unsigned char *tail = minimp4_vector_alloc_tail(h, bytes);
  687. if (tail)
  688. memcpy(tail, buf, bytes);
  689. return tail;
  690. }
  691. /**
  692. * Allocates and initialize mp4 multiplexer
  693. * return multiplexor handle on success; NULL on failure
  694. */
  695. MP4E_mux_t *MP4E_open(int sequential_mode_flag, int enable_fragmentation, void *token,
  696. int (*write_callback)(int64_t offset, const void *buffer, size_t size, void *token))
  697. {
  698. if (write_callback(0, box_ftyp, sizeof(box_ftyp), token)) // Write fixed header: 'ftyp' box
  699. return 0;
  700. MP4E_mux_t *mux = (MP4E_mux_t*)malloc(sizeof(MP4E_mux_t));
  701. if (!mux)
  702. return mux;
  703. mux->sequential_mode_flag = sequential_mode_flag || enable_fragmentation;
  704. mux->enable_fragmentation = enable_fragmentation;
  705. mux->fragments_count = 0;
  706. mux->write_callback = write_callback;
  707. mux->token = token;
  708. mux->text_comment = NULL;
  709. mux->write_pos = sizeof(box_ftyp);
  710. if (!mux->sequential_mode_flag)
  711. { // Write filler, which would be updated later
  712. if (mux->write_callback(mux->write_pos, box_ftyp, 8, mux->token))
  713. {
  714. free(mux);
  715. return 0;
  716. }
  717. mux->write_pos += 16; // box_ftyp + box_free for 32bit or 64bit size encoding
  718. }
  719. minimp4_vector_init(&mux->tracks, 2*sizeof(track_t));
  720. return mux;
  721. }
  722. /**
  723. * Add new track
  724. */
  725. int MP4E_add_track(MP4E_mux_t *mux, const MP4E_track_t *track_data)
  726. {
  727. track_t *tr;
  728. int ntr = mux->tracks.bytes / sizeof(track_t);
  729. if (!mux || !track_data)
  730. return MP4E_STATUS_BAD_ARGUMENTS;
  731. tr = (track_t*)minimp4_vector_alloc_tail(&mux->tracks, sizeof(track_t));
  732. if (!tr)
  733. return MP4E_STATUS_NO_MEMORY;
  734. memset(tr, 0, sizeof(track_t));
  735. memcpy(&tr->info, track_data, sizeof(*track_data));
  736. if (!minimp4_vector_init(&tr->smpl, 256))
  737. return MP4E_STATUS_NO_MEMORY;
  738. minimp4_vector_init(&tr->vsps, 0);
  739. minimp4_vector_init(&tr->vpps, 0);
  740. minimp4_vector_init(&tr->pending_sample, 0);
  741. return ntr;
  742. }
  743. static const unsigned char *next_dsi(const unsigned char *p, const unsigned char *end, int *bytes)
  744. {
  745. if (p < end + 2)
  746. {
  747. *bytes = p[0]*256 + p[1];
  748. return p + 2;
  749. } else
  750. return NULL;
  751. }
  752. static int append_mem(minimp4_vector_t *v, const void *mem, int bytes)
  753. {
  754. int i;
  755. unsigned char size[2];
  756. const unsigned char *p = v->data;
  757. for (i = 0; i + 2 < v->bytes;)
  758. {
  759. int cb = p[i]*256 + p[i + 1];
  760. if (cb == bytes && !memcmp(p + i + 2, mem, cb))
  761. return 1;
  762. i += 2 + cb;
  763. }
  764. size[0] = bytes >> 8;
  765. size[1] = bytes;
  766. return minimp4_vector_put(v, size, 2) && minimp4_vector_put(v, mem, bytes);
  767. }
  768. static int items_count(minimp4_vector_t *v)
  769. {
  770. int i, count = 0;
  771. const unsigned char *p = v->data;
  772. for (i = 0; i + 2 < v->bytes;)
  773. {
  774. int cb = p[i]*256 + p[i + 1];
  775. count++;
  776. i += 2 + cb;
  777. }
  778. return count;
  779. }
  780. int MP4E_set_dsi(MP4E_mux_t *mux, int track_id, const void *dsi, int bytes)
  781. {
  782. track_t* tr = ((track_t*)mux->tracks.data) + track_id;
  783. assert(tr->info.track_media_kind == e_audio ||
  784. tr->info.track_media_kind == e_private);
  785. if (tr->vsps.bytes)
  786. return MP4E_STATUS_ONLY_ONE_DSI_ALLOWED; // only one DSI allowed
  787. return append_mem(&tr->vsps, dsi, bytes) ? MP4E_STATUS_OK : MP4E_STATUS_NO_MEMORY;
  788. }
  789. int MP4E_set_vps(MP4E_mux_t *mux, int track_id, const void *vps, int bytes)
  790. {
  791. track_t* tr = ((track_t*)mux->tracks.data) + track_id;
  792. assert(tr->info.track_media_kind == e_video);
  793. return append_mem(&tr->vvps, vps, bytes) ? MP4E_STATUS_OK : MP4E_STATUS_NO_MEMORY;
  794. }
  795. int MP4E_set_sps(MP4E_mux_t *mux, int track_id, const void *sps, int bytes)
  796. {
  797. track_t* tr = ((track_t*)mux->tracks.data) + track_id;
  798. assert(tr->info.track_media_kind == e_video);
  799. return append_mem(&tr->vsps, sps, bytes) ? MP4E_STATUS_OK : MP4E_STATUS_NO_MEMORY;
  800. }
  801. int MP4E_set_pps(MP4E_mux_t *mux, int track_id, const void *pps, int bytes)
  802. {
  803. track_t* tr = ((track_t*)mux->tracks.data) + track_id;
  804. assert(tr->info.track_media_kind == e_video);
  805. return append_mem(&tr->vpps, pps, bytes) ? MP4E_STATUS_OK : MP4E_STATUS_NO_MEMORY;
  806. }
  807. static unsigned get_duration(const track_t *tr)
  808. {
  809. unsigned i, sum_duration = 0;
  810. const sample_t *s = (const sample_t *)tr->smpl.data;
  811. for (i = 0; i < tr->smpl.bytes/sizeof(sample_t); i++)
  812. {
  813. sum_duration += s[i].duration;
  814. }
  815. return sum_duration;
  816. }
  817. static int write_pending_data(MP4E_mux_t *mux, track_t *tr)
  818. {
  819. // if have pending sample && have at least one sample in the index
  820. if (tr->pending_sample.bytes > 0 && tr->smpl.bytes >= sizeof(sample_t))
  821. {
  822. // Complete pending sample
  823. sample_t *smpl_desc;
  824. unsigned char base[8], *p = base;
  825. assert(mux->sequential_mode_flag);
  826. // Write each sample to a separate atom
  827. assert(mux->sequential_mode_flag); // Separate atom needed for sequential_mode only
  828. WRITE_4(tr->pending_sample.bytes + 8);
  829. WRITE_4(BOX_mdat);
  830. ERR(mux->write_callback(mux->write_pos, base, p - base, mux->token));
  831. mux->write_pos += p - base;
  832. // Update sample descriptor with size and offset
  833. smpl_desc = ((sample_t*)minimp4_vector_alloc_tail(&tr->smpl, 0)) - 1;
  834. smpl_desc->size = tr->pending_sample.bytes;
  835. smpl_desc->offset = (boxsize_t)mux->write_pos;
  836. // Write data
  837. ERR(mux->write_callback(mux->write_pos, tr->pending_sample.data, tr->pending_sample.bytes, mux->token));
  838. mux->write_pos += tr->pending_sample.bytes;
  839. // reset buffer
  840. tr->pending_sample.bytes = 0;
  841. }
  842. return MP4E_STATUS_OK;
  843. }
  844. static int add_sample_descriptor(MP4E_mux_t *mux, track_t *tr, int data_bytes, int duration, int kind)
  845. {
  846. sample_t smp;
  847. smp.size = data_bytes;
  848. smp.offset = (boxsize_t)mux->write_pos;
  849. smp.duration = (duration ? duration : tr->info.default_duration);
  850. smp.flag_random_access = (kind == MP4E_SAMPLE_RANDOM_ACCESS);
  851. return NULL != minimp4_vector_put(&tr->smpl, &smp, sizeof(sample_t));
  852. }
  853. static int mp4e_flush_index(MP4E_mux_t *mux);
  854. /**
  855. * Write Movie Fragment: 'moof' box
  856. */
  857. static int mp4e_write_fragment_header(MP4E_mux_t *mux, int track_num, int data_bytes, int duration, int kind
  858. #if MP4D_TFDT_SUPPORT
  859. , uint64_t timestamp
  860. #endif
  861. )
  862. {
  863. unsigned char base[888], *p = base;
  864. unsigned char *stack_base[20]; // atoms nesting stack
  865. unsigned char **stack = stack_base;
  866. unsigned char *pdata_offset;
  867. unsigned flags;
  868. enum
  869. {
  870. default_sample_duration_present = 0x000008,
  871. default_sample_flags_present = 0x000020,
  872. } e;
  873. track_t *tr = ((track_t*)mux->tracks.data) + track_num;
  874. ATOM(BOX_moof)
  875. ATOM_FULL(BOX_mfhd, 0)
  876. WRITE_4(mux->fragments_count); // start from 1
  877. END_ATOM
  878. ATOM(BOX_traf)
  879. flags = 0;
  880. if (tr->info.track_media_kind == e_video)
  881. flags |= 0x20; // default-sample-flags-present
  882. else
  883. flags |= 0x08; // default-sample-duration-present
  884. flags = (tr->info.track_media_kind == e_video) ? 0x20020 : 0x20008;
  885. ATOM_FULL(BOX_tfhd, flags)
  886. WRITE_4(track_num + 1); // track_ID
  887. if (tr->info.track_media_kind == e_video)
  888. {
  889. WRITE_4(0x1010000); // default_sample_flags
  890. } else
  891. {
  892. WRITE_4(duration);
  893. }
  894. END_ATOM
  895. #if MP4D_TFDT_SUPPORT
  896. ATOM_FULL(BOX_tfdt, 0x01000000) // version 1
  897. WRITE_4(timestamp >> 32); // upper timestamp
  898. WRITE_4(timestamp & 0xffffffff); // lower timestamp
  899. END_ATOM
  900. #endif
  901. if (tr->info.track_media_kind == e_audio)
  902. {
  903. flags = 0;
  904. flags |= 0x001; // data-offset-present
  905. flags |= 0x200; // sample-size-present
  906. ATOM_FULL(BOX_trun, flags)
  907. WRITE_4(1); // sample_count
  908. pdata_offset = p; p += 4; // save ptr to data_offset
  909. WRITE_4(data_bytes);// sample_size
  910. END_ATOM
  911. } else if (kind == MP4E_SAMPLE_RANDOM_ACCESS)
  912. {
  913. flags = 0;
  914. flags |= 0x001; // data-offset-present
  915. flags |= 0x004; // first-sample-flags-present
  916. flags |= 0x100; // sample-duration-present
  917. flags |= 0x200; // sample-size-present
  918. ATOM_FULL(BOX_trun, flags)
  919. WRITE_4(1); // sample_count
  920. pdata_offset = p; p += 4; // save ptr to data_offset
  921. WRITE_4(0x2000000); // first_sample_flags
  922. WRITE_4(duration); // sample_duration
  923. WRITE_4(data_bytes);// sample_size
  924. END_ATOM
  925. } else
  926. {
  927. flags = 0;
  928. flags |= 0x001; // data-offset-present
  929. flags |= 0x100; // sample-duration-present
  930. flags |= 0x200; // sample-size-present
  931. ATOM_FULL(BOX_trun, flags)
  932. WRITE_4(1); // sample_count
  933. pdata_offset = p; p += 4; // save ptr to data_offset
  934. WRITE_4(duration); // sample_duration
  935. WRITE_4(data_bytes);// sample_size
  936. END_ATOM
  937. }
  938. END_ATOM
  939. END_ATOM
  940. WR4(pdata_offset, (p - base) + 8);
  941. ERR(mux->write_callback(mux->write_pos, base, p - base, mux->token));
  942. mux->write_pos += p - base;
  943. return MP4E_STATUS_OK;
  944. }
  945. static int mp4e_write_mdat_box(MP4E_mux_t *mux, uint32_t size)
  946. {
  947. unsigned char base[8], *p = base;
  948. WRITE_4(size);
  949. WRITE_4(BOX_mdat);
  950. ERR(mux->write_callback(mux->write_pos, base, p - base, mux->token));
  951. mux->write_pos += p - base;
  952. return MP4E_STATUS_OK;
  953. }
  954. /**
  955. * Add new sample to specified track
  956. */
  957. int MP4E_put_sample(MP4E_mux_t *mux, int track_num, const void *data, int data_bytes, int duration, int kind)
  958. {
  959. track_t *tr;
  960. if (!mux || !data)
  961. return MP4E_STATUS_BAD_ARGUMENTS;
  962. tr = ((track_t*)mux->tracks.data) + track_num;
  963. if (mux->enable_fragmentation)
  964. {
  965. #if MP4D_TFDT_SUPPORT
  966. // NOTE: assume a constant `duration` to calculate current timestamp
  967. uint64_t timestamp = (uint64_t)mux->fragments_count * duration;
  968. #endif
  969. if (!mux->fragments_count++)
  970. ERR(mp4e_flush_index(mux)); // write file headers before 1st sample
  971. // write MOOF + MDAT + sample data
  972. #if MP4D_TFDT_SUPPORT
  973. ERR(mp4e_write_fragment_header(mux, track_num, data_bytes, duration, kind, timestamp));
  974. #else
  975. ERR(mp4e_write_fragment_header(mux, track_num, data_bytes, duration, kind));
  976. #endif
  977. // write MDAT box for each sample
  978. ERR(mp4e_write_mdat_box(mux, data_bytes + 8));
  979. ERR(mux->write_callback(mux->write_pos, data, data_bytes, mux->token));
  980. mux->write_pos += data_bytes;
  981. return MP4E_STATUS_OK;
  982. }
  983. if (kind != MP4E_SAMPLE_CONTINUATION)
  984. {
  985. if (mux->sequential_mode_flag)
  986. ERR(write_pending_data(mux, tr));
  987. if (!add_sample_descriptor(mux, tr, data_bytes, duration, kind))
  988. return MP4E_STATUS_NO_MEMORY;
  989. } else
  990. {
  991. if (!mux->sequential_mode_flag)
  992. {
  993. sample_t *smpl_desc;
  994. if (tr->smpl.bytes < sizeof(sample_t))
  995. return MP4E_STATUS_NO_MEMORY; // write continuation, but there are no samples in the index
  996. // Accumulate size of the continuation in the sample descriptor
  997. smpl_desc = (sample_t*)(tr->smpl.data + tr->smpl.bytes) - 1;
  998. smpl_desc->size += data_bytes;
  999. }
  1000. }
  1001. if (mux->sequential_mode_flag)
  1002. {
  1003. if (!minimp4_vector_put(&tr->pending_sample, data, data_bytes))
  1004. return MP4E_STATUS_NO_MEMORY;
  1005. } else
  1006. {
  1007. ERR(mux->write_callback(mux->write_pos, data, data_bytes, mux->token));
  1008. mux->write_pos += data_bytes;
  1009. }
  1010. return MP4E_STATUS_OK;
  1011. }
  1012. /**
  1013. * calculate size of length field of OD box
  1014. */
  1015. static int od_size_of_size(int size)
  1016. {
  1017. int i, size_of_size = 1;
  1018. for (i = size; i > 0x7F; i -= 0x7F)
  1019. size_of_size++;
  1020. return size_of_size;
  1021. }
  1022. /**
  1023. * Add or remove MP4 file text comment according to Apple specs:
  1024. * https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html#//apple_ref/doc/uid/TP40000939-CH1-SW1
  1025. * http://atomicparsley.sourceforge.net/mpeg-4files.html
  1026. * note that ISO did not specify comment format.
  1027. */
  1028. int MP4E_set_text_comment(MP4E_mux_t *mux, const char *comment)
  1029. {
  1030. if (!mux || !comment)
  1031. return MP4E_STATUS_BAD_ARGUMENTS;
  1032. if (mux->text_comment)
  1033. free(mux->text_comment);
  1034. mux->text_comment = strdup(comment);
  1035. if (!mux->text_comment)
  1036. return MP4E_STATUS_NO_MEMORY;
  1037. return MP4E_STATUS_OK;
  1038. }
  1039. /**
  1040. * Write file index 'moov' box with all its boxes and indexes
  1041. */
  1042. static int mp4e_flush_index(MP4E_mux_t *mux)
  1043. {
  1044. unsigned char *stack_base[20]; // atoms nesting stack
  1045. unsigned char **stack = stack_base;
  1046. unsigned char *base, *p;
  1047. unsigned int ntr, index_bytes, ntracks = mux->tracks.bytes / sizeof(track_t);
  1048. int i, err;
  1049. // How much memory needed for indexes
  1050. // Experimental data:
  1051. // file with 1 track = 560 bytes
  1052. // file with 2 tracks = 972 bytes
  1053. // track size = 412 bytes;
  1054. // file header size = 148 bytes
  1055. #define FILE_HEADER_BYTES 256
  1056. #define TRACK_HEADER_BYTES 512
  1057. index_bytes = FILE_HEADER_BYTES;
  1058. if (mux->text_comment)
  1059. index_bytes += 128 + strlen(mux->text_comment);
  1060. for (ntr = 0; ntr < ntracks; ntr++)
  1061. {
  1062. track_t *tr = ((track_t*)mux->tracks.data) + ntr;
  1063. index_bytes += TRACK_HEADER_BYTES; // fixed amount (implementation-dependent)
  1064. // may need extra 4 bytes for duration field + 4 bytes for worst-case random access box
  1065. index_bytes += tr->smpl.bytes * (sizeof(sample_t) + 4 + 4) / sizeof(sample_t);
  1066. index_bytes += tr->vsps.bytes;
  1067. index_bytes += tr->vpps.bytes;
  1068. ERR(write_pending_data(mux, tr));
  1069. }
  1070. base = (unsigned char*)malloc(index_bytes);
  1071. if (!base)
  1072. return MP4E_STATUS_NO_MEMORY;
  1073. p = base;
  1074. if (!mux->sequential_mode_flag)
  1075. {
  1076. // update size of mdat box.
  1077. // One of 2 points, which requires random file access.
  1078. // Second is optional duration update at beginning of file in fragmentation mode.
  1079. // This can be avoided using "till eof" size code, but in this case indexes must be
  1080. // written before the mdat....
  1081. int64_t size = mux->write_pos - sizeof(box_ftyp);
  1082. const int64_t size_limit = (int64_t)(uint64_t)0xfffffffe;
  1083. if (size > size_limit)
  1084. {
  1085. WRITE_4(1);
  1086. WRITE_4(BOX_mdat);
  1087. WRITE_4((size >> 32) & 0xffffffff);
  1088. WRITE_4(size & 0xffffffff);
  1089. } else
  1090. {
  1091. WRITE_4(8);
  1092. WRITE_4(BOX_free);
  1093. WRITE_4(size - 8);
  1094. WRITE_4(BOX_mdat);
  1095. }
  1096. ERR(mux->write_callback(sizeof(box_ftyp), base, p - base, mux->token));
  1097. p = base;
  1098. }
  1099. // Write index atoms; order taken from Table 1 of [1]
  1100. #define MOOV_TIMESCALE 1000
  1101. ATOM(BOX_moov);
  1102. ATOM_FULL(BOX_mvhd, 0);
  1103. WRITE_4(0); // creation_time
  1104. WRITE_4(0); // modification_time
  1105. if (ntracks)
  1106. {
  1107. track_t *tr = ((track_t*)mux->tracks.data) + 0; // take 1st track
  1108. unsigned duration = get_duration(tr);
  1109. duration = (unsigned)(duration * 1LL * MOOV_TIMESCALE / tr->info.time_scale);
  1110. WRITE_4(MOOV_TIMESCALE); // duration
  1111. WRITE_4(duration); // duration
  1112. }
  1113. WRITE_4(0x00010000); // rate
  1114. WRITE_2(0x0100); // volume
  1115. WRITE_2(0); // reserved
  1116. WRITE_4(0); // reserved
  1117. WRITE_4(0); // reserved
  1118. // matrix[9]
  1119. WRITE_4(0x00010000); WRITE_4(0); WRITE_4(0);
  1120. WRITE_4(0); WRITE_4(0x00010000); WRITE_4(0);
  1121. WRITE_4(0); WRITE_4(0); WRITE_4(0x40000000);
  1122. // pre_defined[6]
  1123. WRITE_4(0); WRITE_4(0); WRITE_4(0);
  1124. WRITE_4(0); WRITE_4(0); WRITE_4(0);
  1125. //next_track_ID is a non-zero integer that indicates a value to use for the track ID of the next track to be
  1126. //added to this presentation. Zero is not a valid track ID value. The value of next_track_ID shall be
  1127. //larger than the largest track-ID in use.
  1128. WRITE_4(ntracks + 1);
  1129. END_ATOM;
  1130. for (ntr = 0; ntr < ntracks; ntr++)
  1131. {
  1132. track_t *tr = ((track_t*)mux->tracks.data) + ntr;
  1133. unsigned duration = get_duration(tr);
  1134. int samples_count = tr->smpl.bytes / sizeof(sample_t);
  1135. const sample_t *sample = (const sample_t *)tr->smpl.data;
  1136. unsigned handler_type;
  1137. const char *handler_ascii = NULL;
  1138. if (mux->enable_fragmentation)
  1139. samples_count = 0;
  1140. else if (samples_count <= 0)
  1141. continue; // skip empty track
  1142. switch (tr->info.track_media_kind)
  1143. {
  1144. case e_audio:
  1145. handler_type = MP4E_HANDLER_TYPE_SOUN;
  1146. handler_ascii = "SoundHandler";
  1147. break;
  1148. case e_video:
  1149. handler_type = MP4E_HANDLER_TYPE_VIDE;
  1150. handler_ascii = "VideoHandler";
  1151. break;
  1152. case e_private:
  1153. handler_type = MP4E_HANDLER_TYPE_GESM;
  1154. break;
  1155. default:
  1156. return MP4E_STATUS_BAD_ARGUMENTS;
  1157. }
  1158. ATOM(BOX_trak);
  1159. ATOM_FULL(BOX_tkhd, 7); // flag: 1=trak enabled; 2=track in movie; 4=track in preview
  1160. WRITE_4(0); // creation_time
  1161. WRITE_4(0); // modification_time
  1162. WRITE_4(ntr + 1); // track_ID
  1163. WRITE_4(0); // reserved
  1164. WRITE_4((unsigned)(duration * 1LL * MOOV_TIMESCALE / tr->info.time_scale));
  1165. WRITE_4(0); WRITE_4(0); // reserved[2]
  1166. WRITE_2(0); // layer
  1167. WRITE_2(0); // alternate_group
  1168. WRITE_2(0x0100); // volume {if track_is_audio 0x0100 else 0};
  1169. WRITE_2(0); // reserved
  1170. // matrix[9]
  1171. WRITE_4(0x00010000); WRITE_4(0); WRITE_4(0);
  1172. WRITE_4(0); WRITE_4(0x00010000); WRITE_4(0);
  1173. WRITE_4(0); WRITE_4(0); WRITE_4(0x40000000);
  1174. if (tr->info.track_media_kind == e_audio || tr->info.track_media_kind == e_private)
  1175. {
  1176. WRITE_4(0); // width
  1177. WRITE_4(0); // height
  1178. } else
  1179. {
  1180. WRITE_4(tr->info.u.v.width*0x10000); // width
  1181. WRITE_4(tr->info.u.v.height*0x10000); // height
  1182. }
  1183. END_ATOM;
  1184. ATOM(BOX_mdia);
  1185. ATOM_FULL(BOX_mdhd, 0);
  1186. WRITE_4(0); // creation_time
  1187. WRITE_4(0); // modification_time
  1188. WRITE_4(tr->info.time_scale);
  1189. WRITE_4(duration); // duration
  1190. {
  1191. int lang_code = ((tr->info.language[0] & 31) << 10) | ((tr->info.language[1] & 31) << 5) | (tr->info.language[2] & 31);
  1192. WRITE_2(lang_code); // language
  1193. }
  1194. WRITE_2(0); // pre_defined
  1195. END_ATOM;
  1196. ATOM_FULL(BOX_hdlr, 0);
  1197. WRITE_4(0); // pre_defined
  1198. WRITE_4(handler_type); // handler_type
  1199. WRITE_4(0); WRITE_4(0); WRITE_4(0); // reserved[3]
  1200. // name is a null-terminated string in UTF-8 characters which gives a human-readable name for the track type (for debugging and inspection purposes).
  1201. // set mdia hdlr name field to what quicktime uses.
  1202. // Sony smartphone may fail to decode short files w/o handler name
  1203. if (handler_ascii)
  1204. {
  1205. for (i = 0; i < (int)strlen(handler_ascii) + 1; i++)
  1206. {
  1207. WRITE_1(handler_ascii[i]);
  1208. }
  1209. } else
  1210. {
  1211. WRITE_4(0);
  1212. }
  1213. END_ATOM;
  1214. ATOM(BOX_minf);
  1215. if (tr->info.track_media_kind == e_audio)
  1216. {
  1217. // Sound Media Header Box
  1218. ATOM_FULL(BOX_smhd, 0);
  1219. WRITE_2(0); // balance
  1220. WRITE_2(0); // reserved
  1221. END_ATOM;
  1222. }
  1223. if (tr->info.track_media_kind == e_video)
  1224. {
  1225. // mandatory Video Media Header Box
  1226. ATOM_FULL(BOX_vmhd, 1);
  1227. WRITE_2(0); // graphicsmode
  1228. WRITE_2(0); WRITE_2(0); WRITE_2(0); // opcolor[3]
  1229. END_ATOM;
  1230. }
  1231. ATOM(BOX_dinf);
  1232. ATOM_FULL(BOX_dref, 0);
  1233. WRITE_4(1); // entry_count
  1234. // If the flag is set indicating that the data is in the same file as this box, then no string (not even an empty one)
  1235. // shall be supplied in the entry field.
  1236. // ASP the correct way to avoid supply the string, is to use flag 1
  1237. // otherwise ISO reference demux crashes
  1238. ATOM_FULL(BOX_url, 1);
  1239. END_ATOM;
  1240. END_ATOM;
  1241. END_ATOM;
  1242. ATOM(BOX_stbl);
  1243. ATOM_FULL(BOX_stsd, 0);
  1244. WRITE_4(1); // entry_count;
  1245. if (tr->info.track_media_kind == e_audio || tr->info.track_media_kind == e_private)
  1246. {
  1247. // AudioSampleEntry() assume MP4E_HANDLER_TYPE_SOUN
  1248. if (tr->info.track_media_kind == e_audio)
  1249. {
  1250. ATOM(BOX_mp4a);
  1251. } else
  1252. {
  1253. ATOM(BOX_mp4s);
  1254. }
  1255. // SampleEntry
  1256. WRITE_4(0); WRITE_2(0); // reserved[6]
  1257. WRITE_2(1); // data_reference_index; - this is a tag for descriptor below
  1258. if (tr->info.track_media_kind == e_audio)
  1259. {
  1260. // AudioSampleEntry
  1261. WRITE_4(0); WRITE_4(0); // reserved[2]
  1262. WRITE_2(tr->info.u.a.channelcount); // channelcount
  1263. WRITE_2(16); // samplesize
  1264. WRITE_4(0); // pre_defined+reserved
  1265. WRITE_4((tr->info.time_scale << 16)); // samplerate == = {timescale of media}<<16;
  1266. }
  1267. ATOM_FULL(BOX_esds, 0);
  1268. if (tr->vsps.bytes > 0)
  1269. {
  1270. int dsi_bytes = tr->vsps.bytes - 2; // - two bytes size field
  1271. int dsi_size_size = od_size_of_size(dsi_bytes);
  1272. int dcd_bytes = dsi_bytes + dsi_size_size + 1 + (1 + 1 + 3 + 4 + 4);
  1273. int dcd_size_size = od_size_of_size(dcd_bytes);
  1274. int esd_bytes = dcd_bytes + dcd_size_size + 1 + 3;
  1275. #define WRITE_OD_LEN(size) if (size > 0x7F) do { size -= 0x7F; WRITE_1(0x00ff); } while (size > 0x7F); WRITE_1(size)
  1276. WRITE_1(3); // OD_ESD
  1277. WRITE_OD_LEN(esd_bytes);
  1278. WRITE_2(0); // ES_ID(2) // TODO - what is this?
  1279. WRITE_1(0); // flags(1)
  1280. WRITE_1(4); // OD_DCD
  1281. WRITE_OD_LEN(dcd_bytes);
  1282. if (tr->info.track_media_kind == e_audio)
  1283. {
  1284. WRITE_1(MP4_OBJECT_TYPE_AUDIO_ISO_IEC_14496_3); // OD_DCD
  1285. WRITE_1(5 << 2); // stream_type == AudioStream
  1286. } else
  1287. {
  1288. // http://xhelmboyx.tripod.com/formats/mp4-layout.txt
  1289. WRITE_1(208); // 208 = private video
  1290. WRITE_1(32 << 2); // stream_type == user private
  1291. }
  1292. WRITE_3(tr->info.u.a.channelcount * 6144/8); // bufferSizeDB in bytes, constant as in reference decoder
  1293. WRITE_4(0); // maxBitrate TODO
  1294. WRITE_4(0); // avg_bitrate_bps TODO
  1295. WRITE_1(5); // OD_DSI
  1296. WRITE_OD_LEN(dsi_bytes);
  1297. for (i = 0; i < dsi_bytes; i++)
  1298. {
  1299. WRITE_1(tr->vsps.data[2 + i]);
  1300. }
  1301. }
  1302. END_ATOM;
  1303. END_ATOM;
  1304. }
  1305. if (tr->info.track_media_kind == e_video && (MP4_OBJECT_TYPE_AVC == tr->info.object_type_indication || MP4_OBJECT_TYPE_HEVC == tr->info.object_type_indication))
  1306. {
  1307. int numOfSequenceParameterSets = items_count(&tr->vsps);
  1308. int numOfPictureParameterSets = items_count(&tr->vpps);
  1309. if (MP4_OBJECT_TYPE_AVC == tr->info.object_type_indication)
  1310. {
  1311. ATOM(BOX_avc1);
  1312. } else
  1313. {
  1314. ATOM(BOX_hvc1);
  1315. }
  1316. // VisualSampleEntry 8.16.2
  1317. // extends SampleEntry
  1318. WRITE_2(0); // reserved
  1319. WRITE_2(0); // reserved
  1320. WRITE_2(0); // reserved
  1321. WRITE_2(1); // data_reference_index
  1322. WRITE_2(0); // pre_defined
  1323. WRITE_2(0); // reserved
  1324. WRITE_4(0); // pre_defined
  1325. WRITE_4(0); // pre_defined
  1326. WRITE_4(0); // pre_defined
  1327. WRITE_2(tr->info.u.v.width);
  1328. WRITE_2(tr->info.u.v.height);
  1329. WRITE_4(0x00480000); // horizresolution = 72 dpi
  1330. WRITE_4(0x00480000); // vertresolution = 72 dpi
  1331. WRITE_4(0); // reserved
  1332. WRITE_2(1); // frame_count
  1333. for (i = 0; i < 32; i++)
  1334. {
  1335. WRITE_1(0); // compressorname
  1336. }
  1337. WRITE_2(24); // depth
  1338. WRITE_2(-1); // pre_defined
  1339. if (MP4_OBJECT_TYPE_AVC == tr->info.object_type_indication)
  1340. {
  1341. ATOM(BOX_avcC);
  1342. // AVCDecoderConfigurationRecord 5.2.4.1.1
  1343. WRITE_1(1); // configurationVersion
  1344. WRITE_1(tr->vsps.data[2 + 1]);
  1345. WRITE_1(tr->vsps.data[2 + 2]);
  1346. WRITE_1(tr->vsps.data[2 + 3]);
  1347. WRITE_1(255); // 0xfc + NALU_len - 1
  1348. WRITE_1(0xe0 | numOfSequenceParameterSets);
  1349. for (i = 0; i < tr->vsps.bytes; i++)
  1350. {
  1351. WRITE_1(tr->vsps.data[i]);
  1352. }
  1353. WRITE_1(numOfPictureParameterSets);
  1354. for (i = 0; i < tr->vpps.bytes; i++)
  1355. {
  1356. WRITE_1(tr->vpps.data[i]);
  1357. }
  1358. } else
  1359. {
  1360. int numOfVPS = items_count(&tr->vpps);
  1361. ATOM(BOX_hvcC);
  1362. // TODO: read actual params from stream
  1363. WRITE_1(1); // configurationVersion
  1364. WRITE_1(1); // Profile Space (2), Tier (1), Profile (5)
  1365. WRITE_4(0x60000000); // Profile Compatibility
  1366. WRITE_2(0); // progressive, interlaced, non packed constraint, frame only constraint flags
  1367. WRITE_4(0); // constraint indicator flags
  1368. WRITE_1(0); // level_idc
  1369. WRITE_2(0xf000); // Min Spatial Segmentation
  1370. WRITE_1(0xfc); // Parallelism Type
  1371. WRITE_1(0xfc); // Chroma Format
  1372. WRITE_1(0xf8); // Luma Depth
  1373. WRITE_1(0xf8); // Chroma Depth
  1374. WRITE_2(0); // Avg Frame Rate
  1375. WRITE_1(3); // ConstantFrameRate (2), NumTemporalLayers (3), TemporalIdNested (1), LengthSizeMinusOne (2)
  1376. WRITE_1(3); // Num Of Arrays
  1377. WRITE_1((1 << 7) | (HEVC_NAL_VPS & 0x3f)); // Array Completeness + NAL Unit Type
  1378. WRITE_2(numOfVPS);
  1379. for (i = 0; i < tr->vvps.bytes; i++)
  1380. {
  1381. WRITE_1(tr->vvps.data[i]);
  1382. }
  1383. WRITE_1((1 << 7) | (HEVC_NAL_SPS & 0x3f));
  1384. WRITE_2(numOfSequenceParameterSets);
  1385. for (i = 0; i < tr->vsps.bytes; i++)
  1386. {
  1387. WRITE_1(tr->vsps.data[i]);
  1388. }
  1389. WRITE_1((1 << 7) | (HEVC_NAL_PPS & 0x3f));
  1390. WRITE_2(numOfPictureParameterSets);
  1391. for (i = 0; i < tr->vpps.bytes; i++)
  1392. {
  1393. WRITE_1(tr->vpps.data[i]);
  1394. }
  1395. }
  1396. END_ATOM;
  1397. END_ATOM;
  1398. }
  1399. END_ATOM;
  1400. /************************************************************************/
  1401. /* indexes */
  1402. /************************************************************************/
  1403. // Time to Sample Box
  1404. ATOM_FULL(BOX_stts, 0);
  1405. {
  1406. unsigned char *pentry_count = p;
  1407. int cnt = 1, entry_count = 0;
  1408. WRITE_4(0);
  1409. for (i = 0; i < samples_count; i++, cnt++)
  1410. {
  1411. if (i == (samples_count - 1) || sample[i].duration != sample[i + 1].duration)
  1412. {
  1413. WRITE_4(cnt);
  1414. WRITE_4(sample[i].duration);
  1415. cnt = 0;
  1416. entry_count++;
  1417. }
  1418. }
  1419. WR4(pentry_count, entry_count);
  1420. }
  1421. END_ATOM;
  1422. // Sample To Chunk Box
  1423. ATOM_FULL(BOX_stsc, 0);
  1424. if (mux->enable_fragmentation)
  1425. {
  1426. WRITE_4(0); // entry_count
  1427. } else
  1428. {
  1429. WRITE_4(1); // entry_count
  1430. WRITE_4(1); // first_chunk;
  1431. WRITE_4(1); // samples_per_chunk;
  1432. WRITE_4(1); // sample_description_index;
  1433. }
  1434. END_ATOM;
  1435. // Sample Size Box
  1436. ATOM_FULL(BOX_stsz, 0);
  1437. WRITE_4(0); // sample_size If this field is set to 0, then the samples have different sizes, and those sizes
  1438. // are stored in the sample size table.
  1439. WRITE_4(samples_count); // sample_count;
  1440. for (i = 0; i < samples_count; i++)
  1441. {
  1442. WRITE_4(sample[i].size);
  1443. }
  1444. END_ATOM;
  1445. // Chunk Offset Box
  1446. int is_64_bit = 0;
  1447. if (samples_count && sample[samples_count - 1].offset > 0xffffffff)
  1448. is_64_bit = 1;
  1449. if (!is_64_bit)
  1450. {
  1451. ATOM_FULL(BOX_stco, 0);
  1452. WRITE_4(samples_count);
  1453. for (i = 0; i < samples_count; i++)
  1454. {
  1455. WRITE_4(sample[i].offset);
  1456. }
  1457. } else
  1458. {
  1459. ATOM_FULL(BOX_co64, 0);
  1460. WRITE_4(samples_count);
  1461. for (i = 0; i < samples_count; i++)
  1462. {
  1463. WRITE_4((sample[i].offset >> 32) & 0xffffffff);
  1464. WRITE_4(sample[i].offset & 0xffffffff);
  1465. }
  1466. }
  1467. END_ATOM;
  1468. // Sync Sample Box
  1469. {
  1470. int ra_count = 0;
  1471. for (i = 0; i < samples_count; i++)
  1472. {
  1473. ra_count += !!sample[i].flag_random_access;
  1474. }
  1475. if (ra_count != samples_count)
  1476. {
  1477. // If the sync sample box is not present, every sample is a random access point.
  1478. ATOM_FULL(BOX_stss, 0);
  1479. WRITE_4(ra_count);
  1480. for (i = 0; i < samples_count; i++)
  1481. {
  1482. if (sample[i].flag_random_access)
  1483. {
  1484. WRITE_4(i + 1);
  1485. }
  1486. }
  1487. END_ATOM;
  1488. }
  1489. }
  1490. END_ATOM;
  1491. END_ATOM;
  1492. END_ATOM;
  1493. END_ATOM;
  1494. } // tracks loop
  1495. if (mux->text_comment)
  1496. {
  1497. ATOM(BOX_udta);
  1498. ATOM_FULL(BOX_meta, 0);
  1499. ATOM_FULL(BOX_hdlr, 0);
  1500. WRITE_4(0); // pre_defined
  1501. #define MP4E_HANDLER_TYPE_MDIR 0x6d646972
  1502. WRITE_4(MP4E_HANDLER_TYPE_MDIR); // handler_type
  1503. WRITE_4(0); WRITE_4(0); WRITE_4(0); // reserved[3]
  1504. WRITE_4(0); // name is a null-terminated string in UTF-8 characters which gives a human-readable name for the track type (for debugging and inspection purposes).
  1505. END_ATOM;
  1506. ATOM(BOX_ilst);
  1507. ATOM(BOX_ccmt);
  1508. ATOM(BOX_data);
  1509. WRITE_4(1); // type
  1510. WRITE_4(0); // lang
  1511. for (i = 0; i < (int)strlen(mux->text_comment) + 1; i++)
  1512. {
  1513. WRITE_1(mux->text_comment[i]);
  1514. }
  1515. END_ATOM;
  1516. END_ATOM;
  1517. END_ATOM;
  1518. END_ATOM;
  1519. END_ATOM;
  1520. }
  1521. if (mux->enable_fragmentation)
  1522. {
  1523. track_t *tr = ((track_t*)mux->tracks.data) + 0;
  1524. uint32_t movie_duration = get_duration(tr);
  1525. ATOM(BOX_mvex);
  1526. ATOM_FULL(BOX_mehd, 0);
  1527. WRITE_4(movie_duration); // duration
  1528. END_ATOM;
  1529. for (ntr = 0; ntr < ntracks; ntr++)
  1530. {
  1531. ATOM_FULL(BOX_trex, 0);
  1532. WRITE_4(ntr + 1); // track_ID
  1533. WRITE_4(1); // default_sample_description_index
  1534. WRITE_4(0); // default_sample_duration
  1535. WRITE_4(0); // default_sample_size
  1536. WRITE_4(0); // default_sample_flags
  1537. END_ATOM;
  1538. }
  1539. END_ATOM;
  1540. }
  1541. END_ATOM; // moov atom
  1542. assert((unsigned)(p - base) <= index_bytes);
  1543. err = mux->write_callback(mux->write_pos, base, p - base, mux->token);
  1544. mux->write_pos += p - base;
  1545. free(base);
  1546. return err;
  1547. }
  1548. int MP4E_close(MP4E_mux_t *mux)
  1549. {
  1550. int err = MP4E_STATUS_OK;
  1551. unsigned ntr, ntracks;
  1552. if (!mux)
  1553. return MP4E_STATUS_BAD_ARGUMENTS;
  1554. if (!mux->enable_fragmentation)
  1555. err = mp4e_flush_index(mux);
  1556. if (mux->text_comment)
  1557. free(mux->text_comment);
  1558. ntracks = mux->tracks.bytes / sizeof(track_t);
  1559. for (ntr = 0; ntr < ntracks; ntr++)
  1560. {
  1561. track_t *tr = ((track_t*)mux->tracks.data) + ntr;
  1562. minimp4_vector_reset(&tr->vsps);
  1563. minimp4_vector_reset(&tr->vpps);
  1564. minimp4_vector_reset(&tr->smpl);
  1565. minimp4_vector_reset(&tr->pending_sample);
  1566. }
  1567. minimp4_vector_reset(&mux->tracks);
  1568. free(mux);
  1569. return err;
  1570. }
  1571. typedef uint32_t bs_item_t;
  1572. #define BS_BITS 32
  1573. typedef struct
  1574. {
  1575. // Look-ahead bit cache: MSB aligned, 17 bits guaranteed, zero stuffing
  1576. unsigned int cache;
  1577. // Bit counter = 16 - (number of bits in wCache)
  1578. // cache refilled when cache_free_bits >= 0
  1579. int cache_free_bits;
  1580. // Current read position
  1581. const uint16_t *buf;
  1582. // original data buffer
  1583. const uint16_t *origin;
  1584. // original data buffer length, bytes
  1585. unsigned origin_bytes;
  1586. } bit_reader_t;
  1587. #define LOAD_SHORT(x) ((uint16_t)(x << 8) | (x >> 8))
  1588. static unsigned int show_bits(bit_reader_t *bs, int n)
  1589. {
  1590. unsigned int retval;
  1591. assert(n > 0 && n <= 16);
  1592. retval = (unsigned int)(bs->cache >> (32 - n));
  1593. return retval;
  1594. }
  1595. static void flush_bits(bit_reader_t *bs, int n)
  1596. {
  1597. assert(n >= 0 && n <= 16);
  1598. bs->cache <<= n;
  1599. bs->cache_free_bits += n;
  1600. if (bs->cache_free_bits >= 0)
  1601. {
  1602. bs->cache |= ((uint32_t)LOAD_SHORT(*bs->buf)) << bs->cache_free_bits;
  1603. bs->buf++;
  1604. bs->cache_free_bits -= 16;
  1605. }
  1606. }
  1607. static unsigned int get_bits(bit_reader_t *bs, int n)
  1608. {
  1609. unsigned int retval = show_bits(bs, n);
  1610. flush_bits(bs, n);
  1611. return retval;
  1612. }
  1613. static void set_pos_bits(bit_reader_t *bs, unsigned pos_bits)
  1614. {
  1615. assert((int)pos_bits >= 0);
  1616. bs->buf = bs->origin + pos_bits/16;
  1617. bs->cache = 0;
  1618. bs->cache_free_bits = 16;
  1619. flush_bits(bs, 0);
  1620. flush_bits(bs, pos_bits & 15);
  1621. }
  1622. static unsigned get_pos_bits(const bit_reader_t *bs)
  1623. {
  1624. // Current bitbuffer position =
  1625. // position of next wobits in the internal buffer
  1626. // minus bs, available in bit cache wobits
  1627. unsigned pos_bits = (unsigned)(bs->buf - bs->origin)*16;
  1628. pos_bits -= 16 - bs->cache_free_bits;
  1629. assert((int)pos_bits >= 0);
  1630. return pos_bits;
  1631. }
  1632. static int remaining_bits(const bit_reader_t *bs)
  1633. {
  1634. return bs->origin_bytes * 8 - get_pos_bits(bs);
  1635. }
  1636. static void init_bits(bit_reader_t *bs, const void *data, unsigned data_bytes)
  1637. {
  1638. bs->origin = (const uint16_t *)data;
  1639. bs->origin_bytes = data_bytes;
  1640. set_pos_bits(bs, 0);
  1641. }
  1642. #define GetBits(n) get_bits(bs, n)
  1643. /**
  1644. * Unsigned Golomb code
  1645. */
  1646. static int ue_bits(bit_reader_t *bs)
  1647. {
  1648. int clz;
  1649. int val;
  1650. for (clz = 0; !get_bits(bs, 1); clz++) {}
  1651. //get_bits(bs, clz + 1);
  1652. val = (1 << clz) - 1 + (clz ? get_bits(bs, clz) : 0);
  1653. return val;
  1654. }
  1655. #if MINIMP4_TRANSCODE_SPS_ID
  1656. /**
  1657. * Output bitstream
  1658. */
  1659. typedef struct
  1660. {
  1661. int shift; // bit position in the cache
  1662. uint32_t cache; // bit cache
  1663. bs_item_t *buf; // current position
  1664. bs_item_t *origin; // initial position
  1665. } bs_t;
  1666. #define SWAP32(x) (uint32_t)((((x) >> 24) & 0xFF) | (((x) >> 8) & 0xFF00) | (((x) << 8) & 0xFF0000) | ((x & 0xFF) << 24))
  1667. static void h264e_bs_put_bits(bs_t *bs, unsigned n, unsigned val)
  1668. {
  1669. assert(!(val >> n));
  1670. bs->shift -= n;
  1671. assert((unsigned)n <= 32);
  1672. if (bs->shift < 0)
  1673. {
  1674. assert(-bs->shift < 32);
  1675. bs->cache |= val >> -bs->shift;
  1676. *bs->buf++ = SWAP32(bs->cache);
  1677. bs->shift = 32 + bs->shift;
  1678. bs->cache = 0;
  1679. }
  1680. bs->cache |= val << bs->shift;
  1681. }
  1682. static void h264e_bs_flush(bs_t *bs)
  1683. {
  1684. *bs->buf = SWAP32(bs->cache);
  1685. }
  1686. static unsigned h264e_bs_get_pos_bits(const bs_t *bs)
  1687. {
  1688. unsigned pos_bits = (unsigned)((bs->buf - bs->origin)*BS_BITS);
  1689. pos_bits += BS_BITS - bs->shift;
  1690. assert((int)pos_bits >= 0);
  1691. return pos_bits;
  1692. }
  1693. static unsigned h264e_bs_byte_align(bs_t *bs)
  1694. {
  1695. int pos = h264e_bs_get_pos_bits(bs);
  1696. h264e_bs_put_bits(bs, -pos & 7, 0);
  1697. return pos + (-pos & 7);
  1698. }
  1699. /**
  1700. * Golomb code
  1701. * 0 => 1
  1702. * 1 => 01 0
  1703. * 2 => 01 1
  1704. * 3 => 001 00
  1705. * 4 => 001 01
  1706. *
  1707. * [0] => 1
  1708. * [1..2] => 01x
  1709. * [3..6] => 001xx
  1710. * [7..14] => 0001xxx
  1711. *
  1712. */
  1713. static void h264e_bs_put_golomb(bs_t *bs, unsigned val)
  1714. {
  1715. int size = 0;
  1716. unsigned t = val + 1;
  1717. do
  1718. {
  1719. size++;
  1720. } while (t >>= 1);
  1721. h264e_bs_put_bits(bs, 2*size - 1, val + 1);
  1722. }
  1723. static void h264e_bs_init_bits(bs_t *bs, void *data)
  1724. {
  1725. bs->origin = (bs_item_t*)data;
  1726. bs->buf = bs->origin;
  1727. bs->shift = BS_BITS;
  1728. bs->cache = 0;
  1729. }
  1730. static int find_mem_cache(void *cache[], int cache_bytes[], int cache_size, void *mem, int bytes)
  1731. {
  1732. int i;
  1733. if (!bytes)
  1734. return -1;
  1735. for (i = 0; i < cache_size; i++)
  1736. {
  1737. if (cache_bytes[i] == bytes && !memcmp(mem, cache[i], bytes))
  1738. return i; // found
  1739. }
  1740. for (i = 0; i < cache_size; i++)
  1741. {
  1742. if (!cache_bytes[i])
  1743. {
  1744. cache[i] = malloc(bytes);
  1745. if (cache[i])
  1746. {
  1747. memcpy(cache[i], mem, bytes);
  1748. cache_bytes[i] = bytes;
  1749. }
  1750. return i; // put in
  1751. }
  1752. }
  1753. return -1; // no room
  1754. }
  1755. /**
  1756. * 7.4.1.1. "Encapsulation of an SODB within an RBSP"
  1757. */
  1758. static int remove_nal_escapes(unsigned char *dst, const unsigned char *src, int h264_data_bytes)
  1759. {
  1760. int i = 0, j = 0, zero_cnt = 0;
  1761. for (j = 0; j < h264_data_bytes; j++)
  1762. {
  1763. if (zero_cnt == 2 && src[j] <= 3)
  1764. {
  1765. if (src[j] == 3)
  1766. {
  1767. if (j == h264_data_bytes - 1)
  1768. {
  1769. // cabac_zero_word: no action
  1770. } else if (src[j + 1] <= 3)
  1771. {
  1772. j++;
  1773. zero_cnt = 0;
  1774. } else
  1775. {
  1776. // TODO: assume end-of-nal
  1777. //return 0;
  1778. }
  1779. } else
  1780. return 0;
  1781. }
  1782. dst[i++] = src[j];
  1783. if (src[j])
  1784. zero_cnt = 0;
  1785. else
  1786. zero_cnt++;
  1787. }
  1788. //while (--j > i) src[j] = 0;
  1789. return i;
  1790. }
  1791. /**
  1792. * Put NAL escape codes to the output bitstream
  1793. */
  1794. static int nal_put_esc(uint8_t *d, const uint8_t *s, int n)
  1795. {
  1796. int i, j = 4, cntz = 0;
  1797. d[0] = d[1] = d[2] = 0; d[3] = 1; // start code
  1798. for (i = 0; i < n; i++)
  1799. {
  1800. uint8_t byte = *s++;
  1801. if (cntz == 2 && byte <= 3)
  1802. {
  1803. d[j++] = 3;
  1804. cntz = 0;
  1805. }
  1806. if (byte)
  1807. cntz = 0;
  1808. else
  1809. cntz++;
  1810. d[j++] = byte;
  1811. }
  1812. return j;
  1813. }
  1814. static void copy_bits(bit_reader_t *bs, bs_t *bd)
  1815. {
  1816. unsigned cb, bits;
  1817. int bit_count = remaining_bits(bs);
  1818. while (bit_count > 7)
  1819. {
  1820. cb = MINIMP4_MIN(bit_count - 7, 8);
  1821. bits = GetBits(cb);
  1822. h264e_bs_put_bits(bd, cb, bits);
  1823. bit_count -= cb;
  1824. }
  1825. // cut extra zeros after stop-bit
  1826. bits = GetBits(bit_count);
  1827. for (; bit_count && ~bits & 1; bit_count--)
  1828. {
  1829. bits >>= 1;
  1830. }
  1831. if (bit_count)
  1832. {
  1833. h264e_bs_put_bits(bd, bit_count, bits);
  1834. }
  1835. }
  1836. static int change_sps_id(bit_reader_t *bs, bs_t *bd, int new_id, int *old_id)
  1837. {
  1838. unsigned bits, sps_id, i, bytes;
  1839. for (i = 0; i < 3; i++)
  1840. {
  1841. bits = GetBits(8);
  1842. h264e_bs_put_bits(bd, 8, bits);
  1843. }
  1844. sps_id = ue_bits(bs); // max = 31
  1845. *old_id = sps_id;
  1846. sps_id = new_id;
  1847. assert(sps_id <= 31);
  1848. h264e_bs_put_golomb(bd, sps_id);
  1849. copy_bits(bs, bd);
  1850. bytes = h264e_bs_byte_align(bd) / 8;
  1851. h264e_bs_flush(bd);
  1852. return bytes;
  1853. }
  1854. static int patch_pps(h264_sps_id_patcher_t *h, bit_reader_t *bs, bs_t *bd, int new_pps_id, int *old_id)
  1855. {
  1856. int bytes;
  1857. unsigned pps_id = ue_bits(bs); // max = 255
  1858. unsigned sps_id = ue_bits(bs); // max = 31
  1859. *old_id = pps_id;
  1860. sps_id = h->map_sps[sps_id];
  1861. pps_id = new_pps_id;
  1862. assert(sps_id <= 31);
  1863. assert(pps_id <= 255);
  1864. h264e_bs_put_golomb(bd, pps_id);
  1865. h264e_bs_put_golomb(bd, sps_id);
  1866. copy_bits(bs, bd);
  1867. bytes = h264e_bs_byte_align(bd) / 8;
  1868. h264e_bs_flush(bd);
  1869. return bytes;
  1870. }
  1871. static void patch_slice_header(h264_sps_id_patcher_t *h, bit_reader_t *bs, bs_t *bd)
  1872. {
  1873. unsigned first_mb_in_slice = ue_bits(bs);
  1874. unsigned slice_type = ue_bits(bs);
  1875. unsigned pps_id = ue_bits(bs);
  1876. pps_id = h->map_pps[pps_id];
  1877. assert(pps_id <= 255);
  1878. h264e_bs_put_golomb(bd, first_mb_in_slice);
  1879. h264e_bs_put_golomb(bd, slice_type);
  1880. h264e_bs_put_golomb(bd, pps_id);
  1881. copy_bits(bs, bd);
  1882. }
  1883. static int transcode_nalu(h264_sps_id_patcher_t *h, const unsigned char *src, int nalu_bytes, unsigned char *dst)
  1884. {
  1885. int old_id;
  1886. bit_reader_t bst[1];
  1887. bs_t bdt[1];
  1888. bit_reader_t bs[1];
  1889. bs_t bd[1];
  1890. int payload_type = src[0] & 31;
  1891. *dst = *src;
  1892. h264e_bs_init_bits(bd, dst + 1);
  1893. init_bits(bs, src + 1, nalu_bytes - 1);
  1894. h264e_bs_init_bits(bdt, dst + 1);
  1895. init_bits(bst, src + 1, nalu_bytes - 1);
  1896. switch(payload_type)
  1897. {
  1898. case 7:
  1899. {
  1900. int cb = change_sps_id(bst, bdt, 0, &old_id);
  1901. int id = find_mem_cache(h->sps_cache, h->sps_bytes, MINIMP4_MAX_SPS, dst + 1, cb);
  1902. if (id == -1)
  1903. return 0;
  1904. h->map_sps[old_id] = id;
  1905. change_sps_id(bs, bd, id, &old_id);
  1906. }
  1907. break;
  1908. case 8:
  1909. {
  1910. int cb = patch_pps(h, bst, bdt, 0, &old_id);
  1911. int id = find_mem_cache(h->pps_cache, h->pps_bytes, MINIMP4_MAX_PPS, dst + 1, cb);
  1912. if (id == -1)
  1913. return 0;
  1914. h->map_pps[old_id] = id;
  1915. patch_pps(h, bs, bd, id, &old_id);
  1916. }
  1917. break;
  1918. case 1:
  1919. case 2:
  1920. case 5:
  1921. patch_slice_header(h, bs, bd);
  1922. break;
  1923. default:
  1924. memcpy(dst, src, nalu_bytes);
  1925. return nalu_bytes;
  1926. }
  1927. nalu_bytes = 1 + h264e_bs_byte_align(bd) / 8;
  1928. h264e_bs_flush(bd);
  1929. return nalu_bytes;
  1930. }
  1931. #endif
  1932. /**
  1933. * Set pointer just after start code (00 .. 00 01), or to EOF if not found:
  1934. *
  1935. * NZ NZ ... NZ 00 00 00 00 01 xx xx ... xx (EOF)
  1936. * ^ ^
  1937. * non-zero head.............. here ....... or here if no start code found
  1938. *
  1939. */
  1940. static const uint8_t *find_start_code(const uint8_t *h264_data, int h264_data_bytes, int *zcount)
  1941. {
  1942. const uint8_t *eof = h264_data + h264_data_bytes;
  1943. const uint8_t *p = h264_data;
  1944. do
  1945. {
  1946. int zero_cnt = 1;
  1947. const uint8_t* found = (uint8_t*)memchr(p, 0, eof - p);
  1948. p = found ? found : eof;
  1949. while (p + zero_cnt < eof && !p[zero_cnt]) zero_cnt++;
  1950. if (zero_cnt >= 2 && p[zero_cnt] == 1)
  1951. {
  1952. *zcount = zero_cnt + 1;
  1953. return p + zero_cnt + 1;
  1954. }
  1955. p += zero_cnt;
  1956. } while (p < eof);
  1957. *zcount = 0;
  1958. return eof;
  1959. }
  1960. /**
  1961. * Locate NAL unit in given buffer, and calculate it's length
  1962. */
  1963. static const uint8_t *find_nal_unit(const uint8_t *h264_data, int h264_data_bytes, int *pnal_unit_bytes)
  1964. {
  1965. const uint8_t *eof = h264_data + h264_data_bytes;
  1966. int zcount;
  1967. const uint8_t *start = find_start_code(h264_data, h264_data_bytes, &zcount);
  1968. const uint8_t *stop = start;
  1969. if (start)
  1970. {
  1971. stop = find_start_code(start, (int)(eof - start), &zcount);
  1972. while (stop > start && !stop[-1])
  1973. {
  1974. stop--;
  1975. }
  1976. }
  1977. *pnal_unit_bytes = (int)(stop - start - zcount);
  1978. return start;
  1979. }
  1980. int mp4_h26x_write_init(mp4_h26x_writer_t *h, MP4E_mux_t *mux, int width, int height, int is_hevc)
  1981. {
  1982. MP4E_track_t tr;
  1983. tr.track_media_kind = e_video;
  1984. tr.language[0] = 'u';
  1985. tr.language[1] = 'n';
  1986. tr.language[2] = 'd';
  1987. tr.language[3] = 0;
  1988. tr.object_type_indication = is_hevc ? MP4_OBJECT_TYPE_HEVC : MP4_OBJECT_TYPE_AVC;
  1989. tr.time_scale = 90000;
  1990. tr.default_duration = 0;
  1991. tr.u.v.width = width;
  1992. tr.u.v.height = height;
  1993. h->mux_track_id = MP4E_add_track(mux, &tr);
  1994. h->mux = mux;
  1995. h->is_hevc = is_hevc;
  1996. h->need_vps = is_hevc;
  1997. h->need_sps = 1;
  1998. h->need_pps = 1;
  1999. h->need_idr = 1;
  2000. #if MINIMP4_TRANSCODE_SPS_ID
  2001. memset(&h->sps_patcher, 0, sizeof(h264_sps_id_patcher_t));
  2002. #endif
  2003. return MP4E_STATUS_OK;
  2004. }
  2005. void mp4_h26x_write_close(mp4_h26x_writer_t *h)
  2006. {
  2007. #if MINIMP4_TRANSCODE_SPS_ID
  2008. h264_sps_id_patcher_t *p = &h->sps_patcher;
  2009. int i;
  2010. for (i = 0; i < MINIMP4_MAX_SPS; i++)
  2011. {
  2012. if (p->sps_cache[i])
  2013. free(p->sps_cache[i]);
  2014. }
  2015. for (i = 0; i < MINIMP4_MAX_PPS; i++)
  2016. {
  2017. if (p->pps_cache[i])
  2018. free(p->pps_cache[i]);
  2019. }
  2020. #endif
  2021. memset(h, 0, sizeof(*h));
  2022. }
  2023. static int mp4_h265_write_nal(mp4_h26x_writer_t *h, const unsigned char *nal, int sizeof_nal, unsigned timeStamp90kHz_next)
  2024. {
  2025. int payload_type = (nal[0] >> 1) & 0x3f;
  2026. int is_intra = payload_type >= HEVC_NAL_BLA_W_LP && payload_type <= HEVC_NAL_CRA_NUT;
  2027. int err = MP4E_STATUS_OK;
  2028. //printf("payload_type=%d, intra=%d\n", payload_type, is_intra);
  2029. if (is_intra && !h->need_sps && !h->need_pps && !h->need_vps)
  2030. h->need_idr = 0;
  2031. switch (payload_type)
  2032. {
  2033. case HEVC_NAL_VPS:
  2034. MP4E_set_vps(h->mux, h->mux_track_id, nal, sizeof_nal);
  2035. h->need_vps = 0;
  2036. break;
  2037. case HEVC_NAL_SPS:
  2038. MP4E_set_sps(h->mux, h->mux_track_id, nal, sizeof_nal);
  2039. h->need_sps = 0;
  2040. break;
  2041. case HEVC_NAL_PPS:
  2042. MP4E_set_pps(h->mux, h->mux_track_id, nal, sizeof_nal);
  2043. h->need_pps = 0;
  2044. break;
  2045. default:
  2046. if (h->need_vps || h->need_sps || h->need_pps || h->need_idr)
  2047. return MP4E_STATUS_BAD_ARGUMENTS;
  2048. {
  2049. unsigned char *tmp = (unsigned char *)malloc(4 + sizeof_nal);
  2050. if (!tmp)
  2051. return MP4E_STATUS_NO_MEMORY;
  2052. int sample_kind = MP4E_SAMPLE_DEFAULT;
  2053. tmp[0] = (unsigned char)(sizeof_nal >> 24);
  2054. tmp[1] = (unsigned char)(sizeof_nal >> 16);
  2055. tmp[2] = (unsigned char)(sizeof_nal >> 8);
  2056. tmp[3] = (unsigned char)(sizeof_nal);
  2057. memcpy(tmp + 4, nal, sizeof_nal);
  2058. if (is_intra)
  2059. sample_kind = MP4E_SAMPLE_RANDOM_ACCESS;
  2060. err = MP4E_put_sample(h->mux, h->mux_track_id, tmp, 4 + sizeof_nal, timeStamp90kHz_next, sample_kind);
  2061. free(tmp);
  2062. }
  2063. break;
  2064. }
  2065. return err;
  2066. }
  2067. int mp4_h26x_write_nal(mp4_h26x_writer_t *h, const unsigned char *nal, int length, unsigned timeStamp90kHz_next)
  2068. {
  2069. const unsigned char *eof = nal + length;
  2070. int payload_type, sizeof_nal, err = MP4E_STATUS_OK;
  2071. for (;;nal++)
  2072. {
  2073. #if MINIMP4_TRANSCODE_SPS_ID
  2074. unsigned char *nal1, *nal2;
  2075. #endif
  2076. nal = find_nal_unit(nal, (int)(eof - nal), &sizeof_nal);
  2077. if (!sizeof_nal)
  2078. break;
  2079. if (h->is_hevc)
  2080. {
  2081. ERR(mp4_h265_write_nal(h, nal, sizeof_nal, timeStamp90kHz_next));
  2082. continue;
  2083. }
  2084. payload_type = nal[0] & 31;
  2085. if (9 == payload_type)
  2086. continue; // access unit delimiter, nothing to be done
  2087. #if MINIMP4_TRANSCODE_SPS_ID
  2088. // Transcode SPS, PPS and slice headers, reassigning ID's for SPS and PPS:
  2089. // - assign unique ID's to different SPS and PPS
  2090. // - assign same ID's to equal (except ID) SPS and PPS
  2091. // - save all different SPS and PPS
  2092. nal1 = (unsigned char *)malloc(sizeof_nal*17/16 + 32);
  2093. if (!nal1)
  2094. return MP4E_STATUS_NO_MEMORY;
  2095. nal2 = (unsigned char *)malloc(sizeof_nal*17/16 + 32);
  2096. if (!nal2)
  2097. {
  2098. free(nal1);
  2099. return MP4E_STATUS_NO_MEMORY;
  2100. }
  2101. sizeof_nal = remove_nal_escapes(nal2, nal, sizeof_nal);
  2102. if (!sizeof_nal)
  2103. {
  2104. exit_with_free:
  2105. free(nal1);
  2106. free(nal2);
  2107. return MP4E_STATUS_BAD_ARGUMENTS;
  2108. }
  2109. sizeof_nal = transcode_nalu(&h->sps_patcher, nal2, sizeof_nal, nal1);
  2110. sizeof_nal = nal_put_esc(nal2, nal1, sizeof_nal);
  2111. switch (payload_type) {
  2112. case 7:
  2113. MP4E_set_sps(h->mux, h->mux_track_id, nal2 + 4, sizeof_nal - 4);
  2114. h->need_sps = 0;
  2115. break;
  2116. case 8:
  2117. if (h->need_sps)
  2118. goto exit_with_free;
  2119. MP4E_set_pps(h->mux, h->mux_track_id, nal2 + 4, sizeof_nal - 4);
  2120. h->need_pps = 0;
  2121. break;
  2122. case 5:
  2123. if (h->need_sps)
  2124. goto exit_with_free;
  2125. h->need_idr = 0;
  2126. // flow through
  2127. default:
  2128. if (h->need_sps)
  2129. goto exit_with_free;
  2130. if (!h->need_pps && !h->need_idr)
  2131. {
  2132. bit_reader_t bs[1];
  2133. init_bits(bs, nal + 1, sizeof_nal - 4 - 1);
  2134. unsigned first_mb_in_slice = ue_bits(bs);
  2135. //unsigned slice_type = ue_bits(bs);
  2136. int sample_kind = MP4E_SAMPLE_DEFAULT;
  2137. nal2[0] = (unsigned char)((sizeof_nal - 4) >> 24);
  2138. nal2[1] = (unsigned char)((sizeof_nal - 4) >> 16);
  2139. nal2[2] = (unsigned char)((sizeof_nal - 4) >> 8);
  2140. nal2[3] = (unsigned char)((sizeof_nal - 4));
  2141. if (first_mb_in_slice)
  2142. sample_kind = MP4E_SAMPLE_CONTINUATION;
  2143. else if (payload_type == 5)
  2144. sample_kind = MP4E_SAMPLE_RANDOM_ACCESS;
  2145. err = MP4E_put_sample(h->mux, h->mux_track_id, nal2, sizeof_nal, timeStamp90kHz_next, sample_kind);
  2146. }
  2147. break;
  2148. }
  2149. free(nal1);
  2150. free(nal2);
  2151. #else
  2152. // No SPS/PPS transcoding
  2153. // This branch assumes that encoder use correct SPS/PPS ID's
  2154. switch (payload_type) {
  2155. case 7:
  2156. MP4E_set_sps(h->mux, h->mux_track_id, nal, sizeof_nal);
  2157. h->need_sps = 0;
  2158. break;
  2159. case 8:
  2160. MP4E_set_pps(h->mux, h->mux_track_id, nal, sizeof_nal);
  2161. h->need_pps = 0;
  2162. break;
  2163. case 5:
  2164. if (h->need_sps)
  2165. return MP4E_STATUS_BAD_ARGUMENTS;
  2166. h->need_idr = 0;
  2167. // flow through
  2168. default:
  2169. if (h->need_sps)
  2170. return MP4E_STATUS_BAD_ARGUMENTS;
  2171. if (!h->need_pps && !h->need_idr)
  2172. {
  2173. bit_reader_t bs[1];
  2174. unsigned char *tmp = (unsigned char *)malloc(4 + sizeof_nal);
  2175. if (!tmp)
  2176. return MP4E_STATUS_NO_MEMORY;
  2177. init_bits(bs, nal + 1, sizeof_nal - 1);
  2178. unsigned first_mb_in_slice = ue_bits(bs);
  2179. int sample_kind = MP4E_SAMPLE_DEFAULT;
  2180. tmp[0] = (unsigned char)(sizeof_nal >> 24);
  2181. tmp[1] = (unsigned char)(sizeof_nal >> 16);
  2182. tmp[2] = (unsigned char)(sizeof_nal >> 8);
  2183. tmp[3] = (unsigned char)(sizeof_nal);
  2184. memcpy(tmp + 4, nal, sizeof_nal);
  2185. if (first_mb_in_slice)
  2186. sample_kind = MP4E_SAMPLE_CONTINUATION;
  2187. else if (payload_type == 5)
  2188. sample_kind = MP4E_SAMPLE_RANDOM_ACCESS;
  2189. err = MP4E_put_sample(h->mux, h->mux_track_id, tmp, 4 + sizeof_nal, timeStamp90kHz_next, sample_kind);
  2190. free(tmp);
  2191. }
  2192. break;
  2193. }
  2194. #endif
  2195. if (err)
  2196. break;
  2197. }
  2198. return err;
  2199. }
  2200. #if MP4D_TRACE_SUPPORTED
  2201. # define TRACE(x) printf x
  2202. #else
  2203. # define TRACE(x)
  2204. #endif
  2205. #define NELEM(x) (sizeof(x) / sizeof((x)[0]))
  2206. static int minimp4_fgets(MP4D_demux_t *mp4)
  2207. {
  2208. uint8_t c;
  2209. if (mp4->read_callback(mp4->read_pos, &c, 1, mp4->token))
  2210. return -1;
  2211. mp4->read_pos++;
  2212. return c;
  2213. }
  2214. /**
  2215. * Read given number of bytes from input stream
  2216. * Used to read box headers
  2217. */
  2218. static unsigned minimp4_read(MP4D_demux_t *mp4, int nb, int *eof_flag)
  2219. {
  2220. uint32_t v = 0; int last_byte;
  2221. switch (nb)
  2222. {
  2223. case 4: v = (v << 8) | minimp4_fgets(mp4);
  2224. case 3: v = (v << 8) | minimp4_fgets(mp4);
  2225. case 2: v = (v << 8) | minimp4_fgets(mp4);
  2226. default:
  2227. case 1: v = (v << 8) | (last_byte = minimp4_fgets(mp4));
  2228. }
  2229. if (last_byte < 0)
  2230. {
  2231. *eof_flag = 1;
  2232. }
  2233. return v;
  2234. }
  2235. /**
  2236. * Read given number of bytes, but no more than *payload_bytes specifies...
  2237. * Used to read box payload
  2238. */
  2239. static uint32_t read_payload(MP4D_demux_t *mp4, unsigned nb, boxsize_t *payload_bytes, int *eof_flag)
  2240. {
  2241. if (*payload_bytes < nb)
  2242. {
  2243. *eof_flag = 1;
  2244. nb = (int)*payload_bytes;
  2245. }
  2246. *payload_bytes -= nb;
  2247. return minimp4_read(mp4, nb, eof_flag);
  2248. }
  2249. /**
  2250. * Skips given number of bytes.
  2251. * Avoid math operations with fpos_t
  2252. */
  2253. static void my_fseek(MP4D_demux_t *mp4, boxsize_t pos, int *eof_flag)
  2254. {
  2255. mp4->read_pos += pos;
  2256. if (mp4->read_pos >= mp4->read_size)
  2257. *eof_flag = 1;
  2258. }
  2259. #define READ(n) read_payload(mp4, n, &payload_bytes, &eof_flag)
  2260. #define SKIP(n) { boxsize_t t = MINIMP4_MIN(payload_bytes, n); my_fseek(mp4, t, &eof_flag); payload_bytes -= t; }
  2261. #define MALLOC(t, p, size) p = (t)malloc(size); if (!(p)) { ERROR("out of memory"); }
  2262. /*
  2263. * On error: release resources.
  2264. */
  2265. #define RETURN_ERROR(mess) { \
  2266. TRACE(("\nMP4 ERROR: " mess)); \
  2267. MP4D_close(mp4); \
  2268. return 0; \
  2269. }
  2270. /*
  2271. * Any errors, occurred on top-level hierarchy is passed to exit check: 'if (!mp4->track_count) ... '
  2272. */
  2273. #define ERROR(mess) \
  2274. if (!depth) \
  2275. break; \
  2276. else \
  2277. RETURN_ERROR(mess);
  2278. typedef enum { BOX_ATOM, BOX_OD } boxtype_t;
  2279. int MP4D_open(MP4D_demux_t *mp4, int (*read_callback)(int64_t offset, void *buffer, size_t size, void *token), void *token, int64_t file_size)
  2280. {
  2281. // box stack size
  2282. int depth = 0;
  2283. struct
  2284. {
  2285. // remaining bytes for box in the stack
  2286. boxsize_t bytes;
  2287. // kind of box children's: OD chunks handled in the same manner as name chunks
  2288. boxtype_t format;
  2289. } stack[MAX_CHUNKS_DEPTH];
  2290. #if MP4D_TRACE_SUPPORTED
  2291. // path of current element: List0/List1/... etc
  2292. uint32_t box_path[MAX_CHUNKS_DEPTH];
  2293. #endif
  2294. int eof_flag = 0;
  2295. unsigned i;
  2296. MP4D_track_t *tr = NULL;
  2297. if (!mp4 || !read_callback)
  2298. {
  2299. TRACE(("\nERROR: invlaid arguments!"));
  2300. return 0;
  2301. }
  2302. memset(mp4, 0, sizeof(MP4D_demux_t));
  2303. mp4->read_callback = read_callback;
  2304. mp4->token = token;
  2305. mp4->read_size = file_size;
  2306. stack[0].format = BOX_ATOM; // start with atom box
  2307. stack[0].bytes = 0; // never accessed
  2308. do
  2309. {
  2310. // List of boxes, derived from 'FullBox'
  2311. // ~~~~~~~~~~~~~~~~~~~~~
  2312. // need read version field and check version for these boxes
  2313. static const struct
  2314. {
  2315. uint32_t name;
  2316. unsigned max_version;
  2317. unsigned use_track_flag;
  2318. } g_fullbox[] =
  2319. {
  2320. #if MP4D_INFO_SUPPORTED
  2321. {BOX_mdhd, 1, 1},
  2322. {BOX_mvhd, 1, 0},
  2323. {BOX_hdlr, 0, 0},
  2324. {BOX_meta, 0, 0}, // Android can produce meta box without 'FullBox' field, comment this line to simulate the bug
  2325. #endif
  2326. #if MP4D_TRACE_TIMESTAMPS
  2327. {BOX_stts, 0, 0},
  2328. {BOX_ctts, 0, 0},
  2329. #endif
  2330. {BOX_stz2, 0, 1},
  2331. {BOX_stsz, 0, 1},
  2332. {BOX_stsc, 0, 1},
  2333. {BOX_stco, 0, 1},
  2334. {BOX_co64, 0, 1},
  2335. {BOX_stsd, 0, 0},
  2336. {BOX_esds, 0, 1} // esds does not use track, but switches to OD mode. Check here, to avoid OD check
  2337. };
  2338. // List of boxes, which contains other boxes ('envelopes')
  2339. // Parser will descend down for boxes in this list, otherwise parsing will proceed to
  2340. // the next sibling box
  2341. // OD boxes handled in the same way as atom boxes...
  2342. static const struct
  2343. {
  2344. uint32_t name;
  2345. boxtype_t type;
  2346. } g_envelope_box[] =
  2347. {
  2348. {BOX_esds, BOX_OD}, // TODO: BOX_esds can be used for both audio and video, but this code supports audio only!
  2349. {OD_ESD, BOX_OD},
  2350. {OD_DCD, BOX_OD},
  2351. {OD_DSI, BOX_OD},
  2352. {BOX_trak, BOX_ATOM},
  2353. {BOX_moov, BOX_ATOM},
  2354. //{BOX_moof, BOX_ATOM},
  2355. {BOX_mdia, BOX_ATOM},
  2356. {BOX_tref, BOX_ATOM},
  2357. {BOX_minf, BOX_ATOM},
  2358. {BOX_dinf, BOX_ATOM},
  2359. {BOX_stbl, BOX_ATOM},
  2360. {BOX_stsd, BOX_ATOM},
  2361. {BOX_mp4a, BOX_ATOM},
  2362. {BOX_mp4s, BOX_ATOM},
  2363. #if MP4D_AVC_SUPPORTED
  2364. {BOX_mp4v, BOX_ATOM},
  2365. {BOX_avc1, BOX_ATOM},
  2366. //{BOX_avc2, BOX_ATOM},
  2367. //{BOX_svc1, BOX_ATOM},
  2368. #endif
  2369. #if MP4D_HEVC_SUPPORTED
  2370. {BOX_hvc1, BOX_ATOM},
  2371. #endif
  2372. {BOX_udta, BOX_ATOM},
  2373. {BOX_meta, BOX_ATOM},
  2374. {BOX_ilst, BOX_ATOM}
  2375. };
  2376. uint32_t FullAtomVersionAndFlags = 0;
  2377. boxsize_t payload_bytes;
  2378. boxsize_t box_bytes;
  2379. uint32_t box_name;
  2380. #if MP4D_INFO_SUPPORTED
  2381. unsigned char **ptag = NULL;
  2382. #endif
  2383. int read_bytes = 0;
  2384. // Read header box type and it's length
  2385. if (stack[depth].format == BOX_ATOM)
  2386. {
  2387. box_bytes = minimp4_read(mp4, 4, &eof_flag);
  2388. #if FIX_BAD_ANDROID_META_BOX
  2389. broken_android_meta_hack:
  2390. #endif
  2391. if (eof_flag)
  2392. break; // normal exit
  2393. if (box_bytes >= 2 && box_bytes < 8)
  2394. {
  2395. ERROR("invalid box size (broken file?)");
  2396. }
  2397. box_name = minimp4_read(mp4, 4, &eof_flag);
  2398. read_bytes = 8;
  2399. // Decode box size
  2400. if (box_bytes == 0 || // standard indication of 'till eof' size
  2401. box_bytes == (boxsize_t)0xFFFFFFFFU // some files uses non-standard 'till eof' signaling
  2402. )
  2403. {
  2404. box_bytes = ~(boxsize_t)0;
  2405. }
  2406. payload_bytes = box_bytes - 8;
  2407. if (box_bytes == 1) // 64-bit sizes
  2408. {
  2409. TRACE(("\n64-bit chunk encountered"));
  2410. box_bytes = minimp4_read(mp4, 4, &eof_flag);
  2411. #if MP4D_64BIT_SUPPORTED
  2412. box_bytes <<= 32;
  2413. box_bytes |= minimp4_read(mp4, 4, &eof_flag);
  2414. #else
  2415. if (box_bytes)
  2416. {
  2417. ERROR("UNSUPPORTED FEATURE: MP4BoxHeader(): 64-bit boxes not supported!");
  2418. }
  2419. box_bytes = minimp4_read(mp4, 4, &eof_flag);
  2420. #endif
  2421. if (box_bytes < 16)
  2422. {
  2423. ERROR("invalid box size (broken file?)");
  2424. }
  2425. payload_bytes = box_bytes - 16;
  2426. }
  2427. // Read and check box version for some boxes
  2428. for (i = 0; i < NELEM(g_fullbox); i++)
  2429. {
  2430. if (box_name == g_fullbox[i].name)
  2431. {
  2432. FullAtomVersionAndFlags = READ(4);
  2433. read_bytes += 4;
  2434. #if FIX_BAD_ANDROID_META_BOX
  2435. // Fix invalid BOX_meta, found in some Android-produced MP4
  2436. // This branch is optional: bad box would be skipped
  2437. if (box_name == BOX_meta)
  2438. {
  2439. if (FullAtomVersionAndFlags >= 8 && FullAtomVersionAndFlags < payload_bytes)
  2440. {
  2441. if (box_bytes > stack[depth].bytes)
  2442. {
  2443. ERROR("broken file structure!");
  2444. }
  2445. stack[depth].bytes -= box_bytes;;
  2446. depth++;
  2447. stack[depth].bytes = payload_bytes + 4; // +4 need for missing header
  2448. stack[depth].format = BOX_ATOM;
  2449. box_bytes = FullAtomVersionAndFlags;
  2450. TRACE(("Bad metadata box detected (Android bug?)!\n"));
  2451. goto broken_android_meta_hack;
  2452. }
  2453. }
  2454. #endif // FIX_BAD_ANDROID_META_BOX
  2455. if ((FullAtomVersionAndFlags >> 24) > g_fullbox[i].max_version)
  2456. {
  2457. ERROR("unsupported box version!");
  2458. }
  2459. if (g_fullbox[i].use_track_flag && !tr)
  2460. {
  2461. ERROR("broken file structure!");
  2462. }
  2463. }
  2464. }
  2465. } else // stack[depth].format == BOX_OD
  2466. {
  2467. int val;
  2468. box_name = OD_BASE + minimp4_read(mp4, 1, &eof_flag); // 1-byte box type
  2469. read_bytes += 1;
  2470. if (eof_flag)
  2471. break;
  2472. payload_bytes = 0;
  2473. box_bytes = 1;
  2474. do
  2475. {
  2476. val = minimp4_read(mp4, 1, &eof_flag);
  2477. read_bytes += 1;
  2478. if (eof_flag)
  2479. {
  2480. ERROR("premature EOF!");
  2481. }
  2482. payload_bytes = (payload_bytes << 7) | (val & 0x7F);
  2483. box_bytes++;
  2484. } while (val & 0x80);
  2485. box_bytes += payload_bytes;
  2486. }
  2487. #if MP4D_TRACE_SUPPORTED
  2488. box_path[depth] = (box_name >> 24) | (box_name << 24) | ((box_name >> 8) & 0x0000FF00) | ((box_name << 8) & 0x00FF0000);
  2489. TRACE(("%2d %8d %.*s (%d bytes remains for sibilings) \n", depth, (int)box_bytes, depth*4, (char*)box_path, (int)stack[depth].bytes));
  2490. #endif
  2491. // Check that box size <= parent size
  2492. if (depth)
  2493. {
  2494. // Skip box with bad size
  2495. assert(box_bytes > 0);
  2496. if (box_bytes > stack[depth].bytes)
  2497. {
  2498. TRACE(("Wrong %c%c%c%c box size: broken file?\n", (box_name >> 24)&255, (box_name >> 16)&255, (box_name >> 8)&255, box_name&255));
  2499. box_bytes = stack[depth].bytes;
  2500. box_name = 0;
  2501. payload_bytes = box_bytes - read_bytes;
  2502. }
  2503. stack[depth].bytes -= box_bytes;
  2504. }
  2505. // Read box header
  2506. switch(box_name)
  2507. {
  2508. case BOX_stz2: //ISO/IEC 14496-1 Page 38. Section 8.17.2 - Sample Size Box.
  2509. case BOX_stsz:
  2510. {
  2511. int size = 0;
  2512. uint32_t sample_size = READ(4);
  2513. tr->sample_count = READ(4);
  2514. MALLOC(unsigned int*, tr->entry_size, tr->sample_count*4);
  2515. for (i = 0; i < tr->sample_count; i++)
  2516. {
  2517. if (box_name == BOX_stsz)
  2518. {
  2519. tr->entry_size[i] = (sample_size?sample_size:READ(4));
  2520. } else
  2521. {
  2522. switch (sample_size & 0xFF)
  2523. {
  2524. case 16:
  2525. tr->entry_size[i] = READ(2);
  2526. break;
  2527. case 8:
  2528. tr->entry_size[i] = READ(1);
  2529. break;
  2530. case 4:
  2531. if (i & 1)
  2532. {
  2533. tr->entry_size[i] = size & 15;
  2534. } else
  2535. {
  2536. size = READ(1);
  2537. tr->entry_size[i] = (size >> 4);
  2538. }
  2539. break;
  2540. }
  2541. }
  2542. }
  2543. }
  2544. break;
  2545. case BOX_stsc: //ISO/IEC 14496-12 Page 38. Section 8.18 - Sample To Chunk Box.
  2546. tr->sample_to_chunk_count = READ(4);
  2547. MALLOC(MP4D_sample_to_chunk_t*, tr->sample_to_chunk, tr->sample_to_chunk_count*sizeof(tr->sample_to_chunk[0]));
  2548. for (i = 0; i < tr->sample_to_chunk_count; i++)
  2549. {
  2550. tr->sample_to_chunk[i].first_chunk = READ(4);
  2551. tr->sample_to_chunk[i].samples_per_chunk = READ(4);
  2552. SKIP(4); // sample_description_index
  2553. }
  2554. break;
  2555. #if MP4D_TRACE_TIMESTAMPS || MP4D_TIMESTAMPS_SUPPORTED
  2556. case BOX_stts:
  2557. {
  2558. unsigned count = READ(4);
  2559. unsigned j, k = 0, ts = 0, ts_count = count;
  2560. #if MP4D_TIMESTAMPS_SUPPORTED
  2561. MALLOC(unsigned int*, tr->timestamp, ts_count*4);
  2562. MALLOC(unsigned int*, tr->duration, ts_count*4);
  2563. #endif
  2564. for (i = 0; i < count; i++)
  2565. {
  2566. unsigned sc = READ(4);
  2567. int d = READ(4);
  2568. TRACE(("sample %8d count %8d duration %8d\n", i, sc, d));
  2569. #if MP4D_TIMESTAMPS_SUPPORTED
  2570. if (k + sc > ts_count)
  2571. {
  2572. ts_count = k + sc;
  2573. tr->timestamp = (unsigned int*)realloc(tr->timestamp, ts_count * sizeof(unsigned));
  2574. tr->duration = (unsigned int*)realloc(tr->duration, ts_count * sizeof(unsigned));
  2575. }
  2576. for (j = 0; j < sc; j++)
  2577. {
  2578. tr->duration[k] = d;
  2579. tr->timestamp[k++] = ts;
  2580. ts += d;
  2581. }
  2582. #endif
  2583. }
  2584. }
  2585. break;
  2586. case BOX_ctts:
  2587. {
  2588. unsigned count = READ(4);
  2589. for (i = 0; i < count; i++)
  2590. {
  2591. int sc = READ(4);
  2592. int d = READ(4);
  2593. (void)sc;
  2594. (void)d;
  2595. TRACE(("sample %8d count %8d decoding to composition offset %8d\n", i, sc, d));
  2596. }
  2597. }
  2598. break;
  2599. #endif
  2600. case BOX_stco: //ISO/IEC 14496-12 Page 39. Section 8.19 - Chunk Offset Box.
  2601. case BOX_co64:
  2602. tr->chunk_count = READ(4);
  2603. MALLOC(MP4D_file_offset_t*, tr->chunk_offset, tr->chunk_count*sizeof(MP4D_file_offset_t));
  2604. for (i = 0; i < tr->chunk_count; i++)
  2605. {
  2606. tr->chunk_offset[i] = READ(4);
  2607. if (box_name == BOX_co64)
  2608. {
  2609. #if !MP4D_64BIT_SUPPORTED
  2610. if (tr->chunk_offset[i])
  2611. {
  2612. ERROR("UNSUPPORTED FEATURE: 64-bit chunk_offset not supported!");
  2613. }
  2614. #endif
  2615. tr->chunk_offset[i] <<= 32;
  2616. tr->chunk_offset[i] |= READ(4);
  2617. }
  2618. }
  2619. break;
  2620. #if MP4D_INFO_SUPPORTED
  2621. case BOX_mvhd:
  2622. SKIP(((FullAtomVersionAndFlags >> 24) == 1) ? 8 + 8 : 4 + 4);
  2623. mp4->timescale = READ(4);
  2624. mp4->duration_hi = ((FullAtomVersionAndFlags >> 24) == 1) ? READ(4) : 0;
  2625. mp4->duration_lo = READ(4);
  2626. SKIP(4 + 2 + 2 + 4*2 + 4*9 + 4*6 + 4);
  2627. break;
  2628. case BOX_mdhd:
  2629. SKIP(((FullAtomVersionAndFlags >> 24) == 1) ? 8 + 8 : 4 + 4);
  2630. tr->timescale = READ(4);
  2631. tr->duration_hi = ((FullAtomVersionAndFlags >> 24) == 1) ? READ(4) : 0;
  2632. tr->duration_lo = READ(4);
  2633. {
  2634. int ISO_639_2_T = READ(2);
  2635. tr->language[2] = (ISO_639_2_T & 31) + 0x60; ISO_639_2_T >>= 5;
  2636. tr->language[1] = (ISO_639_2_T & 31) + 0x60; ISO_639_2_T >>= 5;
  2637. tr->language[0] = (ISO_639_2_T & 31) + 0x60;
  2638. }
  2639. // the rest of this box is skipped by default ...
  2640. break;
  2641. case BOX_hdlr:
  2642. if (tr) // When this box is within 'meta' box, the track may not be avaialable
  2643. {
  2644. SKIP(4); // pre_defined
  2645. tr->handler_type = READ(4);
  2646. }
  2647. // typically hdlr box does not contain any useful info.
  2648. // the rest of this box is skipped by default ...
  2649. break;
  2650. case BOX_btrt:
  2651. if (!tr)
  2652. {
  2653. ERROR("broken file structure!");
  2654. }
  2655. SKIP(4 + 4);
  2656. tr->avg_bitrate_bps = READ(4);
  2657. break;
  2658. // Set pointer to tag to be read...
  2659. case BOX_calb: ptag = &mp4->tag.album; break;
  2660. case BOX_cART: ptag = &mp4->tag.artist; break;
  2661. case BOX_cnam: ptag = &mp4->tag.title; break;
  2662. case BOX_cday: ptag = &mp4->tag.year; break;
  2663. case BOX_ccmt: ptag = &mp4->tag.comment; break;
  2664. case BOX_cgen: ptag = &mp4->tag.genre; break;
  2665. #endif
  2666. case BOX_stsd:
  2667. SKIP(4); // entry_count, BOX_mp4a & BOX_mp4v boxes follows immediately
  2668. break;
  2669. case BOX_mp4s: // private stream
  2670. if (!tr)
  2671. {
  2672. ERROR("broken file structure!");
  2673. }
  2674. SKIP(6*1 + 2/*Base SampleEntry*/);
  2675. break;
  2676. case BOX_mp4a:
  2677. if (!tr)
  2678. {
  2679. ERROR("broken file structure!");
  2680. }
  2681. #if MP4D_INFO_SUPPORTED
  2682. SKIP(6*1+2/*Base SampleEntry*/ + 4*2);
  2683. tr->SampleDescription.audio.channelcount = READ(2);
  2684. SKIP(2/*samplesize*/ + 2 + 2);
  2685. tr->SampleDescription.audio.samplerate_hz = READ(4) >> 16;
  2686. #else
  2687. SKIP(28);
  2688. #endif
  2689. break;
  2690. #if MP4D_AVC_SUPPORTED
  2691. case BOX_avc1: // AVCSampleEntry extends VisualSampleEntry
  2692. // case BOX_avc2: - no test
  2693. // case BOX_svc1: - no test
  2694. case BOX_mp4v:
  2695. if (!tr)
  2696. {
  2697. ERROR("broken file structure!");
  2698. }
  2699. #if MP4D_INFO_SUPPORTED
  2700. SKIP(6*1 + 2/*Base SampleEntry*/ + 2 + 2 + 4*3);
  2701. tr->SampleDescription.video.width = READ(2);
  2702. tr->SampleDescription.video.height = READ(2);
  2703. // frame_count is always 1
  2704. // compressorname is rarely set..
  2705. SKIP(4 + 4 + 4 + 2/*frame_count*/ + 32/*compressorname*/ + 2 + 2);
  2706. #else
  2707. SKIP(78);
  2708. #endif
  2709. // ^^^ end of VisualSampleEntry
  2710. // now follows for BOX_avc1:
  2711. // BOX_avcC
  2712. // BOX_btrt (optional)
  2713. // BOX_m4ds (optional)
  2714. // for BOX_mp4v:
  2715. // BOX_esds
  2716. break;
  2717. case BOX_avcC: // AVCDecoderConfigurationRecord()
  2718. // hack: AAC-specific DSI field reused (for it have same purpoose as sps/pps)
  2719. // TODO: check this hack if BOX_esds co-exist with BOX_avcC
  2720. tr->object_type_indication = MP4_OBJECT_TYPE_AVC;
  2721. tr->dsi = (unsigned char*)malloc((size_t)box_bytes);
  2722. tr->dsi_bytes = (unsigned)box_bytes;
  2723. {
  2724. int spspps;
  2725. unsigned char *p = tr->dsi;
  2726. unsigned int configurationVersion = READ(1);
  2727. unsigned int AVCProfileIndication = READ(1);
  2728. unsigned int profile_compatibility = READ(1);
  2729. unsigned int AVCLevelIndication = READ(1);
  2730. //bit(6) reserved =
  2731. unsigned int lengthSizeMinusOne = READ(1) & 3;
  2732. (void)configurationVersion;
  2733. (void)AVCProfileIndication;
  2734. (void)profile_compatibility;
  2735. (void)AVCLevelIndication;
  2736. (void)lengthSizeMinusOne;
  2737. for (spspps = 0; spspps < 2; spspps++)
  2738. {
  2739. unsigned int numOfSequenceParameterSets= READ(1);
  2740. if (!spspps)
  2741. {
  2742. numOfSequenceParameterSets &= 31; // clears 3 msb for SPS
  2743. }
  2744. *p++ = numOfSequenceParameterSets;
  2745. for (i = 0; i < numOfSequenceParameterSets; i++)
  2746. {
  2747. unsigned k, sequenceParameterSetLength = READ(2);
  2748. *p++ = sequenceParameterSetLength >> 8;
  2749. *p++ = sequenceParameterSetLength ;
  2750. for (k = 0; k < sequenceParameterSetLength; k++)
  2751. {
  2752. *p++ = READ(1);
  2753. }
  2754. }
  2755. }
  2756. }
  2757. break;
  2758. #endif // MP4D_AVC_SUPPORTED
  2759. case OD_ESD:
  2760. {
  2761. unsigned flags = READ(3); // ES_ID(2) + flags(1)
  2762. if (flags & 0x80) // steamdependflag
  2763. {
  2764. SKIP(2); // dependsOnESID
  2765. }
  2766. if (flags & 0x40) // urlflag
  2767. {
  2768. unsigned bytecount = READ(1);
  2769. SKIP(bytecount); // skip URL
  2770. }
  2771. if (flags & 0x20) // ocrflag (was reserved in MPEG-4 v.1)
  2772. {
  2773. SKIP(2); // OCRESID
  2774. }
  2775. break;
  2776. }
  2777. case OD_DCD: //ISO/IEC 14496-1 Page 28. Section 8.6.5 - DecoderConfigDescriptor.
  2778. assert(tr); // ensured by g_fullbox[] check
  2779. tr->object_type_indication = READ(1);
  2780. #if MP4D_INFO_SUPPORTED
  2781. tr->stream_type = READ(1) >> 2;
  2782. SKIP(3/*bufferSizeDB*/ + 4/*maxBitrate*/);
  2783. tr->avg_bitrate_bps = READ(4);
  2784. #else
  2785. SKIP(1+3+4+4);
  2786. #endif
  2787. break;
  2788. case OD_DSI: //ISO/IEC 14496-1 Page 28. Section 8.6.5 - DecoderConfigDescriptor.
  2789. assert(tr); // ensured by g_fullbox[] check
  2790. if (!tr->dsi && payload_bytes)
  2791. {
  2792. MALLOC(unsigned char*, tr->dsi, (int)payload_bytes);
  2793. for (i = 0; i < payload_bytes; i++)
  2794. {
  2795. tr->dsi[i] = minimp4_read(mp4, 1, &eof_flag); // These bytes available due to check above
  2796. }
  2797. tr->dsi_bytes = i;
  2798. payload_bytes -= i;
  2799. break;
  2800. }
  2801. default:
  2802. TRACE(("[%c%c%c%c] %d\n", box_name >> 24, box_name >> 16, box_name >> 8, box_name, (int)payload_bytes));
  2803. }
  2804. #if MP4D_INFO_SUPPORTED
  2805. // Read tag is tag pointer is set
  2806. if (ptag && !*ptag && payload_bytes > 16)
  2807. {
  2808. #if 0
  2809. uint32_t size = READ(4);
  2810. uint32_t data = READ(4);
  2811. uint32_t class = READ(4);
  2812. uint32_t x1 = READ(4);
  2813. TRACE(("%2d %2d %2d ", size, class, x1));
  2814. #else
  2815. SKIP(4 + 4 + 4 + 4);
  2816. #endif
  2817. MALLOC(unsigned char*, *ptag, (unsigned)payload_bytes + 1);
  2818. for (i = 0; payload_bytes != 0; i++)
  2819. {
  2820. (*ptag)[i] = READ(1);
  2821. }
  2822. (*ptag)[i] = 0; // zero-terminated string
  2823. }
  2824. #endif
  2825. if (box_name == BOX_trak)
  2826. {
  2827. // New track found: allocate memory using realloc()
  2828. // Typically there are 1 audio track for AAC audio file,
  2829. // 4 tracks for movie file,
  2830. // 3-5 tracks for scalable audio (CELP+AAC)
  2831. // and up to 50 tracks for BSAC scalable audio
  2832. void *mem = realloc(mp4->track, (mp4->track_count + 1)*sizeof(MP4D_track_t));
  2833. if (!mem)
  2834. {
  2835. // if realloc fails, it does not deallocate old pointer!
  2836. ERROR("out of memory");
  2837. }
  2838. mp4->track = (MP4D_track_t*)mem;
  2839. tr = mp4->track + mp4->track_count++;
  2840. memset(tr, 0, sizeof(MP4D_track_t));
  2841. } else if (box_name == BOX_meta)
  2842. {
  2843. tr = NULL; // Avoid update of 'hdlr' box, which may contains in the 'meta' box
  2844. }
  2845. // If this box is envelope, save it's size in box stack
  2846. for (i = 0; i < NELEM(g_envelope_box); i++)
  2847. {
  2848. if (box_name == g_envelope_box[i].name)
  2849. {
  2850. if (++depth >= MAX_CHUNKS_DEPTH)
  2851. {
  2852. ERROR("too deep atoms nesting!");
  2853. }
  2854. stack[depth].bytes = payload_bytes;
  2855. stack[depth].format = g_envelope_box[i].type;
  2856. break;
  2857. }
  2858. }
  2859. // if box is not envelope, just skip it
  2860. if (i == NELEM(g_envelope_box))
  2861. {
  2862. if (payload_bytes > file_size)
  2863. {
  2864. eof_flag = 1;
  2865. } else
  2866. {
  2867. SKIP(payload_bytes);
  2868. }
  2869. }
  2870. // remove empty boxes from stack
  2871. // don't touch box with index 0 (which indicates whole file)
  2872. while (depth > 0 && !stack[depth].bytes)
  2873. {
  2874. depth--;
  2875. }
  2876. } while(!eof_flag);
  2877. if (!mp4->track_count)
  2878. {
  2879. RETURN_ERROR("no tracks found");
  2880. }
  2881. return 1;
  2882. }
  2883. /**
  2884. * Find chunk, containing given sample.
  2885. * Returns chunk number, and first sample in this chunk.
  2886. */
  2887. static int sample_to_chunk(MP4D_track_t *tr, unsigned nsample, unsigned *nfirst_sample_in_chunk)
  2888. {
  2889. unsigned chunk_group = 0, nc;
  2890. unsigned sum = 0;
  2891. *nfirst_sample_in_chunk = 0;
  2892. if (tr->chunk_count <= 1)
  2893. {
  2894. return 0;
  2895. }
  2896. for (nc = 0; nc < tr->chunk_count; nc++)
  2897. {
  2898. if (chunk_group + 1 < tr->sample_to_chunk_count // stuck at last entry till EOF
  2899. && nc + 1 == // Chunks counted starting with '1'
  2900. tr->sample_to_chunk[chunk_group + 1].first_chunk) // next group?
  2901. {
  2902. chunk_group++;
  2903. }
  2904. sum += tr->sample_to_chunk[chunk_group].samples_per_chunk;
  2905. if (nsample < sum)
  2906. return nc;
  2907. // TODO: this can be calculated once per file
  2908. *nfirst_sample_in_chunk = sum;
  2909. }
  2910. return -1;
  2911. }
  2912. // Exported API function
  2913. MP4D_file_offset_t MP4D_frame_offset(const MP4D_demux_t *mp4, unsigned ntrack, unsigned nsample, unsigned *frame_bytes, unsigned *timestamp, unsigned *duration)
  2914. {
  2915. MP4D_track_t *tr = mp4->track + ntrack;
  2916. unsigned ns;
  2917. int nchunk = sample_to_chunk(tr, nsample, &ns);
  2918. MP4D_file_offset_t offset;
  2919. if (nchunk < 0)
  2920. {
  2921. *frame_bytes = 0;
  2922. return 0;
  2923. }
  2924. offset = tr->chunk_offset[nchunk];
  2925. for (; ns < nsample; ns++)
  2926. {
  2927. offset += tr->entry_size[ns];
  2928. }
  2929. *frame_bytes = tr->entry_size[ns];
  2930. if (timestamp)
  2931. {
  2932. #if MP4D_TIMESTAMPS_SUPPORTED
  2933. *timestamp = tr->timestamp[ns];
  2934. #else
  2935. *timestamp = 0;
  2936. #endif
  2937. }
  2938. if (duration)
  2939. {
  2940. #if MP4D_TIMESTAMPS_SUPPORTED
  2941. *duration = tr->duration[ns];
  2942. #else
  2943. *duration = 0;
  2944. #endif
  2945. }
  2946. return offset;
  2947. }
  2948. #define FREE(x) if (x) {free(x); x = NULL;}
  2949. // Exported API function
  2950. void MP4D_close(MP4D_demux_t *mp4)
  2951. {
  2952. while (mp4->track_count)
  2953. {
  2954. MP4D_track_t *tr = mp4->track + --mp4->track_count;
  2955. FREE(tr->entry_size);
  2956. #if MP4D_TIMESTAMPS_SUPPORTED
  2957. FREE(tr->timestamp);
  2958. FREE(tr->duration);
  2959. #endif
  2960. FREE(tr->sample_to_chunk);
  2961. FREE(tr->chunk_offset);
  2962. FREE(tr->dsi);
  2963. }
  2964. FREE(mp4->track);
  2965. #if MP4D_INFO_SUPPORTED
  2966. FREE(mp4->tag.title);
  2967. FREE(mp4->tag.artist);
  2968. FREE(mp4->tag.album);
  2969. FREE(mp4->tag.year);
  2970. FREE(mp4->tag.comment);
  2971. FREE(mp4->tag.genre);
  2972. #endif
  2973. }
  2974. static int skip_spspps(const unsigned char *p, int nbytes, int nskip)
  2975. {
  2976. int i, k = 0;
  2977. for (i = 0; i < nskip; i++)
  2978. {
  2979. unsigned segmbytes;
  2980. if (k > nbytes - 2)
  2981. return -1;
  2982. segmbytes = p[k]*256 + p[k+1];
  2983. k += 2 + segmbytes;
  2984. }
  2985. return k;
  2986. }
  2987. static const void *MP4D_read_spspps(const MP4D_demux_t *mp4, unsigned int ntrack, int pps_flag, int nsps, int *sps_bytes)
  2988. {
  2989. int sps_count, skip_bytes;
  2990. int bytepos = 0;
  2991. unsigned char *p = mp4->track[ntrack].dsi;
  2992. if (ntrack >= mp4->track_count)
  2993. return NULL;
  2994. if (mp4->track[ntrack].object_type_indication != MP4_OBJECT_TYPE_AVC)
  2995. return NULL; // SPS/PPS are specific for AVC format only
  2996. if (pps_flag)
  2997. {
  2998. // Skip all SPS
  2999. sps_count = p[bytepos++];
  3000. skip_bytes = skip_spspps(p+bytepos, mp4->track[ntrack].dsi_bytes - bytepos, sps_count);
  3001. if (skip_bytes < 0)
  3002. return NULL;
  3003. bytepos += skip_bytes;
  3004. }
  3005. // Skip sps/pps before the given target
  3006. sps_count = p[bytepos++];
  3007. if (nsps >= sps_count)
  3008. return NULL;
  3009. skip_bytes = skip_spspps(p+bytepos, mp4->track[ntrack].dsi_bytes - bytepos, nsps);
  3010. if (skip_bytes < 0)
  3011. return NULL;
  3012. bytepos += skip_bytes;
  3013. *sps_bytes = p[bytepos]*256 + p[bytepos+1];
  3014. return p + bytepos + 2;
  3015. }
  3016. const void *MP4D_read_sps(const MP4D_demux_t *mp4, unsigned int ntrack, int nsps, int *sps_bytes)
  3017. {
  3018. return MP4D_read_spspps(mp4, ntrack, 0, nsps, sps_bytes);
  3019. }
  3020. const void *MP4D_read_pps(const MP4D_demux_t *mp4, unsigned int ntrack, int npps, int *pps_bytes)
  3021. {
  3022. return MP4D_read_spspps(mp4, ntrack, 1, npps, pps_bytes);
  3023. }
  3024. #if MP4D_PRINT_INFO_SUPPORTED
  3025. /************************************************************************/
  3026. /* Purely informational part, may be removed for embedded applications */
  3027. /************************************************************************/
  3028. //
  3029. // Decodes ISO/IEC 14496 MP4 stream type to ASCII string
  3030. //
  3031. static const char *GetMP4StreamTypeName(int streamType)
  3032. {
  3033. switch (streamType)
  3034. {
  3035. case 0x00: return "Forbidden";
  3036. case 0x01: return "ObjectDescriptorStream";
  3037. case 0x02: return "ClockReferenceStream";
  3038. case 0x03: return "SceneDescriptionStream";
  3039. case 0x04: return "VisualStream";
  3040. case 0x05: return "AudioStream";
  3041. case 0x06: return "MPEG7Stream";
  3042. case 0x07: return "IPMPStream";
  3043. case 0x08: return "ObjectContentInfoStream";
  3044. case 0x09: return "MPEGJStream";
  3045. default:
  3046. if (streamType >= 0x20 && streamType <= 0x3F)
  3047. {
  3048. return "User private";
  3049. } else
  3050. {
  3051. return "Reserved for ISO use";
  3052. }
  3053. }
  3054. }
  3055. //
  3056. // Decodes ISO/IEC 14496 MP4 object type to ASCII string
  3057. //
  3058. static const char *GetMP4ObjectTypeName(int objectTypeIndication)
  3059. {
  3060. switch (objectTypeIndication)
  3061. {
  3062. case 0x00: return "Forbidden";
  3063. case 0x01: return "Systems ISO/IEC 14496-1";
  3064. case 0x02: return "Systems ISO/IEC 14496-1";
  3065. case 0x20: return "Visual ISO/IEC 14496-2";
  3066. case 0x40: return "Audio ISO/IEC 14496-3";
  3067. case 0x60: return "Visual ISO/IEC 13818-2 Simple Profile";
  3068. case 0x61: return "Visual ISO/IEC 13818-2 Main Profile";
  3069. case 0x62: return "Visual ISO/IEC 13818-2 SNR Profile";
  3070. case 0x63: return "Visual ISO/IEC 13818-2 Spatial Profile";
  3071. case 0x64: return "Visual ISO/IEC 13818-2 High Profile";
  3072. case 0x65: return "Visual ISO/IEC 13818-2 422 Profile";
  3073. case 0x66: return "Audio ISO/IEC 13818-7 Main Profile";
  3074. case 0x67: return "Audio ISO/IEC 13818-7 LC Profile";
  3075. case 0x68: return "Audio ISO/IEC 13818-7 SSR Profile";
  3076. case 0x69: return "Audio ISO/IEC 13818-3";
  3077. case 0x6A: return "Visual ISO/IEC 11172-2";
  3078. case 0x6B: return "Audio ISO/IEC 11172-3";
  3079. case 0x6C: return "Visual ISO/IEC 10918-1";
  3080. case 0xFF: return "no object type specified";
  3081. default:
  3082. if (objectTypeIndication >= 0xC0 && objectTypeIndication <= 0xFE)
  3083. return "User private";
  3084. else
  3085. return "Reserved for ISO use";
  3086. }
  3087. }
  3088. /**
  3089. * Print MP4 information to stdout.
  3090. * Subject for customization to particular application
  3091. Output Example #1: movie file
  3092. MP4 FILE: 7 tracks found. Movie time 104.12 sec
  3093. No|type|lng| duration | bitrate| Stream type | Object type
  3094. 0|odsm|fre| 0.00 s 1 frm| 0| Forbidden | Forbidden
  3095. 1|sdsm|fre| 0.00 s 1 frm| 0| Forbidden | Forbidden
  3096. 2|vide|```| 104.12 s 2603 frm| 1960559| VisualStream | Visual ISO/IEC 14496-2 - 720x304
  3097. 3|soun|ger| 104.06 s 2439 frm| 191242| AudioStream | Audio ISO/IEC 14496-3 - 6 ch 24000 hz
  3098. 4|soun|eng| 104.06 s 2439 frm| 194171| AudioStream | Audio ISO/IEC 14496-3 - 6 ch 24000 hz
  3099. 5|subp|ger| 71.08 s 25 frm| 0| Forbidden | Forbidden
  3100. 6|subp|eng| 71.08 s 25 frm| 0| Forbidden | Forbidden
  3101. Output Example #2: audio file with tags
  3102. MP4 FILE: 1 tracks found. Movie time 92.42 sec
  3103. title = 86-Second Blowout
  3104. artist = Yo La Tengo
  3105. album = May I Sing With Me
  3106. year = 1992
  3107. No|type|lng| duration | bitrate| Stream type | Object type
  3108. 0|mdir|und| 92.42 s 3980 frm| 128000| AudioStream | Audio ISO/IEC 14496-3MP4 FILE: 1 tracks found. Movie time 92.42 sec
  3109. */
  3110. void MP4D_printf_info(const MP4D_demux_t *mp4)
  3111. {
  3112. unsigned i;
  3113. printf("\nMP4 FILE: %d tracks found. Movie time %.2f sec\n", mp4->track_count, (4294967296.0*mp4->duration_hi + mp4->duration_lo) / mp4->timescale);
  3114. #define STR_TAG(name) if (mp4->tag.name) printf("%10s = %s\n", #name, mp4->tag.name)
  3115. STR_TAG(title);
  3116. STR_TAG(artist);
  3117. STR_TAG(album);
  3118. STR_TAG(year);
  3119. STR_TAG(comment);
  3120. STR_TAG(genre);
  3121. printf("\nNo|type|lng| duration | bitrate| %-23s| Object type", "Stream type");
  3122. for (i = 0; i < mp4->track_count; i++)
  3123. {
  3124. MP4D_track_t *tr = mp4->track + i;
  3125. printf("\n%2d|%c%c%c%c|%c%c%c|%7.2f s %6d frm| %7d|", i,
  3126. (tr->handler_type >> 24), (tr->handler_type >> 16), (tr->handler_type >> 8), (tr->handler_type >> 0),
  3127. tr->language[0], tr->language[1], tr->language[2],
  3128. (65536.0*65536.0*tr->duration_hi + tr->duration_lo) / tr->timescale,
  3129. tr->sample_count,
  3130. tr->avg_bitrate_bps);
  3131. printf(" %-23s|", GetMP4StreamTypeName(tr->stream_type));
  3132. printf(" %-23s", GetMP4ObjectTypeName(tr->object_type_indication));
  3133. if (tr->handler_type == MP4D_HANDLER_TYPE_SOUN)
  3134. {
  3135. printf(" - %d ch %d hz", tr->SampleDescription.audio.channelcount, tr->SampleDescription.audio.samplerate_hz);
  3136. } else if (tr->handler_type == MP4D_HANDLER_TYPE_VIDE)
  3137. {
  3138. printf(" - %dx%d", tr->SampleDescription.video.width, tr->SampleDescription.video.height);
  3139. }
  3140. }
  3141. printf("\n");
  3142. }
  3143. #endif // MP4D_PRINT_INFO_SUPPORTED
  3144. #endif