mp4.go 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  1. package codec
  2. import "m7s.live/engine/v4/util"
  3. type MP4 interface {
  4. }
  5. type MP4Box interface {
  6. Header() *MP4Header
  7. Body() *MP4Body
  8. }
  9. //
  10. // ISO_IEC_14496-12_2012.pdf Page/17
  11. //
  12. // The standard boxes all use compact types (32-bit) and most boxes will use the compact (32-bit) size
  13. // standard header
  14. type MP4BoxHeader struct {
  15. BoxSize uint32 // 32 bits, is an integer that specifies the number of bytes in this box, including all its fields and contained boxes; if size is 1 then the actual size is in the field largesize; if size is 0, then this box is the last one in the file, and its contents extend to the end of the file (normally only used for a Media Data Box)
  16. BoxType uint32 // 32 bits, identifies the box type; standard boxes use a compact type, which is normally four printable characters, to permit ease of identification, and is shown so in the boxes below. User extensions use an extended type; in this case, the type field is set to ‘uuid’.
  17. }
  18. //
  19. // ISO_IEC_14496-12_2012.pdf Page/17
  20. //
  21. // Many objects also contain a version number and flags field
  22. // full box header
  23. type MP4FullBoxHeader struct {
  24. Version uint8 // 8 bits, is an integer that specifies the version of this format of the box.
  25. Flags [3]byte // 24 bits, is a map of flags
  26. }
  27. //
  28. // ISO_IEC_14496-12_2012.pdf Page/17
  29. //
  30. // Typically only the Media Data Box(es) need the 64-bit size.
  31. // lagesize box header
  32. type MP4BoxLargeHeader struct {
  33. LargeSize uint64 // 64 bits
  34. UUIDs [16]uint8 // 128 bits
  35. }
  36. // if(size == 1)
  37. // {
  38. // unsigned int(64) largesize;
  39. // }
  40. // else if(size == 0)
  41. // {
  42. // // box extends to end of file
  43. // }
  44. // if(boxtype == ‘uuid’)
  45. // {
  46. // unsigned int(8)[16] usertype = extended_type;
  47. // }
  48. type MP4Header struct {
  49. MP4BoxHeader
  50. }
  51. type MP4Body struct{}
  52. // -------------------------------------------------------------------------------------------------------
  53. //
  54. // ISO_IEC_14496-12_2012.pdf Page/18
  55. //
  56. // Box Type: ftyp
  57. // Container: File
  58. // Mandatory: Yes
  59. // Quantity: Exactly one (but see below)
  60. //
  61. // Each brand is a printable four-character code, registered with ISO, that identifies a precise specification
  62. type FileTypeBox struct {
  63. MP4BoxHeader // standard header
  64. MajorBrand uint32 // 32 bits, is a brand identifier
  65. MinorVersion uint32 // 32 bits, is an informative integer for the minor version of the major brand
  66. CompatibleBrands []uint32 // 32 bits array, is a list, to the end of the box, of brands
  67. }
  68. func NewFileTypeBox() (box *FileTypeBox) {
  69. box = new(FileTypeBox)
  70. util.GetBE([]byte("ftyp"), &box.MP4BoxHeader.BoxType)
  71. return
  72. }
  73. // -------------------------------------------------------------------------------------------------------
  74. //
  75. // ISO_IEC_14496-12_2012.pdf Page/30
  76. //
  77. // Box Types: pdin
  78. // Container: File
  79. // Mandatory: No
  80. // Quantity: Zero or One
  81. type ProgressiveDownloadInformationBox struct {
  82. MP4BoxHeader // standard header
  83. MP4FullBoxHeader // full box header
  84. Rate uint32 // 32 bits, is a download rate expressed in bytes/second
  85. InitialDelay uint32 // 32 bits, is the suggested delay to use when playing the file, such that if download continues at the given rate, all data within the file will arrive in time for its use and playback should not need to stall.
  86. }
  87. // -------------------------------------------------------------------------------------------------------
  88. //
  89. // ISO_IEC_14496-12_2012.pdf Page/30
  90. //
  91. // Box Type: moov
  92. // Container: File
  93. // Mandatory: Yes
  94. // Quantity: Exactly one
  95. //
  96. // The metadata for a presentation is stored in the single Movie Box which occurs at the top-level of a file.
  97. // Normally this box is close to the beginning or end of the file, though this is not required
  98. type MovieBox struct {
  99. MP4BoxHeader // standard header
  100. //Mhb MovieHeaderBox // the first child box(header box)
  101. }
  102. func NewMovieBox() (box *MovieBox) {
  103. box = new(MovieBox)
  104. util.GetBE([]byte("moov"), &box.MP4BoxHeader.BoxType)
  105. return
  106. }
  107. // -------------------------------------------------------------------------------------------------------
  108. //
  109. // ISO_IEC_14496-12_2012.pdf Page/31
  110. //
  111. // Box Type: mvhd
  112. // Container: Movie Box ('moov')
  113. // Mandatory: Yes
  114. // Quantity: Exactly one
  115. //
  116. // This box defines overall information which is media-independent, and relevant to the entire presentation
  117. // considered as a whole
  118. type MovieHeaderBox struct {
  119. MP4BoxHeader // standard header
  120. MP4FullBoxHeader // full box header
  121. CreationTime interface{} // uint64 or uint32, is an integer that declares the creation time of the presentation (in seconds since midnight, Jan. 1, 1904, in UTC time)
  122. ModificationTime interface{} // uint64 or uint32, is an integer that declares the most recent time the presentation was modified (in seconds since midnight, Jan. 1, 1904, in UTC time)
  123. TimeScale uint32 // 32 bits, is an integer that specifies the time-scale for the entire presentation; this is the number of time units that pass in one second. For example, a time coordinate system that measures time in sixtieths of a second has a time scale of 60.
  124. Duration interface{} // uint64 or uint32, is an integer that declares length of the presentation (in the indicated timescale). This property is derived from the presentation's tracks: the value of this field corresponds to the duration of the longest track in the presentation. If the duration cannot be determined then duration is set to all 1s.
  125. Rate int32 // 32 bits, is a fixed point 16.16 number that indicates the preferred rate to play the presentation; 1.0 (0x00010000) is normal forward playback
  126. Volume int16 // 16 bits, is a fixed point 8.8 number that indicates the preferred playback volume. 1.0 (0x0100) is full volume.
  127. Reserved1 int16 // 16 bits, bit[16]
  128. Reserved2 [2]uint32 // 32 bits array, const unsigned int(32)[2]
  129. Matrix [9]int32 // 32 bits array, provides a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex values(0,0,0x40000000).
  130. PreDefined [6]int32 // 32 bits array, bit(32)[6]
  131. NextTrackID uint32 // 32 bits, is a non-zero integer that indicates a value to use for the track ID of the next track to be added to this presentation. Zero is not a valid track ID value. The value of next_track_ID shall be larger than the largest track-ID in use. If this value is equal to all 1s (32-bit maxint), and a new media track is to be added, then a search must be made in the file for an unused track identifier.
  132. }
  133. // CreationTime : 创建时间(相对于UTC时间1904-01-01零点的秒数)
  134. // ModificationTime : 修改时间
  135. // TimeScale : 文件媒体在1秒时间内的刻度值,可以理解为1秒长度的时间单元数
  136. // Duration : 该track的时间长度,用duration和time scale值可以计算track时长,比如audio track的time scale = 8000, duration = 560128,时长为70.016,video track的time scale = 600, duration = 42000,时长为70
  137. // Rate : 推荐播放速率,高16位和低16位分别为小数点整数部分和小数部分,即[16.16] 格式,该值为1.0(0x00010000)表示正常前向播放
  138. // Volume : 与rate类似,[8.8] 格式,1.0(0x0100)表示最大音量
  139. // Matrix : 视频变换矩阵 { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }
  140. // NextTrackID : 下一个track使用的id号
  141. // PreDefined:
  142. // Preview Time : 开始预览此movie的时间
  143. // Preview Duration : 以movie的time scale为单位,预览的duration
  144. // Poster Time : The time value of the time of the movie poster.
  145. // Selection Time : The time value for the start time of the current selection.
  146. // Selection Duration : The duration of the current selection in movie time scale units.
  147. // Current Time : 当前时间
  148. // -------------------------------------------------------------------------------------------------------
  149. //
  150. // ISO_IEC_14496-12_2012.pdf Page/32
  151. //
  152. // Box Type: trak
  153. // Container: Movie Box ('moov')
  154. // Mandatory: Yes
  155. // Quantity: One or more
  156. type TrackBox struct {
  157. MP4BoxHeader // standard header
  158. Thb TrackHeaderBox // the first child box(header box)
  159. }
  160. // -------------------------------------------------------------------------------------------------------
  161. //
  162. // ISO_IEC_14496-12_2012.pdf Page/32
  163. //
  164. // Box Type: tkhd
  165. // Container: Track Box ('trak')
  166. // Mandatory: Yes
  167. // Quantity: Exactly one
  168. type TrackHeaderBox struct {
  169. MP4BoxHeader // standard header
  170. MP4FullBoxHeader // full box header
  171. CreationTime interface{} // uint64 or uint32,
  172. ModificationTime interface{} // uint64 or uint32,
  173. TrackID uint32 // 32 bits, is an integer that uniquely identifies this track over the entire life-time of this presentation. Track IDs are never re-used and cannot be zero
  174. Reserved1 uint32 // 32 bits,
  175. Duration interface{} // uint64 or uint32,
  176. Reserved2 [2]uint32 // 32 bits array,
  177. Layer int16 // 16 bits, specifies the front-to-back ordering of video tracks; tracks with lower numbers are closer to the viewer. 0 is the normal value, and -1 would be in front of track 0, and so on
  178. AlternateGroup int16 // 16 bits,
  179. Volume int16 // 16 bits, if track_is_audio 0x0100 else 0
  180. Reserved3 uint16 // 16 bits,
  181. Matrix [9]int32 // 32 bits array, provides a transformation matrix for the video; (u,v,w) are restricted here to (0,0,1), hex (0,0,0x40000000). { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }
  182. Width uint32 // 32 bits,
  183. Height uint32 // 32 bits,
  184. }
  185. // CreationTime : 创建时间
  186. // ModificationTime : 修改时间
  187. // TrackID : id号,不能重复且不能为0
  188. // Reserved1 : 保留位
  189. // Duration : track的时间长度
  190. // Reserved2 : 保留位
  191. // Layer : 视频层,默认为0,值小的在上层
  192. // AlternateGroup : track分组信息,默认为0表示该track未与其他track有群组关系
  193. // Volume : [8.8] 格式,如果为音频track,1.0(0x0100)表示最大音量;否则为0
  194. // Reserved3 : 保留位
  195. // Matrix : 视频变换矩阵 { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }
  196. // Width : 宽
  197. // Height : 高,均为 [16.16] 格式值,与sample描述中的实际画面大小比值,用于播放时的展示宽高
  198. // -------------------------------------------------------------------------------------------------------
  199. //
  200. // ISO_IEC_14496-12_2012.pdf Page/34
  201. //
  202. // Box Type: tref
  203. // Container: Track Box (‘trak’)
  204. // Mandatory: No
  205. // Quantity: Zero or one
  206. type TrackReferenceBox struct {
  207. MP4BoxHeader // standard header
  208. }
  209. type TrackReferenceTypeBox struct {
  210. MP4BoxHeader // standard header
  211. TrackIDs []uint32 // 32 bits, is an integer that provides a reference from the containing track to another track in the presentation. track_IDs are never re-used and cannot be equal to zero
  212. }
  213. // -------------------------------------------------------------------------------------------------------
  214. //
  215. // ISO_IEC_14496-12_2012.pdf Page/35
  216. //
  217. // Box Type: trgr
  218. // Container: Track Box (‘trak’)
  219. // Mandatory: No
  220. // Quantity: Zero or one
  221. type TrackGroupBox struct {
  222. MP4BoxHeader // standard header
  223. }
  224. type TrackGroupTypeBox struct {
  225. MP4BoxHeader // standard header
  226. MP4FullBoxHeader // full box header
  227. TrackGroupID uint32 // 32 bits, indicates the grouping type and shall be set to one of the following values, or a value registered, or a value from a derived specification or registration
  228. }
  229. // -------------------------------------------------------------------------------------------------------
  230. //
  231. // ISO_IEC_14496-12_2012.pdf Page/54
  232. //
  233. // Box Type: edts
  234. // Container: Track Box (‘trak’)
  235. // Mandatory: No
  236. // Quantity: Zero or one
  237. type EditBox struct {
  238. MP4BoxHeader // standard header
  239. }
  240. // -------------------------------------------------------------------------------------------------------
  241. //
  242. // ISO_IEC_14496-12_2012.pdf Page/54
  243. //
  244. // Box Type: elst
  245. // Container: Edit Box (‘edts’)
  246. // Mandatory: No
  247. // Quantity: Zero or one
  248. type EditListBox struct {
  249. MP4BoxHeader // standard header
  250. MP4FullBoxHeader // full box header
  251. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table
  252. Tables []EditListTable // Edit List Table
  253. }
  254. type EditListTable struct {
  255. SegmentDuration interface{} // uint64 or uint32, is an integer that specifies the duration of this edit segment in units of the timescale in the Movie Header Box
  256. MediaTime interface{} // uint64 or uint32, is an integer containing the starting time within the media of this edit segment (in media time scale units, in composition time). If this field is set to –1, it is an empty edit. The last edit in a track shall never be an empty edit. Any difference between the duration in the Movie Header Box, and the track’s duration is expressed as an implicit empty edit at the end.
  257. MediaRateInteger int16 // 16 bits,
  258. MediaRateFraction int16 // 16 bits,
  259. }
  260. // -------------------------------------------------------------------------------------------------------
  261. //
  262. // ISO_IEC_14496-12_2012.pdf Page/36
  263. //
  264. // Box Type: mdia
  265. // Container: Track Box ('trak')
  266. // Mandatory: Yes
  267. // Quantity: Exactly one
  268. //
  269. // The media declaration container contains all the objects that declare information about the media data within a track.
  270. type MediaBox struct {
  271. MP4BoxHeader // standard header
  272. Mhb MediaHeaderBox // the first child box(header box)
  273. }
  274. // -------------------------------------------------------------------------------------------------------
  275. //
  276. // ISO_IEC_14496-12_2012.pdf Page/36
  277. //
  278. // Box Type: mdhd
  279. // Container: Media Box ('mdia')
  280. // Mandatory: Yes
  281. // Quantity: Exactly one
  282. //
  283. // The media header declares overall information that is media-independent, and relevant to characteristics of the media in a track.
  284. type MediaHeaderBox struct {
  285. MP4BoxHeader // standard header
  286. MP4FullBoxHeader // full box header
  287. CreationTime interface{} // int64 or int32, is an integer that declares the creation time of the presentation (in seconds since midnight, Jan. 1, 1904, in UTC time)
  288. ModificationTime interface{} // int64 or int32, is an integer that declares the most recent time the presentation was modified (in seconds since midnight, Jan. 1, 1904, in UTC time)
  289. TimeScale uint32 // 32 bits, is an integer that specifies the time-scale for the entire presentation; this is the number of time units that pass in one second. For example, a time coordinate system that measures time in sixtieths of a second has a time scale of 60.
  290. Duration interface{} // int64 or int32, is an integer that declares length of the presentation (in the indicated timescale). This property is derived from the presentation's tracks: the value of this field corresponds to the duration of the longest track in the presentation. If the duration cannot be determined then duration is set to all 1s.
  291. Pad byte // 1 bit,
  292. Language [2]byte // 15 bits, unsigned int(5)[3], declares the language code for this media. See ISO 639-2/T for the set of three charactercodes. Each character is packed as the difference between its ASCII value and 0x60. Since the code is confined to being three lower-case letters, these values are strictly positive
  293. PreDefined uint16 // 16 bits,
  294. }
  295. // Language : 媒体的语言码
  296. // PreDefined : 媒体的回放质量???怎样生成此质量,什么是参照点
  297. // -------------------------------------------------------------------------------------------------------
  298. //
  299. // ISO_IEC_14496-12_2012.pdf Page/37
  300. //
  301. // Box Type: hdlr
  302. // Container: Media Box ('mdia') or Meta Box ('meta')
  303. // Mandatory: Yes
  304. // Quantity: Exactly one
  305. type HandlerBox struct {
  306. MP4BoxHeader // standard header
  307. MP4FullBoxHeader // full box header
  308. PreDefined uint32 // 32 bits,
  309. HandlerType uint32 // 32 bits, when present in a meta box, contains an appropriate value to indicate the format of the meta box contents. The value 'null' can be used in the primary meta box to indicate that it is merely being used to hold resources
  310. Reserved [3]uint32 // 32 bits,
  311. Name string // string, is a null-terminated string in UTF-8 characters which gives a human-readable name for the track type (for debugging and inspection purposes).
  312. }
  313. // handler_type when present in a media box, is an integer containing one of the following values, or a value from a derived specification:
  314. // 'vide' Video track
  315. // 'soun' Audio track
  316. // 'hint' Hint track
  317. // 'meta' Timed Metadata track
  318. // 'auxv' Auxiliary Video track
  319. // -------------------------------------------------------------------------------------------------------
  320. //
  321. // ISO_IEC_14496-12_2012.pdf Page/38
  322. //
  323. // Box Type: minf
  324. // Container: Media Box ('mdia')
  325. // Mandatory: Yes
  326. // Quantity: Exactly one
  327. //
  328. // This box contains all the objects that declare characteristic information of the media in the track.
  329. type MediaInformationBox struct {
  330. MP4BoxHeader // standard header
  331. MP4FullBoxHeader // full box header
  332. }
  333. // -------------------------------------------------------------------------------------------------------
  334. //
  335. // ISO_IEC_14496-12_2012.pdf Page/38
  336. //
  337. // Box Types: vmhd, smhd, hmhd, nmhd
  338. // Container: Media Information Box (‘minf’)
  339. // Mandatory: Yes
  340. // Quantity: Exactly one specific media header shall be present
  341. //
  342. // There is a different media information header for each track type (corresponding to the media handler-type);
  343. // the matching header shall be present, which may be one of those defined here, or one defined in a derived specification
  344. type MediaInformationHeaderBoxes struct {
  345. // VideoMediaHeaderBox
  346. //
  347. }
  348. // Box Types: vmhd
  349. // The video media header contains general presentation information, independent of the coding, for video media.
  350. // Note that the flags field has the value 1.
  351. type VideoMediaHeaderBox struct {
  352. MP4BoxHeader // standard header
  353. MP4FullBoxHeader // full box header
  354. GraphicsMode uint16 // 16 bits, specifies a composition mode for this video track, from the following enumerated set, which may be extended by derived specifications: copy = 0 copy over the existing image
  355. Opcolor [3]uint16 // 16 bits array, is a set of 3 colour values (red, green, blue) available for use by graphics modes
  356. }
  357. // Box Types: smhd
  358. // The sound media header contains general presentation information, independent of the coding, for audio media.
  359. // This header is used for all tracks containing audio.
  360. type SoundMediaHeaderBox struct {
  361. MP4BoxHeader // standard header
  362. MP4FullBoxHeader // full box header
  363. Balance int16 // 16 bits, is a fixed-point 8.8 number that places mono audio tracks in a stereo space; 0 is centre (the normal value); full left is -1.0 and full right is 1.0
  364. Reserved uint16 // 16 bits,
  365. }
  366. // Box Types: hmhd
  367. // The hint media header contains general information, independent of the protocol, for hint tracks.
  368. // (A PDU is a Protocol Data Unit.)
  369. type HintMediaHeaderBox struct {
  370. MP4BoxHeader // standard header
  371. MP4FullBoxHeader // full box header
  372. MaxPDUSize uint16 // 16 bits, gives the size in bytes of the largest PDU in this (hint) stream
  373. AvgPDUSize uint16 // 16 bits, gives the average size of a PDU over the entire presentation
  374. MaxBitrate uint32 // 32 bits, gives the maximum rate in bits/second over any window of one second
  375. AvgBitrate uint32 // 32 bits, gives the average rate in bits/second over the entire presentation
  376. Reserved uint32 // 32 bits,
  377. }
  378. // Box Types: nmhd
  379. // Streams other than visual and audio (e.g., timed metadata streams) may use a null Media Header Box, as defined here.
  380. type NullMediaHeaderBox struct {
  381. MP4BoxHeader // standard header
  382. MP4FullBoxHeader // full box header
  383. }
  384. // -------------------------------------------------------------------------------------------------------
  385. //
  386. // ISO_IEC_14496-12_2012.pdf Page/56
  387. //
  388. // Box Type: dinf
  389. // Container: Media Information Box ('minf') or Meta Box ('meta')
  390. // Mandatory: Yes (required within 'minf' box) and No (optional within 'meta' box)
  391. // Quantity: Exactly one
  392. //
  393. // The data information box contains objects that declare the location of the media information in a track
  394. type DataInformationBox struct {
  395. MP4BoxHeader // standard header
  396. }
  397. // -------------------------------------------------------------------------------------------------------
  398. //
  399. // ISO_IEC_14496-12_2012.pdf Page/56
  400. //
  401. // Box Types: url, urn, dref
  402. // Container: Data Information Box ('dinf')
  403. // Mandatory: Yes
  404. // Quantity: Exactly one
  405. type DataReferenceBox struct {
  406. MP4BoxHeader // standard header
  407. MP4FullBoxHeader // full box header
  408. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table
  409. DataEntry interface{} // DataEntryUrlBox or DataEntryUrnBox.
  410. }
  411. // aligned(8) class DataReferenceBox
  412. // extends FullBox('dref', version = 0, 0) {
  413. // unsigned int(32) entry_count;
  414. // for (i=1; i <= entry_count; i++) {
  415. // DataEntryBox(entry_version, entry_flags) data_entry;
  416. // }
  417. // }
  418. type DataEntryUrlBox struct {
  419. MP4BoxHeader // standard header
  420. MP4FullBoxHeader // full box header
  421. Location string // string,
  422. }
  423. type DataEntryUrnBox struct {
  424. MP4BoxHeader // standard header
  425. MP4FullBoxHeader // full box header
  426. Name string // string,
  427. Location string // string,
  428. }
  429. // -------------------------------------------------------------------------------------------------------
  430. //
  431. // ISO_IEC_14496-12_2012.pdf Page/40
  432. //
  433. // Box Type: stbl
  434. // Container: Media Information Box ('minf')
  435. // Mandatory: Yes
  436. // Quantity: Exactly one
  437. type SampleTableBox struct {
  438. MP4BoxHeader // standard header
  439. }
  440. // -------------------------------------------------------------------------------------------------------
  441. //
  442. // ISO_IEC_14496-12_2012.pdf Page/40
  443. //
  444. // Box Types: stsd
  445. // Container: Sample Table Box ('stbl')
  446. // Mandatory: Yes
  447. // Quantity: Exactly one
  448. type SampleDescriptionBox struct {
  449. MP4BoxHeader // standard header
  450. MP4FullBoxHeader // full box header
  451. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table
  452. }
  453. // for (i = 1 ; i <= entry_count ; i++) {
  454. // switch (handler_type){
  455. // case ‘soun’: // for audio tracks
  456. // AudioSampleEntry();
  457. // break;
  458. // case ‘vide’: // for video tracks
  459. // VisualSampleEntry();
  460. // break;
  461. // case ‘hint’: // Hint track
  462. // HintSampleEntry();
  463. // break;
  464. // case ‘meta’: // Metadata track
  465. // MetadataSampleEntry();
  466. // break;
  467. // }
  468. // }
  469. // box header和version字段后会有一个entry count字段,根据entry的个数,每个entry会有type信息,如“vide”、“sund”等,
  470. // 根据type不同sample description会提供不同的信息,例如对于video track,会有“VisualSampleEntry”类型信息,
  471. // 对于audio track会有“AudioSampleEntry”类型信息.
  472. // 视频的编码类型、宽高、长度,音频的声道、采样等信息都会出现在这个box中
  473. // is the appropriate sample entry
  474. type SampleEntry struct {
  475. Reserved [6]uint8 // 48 bits,
  476. DataReferenceIndex uint16 // 16 bits, is an integer that contains the index of the data reference to use to retrieve data associated with samples that use this sample description. Data references are stored in Data Reference Boxes. The index ranges from 1 to the number of data references.
  477. }
  478. type HintSampleEntry struct {
  479. Data []uint8 // 8 bits array,
  480. }
  481. // Box Types: btrt
  482. type BitRateBox struct {
  483. MP4BoxHeader // standard header
  484. BufferSizeDB uint32 // 32 bits, gives the size of the decoding buffer for the elementary stream in bytes.
  485. MaxBitrate uint32 // 32 bits, gives the maximum rate in bits/second over any window of one second.
  486. AvgBitrate uint32 // 32 bits, gives the average rate in bits/second over the entire presentation.
  487. }
  488. type MetaDataSampleEntry struct{}
  489. type XMLMetaDataSampleEntry struct {
  490. ContentEncoding string // optional, is a null-terminated string in UTF-8 characters, and provides a MIME type which identifies the content encoding of the timed metadata
  491. NameSpace string // string, gives the namespace of the schema for the timed XML metadata
  492. SchemaLocation string // optional, optionally provides an URL to find the schema corresponding to the namespace. This is needed for decoding of the timed metadata by XML aware encoding mechanisms such as BiM.
  493. Brb BitRateBox // optional
  494. }
  495. type TextMetaDataSampleEntry struct {
  496. ContentEncoding string // optional, is a null-terminated string in UTF-8 characters, and provides a MIME type which identifies the content encoding of the timed metadata
  497. MimeFormat string // string, provides a MIME type which identifies the content format of the timed metadata. Examples for this field are ‘text/html’ and ‘text/plain’.
  498. Brb BitRateBox // optional
  499. }
  500. type URIBox struct {
  501. MP4BoxHeader // standard header
  502. MP4FullBoxHeader // full box header
  503. TheURI string // string, is a URI formatted according to the rules in 6.2.4
  504. }
  505. type URIInitBox struct {
  506. MP4BoxHeader // standard header
  507. MP4FullBoxHeader // full box header
  508. UriInitializationData []uint8 // 8 bits array, is opaque data whose form is defined in the documentation of the URI form.
  509. }
  510. type URIMetaSampleEntry struct {
  511. TheLabel URIBox
  512. Init URIInitBox // optional
  513. //Mpeg4 MPEG4BitRateBox // optional
  514. }
  515. // Box Types: pasp
  516. type PixelAspectRatioBox struct {
  517. MP4BoxHeader // standard header
  518. HSpacing uint32 // 32 bits, define the relative width and height of a pixel;
  519. VSpacing uint32 // 32 bits, define the relative width and height of a pixel;
  520. }
  521. // Box Types: clap
  522. // Visual Sequences
  523. type CleanApertureBox struct {
  524. MP4BoxHeader // standard header
  525. CleanApertureWidthN uint32 // 32 bits, a fractional number which defines the exact clean aperture width, in counted pixels, of the video image
  526. CleanApertureWidthD uint32 // 32 bits, a fractional number which defines the exact clean aperture width, in counted pixels, of the video image
  527. CleanApertureHeightN uint32 // 32 bits, a fractional number which defines the exact clean aperture height, in counted pixels, of the video image
  528. CleanApertureHeightD uint32 // 32 bits, a fractional number which defines the exact clean aperture height, in counted pixels, of the video image
  529. HorizOffN uint32 // 32 bits, a fractional number which defines the horizontal offset of clean aperture centre minus (width-1)/2. Typically 0
  530. HorizOffD uint32 // 32 bits, a fractional number which defines the horizontal offset of clean aperture centre minus (width-1)/2. Typically 0
  531. VertOffN uint32 // 32 bits, a fractional number which defines the vertical offset of clean aperture centre minus (height-1)/2. Typically 0
  532. VertOffD uint32 // 32 bits, a fractional number which defines the vertical offset of clean aperture centre minus (height-1)/2. Typically 0
  533. }
  534. // Box Types: colr
  535. type ColourInformationBox struct {
  536. MP4BoxHeader // standard header
  537. ColourType uint32 // 32 bits, an indication of the type of colour information supplied. For colour_type ‘nclx’: these fields are exactly the four bytes defined for PTM_COLOR_INFO( ) in A.7.2 of ISO/IEC 29199-2 but note that the full range flag is here in a different bit position
  538. }
  539. // if (colour_type == ‘nclx’) /* on-screen colours */
  540. // {
  541. // unsigned int(16) colour_primaries;
  542. // unsigned int(16) transfer_characteristics;
  543. // unsigned int(16) matrix_coefficients;
  544. // unsigned int(1) full_range_flag;
  545. // unsigned int(7) reserved = 0;
  546. // }
  547. // else if (colour_type == ‘rICC’)
  548. // {
  549. // ICC_profile; // restricted ICC profile
  550. // }
  551. // else if (colour_type == ‘prof’)
  552. // {
  553. // ICC_profile; // unrestricted ICC profile
  554. // }
  555. // ICC_profile : an ICC profile as defined in ISO 15076-1 or ICC.1:2010 is supplied.
  556. type VisualSampleEntry struct {
  557. PreDefined1 uint16 // 16 bits,
  558. Reserved1 uint16 // 16 bits,
  559. PreDefined2 [3]uint32 // 96 bits,
  560. Width uint16 // 16 bits, are the maximum visual width and height of the stream described by this sample description, in pixels
  561. Height uint16 // 16 bits, are the maximum visual width and height of the stream described by this sample description, in pixels
  562. HorizreSolution uint32 // 32 bits, fields give the resolution of the image in pixels-per-inch, as a fixed 16.16 number
  563. VertreSolution uint32 // 32 bits, fields give the resolution of the image in pixels-per-inch, as a fixed 16.16 number
  564. Reserved3 uint32 // 32 bits,
  565. FrameCount uint16 // 16 bits, indicates how many frames of compressed video are stored in each sample. The default is 1, for one frame per sample; it may be more than 1 for multiple frames per sample
  566. CompressorName [32]string // 32 string, is a name, for informative purposes. It is formatted in a fixed 32-byte field, with the first byte set to the number of bytes to be displayed, followed by that number of bytes of displayable data, and then padding to complete 32 bytes total (including the size byte). The field may be set to 0.
  567. Depth uint16 // 16 bits, takes one of the following values 0x0018 – images are in colour with no alpha
  568. PreDefined3 int16 // 16 bits,
  569. Cab CleanApertureBox // optional, other boxes from derived specifications
  570. Parb PixelAspectRatioBox // optional, other boxes from derived specifications
  571. }
  572. // Audio Sequences
  573. type AudioSampleEntry struct {
  574. Reserved1 [2]uint32 // 32 bits array,
  575. ChannelCount uint16 // 16 bits, is the number of channels such as 1 (mono) or 2 (stereo)
  576. SampleSize uint16 // 16 bits, is in bits, and takes the default value of 16
  577. PreDefined uint16 // 16 bits,
  578. Reserved2 uint16 // 16 bits,
  579. SampleRate uint32 // 32 bits, is the sampling rate expressed as a 16.16 fixed-point number (hi.lo)
  580. }
  581. // -------------------------------------------------------------------------------------------------------
  582. //
  583. // ISO_IEC_14496-12_2012.pdf Page/48
  584. //
  585. // Box Type: stts
  586. // Container: Sample Table Box ('stbl')
  587. // Mandatory: Yes
  588. // Quantity: Exactly one
  589. type TimeToSampleBox struct {
  590. MP4BoxHeader // standard header
  591. MP4FullBoxHeader // full box header
  592. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table
  593. Table []TimeToSampleTable // Time To Sample Table , EntryCount elements
  594. }
  595. type TimeToSampleTable struct {
  596. SampleCount []uint32 // 32 bits, is an integer that counts the number of consecutive samples that have the given duration
  597. SampleDelta []uint32 // 32 bits, is an integer that gives the delta of these samples in the time-scale of the media.
  598. }
  599. // “stts”存储了sample的duration,描述了sample时序的映射方法,我们通过它可以找到任何时间的sample.
  600. // “stts”可以包含一个压缩的表来映射时间和sample序号,用其他的表来提供每个sample的长度和指针.
  601. // 表中每个条目提供了在同一个时间偏移量里面连续的sample序号,以及samples的偏移量.
  602. // 递增这些偏移量,就可以建立一个完整的time to sample表
  603. // -------------------------------------------------------------------------------------------------------
  604. //
  605. // ISO_IEC_14496-12_2012.pdf Page/49
  606. //
  607. // Box Type: ctts
  608. // Container: Sample Table Box (‘stbl’)
  609. // Mandatory: No
  610. // Quantity: Zero or one
  611. type CompositionOffsetBox struct {
  612. MP4BoxHeader // standard header
  613. MP4FullBoxHeader // full box header
  614. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table
  615. Table []CompositionOffsetTable // Composition Offset Table, EntryCount elements.
  616. }
  617. type CompositionOffsetTable struct {
  618. SampleCount uint32 // 32 bits, is an integer that counts the number of consecutive samples that have the given offset.
  619. SampleOffset interface{} // int32 or uint32, is an integer that gives the offset between CT and DT, such that CT(n) = DT(n) + CTTS(n).
  620. }
  621. // -------------------------------------------------------------------------------------------------------
  622. //
  623. // ISO_IEC_14496-12_2012.pdf Page/50
  624. //
  625. // Box Type: cslg
  626. // Container: Sample Table Box (‘stbl’)
  627. // Mandatory: No
  628. // Quantity: Zero or one
  629. type CompositionToDecodeBox struct {
  630. MP4BoxHeader // standard header
  631. MP4FullBoxHeader // full box header
  632. CompositionToDTSShift int32 // 32 bits, signed, if this value is added to the composition times (as calculated by the CTS offsets from the DTS), then for all samples, their CTS is guaranteed to be greater than or equal to their DTS, and the buffer model implied by the indicated profile/level will be honoured; if leastDecodeToDisplayDelta is positive or zero, this field can be 0; otherwise it should be at least (- leastDecodeToDisplayDelta)
  633. LeastDecodeToDisplayDelta int32 // 32 bits, signed, the smallest composition offset in the CompositionTimeToSample box in this track
  634. GreatestDecodeToDisplayDelta int32 // 32 bits, signed, the largest composition offset in the CompositionTimeToSample box in this track
  635. CompositionStartTime int32 // 32 bits, signed, the smallest computed composition time (CTS) for any sample in the media of this track
  636. CompositionEndTime int32 // 32 bits, signed, the composition time plus the composition duration, of the sample with the largest computed composition time (CTS) in the media of this track; if this field takes the value 0, the composition end time is unknown.
  637. }
  638. // -------------------------------------------------------------------------------------------------------
  639. //
  640. // ISO_IEC_14496-12_2012.pdf Page/58
  641. //
  642. // Box Type: stsc
  643. // Container: Sample Table Box ('stbl')
  644. // Mandatory: Yes
  645. // Quantity: Exactly one
  646. type SampleToChunkBox struct {
  647. MP4BoxHeader // standard header
  648. MP4FullBoxHeader // full box header
  649. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table
  650. Table []SampleToChunkTable // Sample To Chunk Table, entry count elements.
  651. }
  652. type SampleToChunkTable struct {
  653. FirstChunk []uint32 // 32 bits, is an integer that gives the index of the first chunk in this run of chunks that share the same samples-per-chunk and sample-description-index; the index of the first chunk in a track has the value 1 (the first_chunk field in the first record of this box has the value 1, identifying that the first sample maps to the first chunk).
  654. SamplesPerChunk []uint32 // 32 bits, is an integer that gives the number of samples in each of these chunks
  655. SampleDescriptionIndex []uint32 // 32 bits, is an integer that gives the index of the sample entry that describes the samples in this chunk. The index ranges from 1 to the number of sample entries in the Sample Description Box
  656. }
  657. // 用chunk组织sample可以方便优化数据获取,一个thunk包含一个或多个sample.
  658. // “stsc”中用一个表描述了sample与chunk的映射关系,查看这张表就可以找到包含指定sample的thunk,从而找到这个sample
  659. // -------------------------------------------------------------------------------------------------------
  660. //
  661. // ISO_IEC_14496-12_2012.pdf Page/57
  662. //
  663. // Box Type: stsz, stz2
  664. // Container: Sample Table Box (‘stbl’)
  665. // Mandatory: Yes
  666. // Quantity: Exactly one variant must be present
  667. type SampleSizeBoxes struct{}
  668. // Box Type: stsz
  669. type SampleSizeBox struct {
  670. MP4BoxHeader // standard header
  671. MP4FullBoxHeader // full box header
  672. SampleSize uint32 // 32 bits, is integer specifying the default sample size. If all the samples are the same size, this field contains that size value. If this field is set to 0, then the samples have different sizes, and those sizes are stored in the sample size table. If this field is not 0, it specifies the constant sample size, and no array follows.
  673. SampleCount uint32 // 32 bits, is an integer that gives the number of samples in the track; if sample-size is 0, then it is also the number of entries in the following table.
  674. EntrySize interface{} // 32 bits array, SampleCount elements, is an integer specifying the size of a sample, indexed by its number.
  675. }
  676. // if (sample_size == 0) {
  677. // for (i = 1; i <= sample_count; i++) {
  678. // unsigned int(32) entry_size;
  679. // }
  680. // }
  681. // Box Type: stz2
  682. type CompactSampleSizeBox struct {
  683. MP4BoxHeader // standard header
  684. MP4FullBoxHeader // full box header
  685. Reserved [3]uint8 // 24 bits,
  686. FieldSize uint8 // 8 bits, is an integer specifying the size in bits of the entries in the following table; it shall take the value 4, 8 or 16. If the value 4 is used, then each byte contains two values: entry[i]<<4 + entry[i+1]; if the sizes do not fill an integral number of bytes, the last byte is padded with zeros.
  687. SampleCount uint32 // 32 bits, is an integer that gives the number of entries in the following table
  688. EntrySize interface{} //
  689. }
  690. // for (i = 1; i <= sample_count; i++) {
  691. // unsigned int(field_size) entry_size;
  692. // }
  693. // -------------------------------------------------------------------------------------------------------
  694. //
  695. // ISO_IEC_14496-12_2012.pdf Page/59
  696. //
  697. // Box Type: stco, co64
  698. // Container: Sample Table Box (‘stbl’)
  699. // Mandatory: Yes
  700. // Quantity: Exactly one variant must be present
  701. type ChunkOffsetBox struct {
  702. MP4BoxHeader // standard header
  703. MP4FullBoxHeader // full box header
  704. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table
  705. ChunkOffset []uint32 // 32 bits array, entry count elements.
  706. }
  707. // “stco”定义了每个thunk在媒体流中的位置。位置有两种可能,32位的和64位的,后者对非常大的电影很有用。
  708. // 在一个表中只会有一种可能,这个位置是在整个文件中的,而不是在任何box中的,这样做就可以直接在文件中找到媒体数据,
  709. // 而不用解释box。需要注意的是一旦前面的box有了任何改变,这张表都要重新建立,因为位置信息已经改变了
  710. // Box Type: co64
  711. type ChunkLargeOffsetBox struct {
  712. MP4BoxHeader // standard header
  713. MP4FullBoxHeader // full box header
  714. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table
  715. ChunkOffset []uint64 // 64 bits array, entry count elements.
  716. }
  717. // -------------------------------------------------------------------------------------------------------
  718. //
  719. // ISO_IEC_14496-12_2012.pdf Page/51
  720. //
  721. // Box Type: stss
  722. // Container: Sample Table Box (‘stbl’)
  723. // Mandatory: No
  724. // Quantity: Zero or one
  725. //
  726. // This box provides a compact marking of the sync samples within the stream. The table is arranged in strictly increasing order of sample number.
  727. // If the sync sample box is not present, every sample is a sync sample.
  728. type SyncSampleBox struct {
  729. MP4BoxHeader // standard header
  730. MP4FullBoxHeader // full box header
  731. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table. If entry_count is zero, there are no sync samples within the stream and the following table is empty
  732. SampleNumber []uint32 // 32 bits array, entry count elements. gives the numbers of the samples that are sync samples in the stream.
  733. }
  734. // -------------------------------------------------------------------------------------------------------
  735. //
  736. // ISO_IEC_14496-12_2012.pdf Page/52
  737. //
  738. // Box Type: stsh
  739. // Container: Sample Table Box (‘stbl’)
  740. // Mandatory: No
  741. // Quantity: Zero or one
  742. type ShadowSyncSampleBox struct {
  743. MP4BoxHeader // standard header
  744. MP4FullBoxHeader // full box header
  745. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table.
  746. Table []ShadowSyncSampleTable // Shadow Sync Sample Table, entry count elements.
  747. }
  748. type ShadowSyncSampleTable struct {
  749. ShadowedSampleNumber uint32 // 32 bits, gives the number of a sample for which there is an alternative sync sample.
  750. SyncSampleNumber uint32 // 32 bits, gives the number of the alternative sync sample.
  751. }
  752. // -------------------------------------------------------------------------------------------------------
  753. //
  754. // ISO_IEC_14496-12_2012.pdf Page/60
  755. //
  756. // Box Type: padb
  757. // Container: Sample Table (‘stbl’)
  758. // Mandatory: No
  759. // Quantity: Zero or one
  760. //
  761. // In some streams the media samples do not occupy all bits of the bytes given by the sample size, and are
  762. // padded at the end to a byte boundary. In some cases, it is necessary to record externally the number of
  763. // padding bits used. This table supplies that information.
  764. type PaddingBitsBox struct {
  765. MP4BoxHeader // standard header
  766. MP4FullBoxHeader // full box header
  767. SampleCount uint32 // 32 bits, counts the number of samples in the track; it should match the count in other tables
  768. Table []PaddingBitsTable // Padding Bits Table, (sample count + 1) / 2 elements.
  769. }
  770. type PaddingBitsTable struct {
  771. Reserved1 byte // 1 bit,
  772. Pad1 byte // 3 bits, a value from 0 to 7, indicating the number of bits at the end of sample (i*2)+1.
  773. Reserved2 byte // 1 bit,
  774. Pad2 byte // 3 bits, a value from 0 to 7, indicating the number of bits at the end of sample (i*2)+2.
  775. }
  776. // -------------------------------------------------------------------------------------------------------
  777. //
  778. // ISO_IEC_14496-12_2012.pdf Page/46
  779. //
  780. // Box Type: stdp
  781. // Container: Sample Table Box (‘stbl’).
  782. // Mandatory: No.
  783. // Quantity: Zero or one.
  784. //
  785. // This box contains the degradation priority of each sample. The values are stored in the table, one for each
  786. // sample. The size of the table, sample_count is taken from the sample_count in the Sample Size Box
  787. // ('stsz'). Specifications derived from this define the exact meaning and acceptable range of the priority field.
  788. type DegradationPriorityBox struct {
  789. MP4BoxHeader // standard header
  790. MP4FullBoxHeader // full box header
  791. Priority []uint16 // 16 bits array, sample count elements, is integer specifying the degradation priority for each sample.
  792. }
  793. // -------------------------------------------------------------------------------------------------------
  794. //
  795. // ISO_IEC_14496-12_2012.pdf Page/53
  796. //
  797. // Box Types: sdtp
  798. // Container: Sample Table Box (‘stbl’)
  799. // Mandatory: No
  800. // Quantity: Zero or one
  801. type IndependentAndDisposableSamplesBox struct {
  802. MP4BoxHeader // standard header
  803. MP4FullBoxHeader // full box header
  804. Table []IndependentAndDisposableSamplesTable // Independent And Disposable Samples Table, sample count elements
  805. }
  806. type IndependentAndDisposableSamplesTable struct {
  807. IsLeading byte // 2 bits,
  808. SampleDependsOn byte // 2 bits,
  809. SampleIsDependedOn byte // 2 bits,
  810. SampleHasTedundancy byte // 2 bits,
  811. }
  812. // is_leading takes one of the following four values:
  813. // 0: the leading nature of this sample is unknown;
  814. // 1: this sample is a leading sample that has a dependency before the referenced I-picture (and is
  815. // therefore not decodable);
  816. // 2: this sample is not a leading sample;
  817. // 3: this sample is a leading sample that has no dependency before the referenced I-picture (and is
  818. // therefore decodable);
  819. // sample_depends_on takes one of the following four values:
  820. // 0: the dependency of this sample is unknown;
  821. // 1: this sample does depend on others (not an I picture);
  822. // 2: this sample does not depend on others (I picture);
  823. // 3: reserved
  824. // sample_is_depended_on takes one of the following four values:
  825. // 0: the dependency of other samples on this sample is unknown;
  826. // 1: other samples may depend on this one (not disposable);
  827. // 2: no other sample depends on this one (disposable);
  828. // 3: reserved
  829. // sample_has_redundancy takes one of the following four values:
  830. // 0: it is unknown whether there is redundant coding in this sample;
  831. // 1: there is redundant coding in this sample;
  832. // 2: there is no redundant coding in this sample;
  833. // 3: reserved
  834. // -------------------------------------------------------------------------------------------------------
  835. //
  836. // ISO_IEC_14496-12_2012.pdf Page/75
  837. //
  838. // Box Type: sbgp
  839. // Container: Sample Table Box (‘stbl’) or Track Fragment Box (‘traf’)
  840. // Mandatory: No
  841. // Quantity: Zero or more.
  842. type SampleToGroupBox struct {
  843. MP4BoxHeader // standard header
  844. MP4FullBoxHeader // full box header
  845. GroupingType uint32 // 32 bits, is an integer that identifies the type (i.e. criterion used to form the sample groups) of the sample grouping and links it to its sample group description table with the same value for grouping type. At most one occurrence of this box with the same value for grouping_type (and, if used, grouping_type_parameter) shall exist for a track.
  846. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table.
  847. Table []SampleToGroupTable // Sample To Group Table, entry count elements.
  848. }
  849. type SampleToGroupTable struct {
  850. SampleCount uint32 // 32 bits, is an integer that gives the number of consecutive samples with the same sample group descriptor. If the sum of the sample count in this box is less than the total sample count, then the reader should effectively extend it with an entry that associates the remaining samples with no group. It is an error for the total in this box to be greater than the sample_count documented elsewhere, and the reader behaviour would then be undefined.
  851. GroupDescriptionIndex uint32 // 32 bits, is an integer that gives the index of the sample group entry which describes the samples in this group. The index ranges from 1 to the number of sample group entries in the SampleGroupDescription Box, or takes the value 0 to indicate that this sample is a member of no group of this type.
  852. }
  853. // unsigned int(32) grouping_type;
  854. // if (version == 1) {
  855. // unsigned int(32) grouping_type_parameter;
  856. // }
  857. // unsigned int(32) entry_count;
  858. // -------------------------------------------------------------------------------------------------------
  859. //
  860. // ISO_IEC_14496-12_2012.pdf Page/76
  861. //
  862. // Box Type: sgpd
  863. // Container: Sample Table Box (‘stbl’) or Track Fragment Box (‘traf’)
  864. // Mandatory: No
  865. // Quantity: Zero or more, with one for each Sample to Group Box.
  866. type SampleGroupDescriptionBox struct {
  867. MP4BoxHeader // standard header
  868. MP4FullBoxHeader // full box header
  869. GroupingType uint32 // 32 bits, is an integer that identifies the SampleToGroup box that is associated with this sample group description.
  870. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table.
  871. }
  872. // default_length : indicates the length of every group entry (if the length is constant), or zero (0) if it is variable
  873. // description_length : indicates the length of an individual group entry, in the case it varies from entry to entry and default_length is therefore 0
  874. // if (version==1) { unsigned int(32) default_length; }
  875. // for (i = 1 ; i <= entry_count ; i++){
  876. // if (version==1) {
  877. // if (default_length==0) {
  878. // unsigned int(32) description_length;
  879. // }
  880. // }
  881. // switch (handler_type){
  882. // case ‘vide’: // for video tracks
  883. // VisualSampleGroupEntry (grouping_type);
  884. // break;
  885. // case ‘soun’: // for audio tracks
  886. // AudioSampleGroupEntry(grouping_type);
  887. // break;
  888. // case ‘hint’: // for hint tracks
  889. // HintSampleGroupEntry(grouping_type);
  890. // break;
  891. // }
  892. // }
  893. type SampleGroupDescriptionEntry struct{}
  894. type VisualSampleGroupEntry struct{}
  895. type AudioSampleGroupEntry struct{}
  896. type HintSampleGroupEntry struct{}
  897. // -------------------------------------------------------------------------------------------------------
  898. //
  899. // ISO_IEC_14496-12_2012.pdf Page/61
  900. //
  901. // Box Type: subs
  902. // Container: Sample Table Box (‘stbl’) or Track Fragment Box (‘traf’)
  903. // Mandatory: No
  904. // Quantity: Zero or one
  905. type SubSampleInformationBox struct {
  906. MP4BoxHeader // standard header
  907. MP4FullBoxHeader // full box header
  908. EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table.
  909. Table []SubSampleInformationTable // Sub-Sample Information Table, entry count elements.
  910. }
  911. type SubSampleInformationTable struct {
  912. SampleDelta uint32 // 32 bits, is an integer that specifies the sample number of the sample having sub-sample structure. It is coded as the difference between the desired sample number, and the sample number indicated in the previous entry. If the current entry is the first entry, the value indicates the sample number of the first sample having sub-sample information, that is, the value is the difference between the sample number and zero (0).
  913. SubsampleCount uint16 // 16 bits, is an integer that specifies the number of sub-sample for the current sample. If there is no sub-sample structure, then this field takes the value 0.
  914. CountTable []SubSampleCountTable // Sub-Sample Information Table1, subsample count elements.
  915. }
  916. type SubSampleCountTable struct {
  917. SubsampleSize interface{} // uint16 or uint32, is an integer that specifies the size, in bytes, of the current sub-sample
  918. SubsamplePriority uint8 // 8 bits, is an integer specifying the degradation priority for each sub-sample. Higher values of subsample_priority, indicate sub-samples which are important to, and have a greater impact on, the decoded quality.
  919. DiscardAble uint8 // 8 bits, equal to 0 means that the sub-sample is required to decode the current sample, while equal to 1 means the sub-sample is not required to decode the current sample but may be used for enhancements, e.g., the sub-sample consists of supplemental enhancement information (SEI) messages.
  920. Reserved uint32 // 32 bits,
  921. }
  922. // -------------------------------------------------------------------------------------------------------
  923. //
  924. // ISO_IEC_14496-12_2012.pdf Page/62
  925. //
  926. // Box Type: saiz
  927. // Container: Sample Table Box (‘stbl’) or Track Fragment Box ('traf')
  928. // Mandatory: No
  929. // Quantity: Zero or More
  930. type SampleAuxiliaryInformationSizesBox struct {
  931. MP4BoxHeader // standard header
  932. MP4FullBoxHeader // full box header
  933. Table interface{} // SampleAuxiliaryInformationSizesTable1 or SampleAuxiliaryInformationSizesTable2.
  934. }
  935. type SampleAuxiliaryInformationSizesTable1 struct {
  936. AuxInfoType uint32 // 32 bits,
  937. AuxInfoTypeParameter uint32 // 32 bits,
  938. DefaultSampleInfoSize uint8 // 8 bits, is an integer specifying the sample auxiliary information size for the case where all the indicated samples have the same sample auxiliary information size. If the size varies then this field shall be zero.
  939. SampleCount uint32 // 32 bits,
  940. }
  941. type SampleAuxiliaryInformationSizesTable2 struct {
  942. DefaultSampleInfoSize uint8 // 8 bits, is an integer specifying the sample auxiliary information size for the case where all the indicated samples have the same sample auxiliary information size. If the size varies then this field shall be zero.
  943. SampleCount uint32 // 32 bits,
  944. }
  945. // if (flags & 1) {
  946. // unsigned int(32) aux_info_type;
  947. // unsigned int(32) aux_info_type_parameter;
  948. // }
  949. // unsigned int(8) default_sample_info_size;
  950. // unsigned int(32) sample_count;
  951. // if (default_sample_info_size == 0) {
  952. // unsigned int(8) sample_info_size[ sample_count ];
  953. // }
  954. // -------------------------------------------------------------------------------------------------------
  955. //
  956. // ISO_IEC_14496-12_2012.pdf Page/63
  957. //
  958. // Box Type: saio
  959. // Container: Sample Table Box (‘stbl’) or Track Fragment Box ('traf')
  960. // Mandatory: No
  961. // Quantity: Zero or More
  962. type SampleAuxiliaryInformationOffsetsBox struct {
  963. MP4BoxHeader // standard header
  964. MP4FullBoxHeader // full box header
  965. //EntryCount uint32 // 32 bits, is an integer that gives the number of entries in the following table.
  966. }
  967. type AuxInfo struct {
  968. AuxInfoType uint32 // 32 bits,
  969. AuxInfoTypeParameter uint32 // 32 bits,
  970. }
  971. // if (flags & 1) {
  972. // unsigned int(32) aux_info_type;
  973. // unsigned int(32) aux_info_type_parameter;
  974. // }
  975. // unsigned int(32) entry_count;
  976. // if ( version == 0 ) {
  977. // unsigned int(32) offset[ entry_count ];
  978. // }
  979. // else {
  980. // unsigned int(64) offset[ entry_count ];
  981. // }
  982. // -------------------------------------------------------------------------------------------------------
  983. //
  984. // ISO_IEC_14496-12_2012.pdf Page/78
  985. //
  986. // Box Type: udta
  987. // Container: Movie Box (‘moov’) or Track Box (‘trak’)
  988. // Mandatory: No
  989. // Quantity: Zero or one
  990. type UserDataBox struct {
  991. MP4BoxHeader // standard header
  992. }
  993. // -------------------------------------------------------------------------------------------------------
  994. //
  995. // ISO_IEC_14496-12_2012.pdf Page/64
  996. //
  997. // Box Type: mvex
  998. // Container: Movie Box (‘moov’)
  999. // Mandatory: No
  1000. // Quantity: Zero or one
  1001. type MovieExtendsBox struct {
  1002. MP4BoxHeader // standard header
  1003. }
  1004. // -------------------------------------------------------------------------------------------------------
  1005. //
  1006. // ISO_IEC_14496-12_2012.pdf Page/65
  1007. //
  1008. // Box Type: mehd
  1009. // Container: Movie Extends Box(‘mvex’)
  1010. // Mandatory: No
  1011. // Quantity: Zero or one
  1012. //
  1013. // The Movie Extends Header is optional, and provides the overall duration, including fragments, of a fragmented
  1014. // movie. If this box is not present, the overall duration must be computed by examining each fragment.
  1015. type MovieExtendsHeaderBox struct {
  1016. MP4BoxHeader // standard header
  1017. MP4FullBoxHeader // full box header
  1018. }
  1019. // if (version==1) {
  1020. // unsigned int(64) fragment_duration;
  1021. // } else { // version==0
  1022. // unsigned int(32) fragment_duration;
  1023. // }
  1024. // fragment_duration : is an integer that declares length of the presentation of the whole movie including
  1025. // fragments (in the timescale indicated in the Movie Header Box). The value of this field corresponds to
  1026. // the duration of the longest track, including movie fragments. If an MP4 file is created in real-time, such
  1027. // as used in live streaming, it is not likely that the fragment_duration is known in advance and this
  1028. // box may be omitted.
  1029. // -------------------------------------------------------------------------------------------------------
  1030. //
  1031. // ISO_IEC_14496-12_2012.pdf Page/65
  1032. //
  1033. // Box Type: trex
  1034. // Container: Movie Extends Box (‘mvex’)
  1035. // Mandatory: Yes
  1036. // Quantity: Exactly one for each track in the Movie Box
  1037. type TrackExtendsBox struct {
  1038. MP4BoxHeader // standard header
  1039. MP4FullBoxHeader // full box header
  1040. TrackID uint32 // 32 bits, identifies the track; this shall be the track ID of a track in the Movie Box
  1041. DefaultSampleDescriptionIndex uint32 // 32 bits,
  1042. DefaultSampleDuration uint32 // 32 bits,
  1043. DefaultSampleSize uint32 // 32 bits,
  1044. DefaultSampleFlags uint32 // 32 bits,
  1045. }
  1046. // default_ : these fields set up defaults used in the track fragments.
  1047. // -------------------------------------------------------------------------------------------------------
  1048. //
  1049. // ISO_IEC_14496-12_2012.pdf Page/72
  1050. //
  1051. // Box Type: leva
  1052. // Container: Movie Extends Box (`mvex’)
  1053. // Mandatory: No
  1054. // Quantity: Zero or one
  1055. type LevelAssignmentBox struct {
  1056. MP4BoxHeader // standard header
  1057. MP4FullBoxHeader // full box header
  1058. LevelCount uint8 // 8 bits, specifies the number of levels each fraction is grouped into. level_count shall be greater than or equal to 2.
  1059. Table []LevelAssignmentTable // Level Assignment Table, level count elements.
  1060. }
  1061. type LevelAssignmentTable struct {
  1062. TrackId uint32 // 32 bits, for loop entry j specifies the track identifier of the track assigned to level j.
  1063. PaddingFlag byte // 1 bit, equal to 1 indicates that a conforming fraction can be formed by concatenating any positive integer number of levels within a fraction and padding the last Media Data box by zero bytes up to the full size that is indicated in the header of the last Media Data box. The semantics of padding_flag equal to 0 are that this is not assured.
  1064. AssignmentType byte // 7 bits,
  1065. }
  1066. // for (j=1; j <= level_count; j++) {
  1067. // unsigned int(32) track_id;
  1068. // unsigned int(1) padding_flag;
  1069. // unsigned int(7) assignment_type;
  1070. // if (assignment_type == 0) {
  1071. // unsigned int(32) grouping_type;
  1072. // }
  1073. // else if (assignment_type == 1) {
  1074. // unsigned int(32) grouping_type;
  1075. // unsigned int(32) grouping_type_parameter;
  1076. // }
  1077. // else if (assignment_type == 2) {} // no further syntax elements needed
  1078. // else if (assignment_type == 3) {} // no further syntax elements needed
  1079. // else if (assignment_type == 4) {
  1080. // unsigned int(32) sub_track_id;
  1081. // }
  1082. // // other assignment_type values are reserved
  1083. // }
  1084. // assignment_type : indicates the mechanism used to specify the assignment to a level.
  1085. // assignment_type values greater than 4 are reserved, while the semantics for the other values are
  1086. // specified as follows. The sequence of assignment_types is restricted to be a set of zero or more of
  1087. // type 2 or 3, followed by zero or more of exactly one type.
  1088. // • 0: sample groups are used to specify levels, i.e., samples mapped to different sample group
  1089. // description indexes of a particular sample grouping lie in different levels within the identified track;
  1090. // other tracks are not affected and must have all their data in precisely one level;
  1091. // • 1: as for assignment_type 0 except assignment is by a parameterized sample group;
  1092. // • 2, 3: level assignment is by track (see the Subsegment Index Box for the difference in processing
  1093. // of these levels)
  1094. // • 4: the respective level contains the samples for a sub-track. The sub-tracks are specified through
  1095. // the Sub Track box; other tracks are not affected and must have all their data in precisely one
  1096. // level;
  1097. // -------------------------------------------------------------------------------------------------------
  1098. //
  1099. // ISO_IEC_14496-12_2012.pdf Page/66
  1100. //
  1101. // Box Type: moof
  1102. // Container: File
  1103. // Mandatory: No
  1104. // Quantity: Zero or more
  1105. type MovieFragmentBox struct {
  1106. MP4BoxHeader // standard header
  1107. }
  1108. // -------------------------------------------------------------------------------------------------------
  1109. //
  1110. // ISO_IEC_14496-12_2012.pdf Page/66
  1111. //
  1112. // Box Type: mfhd
  1113. // Container: Movie Fragment Box ('moof')
  1114. // Mandatory: Yes
  1115. // Quantity: Exactly one
  1116. //
  1117. // The movie fragment header contains a sequence number, as a safety check. The sequence number usually
  1118. // starts at 1 and must increase for each movie fragment in the file, in the order in which they occur. This allows
  1119. // readers to verify integrity of the sequence; it is an error to construct a file where the fragments are out of sequence.
  1120. type MovieFragmentHeaderBox struct {
  1121. MP4BoxHeader // standard header
  1122. MP4FullBoxHeader // full box header
  1123. SequenceNumber uint32 // 32 bits, the ordinal number of this fragment, in increasing order
  1124. }
  1125. // -------------------------------------------------------------------------------------------------------
  1126. //
  1127. // ISO_IEC_14496-12_2012.pdf Page/67
  1128. //
  1129. // Box Type: traf
  1130. // Container: Movie Fragment Box ('moof')
  1131. // Mandatory: No
  1132. // Quantity: Zero or more
  1133. type TrackFragmentBox struct {
  1134. MP4BoxHeader // standard header
  1135. }
  1136. // -------------------------------------------------------------------------------------------------------
  1137. //
  1138. // ISO_IEC_14496-12_2012.pdf Page/67
  1139. //
  1140. // Box Type: tfhd
  1141. // Container: Track Fragment Box ('traf')
  1142. // Mandatory: Yes
  1143. // Quantity: Exactly one
  1144. type TrackFragmentHeaderBox struct {
  1145. MP4BoxHeader // standard header
  1146. MP4FullBoxHeader // full box header
  1147. TrackID uint32 // 32 bits,
  1148. // all the following are optional fields
  1149. BaseDataOffset uint64 // 64 bits, the base offset to use when calculating data offsets
  1150. SampleDescriptionIndex uint32 // 32 bits,
  1151. DefaultSampleDuration uint32 // 32 bits,
  1152. DefaultSampleSize uint32 // 32 bits,
  1153. DefaultSampleFlags uint32 // 32 bits,
  1154. }
  1155. // -------------------------------------------------------------------------------------------------------
  1156. //
  1157. // ISO_IEC_14496-12_2012.pdf Page/68
  1158. //
  1159. // Box Type: trun
  1160. // Container: Track Fragment Box ('traf')
  1161. // Mandatory: No
  1162. // Quantity: Zero or more
  1163. type TrackFragmentRunBox struct {
  1164. MP4BoxHeader // standard header
  1165. MP4FullBoxHeader // full box header
  1166. SampleCount uint32 // 32 bits, the number of samples being added in this run; also the number of rows in the following table (the rows can be empty)
  1167. // the following are optional fields
  1168. DataOffset int32 // 32 bits, signed, is added to the implicit or explicit data_offset established in the track fragment header.
  1169. FirstSampleFlags uint32 // 32 bits, provides a set of flags for the first sample only of this run.
  1170. // all fields in the following array are optional
  1171. Table []TrackFragmentRunTable // Track Fragment Run Table 1, SampleCount elements.
  1172. }
  1173. type TrackFragmentRunTable struct {
  1174. SampleDuration uint32 // 32 bits,
  1175. SampleSize uint32 // 32 bits,
  1176. SampleFlags uint32 // 32 bits,
  1177. SampleCompositionTimeOffset interface{} // uint32 or int32,
  1178. }
  1179. // if (version == 0){
  1180. // unsigned int(32) sample_composition_time_offset;
  1181. // }
  1182. // else{
  1183. // signed int(32) sample_composition_time_offset;
  1184. // }
  1185. // -------------------------------------------------------------------------------------------------------
  1186. //
  1187. // ISO_IEC_14496-12_2012.pdf Page/71
  1188. //
  1189. // Box Type: tfdt
  1190. // Container: Track Fragment box (‘traf’)
  1191. // Mandatory: No
  1192. // Quantity: Zero or one
  1193. type TrackFragmentBaseMediaDecodeTimeBox struct {
  1194. MP4BoxHeader // standard header
  1195. MP4FullBoxHeader // full box header
  1196. BaseMediaDecodeTime interface{} // uint32 or uint64, is an integer equal to the sum of the decode durations of all earlier samples in the media, expressed in the media's timescale. It does not include the samples added in the enclosing track fragment.
  1197. }
  1198. // if (version==1) {
  1199. // unsigned int(64) baseMediaDecodeTime;
  1200. // } else { // version==0
  1201. // unsigned int(32) baseMediaDecodeTime;
  1202. // -------------------------------------------------------------------------------------------------------
  1203. //
  1204. // ISO_IEC_14496-12_2012.pdf Page/69
  1205. //
  1206. // Box Type: mfra
  1207. // Container: File
  1208. // Mandatory: No
  1209. // Quantity: Zero or one
  1210. type MovieFragmentRandomAccessBox struct {
  1211. MP4BoxHeader // standard header
  1212. }
  1213. // -------------------------------------------------------------------------------------------------------
  1214. //
  1215. // ISO_IEC_14496-12_2012.pdf Page/70
  1216. //
  1217. // Box Type: tfra
  1218. // Container: Movie Fragment Random Access Box (‘mfra’)
  1219. // Mandatory: No
  1220. // Quantity: Zero or one per track
  1221. type TrackFragmentRandomAccessBox struct {
  1222. MP4BoxHeader // standard header
  1223. MP4FullBoxHeader // full box header
  1224. TrackID uint32 // 32 bits, is an integer identifying the track_ID.
  1225. Reserved uint32 // 26 bits,
  1226. LengthSizeOfTrafNum byte // 2 bits, indicates the length in byte of the traf_number field minus one.
  1227. LengthSizeOfTrunNum byte // 2 bits, indicates the length in byte of the trun_number field minus one.
  1228. LengthSizeOfSampleNum byte // 2 bits, indicates the length in byte of the sample_number field minus one.
  1229. NumberOfEntry uint32 // 32 bits, is an integer that gives the number of the entries for this track. If this value is zero, it indicates that every sample is a sync sample and no table entry follows.
  1230. Table []TrackFragmentRandomAccessTable // Track Fragment RandomAccess Table 1, NumberOfEntry elements.
  1231. }
  1232. type TrackFragmentRandomAccessTable struct {
  1233. Time interface{} // uint32 or uint64, is 32 or 64 bits integer that indicates the presentation time of the sync sample in units defined in the ‘mdhd’ of the associated track.
  1234. Moofoffset interface{} // uint32 or uint64, is 32 or 64 bits integer that gives the offset of the ‘moof’ used in this entry. Offset is the byte-offset between the beginning of the file and the beginning of the ‘moof’.
  1235. TrafNumber interface{} // unsigned int((length_size_of_traf_num+1) * 8). indicates the ‘traf’ number that contains the sync sample. The number ranges from 1 (the first ‘traf’ is numbered 1) in each ‘moof’.
  1236. TrunNumber interface{} // unsigned int((length_size_of_trun_num+1) * 8). indicates the ‘trun’ number that contains the sync sample. The number ranges from 1 in each ‘traf’
  1237. SampleNumber interface{} // unsigned int((length_size_of_sample_num+1) * 8) . indicates the sample number of the sync sample. The number ranges from 1 in each ‘trun’.
  1238. }
  1239. // for(i=1; i <= number_of_entry; i++){
  1240. // if(version==1){
  1241. // unsigned int(64) time;
  1242. // unsigned int(64) moof_offset;
  1243. // }else{
  1244. // unsigned int(32) time;
  1245. // unsigned int(32) moof_offset;
  1246. // }
  1247. // unsigned int((length_size_of_traf_num+1) * 8) traf_number;
  1248. // unsigned int((length_size_of_trun_num+1) * 8) trun_number;
  1249. // unsigned int((length_size_of_sample_num+1) * 8) sample_number;
  1250. // }
  1251. // -------------------------------------------------------------------------------------------------------
  1252. //
  1253. // ISO_IEC_14496-12_2012.pdf Page/71
  1254. //
  1255. // Box Type: mfro
  1256. // Container: Movie Fragment Random Access Box (‘mfra’)
  1257. // Mandatory: Yes
  1258. // Quantity: Exactly one
  1259. //
  1260. // The Movie Fragment Random Access Offset Box provides a copy of the length field from the enclosing Movie
  1261. // Fragment Random Access Box. It is placed last within that box, so that the size field is also last in the
  1262. // enclosing Movie Fragment Random Access Box. When the Movie Fragment Random Access Box is also last
  1263. // in the file this permits its easy location. The size field here must be correct. However, neither the presence of
  1264. // the Movie Fragment Random Access Box, nor its placement last in the file, are assured.
  1265. type MovieFragmentRandomAccessOffsetBox struct {
  1266. MP4BoxHeader // standard header
  1267. MP4FullBoxHeader // full box header
  1268. Size uint32 // 32 bits, is an integer gives the number of bytes of the enclosing ‘mfra’ box. This field is placed at the last of the enclosing box to assist readers scanning from the end of the file in finding the ‘mfra’ box.
  1269. }
  1270. // -------------------------------------------------------------------------------------------------------
  1271. //
  1272. // ISO_IEC_14496-12_2012.pdf Page/29
  1273. //
  1274. // Box Type: mdat
  1275. // Container: File
  1276. // Mandatory: No
  1277. // Quantity: Zero or more
  1278. type MediaDataBox struct {
  1279. MP4BoxHeader // standard header
  1280. Data []byte // 8 bits array, is the contained media data.
  1281. }
  1282. // -------------------------------------------------------------------------------------------------------
  1283. //
  1284. // ISO_IEC_14496-12_2012.pdf Page/29
  1285. //
  1286. // Box Types: free, skip
  1287. // Container: File or other box
  1288. // Mandatory: No
  1289. // Quantity: Zero or more
  1290. //
  1291. // The contents of a free-space box are irrelevant and may be ignored, or the object deleted, without affecting
  1292. // the presentation. (Care should be exercised when deleting the object, as this may invalidate the offsets used
  1293. // in the sample table, unless this object is after all the media data).
  1294. type FreeSpaceBox struct {
  1295. MP4BoxHeader // standard header
  1296. Data []uint8 // 8 bits array,
  1297. }
  1298. // -------------------------------------------------------------------------------------------------------
  1299. //
  1300. // ISO_IEC_14496-12_2012.pdf Page/78
  1301. //
  1302. // Box Type: cprt
  1303. // Container: User data box (‘udta’)
  1304. // Mandatory: No
  1305. // Quantity: Zero or more
  1306. //
  1307. // The Copyright box contains a copyright declaration which applies to the entire presentation, when contained
  1308. // within the Movie Box, or, when contained in a track, to that entire track. There may be multiple copyright
  1309. // boxes using different language codes.
  1310. type CopyrightBox struct {
  1311. MP4BoxHeader // standard header
  1312. MP4FullBoxHeader // full box header
  1313. Pad byte // 1 bit,
  1314. Language [2]byte // 15 bits, declares the language code for the following text. See ISO 639-2/T for the set of three character codes. Each character is packed as the difference between its ASCII value and 0x60. The code is confined to being three lower-case letters, so these values are strictly positive.
  1315. Notice string // string, is a null-terminated string in either UTF-8 or UTF-16 characters, giving a copyright notice. If UTF- 16 is used, the string shall start with the BYTE ORDER MARK (0xFEFF), to distinguish it from a UTF- 8 string. This mark does not form part of the final string.
  1316. }
  1317. // -------------------------------------------------------------------------------------------------------
  1318. //
  1319. // ISO_IEC_14496-12_2012.pdf Page/79
  1320. //
  1321. // Box Type: tsel
  1322. // Container: User Data Box (‘udta’)
  1323. // Mandatory: No
  1324. // Quantity: Zero or One
  1325. //
  1326. // The track selection box is contained in the user data box of the track it modifies.
  1327. type TrackSelectionBox struct {
  1328. MP4BoxHeader // standard header
  1329. MP4FullBoxHeader // full box header
  1330. SwitchGroup int32 // 32 bits, is an integer that specifies a group or collection of tracks. If this field is 0 (default value) or if the Track Selection box is absent there is no information on whether the track can be used for switching during playing or streaming. If this integer is not 0 it shall be the same for tracks that can be used for switching between each other. Tracks that belong to the same switch group shall belong to the same alternate group. A switch group may have only one member.
  1331. AttributeList []uint32 // 32 bits array, to end of the box, is a list, to the end of the box, of attributes. The attributes in this list should be used as descriptions of tracks or differentiation criteria for tracks in the same alternate or switch group. Each differentiating attribute is associated with a pointer to the field or information that distinguishes the track.
  1332. }
  1333. // -------------------------------------------------------------------------------------------------------
  1334. //
  1335. // ISO_IEC_14496-12_2012.pdf Page/100
  1336. //
  1337. // Box Type: strk
  1338. // Container: User Data box (‘udta’) of the corresponding Track box (‘trak’)
  1339. // Mandatory: No
  1340. // Quantity: Zero or more
  1341. //
  1342. // This box contains objects that define and provide information about a sub track in the present track.
  1343. type SubTrack struct {
  1344. MP4BoxHeader // standard header
  1345. }
  1346. // -------------------------------------------------------------------------------------------------------
  1347. //
  1348. // ISO_IEC_14496-12_2012.pdf Page/100
  1349. //
  1350. // Box Type: stri
  1351. // Container: Sub Track box (‘strk’)
  1352. // Mandatory: Yes
  1353. // Quantity: One
  1354. type SubTrackInformation struct {
  1355. MP4BoxHeader // standard header
  1356. MP4FullBoxHeader // full box header
  1357. SwitchGroup int16 // 16 bits,
  1358. AlternateGroup int16 // 16 bits,
  1359. SubTrackID uint32 // 32 bits, is an integer. A non-zero value uniquely identifies the sub track locally within the track. A zero value (default) means that sub track ID is not assigned.
  1360. AttributeList []uint32 // 32 bits array, is a list, to the end of the box, of attributes. The attributes in this list should be used as descriptions of sub tracks or differentiating criteria for tracks and sub tracks in the same alternate or switch group
  1361. }
  1362. // -------------------------------------------------------------------------------------------------------
  1363. //
  1364. // ISO_IEC_14496-12_2012.pdf Page/101
  1365. //
  1366. // Box Type: strd
  1367. // Container: Sub Track box (‘strk’)
  1368. // Mandatory: Yes
  1369. // Quantity: One
  1370. //
  1371. // This box contains objects that provide a definition of the sub track.
  1372. type SubTrackDefinition struct {
  1373. MP4BoxHeader // standard header
  1374. }
  1375. // -------------------------------------------------------------------------------------------------------
  1376. //
  1377. // ISO_IEC_14496-12_2012.pdf Page/81
  1378. //
  1379. // Box Type: meta
  1380. // Container: File, Movie Box (‘moov’), Track Box (‘trak’), or Additional Metadata Container Box (‘meco’)
  1381. // Mandatory: No
  1382. // Quantity: Zero or one (in File, ‘moov’, and ‘trak’), One or more (in ‘meco’)
  1383. type MetaBox struct {
  1384. MP4BoxHeader // standard header
  1385. MP4FullBoxHeader // full box header
  1386. PrimaryResource PrimaryItemBox // optional
  1387. FileLocations DataInformationBox // optional
  1388. ItemLocations ItemLocationBox // optional
  1389. Protections ItemProtectionBox // optional
  1390. ItemInfos ItemInfoBox // optional
  1391. IPMPControl IPMPControlBox // optional
  1392. ItemRefs ItemReferenceBox // optional
  1393. ItemData ItemDataBox // optional
  1394. //OtherBoxes []Box // optional
  1395. }
  1396. type IPMPControlBox struct{}
  1397. // -------------------------------------------------------------------------------------------------------
  1398. //
  1399. // ISO_IEC_14496-12_2012.pdf Page/82
  1400. //
  1401. // Box Type: iloc
  1402. // Container: Meta box (‘meta’)
  1403. // Mandatory: No
  1404. // Quantity: Zero or one
  1405. type ItemLocationBox struct {
  1406. MP4BoxHeader // standard header
  1407. MP4FullBoxHeader // full box header
  1408. OffsetSize byte // 4 bits,
  1409. LengthSize byte // 4 bits,
  1410. BaseOffsetSize byte // 4 bits,
  1411. IndexSize byte // 4 bits, if version == 1, index_size replace to reserved.
  1412. ItemCount uint16 // 16 bits,
  1413. Table interface{} // version == 1 -> ItemLocationTable1 , version == 2 -> ItemLocationTable2, ItemCount elements.
  1414. }
  1415. type ItemLocationTable1 struct {
  1416. ItemID uint16 // 16 bits,
  1417. Reserved uint16 // 12 bits,
  1418. ConstructionMethod byte // 4 bits,
  1419. DataReferenceIndex uint16 // 16 bits,
  1420. BaseOffset interface{} // unsigned int(base_offset_size*8),
  1421. ExtentCount uint16 // 16 bits,
  1422. ExtentTable []ItemLocationExtentTable1 // Item Location Extent Table1, ExtentCount elements.
  1423. }
  1424. type ItemLocationTable2 struct {
  1425. ItemID uint16 // 16 bits,
  1426. DataReferenceIndex uint16 // 16 bits,
  1427. BaseOffset interface{} // unsigned int(base_offset_size*8),
  1428. ExtentCount uint16 // 16 bits,
  1429. ExtentTable []ItemLocationExtentTable2 // Item Location Extent Table2, ExtentCount elements.
  1430. }
  1431. type ItemLocationExtentTable1 struct {
  1432. ExtentIndex interface{} // unsigned int(index_size*8)
  1433. ItemLocationExtentTable2
  1434. }
  1435. type ItemLocationExtentTable2 struct {
  1436. ExtentOffset interface{} // unsigned int(offset_size*8)
  1437. ExtentLength interface{} // unsigned int(length_size*8)
  1438. }
  1439. // for (i=0; i<item_count; i++) {
  1440. // unsigned int(16) item_ID;
  1441. // if (version == 1) {
  1442. // unsigned int(12) reserved = 0;
  1443. // unsigned int(4) construction_method;
  1444. // }
  1445. // unsigned int(16) data_reference_index;
  1446. // unsigned int(base_offset_size*8) base_offset;
  1447. // unsigned int(16) extent_count;
  1448. // for (j=0; j<extent_count; j++) {
  1449. // if ((version == 1) && (index_size > 0)) {
  1450. // unsigned int(index_size*8) extent_index;
  1451. // }
  1452. // unsigned int(offset_size*8) extent_offset;
  1453. // unsigned int(length_size*8) extent_length;
  1454. // }
  1455. // offset_size : is taken from the set {0, 4, 8} and indicates the length in bytes of the offset field.
  1456. // length_size : is taken from the set {0, 4, 8} and indicates the length in bytes of the length field.
  1457. // base_offset_size : is taken from the set {0, 4, 8} and indicates the length in bytes of the base_offset field.
  1458. // index_size : is taken from the set {0, 4, 8} and indicates the length in bytes of the extent_index field.
  1459. // item_count : counts the number of resources in the following array.
  1460. // item_ID : is an arbitrary integer ‘name’ for this resource which can be used to refer to it (e.g. in a URL).
  1461. // construction_method : is taken from the set 0 (file), 1 (idat) or 2 (item)
  1462. // data-reference-index : is either zero (‘this file’) or a 1-based index into the data references in the data information box.
  1463. // base_offset : provides a base value for offset calculations within the referenced data. If
  1464. // base_offset_size : is 0, base_offset takes the value 0, i.e. it is unused.
  1465. // extent_count : provides the count of the number of extents into which the resource is fragmented; it must have the value 1 or greater
  1466. // extent_index : provides an index as defined for the construction method
  1467. // extent_offset : provides the absolute offset in bytes from the beginning of the containing file, of this item. If offset_size is 0, offset takes the value 0
  1468. // extent_length : provides the absolute length in bytes of this metadata item. If length_size is 0, length takes the value 0. If the value is 0, then length of the item is the length of the entire referenced file.
  1469. // -------------------------------------------------------------------------------------------------------
  1470. //
  1471. // ISO_IEC_14496-12_2012.pdf Page/85
  1472. //
  1473. // Box Type: ipro
  1474. // Container: Meta box (‘meta’)
  1475. // Mandatory: No
  1476. // Quantity: Zero or one
  1477. //
  1478. // The item protection box provides an array of item protection information, for use by the Item Information Box.
  1479. type ItemProtectionBox struct {
  1480. MP4BoxHeader // standard header
  1481. MP4FullBoxHeader // full box header
  1482. ProtectionCount uint16 // 16 bits,
  1483. Table []ItemProtectionTable // Item Protection Table, ProtectionCount elements.
  1484. }
  1485. type ItemProtectionTable struct {
  1486. ProtectionInformation ProtectionSchemeInfoBox
  1487. }
  1488. // -------------------------------------------------------------------------------------------------------
  1489. //
  1490. // ISO_IEC_14496-12_2012.pdf Page/92
  1491. //
  1492. // Box Types: sinf
  1493. // Container: Protected Sample Entry, or Item Protection Box (‘ipro’)
  1494. // Mandatory: Yes
  1495. // Quantity: One or More
  1496. type ProtectionSchemeInfoBox struct {
  1497. MP4BoxHeader // standard header
  1498. OriginalFormat OriginalFormatBox //
  1499. Type SchemeTypeBox // optional
  1500. Info SchemeInformationBox // optional
  1501. }
  1502. // -------------------------------------------------------------------------------------------------------
  1503. //
  1504. // ISO_IEC_14496-12_2012.pdf Page/93
  1505. //
  1506. // Box Types: frma
  1507. // Container: Protection Scheme Information Box (‘sinf’) or Restricted Scheme Information Box (‘rinf’)
  1508. // Mandatory: Yes when used in a protected sample entry or in a restricted sample entry
  1509. // Quantity: Exactly one
  1510. //
  1511. // The Original Format Box ‘frma’ contains the four-character-code of the original un-transformed sample description:
  1512. type OriginalFormatBox struct {
  1513. MP4BoxHeader // standard header
  1514. DataFormat uint32 // 32 bits, is the four-character-code of the original un-transformed sample entry (e.g. “mp4v” if the stream contains protected or restricted MPEG-4 visual material).
  1515. }
  1516. // -------------------------------------------------------------------------------------------------------
  1517. //
  1518. // ISO_IEC_14496-12_2012.pdf Page/93
  1519. //
  1520. // Box Types: schm
  1521. // Container: Protection Scheme Information Box (‘sinf’), Restricted Scheme Information Box (‘rinf’),
  1522. // or SRTP Process box (‘srpp‘)
  1523. // Mandatory: No
  1524. //
  1525. // Quantity: Zero or one in ‘sinf’, depending on the protection structure; Exactly one in ‘rinf’ and ‘srpp’
  1526. // The Scheme Type Box (‘schm’) identifies the protection or restriction scheme.
  1527. type SchemeTypeBox struct {
  1528. MP4BoxHeader // standard header
  1529. MP4FullBoxHeader // full box header
  1530. SchemeType uint32 // 32 bits, is the code defining the protection or restriction scheme.
  1531. SchemeVersion uint32 // 32 bits, is the version of the scheme (used to create the content)
  1532. }
  1533. // if (flags & 0x000001) {
  1534. // unsigned int(8) scheme_uri[]; // browser uri
  1535. // }
  1536. // scheme_URI : allows for the option of directing the user to a web-page if they do not have the scheme installed on their system. It is an absolute URI formed as a null-terminated string in UTF-8 characters.
  1537. // -------------------------------------------------------------------------------------------------------
  1538. //
  1539. // ISO_IEC_14496-12_2012.pdf Page/94
  1540. //
  1541. // Box Types: schi
  1542. // Container: Protection Scheme Information Box (‘sinf’), Restricted Scheme Information Box (‘rinf’),
  1543. // or SRTP Process box (‘srpp‘)
  1544. // Mandatory: No
  1545. // Quantity: Zero or one
  1546. // The Scheme Information Box is a container Box that is only interpreted by the scheme being used. Any
  1547. // information the encryption or restriction system needs is stored here. The content of this box is a series of
  1548. // boxes whose type and format are defined by the scheme declared in the Scheme Type Box.
  1549. type SchemeInformationBox struct {
  1550. MP4BoxHeader // standard header
  1551. SchemeSpecificData []SchemeTypeBox
  1552. }
  1553. // -------------------------------------------------------------------------------------------------------
  1554. //
  1555. // ISO_IEC_14496-12_2012.pdf Page/85
  1556. //
  1557. // Box Type: iinf
  1558. // Container: Meta Box (‘meta’)
  1559. // Mandatory: No
  1560. // Quantity: Zero or one
  1561. type ItemInfoBox struct {
  1562. MP4BoxHeader // standard header
  1563. MP4FullBoxHeader // full box header
  1564. EntryCount uint16 // 16 bits,
  1565. ItemInfos []ItemInfoEntry // EntryCount elements.
  1566. }
  1567. // Box Type: infe
  1568. type ItemInfoEntry struct {
  1569. MP4BoxHeader // standard header
  1570. MP4FullBoxHeader // full box header
  1571. ItemID uint16 // 16 bits
  1572. ItemProtectionIndex uint16 // 16 bits
  1573. ItemType uint32 // 32 bits,
  1574. ItemName string // string,
  1575. ContentType string // string,
  1576. ContentEncoding string // string, optional
  1577. ItemUriType string // string,
  1578. ExtensionType uint32 // 32 bits, optional
  1579. ItemInfoExtension // optional
  1580. }
  1581. type ItemInfoExtension struct {
  1582. }
  1583. // if ((version == 0) || (version == 1)) {
  1584. // unsigned int(16) item_ID;
  1585. // unsigned int(16) item_protection_index
  1586. // string item_name;
  1587. // string content_type;
  1588. // string content_encoding; //optional
  1589. // }
  1590. // if (version == 1) {
  1591. // unsigned int(32) extension_type; //optional
  1592. // ItemInfoExtension(extension_type); //optional
  1593. // }
  1594. // if (version == 2) {
  1595. // unsigned int(16) item_ID;
  1596. // unsigned int(16) item_protection_index;
  1597. // unsigned int(32) item_type;
  1598. // string item_name;
  1599. // if (item_type==’mime’) {
  1600. // string content_type;
  1601. // string content_encoding; //optional
  1602. // } else if (item_type == ‘uri ‘) {
  1603. // string item_uri_type;
  1604. // }
  1605. // }
  1606. // item_id : contains either 0 for the primary resource (e.g., the XML contained in an ‘xml ‘ box) or the ID of the item for which the following information is defined.
  1607. // item_protection_index : contains either 0 for an unprotected item, or the one-based index into the item protection box defining the protection applied to this item (the first box in the item protection box has the index 1).
  1608. // item_name : is a null-terminated string in UTF-8 characters containing a symbolic name of the item (source file for file delivery transmissions).
  1609. // item_type : is a 32-bit value, typically 4 printable characters, that is a defined valid item type indicator, such as ‘mime’
  1610. // content_type : is a null-terminated string in UTF-8 characters with the MIME type of the item. If the item is content encoded (see below), then the content type refers to the item after content decoding.
  1611. // item_uri_type : is a string that is an absolute URI, that is used as a type indicator.
  1612. // content_encoding : is an optional null-terminated string in UTF-8 characters used to indicate that the binary file is encoded and needs to be decoded before interpreted. The values are as defined for Content-Encoding for HTTP/1.1. Some possible values are “gzip”, “compress” and “deflate”. An empty string indicates no content encoding. Note that the item is stored after the content encoding has been applied.
  1613. // extension_type : is a printable four-character code that identifies the extension fields of version 1 with respect to version 0 of the Item information entry.
  1614. // content_location : is a null-terminated string in UTF-8 characters containing the URI of the file as defined in HTTP/1.1 (RFC 2616).
  1615. // content_MD5 : is a null-terminated string in UTF-8 characters containing an MD5 digest of the file. See HTTP/1.1 (RFC 2616) and RFC 1864.
  1616. // content_length : gives the total length (in bytes) of the (un-encoded) file.
  1617. // transfer_length : gives the total length (in bytes) of the (encoded) file. Note that transfer length is equal to content length if no content encoding is applied (see above).
  1618. // entry_count provides : a count of the number of entries in the following array.
  1619. // group_ID : indicates a file group to which the file item (source file) belongs. See 3GPP TS 26.346 for more details on file groups.
  1620. // -------------------------------------------------------------------------------------------------------
  1621. //
  1622. // ISO_IEC_14496-12_2012.pdf Page/82
  1623. //
  1624. // Box Type: ‘xml ‘ or ‘bxml’
  1625. // Container: Meta box (‘meta’)
  1626. // Mandatory: No
  1627. // Quantity: Zero or one
  1628. //
  1629. // When the primary data is in XML format and it is desired that the XML be stored directly in the meta-box, one
  1630. // of these forms may be used. The Binary XML Box may only be used when there is a single well-defined
  1631. // binarization of the XML for that defined format as identified by the handler.
  1632. // Within an XML box the data is in UTF-8 format unless the data starts with a byte-order-mark (BOM), which
  1633. // indicates that the data is in UTF-16 format.
  1634. type XMLBox struct {
  1635. MP4BoxHeader // standard header
  1636. MP4FullBoxHeader // full box header
  1637. XML string // string,
  1638. }
  1639. type BinaryXMLBox struct {
  1640. MP4BoxHeader // standard header
  1641. MP4FullBoxHeader // full box header
  1642. Data []uint8 // 8 bits array,
  1643. }
  1644. // -------------------------------------------------------------------------------------------------------
  1645. //
  1646. // ISO_IEC_14496-12_2012.pdf Page/93
  1647. //
  1648. // Box Type: pitm
  1649. // Container: Meta box (‘meta’)
  1650. // Mandatory: No
  1651. // Quantity: Zero or one
  1652. //
  1653. // For a given handler, the primary data may be one of the referenced items when it is desired that it be stored
  1654. // elsewhere, or divided into extents; or the primary metadata may be contained in the meta-box (e.g. in an XML
  1655. // box). Either this box must occur, or there must be a box within the meta-box (e.g. an XML box) containing the
  1656. // primary information in the format required by the identified handler.
  1657. type PrimaryItemBox struct {
  1658. MP4BoxHeader // standard header
  1659. MP4FullBoxHeader // full box header
  1660. ItemID uint16 // 16 bits, is the identifier of the primary item
  1661. }
  1662. // -------------------------------------------------------------------------------------------------------
  1663. //
  1664. // ISO_IEC_14496-12_2012.pdf Page/95
  1665. //
  1666. // Box Type: fiin
  1667. // Container: Meta Box (‘meta’)
  1668. // Mandatory: No
  1669. // Quantity: Zero or one
  1670. //
  1671. // The FD item information box is optional, although it is mandatory for files using FD hint tracks. It provides
  1672. // information on the partitioning of source files and how FD hint tracks are combined into FD sessions. Each
  1673. // partition entry provides details on a particular file partitioning, FEC encoding and associated File and FEC
  1674. // reservoirs. It is possible to provide multiple entries for one source file (identified by its item ID) if alternative
  1675. // FEC encoding schemes or partitionings are used in the file. All partition entries are implicitly numbered and
  1676. // the first entry has number 1.
  1677. type FDItemInformationBox struct {
  1678. MP4BoxHeader // standard header
  1679. MP4FullBoxHeader // full box header
  1680. EntryCount uint16 // 16 bits,
  1681. PE []PartitionEntry // EntryCount elements.
  1682. FDSGB FDSessionGroupBox // optional
  1683. GidToNameB GroupIdToNameBox // optional
  1684. }
  1685. // Box Type: paen
  1686. type PartitionEntry struct {
  1687. FPB FilePartitionBox //
  1688. FECRB FECReservoirBox //optional
  1689. FRB FileReservoirBox //optional
  1690. }
  1691. // -------------------------------------------------------------------------------------------------------
  1692. //
  1693. // ISO_IEC_14496-12_2012.pdf Page/99
  1694. //
  1695. // Box Type: fire
  1696. // Container: Partition Entry (‘paen’)
  1697. // Mandatory: No
  1698. // Quantity: Zero or One
  1699. //
  1700. // The File reservoir box associates the source file identified in the file partition box ('fpar') with File reservoirs
  1701. // stored as additional items. It contains a list that starts with the first File reservoir associated with the first
  1702. // source block of the source file and continues sequentially through the source blocks of the source file.
  1703. type FileReservoirBox struct {
  1704. MP4BoxHeader // standard header
  1705. MP4FullBoxHeader // full box header
  1706. EntryCount uint16 // 16 bits, gives the number of entries in the following list. An entry count here should match the total number or blocks in the corresponding file partition box.
  1707. Table []FileReservoirTable // EntryCount elements.
  1708. }
  1709. type FileReservoirTable struct {
  1710. ItemID uint16 // 16 bits, indicates the location of the File reservoir associated with a source block.
  1711. SymbolCount uint32 // 32 bits, indicates the number of source symbols contained in the File reservoir.
  1712. }
  1713. // -------------------------------------------------------------------------------------------------------
  1714. //
  1715. // ISO_IEC_14496-12_2012.pdf Page/95
  1716. //
  1717. // Box Type: fpar
  1718. // Container: Partition Entry (‘paen’)
  1719. // Mandatory: Yes
  1720. // Quantity: Exactly one
  1721. //
  1722. // The File Partition box identifies the source file and provides a partitioning of that file into source blocks and
  1723. // symbols. Further information about the source file, e.g., filename, content location and group IDs, is contained
  1724. // in the Item Information box ('iinf'), where the Item Information entry corresponding to the item ID of the
  1725. // source file is of version 1 and includes a File Delivery Item Information Extension ('fdel').
  1726. type FilePartitionBox struct {
  1727. MP4BoxHeader // standard header
  1728. MP4FullBoxHeader // full box header
  1729. ItemID uint16 // 16 bits,
  1730. PacketPayloadSize uint16 // 16 bits,
  1731. Reserved uint8 // 8 bits,
  1732. FECEncodingID uint8 // 8 bits,
  1733. FECInstanceID uint16 // 16 bits,
  1734. MaxSourceBlockLength uint16 // 16 bits,
  1735. EncodingSymbolLength uint16 // 16 bits,
  1736. MaxNumberOfEncodingSymbols uint16 // 16 bits,
  1737. SchemeSpecificInfo string // string,
  1738. EntryCount uint16 // 16 bits,
  1739. Tanble []FilePartitionTable //File Partition Table, EntryCount elements.
  1740. }
  1741. type FilePartitionTable struct {
  1742. BlockCount uint16 // 16 bits,
  1743. BlockSize uint32 // 32 bits,
  1744. }
  1745. // item_ID : references the item in the item location box ('iloc') that the file partitioning applies to.
  1746. // packet_payload_size : gives the target ALC/LCT or FLUTE packet payload size of the partitioning algorithm. Note that UDP packet payloads are larger, as they also contain ALC/LCT or FLUTE headers.
  1747. // FEC_encoding_ID : identifies the FEC encoding scheme and is subject to IANA registration (see RFC 5052). Note that i) value zero corresponds to the "Compact No-Code FEC scheme" also known as "Null-FEC" (RFC 3695); ii) value one corresponds to the “MBMS FEC” (3GPP TS 26.346); iii) for values in the range of 0 to 127, inclusive, the FEC scheme is Fully-Specified, whereas for values in the range of 128 to 255, inclusive, the FEC scheme is Under-Specified.
  1748. // FEC_instance_ID : provides a more specific identification of the FEC encoder being used for an UnderSpecified FEC scheme. This value should be set to zero for Fully-Specified FEC schemes and shall be ignored when parsing a file with FEC_encoding_ID in the range of 0 to 127, inclusive. FEC_instance_ID is scoped by the FEC_encoding_ID. See RFC 5052 for further details.
  1749. // max_source_block_length : gives the maximum number of source symbols per source block.
  1750. // encoding_symbol_length : gives the size (in bytes) of one encoding symbol. All encoding symbols of one item have the same length, except the last symbol which may be shorter.
  1751. // max_number_of_encoding_symbols : gives the maximum number of encoding symbols that can be generated for a source block for those FEC schemes in which the maximum number of encoding symbols is relevant, such as FEC encoding ID 129 defined in RFC 5052. For those FEC schemes in which the maximum number of encoding symbols is not relevant, the semantics of this field is unspecified.
  1752. // scheme_specific_info : is a base64-encoded null-terminated string of the scheme-specific object transfer information (FEC-OTI-Scheme-Specific-Info). The definition of the information depends on the FEC encoding ID.
  1753. // entry_count : gives the number of entries in the list of (block_count, block_size) pairs that provides a partitioning of the source file. Starting from the beginning of the file, each entry indicates how the next segment of the file is divided into source blocks and source symbols.
  1754. // block_count : indicates the number of consecutive source blocks of size block_size.
  1755. // block_size : indicates the size of a block (in bytes). A block_size that is not a multiple of the encoding_symbol_length symbol size indicates with Compact No-Code FEC that the last source symbols includes padding that is not stored in the item. With MBMS FEC (3GPP TS 26.346) the padding may extend across multiple symbols but the size of padding should never be more than encoding_symbol_length.
  1756. // -------------------------------------------------------------------------------------------------------
  1757. //
  1758. // ISO_IEC_14496-12_2012.pdf Page/97
  1759. //
  1760. // Box Type: fecr
  1761. // Container: Partition Entry (‘paen’)
  1762. // Mandatory: No
  1763. // Quantity: Zero or One
  1764. //
  1765. // The FEC reservoir box associates the source file identified in the file partition box ('fpar') with FEC
  1766. // reservoirs stored as additional items. It contains a list that starts with the first FEC reservoir associated with
  1767. // the first source block of the source file and continues sequentially through the source blocks of the source file.
  1768. type FECReservoirBox struct {
  1769. MP4BoxHeader // standard header
  1770. MP4FullBoxHeader // full box header
  1771. EntryCount uint16 // 16 bits,
  1772. Table []FECReservoirTable // FEC Reservoir Table, EntryCount elements.
  1773. }
  1774. type FECReservoirTable struct {
  1775. ItemID uint16 // 16 bits, indicates the location of the FEC reservoir associated with a source block.
  1776. SymbolCount uint32 // 32 bits, indicates the number of repair symbols contained in the FEC reservoir.
  1777. }
  1778. // -------------------------------------------------------------------------------------------------------
  1779. //
  1780. // ISO_IEC_14496-12_2012.pdf Page/97
  1781. //
  1782. // Box Type: segr
  1783. // Container: FD Information Box (‘fiin’)
  1784. // Mandatory: No
  1785. // Quantity: Zero or One
  1786. type FDSessionGroupBox struct {
  1787. MP4BoxHeader // standard header
  1788. NumSessionGroups uint16 // 16 bits,
  1789. Table []FDSessionGroupTable // FD Session Group Table, NumSessionGroups elements.
  1790. }
  1791. type FDSessionGroupTable struct {
  1792. EntryCount uint8 // 8 bits,
  1793. GIDTable []FDSessionGroupIDTable // FDSession Group ID Table, EntryCount elements.
  1794. NumChannelsInSessionGroup uint16 // 16 bits
  1795. HTIDTable []FDSessionHintTrackIDTable // FDSession Hint Track ID Table, NumChannelsInSessionGroup elements.
  1796. }
  1797. type FDSessionGroupIDTable struct {
  1798. GroupID uint32 // 32 bits
  1799. }
  1800. type FDSessionHintTrackIDTable struct {
  1801. HintTrackID uint32 // 32 bits
  1802. }
  1803. // for(i=0; i < num_session_groups; i++) {
  1804. // unsigned int(8) entry_count;
  1805. // for (j=0; j < entry_count; j++) {
  1806. // unsigned int(32) group_ID;
  1807. // }
  1808. // unsigned int(16) num_channels_in_session_group;
  1809. // for(k=0; k < num_channels_in_session_group; k++) {
  1810. // unsigned int(32) hint_track_id;
  1811. // }
  1812. // }
  1813. // num_session_groups : specifies the number of session groups.
  1814. // entry_count : gives the number of entries in the following list comprising all file groups that the session group complies with. The session group contains all files included in the listed file groups as specified by the item information entry of each source file. Note that the FDT for the session group should only contain those groups that are listed in this structure.
  1815. // group_ID : indicates a file group that the session group complies with.
  1816. // num_channels_in_session_groups : specifies the number of channels in the session group. The value of num_channels_in_session_groups shall be a positive integer.
  1817. // hint_track_ID : specifies the track ID of the FD hint track belonging to a particular session group. Note that one FD hint track corresponds to one LCT channel.
  1818. // -------------------------------------------------------------------------------------------------------
  1819. //
  1820. // ISO_IEC_14496-12_2012.pdf Page/98
  1821. //
  1822. // Box Type: gitn
  1823. // Container: FD Information Box (‘fiin’)
  1824. // Mandatory: No
  1825. // Quantity: Zero or One
  1826. //
  1827. // The Group ID to Name box associates file group names to file group IDs used in the version 1 item
  1828. // information entries in the item information box ('iinf').
  1829. type GroupIdToNameBox struct {
  1830. MP4BoxHeader // standard header
  1831. MP4FullBoxHeader // full box header
  1832. EntryCount uint16 // 16 bits, gives the number of entries in the following list.
  1833. Table []GroupIdToNameBox // Group Id To Name Table, EntryCount elements.
  1834. }
  1835. type GroupIdToNameTable struct {
  1836. GroupID uint32 // 32 bits, indicates a file group.
  1837. GroupName string // string, is a null-terminated string in UTF-8 characters containing a file group name.
  1838. }
  1839. // -------------------------------------------------------------------------------------------------------
  1840. //
  1841. // ISO_IEC_14496-12_2012.pdf Page/90
  1842. //
  1843. // Box Type: idat
  1844. // Container: Metadata box (‘meta’)
  1845. // Mandatory: No
  1846. // Quantity: Zero or one
  1847. //
  1848. // This box contains the data of metadata items that use the construction method indicating that an item’s data
  1849. // extents are stored within this box.
  1850. type ItemDataBox struct {
  1851. MP4BoxHeader // standard header
  1852. Data []byte // 8 bits array, is the contained meta data
  1853. }
  1854. // -------------------------------------------------------------------------------------------------------
  1855. //
  1856. // ISO_IEC_14496-12_2012.pdf Page/91
  1857. //
  1858. // Box Type: iref
  1859. // Container: Metadata box (‘meta’)
  1860. // Mandatory: No
  1861. // Quantity: Zero or one
  1862. //
  1863. // The item reference box allows the linking of one item to others via typed references. All the references for one
  1864. // item of a specific type are collected into a single item type reference box, whose type is the reference type,
  1865. // and which has a ‘from item ID’ field indicating which item is linked. The items linked to are then represented by
  1866. // an array of ‘to item ID’s. All these single item type reference boxes are then collected into the item reference
  1867. // box. The reference types defined for the track reference box defined in 8.3.3 may be used here if appropriate,
  1868. // or other registered reference types.
  1869. type ItemReferenceBox struct {
  1870. MP4BoxHeader // standard header
  1871. MP4FullBoxHeader // full box header
  1872. SITRB []SingleItemTypeReferenceBox
  1873. }
  1874. type SingleItemTypeReferenceBox struct {
  1875. MP4BoxHeader // standard header
  1876. FromItemID uint16 // 16 bits, contains the ID of the item that refers to other items
  1877. ReferenceCount uint16 // 16 bits, is the number of references
  1878. Table []SingleItemTypeReferenceTable // Single Item Type Reference Table, ReferenceCount elements.
  1879. }
  1880. type SingleItemTypeReferenceTable struct {
  1881. ToItemID uint16 // 16 bits, contains the ID of the item referred to
  1882. }
  1883. // -------------------------------------------------------------------------------------------------------
  1884. //
  1885. // ISO_IEC_14496-12_2012.pdf Page/87
  1886. //
  1887. // Box Type: meco
  1888. // Container: File, Movie Box (‘moov’), or Track Box (‘trak’)
  1889. // Mandatory: No
  1890. // Quantity: Zero or one
  1891. type AdditionalMetadataContainerBox struct {
  1892. MP4BoxHeader // standard header
  1893. }
  1894. // -------------------------------------------------------------------------------------------------------
  1895. //
  1896. // ISO_IEC_14496-12_2012.pdf Page/88
  1897. //
  1898. // Box Type: mere
  1899. // Container: Additional Metadata Container Box (‘meco’)
  1900. // Mandatory: No
  1901. // Quantity: Zero or more
  1902. //
  1903. // The metabox relation box indicates a relation between two meta boxes at the same level, i.e., the top level of
  1904. // the file, the Movie Box, or Track Box. The relation between two meta boxes is unspecified if there is no
  1905. // metabox relation box for those meta boxes. Meta boxes are referenced by specifying their handler types.
  1906. type MetaboxRelationBox struct {
  1907. MP4BoxHeader // standard header
  1908. MP4FullBoxHeader // full box header
  1909. FirstMetaboxHandlerType uint32 // 32 bits, indicates the first meta box to be related.
  1910. SecondMetaboxHandlerType uint32 // 32 bits, indicates the second meta box to be related.
  1911. MetaboxRelation uint8 // 8 bits, indicates the relation between the two meta boxes.
  1912. }
  1913. // metabox_relation indicates the relation between the two meta boxes. The following values are defined:
  1914. // 1 The relationship between the boxes is unknown (which is the default when this box is not present);
  1915. // 2 the two boxes are semantically un-related (e.g., one is presentation, the other annotation);
  1916. // 3 the two boxes are semantically related but complementary (e.g., two disjoint sets of meta-data expressed in two different meta-data systems);
  1917. // 4 the two boxes are semantically related but overlap (e.g., two sets of meta-data neither of which is a subset of the other); neither is ‘preferred’ to the other;
  1918. // 5 the two boxes are semantically related but the second is a proper subset or weaker version of the first; the first is preferred;
  1919. // 6 the two boxes are semantically related and equivalent (e.g., two essentially identical sets of meta-data expressed in two different meta-data systems).
  1920. // -------------------------------------------------------------------------------------------------------
  1921. //
  1922. // ISO_IEC_14496-12_2012.pdf Page/105
  1923. //
  1924. // Box Type: styp
  1925. // Container: File
  1926. // Mandatory: No
  1927. // Quantity: Zero or more
  1928. //
  1929. // If segments are stored in separate files (e.g. on a standard HTTP server) it is recommended that these
  1930. // ‘segment files’ contain a segment-type box, which must be first if present, to enable identification of those files,
  1931. // and declaration of the specifications with which they are compliant.
  1932. // A segment type has the same format as an 'ftyp' box [4.3], except that it takes the box type 'styp'. The
  1933. // brands within it may include the same brands that were included in the 'ftyp' box that preceded the
  1934. // ‘moov’ box, and may also include additional brands to indicate the compatibility of this segment with various
  1935. // specification(s).
  1936. // Valid segment type boxes shall be the first box in a segment. Segment type boxes may be removed if
  1937. // segments are concatenated (e.g. to form a full file), but this is not required. Segment type boxes that are not
  1938. // first in their files may be ignored.
  1939. // -------------------------------------------------------------------------------------------------------
  1940. //
  1941. // ISO_IEC_14496-12_2012.pdf Page/106
  1942. //
  1943. // Box Type: sidx
  1944. // Container: File
  1945. // Mandatory: No
  1946. // Quantity: Zero or more
  1947. type SegmentIndexBox struct {
  1948. MP4BoxHeader // standard header
  1949. MP4FullBoxHeader // full box header
  1950. ReferenceID uint32 // 32 bits,
  1951. TimeScale uint32 // 32 bits,
  1952. EarliestPresentationTime interface{} // uint32 or uint64,
  1953. FirstOffset interface{} // uint32 or uint64,
  1954. Reserved uint16 // 16 bits,
  1955. ReferenceCount uint16 // 16 bits,
  1956. Table []SegmentIndexTable // Segment Index Table, ReferenceCount elements
  1957. }
  1958. type SegmentIndexTable struct {
  1959. ReferenceType byte // 1 bit
  1960. ReferencedSize uint32 // 32 bits
  1961. SubSegmentDuration uint32 // 32 bits,
  1962. StartsWithSAP byte // 1 bit
  1963. SAPType byte // 3 bits,
  1964. SAPDeltaTime uint32 // 28 bits,
  1965. }
  1966. // if (version==0) {
  1967. // unsigned int(32) earliest_presentation_time;
  1968. // unsigned int(32) first_offset;
  1969. // }
  1970. // else {
  1971. // unsigned int(64) earliest_presentation_time;
  1972. // unsigned int(64) first_offset;
  1973. // }
  1974. // unsigned int(16) reserved = 0;
  1975. // unsigned int(16) reference_count;
  1976. // for(i=1; i <= reference_count; i++)
  1977. // {
  1978. // bit (1) reference_type;
  1979. // unsigned int(31) referenced_size;
  1980. // unsigned int(32) subsegment_duration;
  1981. // bit(1) starts_with_SAP;
  1982. // unsigned int(3) SAP_type;
  1983. // unsigned int(28) SAP_delta_time;
  1984. // }
  1985. // reference_ID : provides the stream ID for the reference stream; if this Segment Index box is referenced from a “parent” Segment Index box, the value of reference_ID shall be the same as the value of reference_ID of the “parent” Segment Index box;
  1986. // timescale : provides the timescale, in ticks per second, for the time and duration fields within this box; it is recommended that this match the timescale of the reference stream or track; for files based on this specification, that is the timescale field of the Media Header Box of the track;
  1987. // earliest_presentation_time : is the earliest presentation time of any access unit in the reference stream in the first subsegment, in the timescale indicated in the timescale field;
  1988. // first_offset : is the distance in bytes, in the file containing media, from the anchor point, to the first byte of the indexed material;
  1989. // reference_count : provides the number of referenced items;
  1990. // reference_type : when set to 1 indicates that the reference is to a segment index (‘sidx’) box; otherwise the reference is to media content (e.g., in the case of files based on this specification, to a movie fragment box); if a separate index segment is used, then entries with reference type 1 are in the index segment, and entries with reference type 0 are in the media file;
  1991. // referenced_size : the distance in bytes from the first byte of the referenced item to the first byte of the next referenced item, or in the case of the last entry, the end of the referenced material;
  1992. // subsegment_duration : when the reference is to Segment Index box, this field carries the sum of the subsegment_duration fields in that box; when the reference is to a subsegment, this field carries the difference between the earliest presentation time of any access unit of the reference stream in the next subsegment (or the first subsegment of the next segment, if this is the last subsegment of the segment, or the end presentation time of the reference stream if this is the last subsegment of the stream) and the earliest presentation time of any access unit of the reference stream in the referenced subsegment; the duration is in the same units as earliest_presentation_time;
  1993. // starts_with_SAP : indicates whether the referenced subsegments start with a SAP. For the detailed semantics of this field in combination with other fields, see the table below.
  1994. // SAP_type : indicates a SAP type as specified in Annex I, or the value 0. Other type values are reserved. For the detailed semantics of this field in combination with other fields, see the table below.
  1995. // SAP_delta_time : indicates TSAP of the first SAP, in decoding order, in the referenced subsegment for the reference stream. If the referenced subsegments do not contain a SAP, SAP_delta_time is reserved with the value 0; otherwise SAP_delta_time is the difference between the earliest presentation time of the subsegment, and the TSAP (note that this difference may be zero, in the case that the subsegment starts with a SAP).
  1996. // -------------------------------------------------------------------------------------------------------
  1997. //
  1998. // ISO_IEC_14496-12_2012.pdf Page/109
  1999. //
  2000. // Box Type: ssix
  2001. // Container: File
  2002. // Mandatory: No
  2003. // Quantity: Zero or more
  2004. type SubsegmentIndexBox struct {
  2005. MP4BoxHeader // standard header
  2006. MP4FullBoxHeader // full box header
  2007. SubSegmentCount uint32 // 32 bits, is a positive integer specifying the number of subsegments for which partial subsegment information is specified in this box. subsegment_count shall be equal to reference_count (i.e., the number of movie fragment references) in the immediately preceding Segment Index box.
  2008. Table []SubsegmentIndexTable // Subsegment Index Table, SubSegmentCount elements.
  2009. }
  2010. type SubsegmentIndexTable struct {
  2011. RangesCount uint32 // 32 bits, specifies the number of partial subsegment levels into which the media data is grouped. This value shall be greater than or equal to 2.
  2012. Rtable []SubsegmentRangesTable // Subsegment Ranges Table, RangesCount elements.
  2013. }
  2014. type SubsegmentRangesTable struct {
  2015. level uint8 // 8 bits, specifies the level to which this partial subsegment is assigned.
  2016. range_size [3]byte // 24 bits, indicates the size of the partial subsegment.
  2017. }
  2018. // -------------------------------------------------------------------------------------------------------
  2019. //
  2020. // ISO_IEC_14496-12_2012.pdf Page/111
  2021. //
  2022. // Box Type: prft
  2023. // Container: File
  2024. // Mandatory: No
  2025. // Quantity: Zero or more
  2026. type ProducerReferenceTimeBox struct {
  2027. MP4BoxHeader // standard header
  2028. MP4FullBoxHeader // full box header
  2029. ReferenceTrackID uint32 // 32 bits, provides the track_ID for the reference track.
  2030. NtpTimestamp uint64 // 64 bits, indicates a UTC time in NTP format corresponding to decoding_time.
  2031. MediaTime interface{} // uint32 or uint64, corresponds to the same time as ntp_timestamp, but in the time units used for the reference track, and is measured on this media clock as the media is produced.
  2032. }
  2033. // if (version==0) {
  2034. // unsigned int(32) media_time;
  2035. // } else {
  2036. // unsigned int(64) media_time;
  2037. // }
  2038. // -------------------------------------------------------------------------------------------------------