QGMP4Box.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // QGMP4Box.m
  2. // Tencent is pleased to support the open source community by making vap available.
  3. //
  4. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. //
  6. // Licensed under the MIT License (the "License"); you may not use this file except in
  7. // compliance with the License. You may obtain a copy of the License at
  8. //
  9. // http://opensource.org/licenses/MIT
  10. //
  11. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. // either express or implied. See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import "QGMP4Box.h"
  16. #import "QGMP4Parser.h"
  17. NSInteger const kQGBoxSizeLengthInBytes = 4;
  18. NSInteger const kQGBoxTypeLengthInBytes = 4;
  19. NSInteger const kQGBoxLargeSizeLengthInBytes = 8;
  20. NSInteger const kQGBoxLargeSizeFlagLengthInBytes = 1;
  21. #pragma mark - boxes
  22. #pragma mark -- base box
  23. @implementation QGMP4Box
  24. - (instancetype)initWithType:(QGMP4BoxType)type startIndex:(unsigned long long)startIndexInBytes length:(unsigned long long)length {
  25. if (self = [super init]) {
  26. _type = type;
  27. _startIndexInBytes = startIndexInBytes;
  28. _length = length;
  29. }
  30. return self;
  31. }
  32. /**
  33. 前序遍历递归查找指定类型的子box,不包含自身。
  34. @param type box类型
  35. @return 符合条件的第一个子box
  36. */
  37. - (id)subBoxOfType:(QGMP4BoxType)type {
  38. if (self.subBoxes) {
  39. for (QGMP4Box *subBox in self.subBoxes) {
  40. if (subBox.type == type) {
  41. return subBox;
  42. }
  43. QGMP4Box *box = [subBox subBoxOfType:type];
  44. if (box) {
  45. return box;
  46. }
  47. }
  48. }
  49. return nil;
  50. }
  51. /**
  52. 向上查找指定类型的box,不包含自身
  53. @param type box类型
  54. @return h符合条件的第一个box
  55. */
  56. - (id)superBoxOfType:(QGMP4BoxType)type {
  57. if (self.superBox) {
  58. if (self.superBox.type == type) {
  59. return self.superBox;
  60. }
  61. QGMP4Box *box = [self.superBox superBoxOfType:type];
  62. if (box) {
  63. return box;
  64. }
  65. }
  66. return nil;
  67. }
  68. - (NSString *)description {
  69. return [self descriptionForRecursionLevel:0];
  70. }
  71. - (NSString *)descriptionForRecursionLevel:(NSInteger)level {
  72. __block NSString *des = [NSString stringWithFormat:@"Box:%@ offset:%@ size:%@ ",self.typeString,@(self.startIndexInBytes),@(self.length)];
  73. for (int i = 0; i < level; i++) {
  74. des = [NSString stringWithFormat:@"|--%@",des];
  75. }
  76. des = [NSString stringWithFormat:@"\n%@",des];
  77. [self.subBoxes enumerateObjectsUsingBlock:^(QGMP4Box *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  78. des = [des stringByAppendingString:[obj descriptionForRecursionLevel:(level+1)]];
  79. }];
  80. return des;
  81. }
  82. - (NSString *)typeString {
  83. NSUInteger value = self.type;
  84. NSString *des = @"";
  85. while (value > 0) {
  86. NSUInteger hexValue = value&0xff;
  87. value = value>>8;
  88. des = [NSString stringWithFormat:@"%c%@",(int)hexValue,des];
  89. }
  90. return des;
  91. }
  92. @end
  93. #pragma mark -- hvcc box
  94. /**
  95. * QGMP4HvccBox
  96. */
  97. @implementation QGMP4HvccBox
  98. @end
  99. /**
  100. * QGCttsEntry 通过dts计算pts
  101. */
  102. @implementation QGCttsEntry
  103. @end
  104. /**
  105. * QGMP4CttsBox 通过dts计算pts
  106. */
  107. @implementation QGMP4CttsBox
  108. - (void)boxDidParsed:(QGMp4BoxDataFetcher)datablock {
  109. if (!_compositionOffsets) {
  110. _compositionOffsets = [NSMutableArray new];
  111. }
  112. NSData *cttsData = datablock(self);
  113. const char *bytes = cttsData.bytes;
  114. uint32_t entryCount = READ32BIT(&bytes[12]);
  115. for (int i = 0; i < entryCount; ++i) {
  116. uint32_t sampleCount = READ32BIT(&bytes[16+i*8]);
  117. uint32_t compositionOffset = READ32BIT(&bytes[16+i*8+4]);
  118. for (int j = 0; j < sampleCount; j++) {
  119. [_compositionOffsets addObject:@(compositionOffset)];
  120. }
  121. }
  122. }
  123. @end
  124. #pragma mark -- mdat box
  125. @implementation QGMP4MdatBox
  126. @end
  127. #pragma mark -- avcc box
  128. @implementation QGMP4AvccBox
  129. @end
  130. @implementation QGMP4MvhdBox
  131. @end
  132. /**
  133. 在video track中,stsd包含了sps&pps等编解码数据
  134. stsd是⼀个必不可少的atom,在它的 header和version字段后会有⼀个entry count字段,
  135. 根据entry的个数,每个entry会有type信息,根据type不同sample description会提供不
  136. 同的信息,例如对于video track,会有“VisualSampleEntry”类型信息,对于audio track
  137. 会有“AudioSampleEntry”类型信息。视频的关键信息如SPS和PPS,编码类型、宽⾼、
  138. ⻓度,⾳频的声道、采样等信息都会出现在这个box中。
  139. */
  140. @implementation QGMP4StsdBox
  141. @end
  142. /**
  143. There are two variants of the sample size box. The first variant has a fixed size 32-bit field for representing the sample sizes; it permits defining a constant size for all samples in a track. The second variant permits smaller size fields, to save space when the sizes are varying but small. One of these boxes must be present; the first version is preferred for maximum compatibility.
  144. 记录了每个sample的大小
  145. */
  146. @implementation QGMP4StszBox
  147. - (void)boxDidParsed:(QGMp4BoxDataFetcher)datablock {
  148. if (!_sampleSizes) {
  149. _sampleSizes = [NSMutableArray new];
  150. }
  151. NSData *stszData = datablock(self);
  152. const char *bytes = stszData.bytes;
  153. uint32_t sampleSize = READ32BIT(&bytes[12]);
  154. uint32_t sampleCount = READ32BIT(&bytes[16]);
  155. self.sampleCount = sampleCount;
  156. for (int i = 0; i < sampleCount; i ++) {
  157. if (sampleSize > 0) {
  158. [self.sampleSizes addObject:@(sampleSize)];
  159. } else {
  160. uint32_t entryValue = READ32BIT(&bytes[20+i*4]);
  161. [self.sampleSizes addObject:@(entryValue)];
  162. }
  163. }
  164. }
  165. @end
  166. /**
  167. Samples within the media data are grouped into chunks. Chunks can be of different sizes, and the samples within a chunk can have different sizes. This table can be used to find the chunk that contains a sample, its position, and the associated sample description.
  168. stsc记录了每个chunk有多少个Sample https://img-blog.csdn.net/20140613154636296
  169. */
  170. @implementation QGStscEntry
  171. @end
  172. /*
  173. stsc,记录了每个chunk有多少个sample,通过这个表可以找到指定的sample
  174. */
  175. @implementation QGMP4StscBox
  176. - (void)boxDidParsed:(QGMp4BoxDataFetcher)datablock {
  177. if (!_entries) {
  178. _entries = [NSMutableArray new];
  179. }
  180. NSData *stscData = datablock(self);
  181. const char *bytes = stscData.bytes;
  182. uint32_t entry_count = READ32BIT(&bytes[12]);
  183. for (int i = 0; i < entry_count; ++i) {
  184. QGStscEntry *entry = [QGStscEntry new];
  185. entry.firstChunk = READ32BIT(&bytes[16+i*12]);
  186. entry.samplesPerChunk = READ32BIT(&bytes[16+i*12+4]);
  187. entry.sampleDescriptionIndex = READ32BIT(&bytes[16+i*12+8]);
  188. [_entries addObject:entry];
  189. }
  190. }
  191. @end
  192. /**
  193. stco 记录每个chunk位置信息,与stsc结合可以算出每个sample的位置和大小
  194. entry_count is an integer that gives the number of entries in the following table
  195. chunk_offset is a 32 or 64 bit integer that gives the offset of the start of a chunk into its containing
  196. media file.
  197. */
  198. @implementation QGMP4StcoBox
  199. - (void)boxDidParsed:(QGMp4BoxDataFetcher)datablock {
  200. if (!_chunkOffsets) {
  201. _chunkOffsets = [NSMutableArray new];
  202. }
  203. NSData *stcoData = datablock(self);
  204. const char *bytes = stcoData.bytes;
  205. uint32_t entry_count = READ32BIT(&bytes[12]);
  206. self.chunkCount = entry_count;
  207. for (int i = 0; i < entry_count; ++i) {
  208. [self.chunkOffsets addObject:@(READ32BIT(&bytes[16+i*4]))];
  209. }
  210. }
  211. @end
  212. @implementation QGMP4StssBox
  213. - (void)boxDidParsed:(QGMp4BoxDataFetcher)datablock {
  214. if (!_syncSamples) {
  215. _syncSamples = [NSMutableArray new];
  216. }
  217. NSData *stssData = datablock(self);
  218. const char *bytes = stssData.bytes;
  219. uint32_t sample_count = READ32BIT(&bytes[12]);
  220. for (int i = 0; i < sample_count; i++) {
  221. NSInteger index = READ32BIT(&bytes[16 + 4 * i]) - 1;
  222. [_syncSamples addObject:[NSNumber numberWithInteger:index]];
  223. }
  224. }
  225. @end
  226. /**
  227. Decoding Time to Sample Box
  228. 用来计算dts
  229. */
  230. @implementation QGSttsEntry
  231. @end
  232. /*
  233. stts记录了sample的时间信息,⾥⾯有多个entry,每个entry⾥⾯的的sample的时⻓都是
  234. 对应这个entry的delta值,通过这个atom可以得到⼀个时间和sample的映射表。
  235. */
  236. @implementation QGMP4SttsBox
  237. - (void)boxDidParsed:(QGMp4BoxDataFetcher)datablock {
  238. if (!_entries) {
  239. _entries = [NSMutableArray new];
  240. }
  241. NSData *sttsData = datablock(self);
  242. const char *bytes = sttsData.bytes;
  243. uint32_t entry_count = READ32BIT(&bytes[12]);
  244. for (int i = 0; i < entry_count; ++i) {
  245. QGSttsEntry *entry = [QGSttsEntry new];
  246. entry.sampleCount = READ32BIT(&bytes[16+i*8]);;
  247. entry.sampleDelta = READ32BIT(&bytes[16+i*8+4]);
  248. [_entries addObject:entry];
  249. }
  250. }
  251. @end
  252. @implementation QGMP4TrackBox
  253. @end
  254. @implementation QGMP4HdlrBox
  255. - (void)boxDidParsed:(QGMp4BoxDataFetcher)datablock {
  256. NSData *hdlrData = datablock(self);
  257. const char *bytes = hdlrData.bytes;
  258. uint32_t trackType = READ32BIT(&bytes[16]);
  259. self.trackType = trackType;
  260. }
  261. @end
  262. @implementation QGMP4Sample
  263. @end
  264. @implementation QGChunkOffsetEntry
  265. @end
  266. @implementation QGMP4BoxFactory
  267. + (QGMP4Box *)createBoxForType:(QGMP4BoxType)type startIndex:(unsigned long long)startIndexInBytes length:(unsigned long long)length {
  268. Class boxClass = [self boxClassForType:type] ?: [QGMP4Box class];
  269. QGMP4Box *box = [[boxClass alloc] initWithType:type startIndex:startIndexInBytes length:length];
  270. return box;
  271. }
  272. + (Class)boxClassForType:(QGMP4BoxType)type {
  273. switch (type) {
  274. case QGMP4BoxType_ftyp:
  275. case QGMP4BoxType_free:
  276. case QGMP4BoxType_moov:
  277. case QGMP4BoxType_mvhd:
  278. case QGMP4BoxType_trak:
  279. case QGMP4BoxType_tkhd:
  280. case QGMP4BoxType_edts:
  281. case QGMP4BoxType_elst:
  282. case QGMP4BoxType_mdia:
  283. case QGMP4BoxType_minf:
  284. case QGMP4BoxType_vmhd:
  285. case QGMP4BoxType_dinf:
  286. case QGMP4BoxType_dref:
  287. case QGMP4BoxType_url:
  288. case QGMP4BoxType_stbl:
  289. case QGMP4BoxType_avc1:
  290. case QGMP4BoxType_udta:
  291. case QGMP4BoxType_meta:
  292. case QGMP4BoxType_ilst:
  293. case QGMP4BoxType_data:
  294. case QGMP4BoxType_iods:
  295. case QGMP4BoxType_wide:
  296. case QGMP4BoxType_loci:
  297. case QGMP4BoxType_smhd:
  298. return [QGMP4Box class];
  299. case QGMP4BoxType_stss:
  300. return [QGMP4StssBox class];
  301. case QGMP4BoxType_mdat:
  302. return [QGMP4MdatBox class];
  303. case QGMP4BoxType_avcC:
  304. return [QGMP4AvccBox class];
  305. case QGMP4BoxType_mdhd:
  306. return [QGMP4MvhdBox class];
  307. case QGMP4BoxType_stsd:
  308. return [QGMP4StsdBox class];
  309. case QGMP4BoxType_stsz:
  310. return [QGMP4StszBox class];
  311. case QGMP4BoxType_hdlr:
  312. return [QGMP4HdlrBox class];
  313. case QGMP4BoxType_stsc:
  314. return [QGMP4StscBox class];
  315. case QGMP4BoxType_stts:
  316. return [QGMP4SttsBox class];
  317. case QGMP4BoxType_stco:
  318. return [QGMP4StcoBox class];
  319. case QGMP4BoxType_hvcC:
  320. return [QGMP4HvccBox class];
  321. case QGMP4BoxType_ctts:
  322. return [QGMP4CttsBox class];
  323. default:
  324. return nil;
  325. }
  326. }
  327. /**
  328. 根据boxClassForType:方法的返回判断是否为一个合法的box类型
  329. @param type box类型
  330. @return 除QGMP4BoxType定义的类型(不包含QGMP4BoxType_unknown)外全部为不合法类型
  331. */
  332. + (BOOL)isTypeValueValid:(QGMP4BoxType)type {
  333. Class class = [self boxClassForType:type];
  334. if (class) {
  335. return YES;
  336. }
  337. return NO;
  338. }
  339. @end