MpegDashDemuxer.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. #include "MpegDashDemuxer.h"
  2. using namespace bell::mpeg;
  3. MpegDashDemuxer::MpegDashDemuxer(std::shared_ptr<bell::ByteStream> stream)
  4. {
  5. this->reader = std::make_shared<bell::BinaryReader>(stream);
  6. }
  7. int readIntFromVector(std::vector<uint8_t> c, size_t offset)
  8. {
  9. return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | (c[3])) & 0xffffffffL;
  10. }
  11. void MpegDashDemuxer::close() {
  12. reader->close();
  13. }
  14. void MpegDashDemuxer::parse()
  15. {
  16. // Skip FTYP box
  17. lastBox = readBox();
  18. ensureBox(lastBox);
  19. auto moov = std::make_unique<Moov>();
  20. while (lastBox->type != ATOM_MOOF)
  21. {
  22. ensureBox(lastBox);
  23. lastBox = readBox();
  24. if (lastBox->type == ATOM_MOOV)
  25. {
  26. // parse moov
  27. moov = parseMoov(lastBox);
  28. }
  29. }
  30. tracks = std::vector<Mp4Track>(moov->trak.size());
  31. for (int i = 0; i < tracks.size(); i++)
  32. {
  33. tracks[i] = Mp4Track();
  34. tracks[i].trak = std::move(moov->trak[i]);
  35. }
  36. }
  37. int MpegDashDemuxer::parseMfhd()
  38. {
  39. reader->skip(4);
  40. return reader->readInt();
  41. }
  42. long MpegDashDemuxer::parseTfdt()
  43. {
  44. uint8_t version = reader->readByte();
  45. reader->skip(3);
  46. return version == 0 ? reader->readUInt() : reader->readLong();
  47. }
  48. std::unique_ptr<Mp4DashSample> MpegDashDemuxer::getNextSample(std::unique_ptr<Chunk> &chunk)
  49. {
  50. auto sample = std::make_unique<Mp4DashSample>();
  51. if (chunk->size == 0)
  52. {
  53. return nullptr;
  54. }
  55. if (chunk->i >= chunk->moof->traf->trun->entryCount)
  56. {
  57. return nullptr;
  58. }
  59. sample->info = getAbsoluteTrunEntry(chunk->moof->traf->trun, chunk->i++, chunk->moof->traf->tfhd);
  60. sample->data = reader->readBytes(sample->info->sampleSize);
  61. chunk->sampleRead += sample->info->sampleSize;
  62. return sample;
  63. }
  64. std::unique_ptr<Chunk> MpegDashDemuxer::getNextChunk(bool infoOnly)
  65. {
  66. while (reader->position() < reader->size())
  67. {
  68. if (chunkZero)
  69. {
  70. ensureBox(lastBox);
  71. lastBox = readBox();
  72. }
  73. else
  74. {
  75. chunkZero = true;
  76. }
  77. if (lastBox->type == ATOM_MOOF)
  78. {
  79. lastMoof = parseMoof(lastBox, tracks[0].trak->tkhd->trackId);
  80. if (lastMoof->traf != nullptr)
  81. {
  82. if (hasFlag(lastMoof->traf->trun->bFlags, 0x0001))
  83. {
  84. lastMoof->traf->trun->dataOffset -= lastBox->size + 8;
  85. }
  86. }
  87. if (lastMoof->traf->trun->chunkSize < 1)
  88. {
  89. if (hasFlag(lastMoof->traf->tfhd->bFlags, 0x10))
  90. {
  91. lastMoof->traf->trun->chunkSize = lastMoof->traf->tfhd->defaultSampleSize * lastMoof->traf->trun->entryCount;
  92. }
  93. else
  94. {
  95. lastMoof->traf->trun->chunkSize = lastBox->size = 8;
  96. }
  97. }
  98. if (!hasFlag(lastMoof->traf->trun->bFlags, 0x900) && lastMoof->traf->trun->chunkDuration == 0)
  99. {
  100. if (hasFlag(lastMoof->traf->tfhd->bFlags, 0x20))
  101. {
  102. lastMoof->traf->trun->chunkDuration = lastMoof->traf->tfhd->defaultSampleDuration * lastMoof->traf->trun->entryCount;
  103. }
  104. }
  105. }
  106. if (lastBox->type == ATOM_MDAT)
  107. {
  108. if (lastMoof->traf == nullptr)
  109. {
  110. lastMoof = nullptr;
  111. continue;
  112. }
  113. auto chunk = std::make_unique<Chunk>();
  114. chunk->moof = std::move(lastMoof);
  115. if (!infoOnly)
  116. {
  117. chunk->size = chunk->moof->traf->trun->chunkSize;
  118. }
  119. lastMoof = nullptr;
  120. reader->skip(chunk->moof->traf->trun->dataOffset);
  121. return chunk;
  122. }
  123. }
  124. return std::unique_ptr<Chunk>(nullptr);
  125. }
  126. size_t MpegDashDemuxer::position() {
  127. return reader->position();
  128. }
  129. std::unique_ptr<Moof> MpegDashDemuxer::parseMoof(std::unique_ptr<MpegBox> &ref, int trackId)
  130. {
  131. auto moof = std::make_unique<Moof>();
  132. auto box = readBox();
  133. moof->mfgdSequenceNumber = parseMfhd();
  134. ensureBox(box);
  135. box = untilBox(ref, ATOM_TRAF);
  136. while (box)
  137. {
  138. moof->traf = parseTraf(box, trackId);
  139. ensureBox(box);
  140. if (moof->traf->tfdt != -1)
  141. {
  142. return moof;
  143. }
  144. box = untilBox(ref, ATOM_TRAF);
  145. }
  146. return moof;
  147. }
  148. std::unique_ptr<Traf> MpegDashDemuxer::parseTraf(std::unique_ptr<MpegBox> &ref, int trackId)
  149. {
  150. auto traf = std::make_unique<Traf>();
  151. auto box = readBox();
  152. traf->tfhd = parseTfhd(trackId);
  153. ensureBox(box);
  154. box = untilBox(ref, ATOM_TRUN, ATOM_TFDT);
  155. if (box->type == ATOM_TFDT)
  156. {
  157. traf->tfdt = parseTfdt();
  158. ensureBox(box);
  159. box = readBox();
  160. }
  161. traf->trun = parseTrun();
  162. ensureBox(box);
  163. return traf;
  164. }
  165. std::unique_ptr<Trun> MpegDashDemuxer::parseTrun()
  166. {
  167. auto trun = std::make_unique<Trun>();
  168. trun->bFlags = reader->readInt();
  169. trun->entryCount = reader->readInt();
  170. trun->entriesRowSize = 0;
  171. if (hasFlag(trun->bFlags, 0x0100))
  172. {
  173. trun->entriesRowSize += 4;
  174. }
  175. if (hasFlag(trun->bFlags, 0x0200))
  176. {
  177. trun->entriesRowSize += 4;
  178. }
  179. if (hasFlag(trun->bFlags, 0x0400))
  180. {
  181. trun->entriesRowSize += 4;
  182. }
  183. if (hasFlag(trun->bFlags, 0x0800))
  184. {
  185. trun->entriesRowSize += 4;
  186. }
  187. if (hasFlag(trun->bFlags, 0x0001))
  188. {
  189. trun->dataOffset = reader->readInt();
  190. }
  191. if (hasFlag(trun->bFlags, 0x0004))
  192. {
  193. trun->bFirstSampleFlags = reader->readInt();
  194. }
  195. trun->bEntries = reader->readBytes(trun->entriesRowSize * trun->entryCount);
  196. trun->chunkSize = 0;
  197. for (int i = 0; i < trun->entryCount; i++)
  198. {
  199. auto entry = getTrunEntry(trun, i);
  200. if (hasFlag(trun->bFlags, 0x0100))
  201. {
  202. trun->chunkDuration += entry->sampleDuration;
  203. }
  204. if (hasFlag(trun->bFlags, 0x200))
  205. {
  206. trun->chunkSize += entry->sampleSize;
  207. }
  208. if (hasFlag(trun->bFlags, 0x0800))
  209. {
  210. if (!hasFlag(trun->bFlags, 0x0100))
  211. {
  212. trun->chunkDuration += entry->sampleCompositionTimeOffset;
  213. }
  214. }
  215. }
  216. return trun;
  217. }
  218. std::unique_ptr<TrunEntry> MpegDashDemuxer::getTrunEntry(std::unique_ptr<Trun> &trun, int i)
  219. {
  220. std::vector<uint8_t> subBuffer(
  221. trun->bEntries.begin() + (i * trun->entriesRowSize),
  222. trun->bEntries.begin() + ((i + 1) * trun->entriesRowSize));
  223. auto entry = std::make_unique<TrunEntry>();
  224. if (hasFlag(trun->bFlags, 0x0100))
  225. {
  226. entry->sampleDuration = readIntFromVector(subBuffer, 0);
  227. }
  228. if (hasFlag(trun->bFlags, 0x0200))
  229. {
  230. entry->sampleSize = readIntFromVector(subBuffer, 0);
  231. }
  232. if (hasFlag(trun->bFlags, 0x0400))
  233. {
  234. entry->sampleFlags = readIntFromVector(subBuffer, 0);
  235. }
  236. if (hasFlag(trun->bFlags, 0x800))
  237. {
  238. entry->sampleCompositionTimeOffset = readIntFromVector(subBuffer, 0);
  239. }
  240. entry->hasCompositionTimeOffset = hasFlag(trun->bFlags, 0x0800);
  241. entry->isKeyframe = !hasFlag(entry->sampleFlags, 0x10000);
  242. return entry;
  243. }
  244. std::unique_ptr<TrunEntry> MpegDashDemuxer::getAbsoluteTrunEntry(std::unique_ptr<Trun> &trun, int i, std::unique_ptr<Tfhd> &header)
  245. {
  246. std::unique_ptr<TrunEntry> entry = getTrunEntry(trun, i);
  247. if (!hasFlag(trun->bFlags, 0x0100) && hasFlag(header->bFlags, 0x20))
  248. {
  249. entry->sampleFlags = header->defaultSampleFlags;
  250. }
  251. if (!hasFlag(trun->bFlags, 0x0200) && hasFlag(header->bFlags, 0x10))
  252. {
  253. entry->sampleSize = header->defaultSampleSize;
  254. }
  255. if (!hasFlag(trun->bFlags, 0x0100) && hasFlag(header->bFlags, 0x08))
  256. {
  257. entry->sampleDuration = header->defaultSampleDuration;
  258. }
  259. if (i == 0 && hasFlag(trun->bFlags, 0x0004))
  260. {
  261. entry->sampleFlags = trun->bFirstSampleFlags;
  262. }
  263. return entry;
  264. }
  265. std::unique_ptr<Tfhd> MpegDashDemuxer::parseTfhd(int trackId)
  266. {
  267. auto tfhd = std::make_unique<Tfhd>();
  268. tfhd->bFlags = reader->readInt();
  269. tfhd->trackId = reader->readInt();
  270. if (trackId != -1 && tfhd->trackId != trackId)
  271. {
  272. return tfhd;
  273. }
  274. if (hasFlag(tfhd->bFlags, 0x01))
  275. {
  276. reader->skip(8);
  277. }
  278. if (hasFlag(tfhd->bFlags, 0x02))
  279. {
  280. reader->skip(4);
  281. }
  282. if (hasFlag(tfhd->bFlags, 0x08))
  283. {
  284. tfhd->defaultSampleDuration = reader->readInt();
  285. }
  286. if (hasFlag(tfhd->bFlags, 0x10))
  287. {
  288. tfhd->defaultSampleSize = reader->readInt();
  289. }
  290. if (hasFlag(tfhd->bFlags, 0x20))
  291. {
  292. tfhd->defaultSampleFlags = reader->readInt();
  293. }
  294. return tfhd;
  295. }
  296. bool MpegDashDemuxer::hasFlag(int flags, int mask)
  297. {
  298. return (flags & mask) == mask;
  299. }
  300. std::unique_ptr<Moov> MpegDashDemuxer::parseMoov(std::unique_ptr<MpegBox> &ref)
  301. {
  302. auto moov = std::make_unique<Moov>();
  303. auto box = readBox();
  304. moov->mvhd = parseMvhd();
  305. moov->mvexTrex = std::vector<Trex>();
  306. ensureBox(box);
  307. moov->trak = std::vector<std::unique_ptr<Trak>>();
  308. box = untilBox(ref, ATOM_TRAK, ATOM_MVEX);
  309. while (box)
  310. {
  311. if (box->type == ATOM_TRAK)
  312. {
  313. moov->trak.push_back(parseTrak(box));
  314. }
  315. if (box->type == ATOM_MVEX)
  316. {
  317. moov->mvexTrex = parseMvex(box, moov->mvhd->nextTrackId);
  318. }
  319. ensureBox(box);
  320. box = untilBox(ref, ATOM_TRAK, ATOM_MVEX);
  321. }
  322. return moov;
  323. }
  324. Trex MpegDashDemuxer::parseTrex()
  325. {
  326. reader->skip(4);
  327. Trex trex;
  328. trex.trackId = reader->readInt();
  329. trex.defaultSampleDescriptionIndex = reader->readInt();
  330. trex.defaultSampleDuration = reader->readInt();
  331. trex.defaultSampleSize = reader->readInt();
  332. trex.defaultSampleFlags = reader->readInt();
  333. return trex;
  334. }
  335. std::vector<Trex> MpegDashDemuxer::parseMvex(std::unique_ptr<MpegBox> &ref, int possibleTrackCount)
  336. {
  337. auto trexs = std::vector<Trex>();
  338. auto box = untilBox(ref, ATOM_TREX);
  339. while (box)
  340. {
  341. trexs.push_back(parseTrex());
  342. ensureBox(box);
  343. box = untilBox(ref, ATOM_TREX);
  344. }
  345. return trexs;
  346. }
  347. std::vector<uint8_t> MpegDashDemuxer::readFullBox(std::unique_ptr<MpegBox> &ref)
  348. {
  349. auto size = ref->size;
  350. std::vector<uint8_t> header(8);
  351. header.at(0) = *((uint8_t *)&ref->size + 0);
  352. header.at(1) = *((uint8_t *)&ref->size + 1);
  353. header.at(2) = *((uint8_t *)&ref->size + 2);
  354. header.at(3) = *((uint8_t *)&ref->size + 3);
  355. header.at(4) = *((uint8_t *)&ref->type + 0);
  356. header.at(5) = *((uint8_t *)&ref->type + 1);
  357. header.at(6) = *((uint8_t *)&ref->type + 2);
  358. header.at(7) = *((uint8_t *)&ref->type + 3);
  359. std::vector<uint8_t> data = reader->readBytes(size - 8);
  360. header.insert(header.end(), data.begin(), data.end());
  361. return header;
  362. }
  363. std::unique_ptr<Tkhd> MpegDashDemuxer::parseTkhd()
  364. {
  365. auto tkhd = std::make_unique<Tkhd>();
  366. uint8_t version = reader->readByte();
  367. // flags
  368. // creation entries_time
  369. // modification entries_time
  370. reader->skip(3 + (2 * (version == 0 ? 4 : 8)));
  371. tkhd->trackId = reader->readInt();
  372. reader->skip(4);
  373. tkhd->duration = version == 0 ? reader->readUInt() : reader->readLong();
  374. reader->skip(2 * 4);
  375. tkhd->bLayer = reader->readShort();
  376. tkhd->bAlternateGroup = reader->readShort();
  377. tkhd->bVolume = reader->readShort();
  378. reader->skip(2);
  379. tkhd->matrix = reader->readBytes(9 * 4);
  380. tkhd->bWidth = reader->readInt();
  381. tkhd->bHeight = reader->readInt();
  382. return tkhd;
  383. }
  384. std::unique_ptr<Trak> MpegDashDemuxer::parseTrak(std::unique_ptr<MpegBox> &ref)
  385. {
  386. auto trak = std::make_unique<Trak>();
  387. auto box = readBox();
  388. trak->tkhd = parseTkhd();
  389. ensureBox(box);
  390. box = untilBox(ref, ATOM_MDIA, ATOM_EDTS);
  391. while (box)
  392. {
  393. if (box->type == ATOM_MDIA)
  394. {
  395. trak->mdia = parseMdia(box);
  396. }
  397. if (box->type == ATOM_EDTS)
  398. {
  399. trak->edstElst = parseEdts(box);
  400. }
  401. ensureBox(box);
  402. box = untilBox(ref, ATOM_MDIA, ATOM_EDTS);
  403. }
  404. return trak;
  405. }
  406. std::unique_ptr<Minf> MpegDashDemuxer::parseMinf(std::unique_ptr<MpegBox> &ref)
  407. {
  408. auto minf = std::make_unique<Minf>();
  409. auto box = untilAnyBox(ref);
  410. while (box)
  411. {
  412. if (box->type == ATOM_DINF)
  413. {
  414. minf->dinf = readFullBox(box);
  415. }
  416. if (box->type == ATOM_STBL)
  417. {
  418. minf->stblStsd = parseStbl(box);
  419. }
  420. if (box->type == ATOM_VMHD || box->type == ATOM_SMHD)
  421. {
  422. minf->mhd = readFullBox(box);
  423. }
  424. ensureBox(box);
  425. box = untilAnyBox(ref);
  426. }
  427. return minf;
  428. }
  429. std::vector<uint8_t> MpegDashDemuxer::parseStbl(std::unique_ptr<MpegBox> &ref)
  430. {
  431. auto box = untilBox(ref, ATOM_STSD);
  432. return readFullBox(box);
  433. }
  434. std::unique_ptr<Elst> MpegDashDemuxer::parseEdts(std::unique_ptr<MpegBox> &ref)
  435. {
  436. auto elst = std::make_unique<Elst>();
  437. auto box = untilBox(ref, ATOM_ELST);
  438. bool v1 = reader->readByte() == 1;
  439. reader->skip(3); // flags
  440. int entryCount = reader->readInt();
  441. if (entryCount < 1)
  442. {
  443. elst->bMediaRate = 0x00010000;
  444. return elst;
  445. }
  446. if (v1)
  447. {
  448. reader->skip(8); // duration
  449. elst->mediaTime = reader->readLong();
  450. // ignore all entries
  451. reader->skip((entryCount - 1) * 16);
  452. }
  453. else
  454. {
  455. reader->skip(4); // segment duration
  456. elst->mediaTime = reader->readInt();
  457. }
  458. elst->bMediaRate = reader->readInt();
  459. return elst;
  460. }
  461. std::unique_ptr<Hdlr> MpegDashDemuxer::parseHdlr(std::unique_ptr<MpegBox> &box)
  462. {
  463. auto hdlr = std::make_unique<Hdlr>();
  464. reader->skip(4);
  465. hdlr->type = reader->readInt();
  466. hdlr->subType = reader->readInt();
  467. hdlr->bReserved = reader->readBytes(12);
  468. reader->skip((box->offset + box->size) - reader->position());
  469. return hdlr;
  470. }
  471. std::unique_ptr<Mdia> MpegDashDemuxer::parseMdia(std::unique_ptr<MpegBox> &ref)
  472. {
  473. auto mdia = std::make_unique<Mdia>();
  474. auto box = untilBox(ref, ATOM_MDHD, ATOM_HDLR, ATOM_MINF);
  475. while (box)
  476. {
  477. if (box->type == ATOM_MDHD)
  478. {
  479. mdia->mdhd = readFullBox(box);
  480. }
  481. if (box->type == ATOM_HDLR)
  482. {
  483. mdia->hdlr = parseHdlr(box);
  484. }
  485. if (box->type == ATOM_MINF)
  486. {
  487. mdia->minf = parseMinf(box);
  488. }
  489. ensureBox(box);
  490. box = untilBox(ref, ATOM_MDHD, ATOM_HDLR, ATOM_MINF);
  491. }
  492. return mdia;
  493. }
  494. std::unique_ptr<Mvhd> MpegDashDemuxer::parseMvhd()
  495. {
  496. auto mvhd = std::make_unique<Mvhd>();
  497. uint8_t version = reader->readByte();
  498. reader->skip(3); // flags
  499. // creation entries_time
  500. // modification entries_time
  501. reader->skip(2 * (version == 0 ? 4 : 8));
  502. mvhd->timeScale = reader->readUInt();
  503. // chunkDuration
  504. reader->skip(version == 0 ? 4 : 8);
  505. // rate
  506. // volume
  507. // reserved
  508. // matrix array
  509. // predefined
  510. reader->skip(76);
  511. mvhd->nextTrackId = reader->readUInt();
  512. return mvhd;
  513. }
  514. void MpegDashDemuxer::ensureBox(std::unique_ptr<MpegBox> &box)
  515. {
  516. reader->skip(box->offset + box->size - reader->position());
  517. }
  518. std::unique_ptr<MpegBox> MpegDashDemuxer::readBox()
  519. {
  520. auto box = std::make_unique<MpegBox>();
  521. box->offset = reader->position();
  522. box->size = reader->readUInt();
  523. box->type = reader->readInt();
  524. if (box->size == 1)
  525. {
  526. box->size = reader->readLong();
  527. }
  528. return box;
  529. }
  530. std::unique_ptr<MpegBox> MpegDashDemuxer::untilBox(std::unique_ptr<MpegBox> &ref, int boxType1, int boxType2, int boxType3)
  531. {
  532. auto box = std::make_unique<MpegBox>();
  533. while (reader->position() < (ref->offset + ref->size))
  534. {
  535. box = readBox();
  536. if (box->type == boxType1 || box->type == boxType2 || box->type == boxType3)
  537. {
  538. return box;
  539. }
  540. ensureBox(box);
  541. }
  542. return std::unique_ptr<MpegBox>(nullptr);
  543. }
  544. std::unique_ptr<MpegBox> MpegDashDemuxer::untilBox(std::unique_ptr<MpegBox> &ref, int boxType1, int boxType2)
  545. {
  546. auto box = std::make_unique<MpegBox>();
  547. while (reader->position() < (ref->offset + ref->size))
  548. {
  549. box = readBox();
  550. if (box->type == boxType1 || box->type == boxType2)
  551. {
  552. return box;
  553. }
  554. ensureBox(box);
  555. }
  556. return std::unique_ptr<MpegBox>(nullptr);
  557. }
  558. std::unique_ptr<MpegBox> MpegDashDemuxer::untilBox(std::unique_ptr<MpegBox> &ref, int boxType1)
  559. {
  560. auto box = std::make_unique<MpegBox>();
  561. while (reader->position() < (ref->offset + ref->size))
  562. {
  563. box = readBox();
  564. if (box->type == boxType1)
  565. {
  566. return box;
  567. }
  568. ensureBox(box);
  569. }
  570. return std::unique_ptr<MpegBox>(nullptr);
  571. }
  572. std::unique_ptr<MpegBox> MpegDashDemuxer::untilAnyBox(std::unique_ptr<MpegBox> &ref)
  573. {
  574. if (reader->position() >= (ref->offset + ref->size))
  575. {
  576. return std::unique_ptr<MpegBox>(nullptr);
  577. }
  578. return readBox();
  579. }