box_definitions.cc 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/formats/mp4/box_definitions.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/big_endian.h"
  8. #include "base/command_line.h"
  9. #include "base/logging.h"
  10. #include "base/numerics/safe_math.h"
  11. #include "base/strings/string_number_conversions.h"
  12. #include "build/build_config.h"
  13. #include "media/base/media_switches.h"
  14. #include "media/base/media_util.h"
  15. #include "media/base/video_types.h"
  16. #include "media/base/video_util.h"
  17. #include "media/formats/common/opus_constants.h"
  18. #include "media/formats/mp4/es_descriptor.h"
  19. #include "media/formats/mp4/rcheck.h"
  20. #include "media/media_buildflags.h"
  21. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  22. #include "media/formats/mp4/avc.h"
  23. #include "media/video/h264_parser.h" // nogncheck
  24. #if BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  25. #include "media/formats/mp4/dolby_vision.h"
  26. #include "third_party/abseil-cpp/absl/types/optional.h"
  27. #endif // BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  28. #if BUILDFLAG(ENABLE_PLATFORM_HEVC)
  29. #include "media/formats/mp4/hevc.h"
  30. #endif // BUILDFLAG(ENABLE_PLATFORM_HEVC)
  31. #endif // BUILDFLAG(USE_PROPRIETARY_CODECS)
  32. namespace media {
  33. namespace mp4 {
  34. namespace {
  35. const size_t kKeyIdSize = 16;
  36. const size_t kFlacMetadataBlockStreaminfoSize = 34;
  37. #if BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  38. // Parse dvcC or dvvC box.
  39. absl::optional<DOVIDecoderConfigurationRecord> ParseDOVIConfig(
  40. BoxReader* reader) {
  41. {
  42. DolbyVisionConfiguration dvcc;
  43. if (reader->HasChild(&dvcc) && reader->ReadChild(&dvcc)) {
  44. DCHECK_LE(dvcc.dovi_config.dv_profile, 7);
  45. return dvcc.dovi_config;
  46. }
  47. }
  48. {
  49. DolbyVisionConfiguration8 dvvc;
  50. if (reader->HasChild(&dvvc) && reader->ReadChild(&dvvc)) {
  51. DCHECK_GT(dvvc.dovi_config.dv_profile, 7);
  52. return dvvc.dovi_config;
  53. }
  54. }
  55. return absl::nullopt;
  56. }
  57. #endif // BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  58. // Read color coordinate value as defined in the MasteringDisplayColorVolume
  59. // ('mdcv') box. Each coordinate is a float encoded in uint16_t, with upper
  60. // bound set to 50000.
  61. bool ReadMdcvColorCoordinate(BoxReader* reader,
  62. float* normalized_value_in_float) {
  63. const float kColorCoordinateUpperBound = 50000.;
  64. uint16_t value_in_uint16;
  65. RCHECK(reader->Read2(&value_in_uint16));
  66. float value_in_float = static_cast<float>(value_in_uint16);
  67. if (value_in_float >= kColorCoordinateUpperBound) {
  68. *normalized_value_in_float = 1.f;
  69. return true;
  70. }
  71. *normalized_value_in_float = value_in_float / kColorCoordinateUpperBound;
  72. return true;
  73. }
  74. // Read "fixed point" value as defined in the
  75. // SMPTE2086MasteringDisplayMetadataBox ('SmDm') box. See
  76. // https://www.webmproject.org/vp9/mp4/#data-types-and-fields
  77. bool ReadFixedPoint16(float fixed_point_divisor,
  78. BoxReader* reader,
  79. float* normalized_value_in_float) {
  80. uint16_t value;
  81. RCHECK(reader->Read2(&value));
  82. *normalized_value_in_float = value / fixed_point_divisor;
  83. return true;
  84. }
  85. bool ReadFixedPoint32(float fixed_point_divisor,
  86. BoxReader* reader,
  87. float* normalized_value_in_float) {
  88. uint32_t value;
  89. RCHECK(reader->Read4(&value));
  90. *normalized_value_in_float = value / fixed_point_divisor;
  91. return true;
  92. }
  93. VideoColorSpace ConvertColorParameterInformationToColorSpace(
  94. const ColorParameterInformation& info) {
  95. auto primary_id =
  96. static_cast<VideoColorSpace::PrimaryID>(info.colour_primaries);
  97. auto transfer_id =
  98. static_cast<VideoColorSpace::TransferID>(info.transfer_characteristics);
  99. auto matrix_id =
  100. static_cast<VideoColorSpace::MatrixID>(info.matrix_coefficients);
  101. // Note that we don't check whether the embedded ids are valid. We rely on
  102. // the underlying video decoder to reject any ids that it doesn't support.
  103. return VideoColorSpace(primary_id, transfer_id, matrix_id,
  104. info.full_range ? gfx::ColorSpace::RangeID::FULL
  105. : gfx::ColorSpace::RangeID::LIMITED);
  106. }
  107. } // namespace
  108. FileType::FileType() = default;
  109. FileType::FileType(const FileType& other) = default;
  110. FileType::~FileType() = default;
  111. FourCC FileType::BoxType() const { return FOURCC_FTYP; }
  112. bool FileType::Parse(BoxReader* reader) {
  113. RCHECK(reader->ReadFourCC(&major_brand) && reader->Read4(&minor_version));
  114. size_t num_brands = (reader->box_size() - reader->pos()) / sizeof(FourCC);
  115. return reader->SkipBytes(sizeof(FourCC) * num_brands); // compatible_brands
  116. }
  117. ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader() = default;
  118. ProtectionSystemSpecificHeader::ProtectionSystemSpecificHeader(
  119. const ProtectionSystemSpecificHeader& other) = default;
  120. ProtectionSystemSpecificHeader::~ProtectionSystemSpecificHeader() = default;
  121. FourCC ProtectionSystemSpecificHeader::BoxType() const { return FOURCC_PSSH; }
  122. bool ProtectionSystemSpecificHeader::Parse(BoxReader* reader) {
  123. // Don't bother validating the box's contents.
  124. // Copy the entire box, including the header, for passing to EME as initData.
  125. DCHECK(raw_box.empty());
  126. raw_box.assign(reader->buffer(), reader->buffer() + reader->box_size());
  127. return true;
  128. }
  129. FullProtectionSystemSpecificHeader::FullProtectionSystemSpecificHeader() =
  130. default;
  131. FullProtectionSystemSpecificHeader::FullProtectionSystemSpecificHeader(
  132. const FullProtectionSystemSpecificHeader& other) = default;
  133. FullProtectionSystemSpecificHeader::~FullProtectionSystemSpecificHeader() =
  134. default;
  135. FourCC FullProtectionSystemSpecificHeader::BoxType() const {
  136. return FOURCC_PSSH;
  137. }
  138. // The format of a 'pssh' box is as follows:
  139. // unsigned int(32) size;
  140. // unsigned int(32) type = "pssh";
  141. // if (size==1) {
  142. // unsigned int(64) largesize;
  143. // } else if (size==0) {
  144. // -- box extends to end of file
  145. // }
  146. // unsigned int(8) version;
  147. // bit(24) flags;
  148. // unsigned int(8)[16] SystemID;
  149. // if (version > 0)
  150. // {
  151. // unsigned int(32) KID_count;
  152. // {
  153. // unsigned int(8)[16] KID;
  154. // } [KID_count]
  155. // }
  156. // unsigned int(32) DataSize;
  157. // unsigned int(8)[DataSize] Data;
  158. bool FullProtectionSystemSpecificHeader::Parse(mp4::BoxReader* reader) {
  159. RCHECK(reader->type() == BoxType() && reader->ReadFullBoxHeader());
  160. // Only versions 0 and 1 of the 'pssh' boxes are supported. Any other
  161. // versions are ignored.
  162. RCHECK(reader->version() == 0 || reader->version() == 1);
  163. RCHECK(reader->flags() == 0);
  164. RCHECK(reader->ReadVec(&system_id, 16));
  165. if (reader->version() > 0) {
  166. uint32_t kid_count;
  167. RCHECK(reader->Read4(&kid_count));
  168. for (uint32_t i = 0; i < kid_count; ++i) {
  169. std::vector<uint8_t> kid;
  170. RCHECK(reader->ReadVec(&kid, 16));
  171. key_ids.push_back(kid);
  172. }
  173. }
  174. uint32_t data_size;
  175. RCHECK(reader->Read4(&data_size));
  176. RCHECK(reader->ReadVec(&data, data_size));
  177. return true;
  178. }
  179. SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset() = default;
  180. SampleAuxiliaryInformationOffset::SampleAuxiliaryInformationOffset(
  181. const SampleAuxiliaryInformationOffset& other) = default;
  182. SampleAuxiliaryInformationOffset::~SampleAuxiliaryInformationOffset() = default;
  183. FourCC SampleAuxiliaryInformationOffset::BoxType() const { return FOURCC_SAIO; }
  184. bool SampleAuxiliaryInformationOffset::Parse(BoxReader* reader) {
  185. RCHECK(reader->ReadFullBoxHeader());
  186. if (reader->flags() & 1)
  187. RCHECK(reader->SkipBytes(8));
  188. uint32_t count;
  189. RCHECK(reader->Read4(&count));
  190. int bytes_per_offset = reader->version() == 1 ? 8 : 4;
  191. // Cast |count| to size_t before multiplying to support maximum platform size.
  192. base::CheckedNumeric<size_t> bytes_needed =
  193. base::CheckMul(bytes_per_offset, static_cast<size_t>(count));
  194. RCHECK_MEDIA_LOGGED(bytes_needed.IsValid(), reader->media_log(),
  195. "Extreme SAIO count exceeds implementation limit.");
  196. RCHECK(reader->HasBytes(bytes_needed.ValueOrDie()));
  197. RCHECK(count <= offsets.max_size());
  198. offsets.resize(count);
  199. for (uint32_t i = 0; i < count; i++) {
  200. if (reader->version() == 1) {
  201. RCHECK(reader->Read8(&offsets[i]));
  202. } else {
  203. RCHECK(reader->Read4Into8(&offsets[i]));
  204. }
  205. }
  206. return true;
  207. }
  208. SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize()
  209. : default_sample_info_size(0), sample_count(0) {
  210. }
  211. SampleAuxiliaryInformationSize::SampleAuxiliaryInformationSize(
  212. const SampleAuxiliaryInformationSize& other) = default;
  213. SampleAuxiliaryInformationSize::~SampleAuxiliaryInformationSize() = default;
  214. FourCC SampleAuxiliaryInformationSize::BoxType() const { return FOURCC_SAIZ; }
  215. bool SampleAuxiliaryInformationSize::Parse(BoxReader* reader) {
  216. RCHECK(reader->ReadFullBoxHeader());
  217. if (reader->flags() & 1)
  218. RCHECK(reader->SkipBytes(8));
  219. RCHECK(reader->Read1(&default_sample_info_size) &&
  220. reader->Read4(&sample_count));
  221. if (default_sample_info_size == 0)
  222. return reader->ReadVec(&sample_info_sizes, sample_count);
  223. return true;
  224. }
  225. SampleEncryptionEntry::SampleEncryptionEntry() = default;
  226. SampleEncryptionEntry::SampleEncryptionEntry(
  227. const SampleEncryptionEntry& other) = default;
  228. SampleEncryptionEntry::~SampleEncryptionEntry() = default;
  229. bool SampleEncryptionEntry::Parse(BufferReader* reader,
  230. uint8_t iv_size,
  231. bool has_subsamples) {
  232. // According to ISO/IEC FDIS 23001-7: CENC spec, IV should be either
  233. // 64-bit (8-byte) or 128-bit (16-byte). The 3rd Edition allows |iv_size|
  234. // to be 0, for the case of a "constant IV". In this case, the existence of
  235. // the constant IV must be ensured by the caller.
  236. RCHECK(iv_size == 0 || iv_size == 8 || iv_size == 16);
  237. memset(initialization_vector, 0, sizeof(initialization_vector));
  238. for (uint8_t i = 0; i < iv_size; i++)
  239. RCHECK(reader->Read1(initialization_vector + i));
  240. if (!has_subsamples) {
  241. subsamples.clear();
  242. return true;
  243. }
  244. uint16_t subsample_count;
  245. RCHECK(reader->Read2(&subsample_count));
  246. RCHECK(subsample_count > 0);
  247. RCHECK(subsample_count <= subsamples.max_size());
  248. subsamples.resize(subsample_count);
  249. for (SubsampleEntry& subsample : subsamples) {
  250. uint16_t clear_bytes;
  251. uint32_t cypher_bytes;
  252. RCHECK(reader->Read2(&clear_bytes) && reader->Read4(&cypher_bytes));
  253. subsample.clear_bytes = clear_bytes;
  254. subsample.cypher_bytes = cypher_bytes;
  255. }
  256. return true;
  257. }
  258. bool SampleEncryptionEntry::GetTotalSizeOfSubsamples(size_t* total_size) const {
  259. size_t size = 0;
  260. for (const SubsampleEntry& subsample : subsamples) {
  261. size += subsample.clear_bytes;
  262. RCHECK(size >= subsample.clear_bytes); // overflow
  263. size += subsample.cypher_bytes;
  264. RCHECK(size >= subsample.cypher_bytes); // overflow
  265. }
  266. *total_size = size;
  267. return true;
  268. }
  269. SampleEncryption::SampleEncryption() : use_subsample_encryption(false) {}
  270. SampleEncryption::SampleEncryption(const SampleEncryption& other) = default;
  271. SampleEncryption::~SampleEncryption() = default;
  272. FourCC SampleEncryption::BoxType() const {
  273. return FOURCC_SENC;
  274. }
  275. bool SampleEncryption::Parse(BoxReader* reader) {
  276. RCHECK(reader->ReadFullBoxHeader());
  277. use_subsample_encryption = (reader->flags() & kUseSubsampleEncryption) != 0;
  278. sample_encryption_data.assign(reader->buffer() + reader->pos(),
  279. reader->buffer() + reader->box_size());
  280. return true;
  281. }
  282. OriginalFormat::OriginalFormat() : format(FOURCC_NULL) {}
  283. OriginalFormat::OriginalFormat(const OriginalFormat& other) = default;
  284. OriginalFormat::~OriginalFormat() = default;
  285. FourCC OriginalFormat::BoxType() const { return FOURCC_FRMA; }
  286. bool OriginalFormat::Parse(BoxReader* reader) {
  287. return reader->ReadFourCC(&format);
  288. }
  289. SchemeType::SchemeType() : type(FOURCC_NULL), version(0) {}
  290. SchemeType::SchemeType(const SchemeType& other) = default;
  291. SchemeType::~SchemeType() = default;
  292. FourCC SchemeType::BoxType() const { return FOURCC_SCHM; }
  293. bool SchemeType::Parse(BoxReader* reader) {
  294. RCHECK(reader->ReadFullBoxHeader() &&
  295. reader->ReadFourCC(&type) &&
  296. reader->Read4(&version));
  297. return true;
  298. }
  299. TrackEncryption::TrackEncryption()
  300. : is_encrypted(false),
  301. default_iv_size(0),
  302. default_crypt_byte_block(0),
  303. default_skip_byte_block(0),
  304. default_constant_iv_size(0) {}
  305. TrackEncryption::TrackEncryption(const TrackEncryption& other) = default;
  306. TrackEncryption::~TrackEncryption() = default;
  307. FourCC TrackEncryption::BoxType() const { return FOURCC_TENC; }
  308. bool TrackEncryption::Parse(BoxReader* reader) {
  309. uint8_t flag;
  310. uint8_t possible_pattern_info;
  311. RCHECK(reader->ReadFullBoxHeader() &&
  312. reader->SkipBytes(1) && // skip reserved byte
  313. reader->Read1(&possible_pattern_info) && reader->Read1(&flag) &&
  314. reader->Read1(&default_iv_size) &&
  315. reader->ReadVec(&default_kid, kKeyIdSize));
  316. is_encrypted = (flag != 0);
  317. if (is_encrypted) {
  318. if (reader->version() > 0) {
  319. default_crypt_byte_block = (possible_pattern_info >> 4) & 0x0f;
  320. default_skip_byte_block = possible_pattern_info & 0x0f;
  321. }
  322. if (default_iv_size == 0) {
  323. RCHECK(reader->Read1(&default_constant_iv_size));
  324. RCHECK(default_constant_iv_size == 8 || default_constant_iv_size == 16);
  325. memset(default_constant_iv, 0, sizeof(default_constant_iv));
  326. for (uint8_t i = 0; i < default_constant_iv_size; i++)
  327. RCHECK(reader->Read1(default_constant_iv + i));
  328. } else {
  329. RCHECK(default_iv_size == 8 || default_iv_size == 16);
  330. }
  331. } else {
  332. RCHECK(default_iv_size == 0);
  333. }
  334. return true;
  335. }
  336. SchemeInfo::SchemeInfo() = default;
  337. SchemeInfo::SchemeInfo(const SchemeInfo& other) = default;
  338. SchemeInfo::~SchemeInfo() = default;
  339. FourCC SchemeInfo::BoxType() const { return FOURCC_SCHI; }
  340. bool SchemeInfo::Parse(BoxReader* reader) {
  341. return reader->ScanChildren() && reader->ReadChild(&track_encryption);
  342. }
  343. ProtectionSchemeInfo::ProtectionSchemeInfo() = default;
  344. ProtectionSchemeInfo::ProtectionSchemeInfo(const ProtectionSchemeInfo& other) =
  345. default;
  346. ProtectionSchemeInfo::~ProtectionSchemeInfo() = default;
  347. FourCC ProtectionSchemeInfo::BoxType() const { return FOURCC_SINF; }
  348. bool ProtectionSchemeInfo::Parse(BoxReader* reader) {
  349. RCHECK(reader->ScanChildren() &&
  350. reader->ReadChild(&format) &&
  351. reader->ReadChild(&type));
  352. if (HasSupportedScheme())
  353. RCHECK(reader->ReadChild(&info));
  354. // Other protection schemes are silently ignored. Since the protection scheme
  355. // type can't be determined until this box is opened, we return 'true' for
  356. // unsupported protection schemes. It is the parent box's responsibility to
  357. // ensure that this scheme type is a supported one.
  358. return true;
  359. }
  360. bool ProtectionSchemeInfo::HasSupportedScheme() const {
  361. FourCC four_cc = type.type;
  362. return (four_cc == FOURCC_CENC || four_cc == FOURCC_CBCS);
  363. }
  364. bool ProtectionSchemeInfo::IsCbcsEncryptionScheme() const {
  365. FourCC four_cc = type.type;
  366. return (four_cc == FOURCC_CBCS);
  367. }
  368. MovieHeader::MovieHeader()
  369. : version(0),
  370. creation_time(0),
  371. modification_time(0),
  372. timescale(0),
  373. duration(0),
  374. rate(-1),
  375. volume(-1),
  376. next_track_id(0) {}
  377. MovieHeader::MovieHeader(const MovieHeader& other) = default;
  378. MovieHeader::~MovieHeader() = default;
  379. FourCC MovieHeader::BoxType() const { return FOURCC_MVHD; }
  380. bool MovieHeader::Parse(BoxReader* reader) {
  381. RCHECK(reader->ReadFullBoxHeader());
  382. version = reader->version();
  383. if (version == 1) {
  384. RCHECK(reader->Read8(&creation_time) &&
  385. reader->Read8(&modification_time) &&
  386. reader->Read4(&timescale) &&
  387. reader->Read8(&duration));
  388. } else {
  389. RCHECK(reader->Read4Into8(&creation_time) &&
  390. reader->Read4Into8(&modification_time) &&
  391. reader->Read4(&timescale) &&
  392. reader->Read4Into8(&duration));
  393. }
  394. RCHECK_MEDIA_LOGGED(timescale > 0, reader->media_log(),
  395. "Movie header's timescale must not be 0");
  396. RCHECK(reader->Read4s(&rate) &&
  397. reader->Read2s(&volume) &&
  398. reader->SkipBytes(10) && // reserved
  399. reader->ReadDisplayMatrix(display_matrix) &&
  400. reader->SkipBytes(24) && // predefined zero
  401. reader->Read4(&next_track_id));
  402. return true;
  403. }
  404. TrackHeader::TrackHeader()
  405. : creation_time(0),
  406. modification_time(0),
  407. track_id(0),
  408. duration(0),
  409. layer(-1),
  410. alternate_group(-1),
  411. volume(-1),
  412. width(0),
  413. height(0) {}
  414. TrackHeader::TrackHeader(const TrackHeader& other) = default;
  415. TrackHeader::~TrackHeader() = default;
  416. FourCC TrackHeader::BoxType() const { return FOURCC_TKHD; }
  417. bool TrackHeader::Parse(BoxReader* reader) {
  418. RCHECK(reader->ReadFullBoxHeader());
  419. if (reader->version() == 1) {
  420. RCHECK(reader->Read8(&creation_time) &&
  421. reader->Read8(&modification_time) &&
  422. reader->Read4(&track_id) &&
  423. reader->SkipBytes(4) && // reserved
  424. reader->Read8(&duration));
  425. } else {
  426. RCHECK(reader->Read4Into8(&creation_time) &&
  427. reader->Read4Into8(&modification_time) &&
  428. reader->Read4(&track_id) &&
  429. reader->SkipBytes(4) && // reserved
  430. reader->Read4Into8(&duration));
  431. }
  432. RCHECK(reader->SkipBytes(8) && // reserved
  433. reader->Read2s(&layer) &&
  434. reader->Read2s(&alternate_group) &&
  435. reader->Read2s(&volume) &&
  436. reader->SkipBytes(2) && // reserved
  437. reader->ReadDisplayMatrix(display_matrix) &&
  438. reader->Read4(&width) &&
  439. reader->Read4(&height));
  440. // Round width and height to the nearest number.
  441. // Note: width and height are fixed-point 16.16 values. The following code
  442. // rounds a.1x to a + 1, and a.0x to a.
  443. width >>= 15;
  444. width += 1;
  445. width >>= 1;
  446. height >>= 15;
  447. height += 1;
  448. height >>= 1;
  449. return true;
  450. }
  451. SampleDescription::SampleDescription() : type(kInvalid) {}
  452. SampleDescription::SampleDescription(const SampleDescription& other) = default;
  453. SampleDescription::~SampleDescription() = default;
  454. FourCC SampleDescription::BoxType() const { return FOURCC_STSD; }
  455. bool SampleDescription::Parse(BoxReader* reader) {
  456. uint32_t count;
  457. RCHECK(reader->SkipBytes(4) &&
  458. reader->Read4(&count));
  459. video_entries.clear();
  460. audio_entries.clear();
  461. // Note: this value is preset before scanning begins. See comments in the
  462. // Parse(Media*) function.
  463. if (type == kVideo) {
  464. RCHECK(reader->ReadAllChildren(&video_entries));
  465. } else if (type == kAudio) {
  466. RCHECK(reader->ReadAllChildren(&audio_entries));
  467. }
  468. return true;
  469. }
  470. SampleTable::SampleTable() = default;
  471. SampleTable::SampleTable(const SampleTable& other) = default;
  472. SampleTable::~SampleTable() = default;
  473. FourCC SampleTable::BoxType() const { return FOURCC_STBL; }
  474. bool SampleTable::Parse(BoxReader* reader) {
  475. RCHECK(reader->ScanChildren() &&
  476. reader->ReadChild(&description));
  477. // There could be multiple SampleGroupDescription boxes with different
  478. // grouping types. For common encryption, the relevant grouping type is
  479. // 'seig'. Continue reading until 'seig' is found, or until running out of
  480. // child boxes.
  481. while (reader->HasChild(&sample_group_description)) {
  482. RCHECK(reader->ReadChild(&sample_group_description));
  483. if (sample_group_description.grouping_type == FOURCC_SEIG)
  484. break;
  485. sample_group_description.entries.clear();
  486. }
  487. return true;
  488. }
  489. EditList::EditList() = default;
  490. EditList::EditList(const EditList& other) = default;
  491. EditList::~EditList() = default;
  492. FourCC EditList::BoxType() const { return FOURCC_ELST; }
  493. bool EditList::Parse(BoxReader* reader) {
  494. uint32_t count;
  495. RCHECK(reader->ReadFullBoxHeader() && reader->Read4(&count));
  496. const size_t bytes_per_edit = reader->version() == 1 ? 20 : 12;
  497. // Cast |count| to size_t before multiplying to support maximum platform size.
  498. base::CheckedNumeric<size_t> bytes_needed =
  499. base::CheckMul(bytes_per_edit, static_cast<size_t>(count));
  500. RCHECK_MEDIA_LOGGED(bytes_needed.IsValid(), reader->media_log(),
  501. "Extreme ELST count exceeds implementation limit.");
  502. RCHECK(reader->HasBytes(bytes_needed.ValueOrDie()));
  503. RCHECK(count <= edits.max_size());
  504. edits.resize(count);
  505. for (auto edit = edits.begin(); edit != edits.end(); ++edit) {
  506. if (reader->version() == 1) {
  507. RCHECK(reader->Read8(&edit->segment_duration) &&
  508. reader->Read8s(&edit->media_time));
  509. } else {
  510. RCHECK(reader->Read4Into8(&edit->segment_duration) &&
  511. reader->Read4sInto8s(&edit->media_time));
  512. }
  513. RCHECK(reader->Read2s(&edit->media_rate_integer) &&
  514. reader->Read2s(&edit->media_rate_fraction));
  515. }
  516. return true;
  517. }
  518. Edit::Edit() = default;
  519. Edit::Edit(const Edit& other) = default;
  520. Edit::~Edit() = default;
  521. FourCC Edit::BoxType() const { return FOURCC_EDTS; }
  522. bool Edit::Parse(BoxReader* reader) {
  523. return reader->ScanChildren() && reader->ReadChild(&list);
  524. }
  525. HandlerReference::HandlerReference() : type(kInvalid) {}
  526. HandlerReference::HandlerReference(const HandlerReference& other) = default;
  527. HandlerReference::~HandlerReference() = default;
  528. FourCC HandlerReference::BoxType() const { return FOURCC_HDLR; }
  529. bool HandlerReference::Parse(BoxReader* reader) {
  530. FourCC hdlr_type;
  531. RCHECK(reader->ReadFullBoxHeader() && reader->SkipBytes(4) &&
  532. reader->ReadFourCC(&hdlr_type) && reader->SkipBytes(12));
  533. // Now we should be at the beginning of the |name| field of HDLR box. The
  534. // |name| is a zero-terminated ASCII string in ISO BMFF, but it was a
  535. // Pascal-style counted string in older QT/Mov formats. So we'll read the
  536. // remaining box bytes first, then if the last one is zero, we strip the last
  537. // zero byte, otherwise we'll string the first byte (containing the length of
  538. // the Pascal-style string).
  539. std::vector<uint8_t> name_bytes;
  540. RCHECK(reader->ReadVec(&name_bytes, reader->box_size() - reader->pos()));
  541. if (name_bytes.size() == 0) {
  542. name = "";
  543. } else if (name_bytes.back() == 0) {
  544. // This is a zero-terminated C-style string, exclude the last byte.
  545. name = std::string(name_bytes.begin(), name_bytes.end() - 1);
  546. } else {
  547. // Check that the length of the Pascal-style string is correct.
  548. RCHECK(name_bytes[0] == (name_bytes.size() - 1));
  549. // Skip the first byte, containing the length of the Pascal-string.
  550. name = std::string(name_bytes.begin() + 1, name_bytes.end());
  551. }
  552. if (hdlr_type == FOURCC_VIDE) {
  553. type = kVideo;
  554. } else if (hdlr_type == FOURCC_SOUN) {
  555. type = kAudio;
  556. } else if (hdlr_type == FOURCC_META || hdlr_type == FOURCC_SUBT ||
  557. hdlr_type == FOURCC_TEXT || hdlr_type == FOURCC_SBTL) {
  558. // For purposes of detection, we include 'sbtl' handler here. Note, though
  559. // that ISO-14496-12 and its 2012 Amendment 2, and the spec for sourcing
  560. // inband tracks all reference only 'text' or 'subt', and 14496-30
  561. // references only 'subt'. Yet ffmpeg can encode subtitles as 'sbtl'.
  562. type = kText;
  563. } else {
  564. type = kInvalid;
  565. }
  566. return true;
  567. }
  568. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  569. AVCDecoderConfigurationRecord::AVCDecoderConfigurationRecord()
  570. : version(0),
  571. profile_indication(0),
  572. profile_compatibility(0),
  573. avc_level(0),
  574. length_size(0) {}
  575. AVCDecoderConfigurationRecord::AVCDecoderConfigurationRecord(
  576. const AVCDecoderConfigurationRecord& other) = default;
  577. AVCDecoderConfigurationRecord::~AVCDecoderConfigurationRecord() = default;
  578. FourCC AVCDecoderConfigurationRecord::BoxType() const { return FOURCC_AVCC; }
  579. bool AVCDecoderConfigurationRecord::Parse(BoxReader* reader) {
  580. return ParseInternal(reader, reader->media_log());
  581. }
  582. bool AVCDecoderConfigurationRecord::Parse(const uint8_t* data, int data_size) {
  583. BufferReader reader(data, data_size);
  584. // TODO(wolenetz): Questionable MediaLog usage, http://crbug.com/712310
  585. NullMediaLog media_log;
  586. return ParseInternal(&reader, &media_log);
  587. }
  588. bool AVCDecoderConfigurationRecord::ParseInternal(BufferReader* reader,
  589. MediaLog* media_log) {
  590. RCHECK(reader->Read1(&version) && version == 1 &&
  591. reader->Read1(&profile_indication) &&
  592. reader->Read1(&profile_compatibility) &&
  593. reader->Read1(&avc_level));
  594. uint8_t length_size_minus_one;
  595. RCHECK(reader->Read1(&length_size_minus_one));
  596. length_size = (length_size_minus_one & 0x3) + 1;
  597. RCHECK(length_size != 3); // Only values of 1, 2, and 4 are valid.
  598. uint8_t num_sps;
  599. RCHECK(reader->Read1(&num_sps));
  600. num_sps &= 0x1f;
  601. sps_list.resize(num_sps);
  602. for (int i = 0; i < num_sps; i++) {
  603. uint16_t sps_length;
  604. RCHECK(reader->Read2(&sps_length) &&
  605. reader->ReadVec(&sps_list[i], sps_length));
  606. RCHECK(sps_list[i].size() > 4);
  607. }
  608. uint8_t num_pps;
  609. RCHECK(reader->Read1(&num_pps));
  610. pps_list.resize(num_pps);
  611. for (int i = 0; i < num_pps; i++) {
  612. uint16_t pps_length;
  613. RCHECK(reader->Read2(&pps_length) &&
  614. reader->ReadVec(&pps_list[i], pps_length));
  615. }
  616. return true;
  617. }
  618. bool AVCDecoderConfigurationRecord::Serialize(
  619. std::vector<uint8_t>& output) const {
  620. // See ISO/IEC 14496-15 5.3.3.1.2 for the format description
  621. constexpr uint8_t sps_list_size_mask = (1 << 5) - 1; // 5 bits
  622. if (sps_list.size() > sps_list_size_mask)
  623. return false;
  624. constexpr uint8_t pps_list_size_mask = 0xff;
  625. if (pps_list.size() > pps_list_size_mask)
  626. return false;
  627. if (length_size > 4)
  628. return false;
  629. // Calculating total size of the buffer we'll need for serialization
  630. size_t expected_size =
  631. 1 + // configurationVersion
  632. 1 + // AVCProfileIndication
  633. 1 + // profile_compatibility
  634. 1 + // AVCLevelIndication
  635. 1 + // lengthSizeMinusOne
  636. 1 + // numOfSequenceParameterSets, i.e. length of sps_list
  637. 1; // numOfPictureParameterSets, i.e. length of pps_list
  638. constexpr size_t max_vector_size = (1 << 16) - 1; // 2 bytes
  639. for (auto& sps : sps_list) {
  640. expected_size += 2; // 2 bytes for sequenceParameterSetLength
  641. if (sps.size() > max_vector_size)
  642. return false;
  643. expected_size += sps.size();
  644. }
  645. for (auto& pps : pps_list) {
  646. expected_size += 2; // 2 bytes for pictureParameterSetLength;
  647. if (pps.size() > max_vector_size)
  648. return false;
  649. expected_size += pps.size();
  650. }
  651. output.clear();
  652. output.resize(expected_size);
  653. base::BigEndianWriter writer(reinterpret_cast<char*>(output.data()),
  654. output.size());
  655. bool result = true;
  656. // configurationVersion
  657. result &= writer.WriteU8(version);
  658. // AVCProfileIndication
  659. result &= writer.WriteU8(profile_indication);
  660. // profile_compatibility
  661. result &= writer.WriteU8(profile_compatibility);
  662. // AVCLevelIndication
  663. result &= writer.WriteU8(avc_level);
  664. // lengthSizeMinusOne
  665. uint8_t length_size_minus_one = (length_size - 1) | 0xfc;
  666. result &= writer.WriteU8(length_size_minus_one);
  667. // numOfSequenceParameterSets
  668. uint8_t sps_size = sps_list.size() | ~sps_list_size_mask;
  669. result &= writer.WriteU8(sps_size);
  670. // sequenceParameterSetNALUnits
  671. for (auto& sps : sps_list) {
  672. result &= writer.WriteU16(sps.size());
  673. writer.WriteBytes(sps.data(), sps.size());
  674. }
  675. // numOfPictureParameterSets
  676. uint8_t pps_size = pps_list.size();
  677. result &= writer.WriteU8(pps_size);
  678. // pictureParameterSetNALUnit
  679. for (auto& pps : pps_list) {
  680. result &= writer.WriteU16(pps.size());
  681. writer.WriteBytes(pps.data(), pps.size());
  682. }
  683. return result;
  684. }
  685. #endif // BUILDFLAG(USE_PROPRIETARY_CODECS)
  686. VPCodecConfigurationRecord::VPCodecConfigurationRecord()
  687. : profile(VIDEO_CODEC_PROFILE_UNKNOWN) {}
  688. VPCodecConfigurationRecord::VPCodecConfigurationRecord(
  689. const VPCodecConfigurationRecord& other) = default;
  690. VPCodecConfigurationRecord::~VPCodecConfigurationRecord() = default;
  691. FourCC VPCodecConfigurationRecord::BoxType() const {
  692. return FOURCC_VPCC;
  693. }
  694. bool VPCodecConfigurationRecord::Parse(BoxReader* reader) {
  695. uint8_t profile_indication = 0;
  696. RCHECK(reader->ReadFullBoxHeader() && reader->Read1(&profile_indication));
  697. // The remaining fields are not parsed as we don't care about them for now.
  698. switch (profile_indication) {
  699. case 0:
  700. profile = VP9PROFILE_PROFILE0;
  701. break;
  702. case 1:
  703. profile = VP9PROFILE_PROFILE1;
  704. break;
  705. case 2:
  706. profile = VP9PROFILE_PROFILE2;
  707. break;
  708. case 3:
  709. profile = VP9PROFILE_PROFILE3;
  710. break;
  711. default:
  712. MEDIA_LOG(ERROR, reader->media_log())
  713. << "Unsupported VP9 profile: 0x" << std::hex
  714. << static_cast<uint32_t>(profile_indication);
  715. return false;
  716. }
  717. RCHECK(reader->Read1(&level));
  718. uint8_t depth_chroma_full_range;
  719. RCHECK(reader->Read1(&depth_chroma_full_range));
  720. uint8_t primary_id;
  721. RCHECK(reader->Read1(&primary_id));
  722. uint8_t transfer_id;
  723. RCHECK(reader->Read1(&transfer_id));
  724. uint8_t matrix_id;
  725. RCHECK(reader->Read1(&matrix_id));
  726. color_space = VideoColorSpace(primary_id, transfer_id, matrix_id,
  727. depth_chroma_full_range & 1
  728. ? gfx::ColorSpace::RangeID::FULL
  729. : gfx::ColorSpace::RangeID::LIMITED);
  730. return true;
  731. }
  732. #if BUILDFLAG(ENABLE_AV1_DECODER)
  733. AV1CodecConfigurationRecord::AV1CodecConfigurationRecord()
  734. : profile(VIDEO_CODEC_PROFILE_UNKNOWN) {}
  735. AV1CodecConfigurationRecord::AV1CodecConfigurationRecord(
  736. const AV1CodecConfigurationRecord& other) = default;
  737. AV1CodecConfigurationRecord::~AV1CodecConfigurationRecord() = default;
  738. FourCC AV1CodecConfigurationRecord::BoxType() const {
  739. return FOURCC_AV1C;
  740. }
  741. // Parse the AV1CodecConfigurationRecord, which has the following format:
  742. // unsigned int (1) marker = 1;
  743. // unsigned int (7) version = 1;
  744. // unsigned int (3) seq_profile;
  745. // unsigned int (5) seq_level_idx_0;
  746. // unsigned int (1) seq_tier_0;
  747. // unsigned int (1) high_bitdepth;
  748. // unsigned int (1) twelve_bit;
  749. // unsigned int (1) monochrome;
  750. // unsigned int (1) chroma_subsampling_x;
  751. // unsigned int (1) chroma_subsampling_y;
  752. // unsigned int (2) chroma_sample_position;
  753. // unsigned int (3) reserved = 0;
  754. //
  755. // unsigned int (1) initial_presentation_delay_present;
  756. // if (initial_presentation_delay_present) {
  757. // unsigned int (4) initial_presentation_delay_minus_one;
  758. // } else {
  759. // unsigned int (4) reserved = 0;
  760. // }
  761. //
  762. // unsigned int (8)[] configOBUs;
  763. bool AV1CodecConfigurationRecord::Parse(BoxReader* reader) {
  764. uint8_t av1c_byte = 0;
  765. RCHECK(reader->Read1(&av1c_byte));
  766. const uint8_t av1c_marker = av1c_byte >> 7;
  767. if (!av1c_marker) {
  768. MEDIA_LOG(ERROR, reader->media_log()) << "Unsupported av1C: marker unset.";
  769. return false;
  770. }
  771. const uint8_t av1c_version = av1c_byte & 0b01111111;
  772. if (av1c_version != 1) {
  773. MEDIA_LOG(ERROR, reader->media_log())
  774. << "Unsupported av1C: unexpected version number: " << av1c_version;
  775. return false;
  776. }
  777. RCHECK(reader->Read1(&av1c_byte));
  778. const uint8_t seq_profile = av1c_byte >> 5;
  779. switch (seq_profile) {
  780. case 0:
  781. profile = AV1PROFILE_PROFILE_MAIN;
  782. break;
  783. case 1:
  784. profile = AV1PROFILE_PROFILE_HIGH;
  785. break;
  786. case 2:
  787. profile = AV1PROFILE_PROFILE_PRO;
  788. break;
  789. default:
  790. MEDIA_LOG(ERROR, reader->media_log())
  791. << "Unsupported av1C: unknown profile 0x" << std::hex << seq_profile;
  792. return false;
  793. }
  794. // The remaining fields are ignored since we don't care about them yet.
  795. return true;
  796. }
  797. #endif // BUILDFLAG(ENABLE_AV1_DECODER)
  798. PixelAspectRatioBox::PixelAspectRatioBox() : h_spacing(1), v_spacing(1) {}
  799. PixelAspectRatioBox::PixelAspectRatioBox(const PixelAspectRatioBox& other) =
  800. default;
  801. PixelAspectRatioBox::~PixelAspectRatioBox() = default;
  802. FourCC PixelAspectRatioBox::BoxType() const { return FOURCC_PASP; }
  803. bool PixelAspectRatioBox::Parse(BoxReader* reader) {
  804. RCHECK(reader->Read4(&h_spacing) && reader->Read4(&v_spacing));
  805. return true;
  806. }
  807. ColorParameterInformation::ColorParameterInformation() = default;
  808. ColorParameterInformation::ColorParameterInformation(
  809. const ColorParameterInformation& other) = default;
  810. ColorParameterInformation::~ColorParameterInformation() = default;
  811. FourCC ColorParameterInformation::BoxType() const {
  812. return FOURCC_COLR;
  813. }
  814. bool ColorParameterInformation::Parse(BoxReader* reader) {
  815. fully_parsed = false;
  816. FourCC type;
  817. RCHECK(reader->ReadFourCC(&type));
  818. if (type != FOURCC_NCLX) {
  819. // Ignore currently unsupported color information metadata parsing.
  820. // TODO: Support 'nclc', 'rICC', and 'prof'.
  821. return true;
  822. }
  823. uint8_t full_range_byte;
  824. RCHECK(reader->Read2(&colour_primaries) &&
  825. reader->Read2(&transfer_characteristics) &&
  826. reader->Read2(&matrix_coefficients) &&
  827. reader->Read1(&full_range_byte));
  828. full_range = full_range_byte & 0x80;
  829. fully_parsed = true;
  830. return true;
  831. }
  832. MasteringDisplayColorVolume::MasteringDisplayColorVolume() = default;
  833. MasteringDisplayColorVolume::MasteringDisplayColorVolume(
  834. const MasteringDisplayColorVolume& other) = default;
  835. MasteringDisplayColorVolume::~MasteringDisplayColorVolume() = default;
  836. FourCC MasteringDisplayColorVolume::BoxType() const {
  837. return FOURCC_MDCV;
  838. }
  839. bool MasteringDisplayColorVolume::Parse(BoxReader* reader) {
  840. // Technically the color coordinates may be in any order. The spec recommends
  841. // GBR and it is assumed that the color coordinates are in such order.
  842. constexpr float kUnitOfMasteringLuminance = 10000;
  843. RCHECK(ReadMdcvColorCoordinate(reader, &display_primaries_gx) &&
  844. ReadMdcvColorCoordinate(reader, &display_primaries_gy) &&
  845. ReadMdcvColorCoordinate(reader, &display_primaries_bx) &&
  846. ReadMdcvColorCoordinate(reader, &display_primaries_by) &&
  847. ReadMdcvColorCoordinate(reader, &display_primaries_rx) &&
  848. ReadMdcvColorCoordinate(reader, &display_primaries_ry) &&
  849. ReadMdcvColorCoordinate(reader, &white_point_x) &&
  850. ReadMdcvColorCoordinate(reader, &white_point_y) &&
  851. ReadFixedPoint32(kUnitOfMasteringLuminance, reader,
  852. &max_display_mastering_luminance) &&
  853. ReadFixedPoint32(kUnitOfMasteringLuminance, reader,
  854. &min_display_mastering_luminance));
  855. return true;
  856. }
  857. FourCC SMPTE2086MasteringDisplayMetadataBox::BoxType() const {
  858. return FOURCC_SMDM;
  859. }
  860. bool SMPTE2086MasteringDisplayMetadataBox::Parse(BoxReader* reader) {
  861. constexpr float kColorCoordinateUnit = 1 << 16;
  862. constexpr float kLuminanceMaxUnit = 1 << 8;
  863. constexpr float kLuminanceMinUnit = 1 << 14;
  864. RCHECK(reader->ReadFullBoxHeader());
  865. // Technically the color coordinates may be in any order. The spec recommends
  866. // RGB and it is assumed that the color coordinates are in such order.
  867. RCHECK(
  868. ReadFixedPoint16(kColorCoordinateUnit, reader, &display_primaries_rx) &&
  869. ReadFixedPoint16(kColorCoordinateUnit, reader, &display_primaries_ry) &&
  870. ReadFixedPoint16(kColorCoordinateUnit, reader, &display_primaries_gx) &&
  871. ReadFixedPoint16(kColorCoordinateUnit, reader, &display_primaries_gy) &&
  872. ReadFixedPoint16(kColorCoordinateUnit, reader, &display_primaries_bx) &&
  873. ReadFixedPoint16(kColorCoordinateUnit, reader, &display_primaries_by) &&
  874. ReadFixedPoint16(kColorCoordinateUnit, reader, &white_point_x) &&
  875. ReadFixedPoint16(kColorCoordinateUnit, reader, &white_point_y) &&
  876. ReadFixedPoint32(kLuminanceMaxUnit, reader,
  877. &max_display_mastering_luminance) &&
  878. ReadFixedPoint32(kLuminanceMinUnit, reader,
  879. &min_display_mastering_luminance));
  880. return true;
  881. }
  882. ContentLightLevelInformation::ContentLightLevelInformation() = default;
  883. ContentLightLevelInformation::ContentLightLevelInformation(
  884. const ContentLightLevelInformation& other) = default;
  885. ContentLightLevelInformation::~ContentLightLevelInformation() = default;
  886. FourCC ContentLightLevelInformation::BoxType() const {
  887. return FOURCC_CLLI;
  888. }
  889. bool ContentLightLevelInformation::Parse(BoxReader* reader) {
  890. return reader->Read2(&max_content_light_level) &&
  891. reader->Read2(&max_pic_average_light_level);
  892. }
  893. bool ContentLightLevel::Parse(BoxReader* reader) {
  894. RCHECK(reader->ReadFullBoxHeader());
  895. return ContentLightLevelInformation::Parse(reader);
  896. }
  897. FourCC ContentLightLevel::BoxType() const {
  898. return FOURCC_COLL;
  899. }
  900. VideoSampleEntry::VideoSampleEntry()
  901. : format(FOURCC_NULL),
  902. data_reference_index(0),
  903. width(0),
  904. height(0),
  905. video_codec(VideoCodec::kUnknown),
  906. video_codec_profile(VIDEO_CODEC_PROFILE_UNKNOWN),
  907. video_codec_level(kNoVideoCodecLevel) {}
  908. VideoSampleEntry::VideoSampleEntry(const VideoSampleEntry& other) = default;
  909. VideoSampleEntry::~VideoSampleEntry() = default;
  910. FourCC VideoSampleEntry::BoxType() const {
  911. DCHECK(false) << "VideoSampleEntry should be parsed according to the "
  912. << "handler type recovered in its Media ancestor.";
  913. return FOURCC_NULL;
  914. }
  915. bool VideoSampleEntry::Parse(BoxReader* reader) {
  916. format = reader->type();
  917. RCHECK(reader->SkipBytes(6) &&
  918. reader->Read2(&data_reference_index) &&
  919. reader->SkipBytes(16) &&
  920. reader->Read2(&width) &&
  921. reader->Read2(&height) &&
  922. reader->SkipBytes(50));
  923. RCHECK(reader->ScanChildren());
  924. if (reader->HasChild(&pixel_aspect)) {
  925. RCHECK(reader->MaybeReadChild(&pixel_aspect));
  926. }
  927. if (format == FOURCC_ENCV) {
  928. // Continue scanning until a recognized protection scheme is found, or until
  929. // we run out of protection schemes.
  930. while (!sinf.HasSupportedScheme()) {
  931. if (!reader->ReadChild(&sinf))
  932. return false;
  933. }
  934. }
  935. const FourCC actual_format =
  936. format == FOURCC_ENCV ? sinf.format.format : format;
  937. switch (actual_format) {
  938. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  939. case FOURCC_AVC1:
  940. case FOURCC_AVC3: {
  941. DVLOG(2) << __func__ << " reading AVCDecoderConfigurationRecord (avcC)";
  942. std::unique_ptr<AVCDecoderConfigurationRecord> avcConfig(
  943. new AVCDecoderConfigurationRecord());
  944. RCHECK(reader->ReadChild(avcConfig.get()));
  945. video_codec = VideoCodec::kH264;
  946. video_codec_profile = H264Parser::ProfileIDCToVideoCodecProfile(
  947. avcConfig->profile_indication);
  948. frame_bitstream_converter =
  949. base::MakeRefCounted<AVCBitstreamConverter>(std::move(avcConfig));
  950. #if BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  951. // It can be Dolby Vision stream if there is DVCC box.
  952. auto dv_config = ParseDOVIConfig(reader);
  953. if (dv_config.has_value()) {
  954. DVLOG(2) << __func__ << " reading DolbyVisionConfiguration (dvcC/dvvC)";
  955. video_codec = VideoCodec::kDolbyVision;
  956. video_codec_profile = dv_config->codec_profile;
  957. video_codec_level = dv_config->dv_level;
  958. }
  959. #endif // BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  960. break;
  961. }
  962. #if BUILDFLAG(ENABLE_PLATFORM_HEVC)
  963. case FOURCC_HEV1:
  964. case FOURCC_HVC1: {
  965. DVLOG(2) << __func__ << " parsing HEVCDecoderConfigurationRecord (hvcC)";
  966. std::unique_ptr<HEVCDecoderConfigurationRecord> hevcConfig(
  967. new HEVCDecoderConfigurationRecord());
  968. RCHECK(reader->ReadChild(hevcConfig.get()));
  969. video_codec = VideoCodec::kHEVC;
  970. video_codec_profile = hevcConfig->GetVideoProfile();
  971. #if BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
  972. video_color_space = hevcConfig->GetColorSpace();
  973. #endif // BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
  974. frame_bitstream_converter =
  975. base::MakeRefCounted<HEVCBitstreamConverter>(std::move(hevcConfig));
  976. #if BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  977. // It can be Dolby Vision stream if there is DVCC box.
  978. auto dv_config = ParseDOVIConfig(reader);
  979. if (dv_config.has_value()) {
  980. DVLOG(2) << __func__ << " reading DolbyVisionConfiguration (dvcC/dvvC)";
  981. video_codec = VideoCodec::kDolbyVision;
  982. video_codec_profile = dv_config->codec_profile;
  983. video_codec_level = dv_config->dv_level;
  984. }
  985. #endif // BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  986. break;
  987. }
  988. #endif // BUILDFLAG(ENABLE_PLATFORM_HEVC)
  989. #if BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  990. case FOURCC_DVA1:
  991. case FOURCC_DVAV: {
  992. DVLOG(2) << __func__ << " reading AVCDecoderConfigurationRecord (avcC)";
  993. std::unique_ptr<AVCDecoderConfigurationRecord> avcConfig(
  994. new AVCDecoderConfigurationRecord());
  995. RCHECK(reader->ReadChild(avcConfig.get()));
  996. frame_bitstream_converter =
  997. base::MakeRefCounted<AVCBitstreamConverter>(std::move(avcConfig));
  998. DVLOG(2) << __func__ << " reading DolbyVisionConfiguration (dvcC/dvvC)";
  999. auto dv_config = ParseDOVIConfig(reader);
  1000. RCHECK(dv_config.has_value());
  1001. video_codec = VideoCodec::kDolbyVision;
  1002. video_codec_profile = dv_config->codec_profile;
  1003. video_codec_level = dv_config->dv_level;
  1004. break;
  1005. }
  1006. #if BUILDFLAG(ENABLE_PLATFORM_HEVC)
  1007. case FOURCC_DVH1:
  1008. case FOURCC_DVHE: {
  1009. DVLOG(2) << __func__ << " reading HEVCDecoderConfigurationRecord (hvcC)";
  1010. std::unique_ptr<HEVCDecoderConfigurationRecord> hevcConfig(
  1011. new HEVCDecoderConfigurationRecord());
  1012. RCHECK(reader->ReadChild(hevcConfig.get()));
  1013. #if BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
  1014. video_color_space = hevcConfig->GetColorSpace();
  1015. #endif // BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
  1016. frame_bitstream_converter =
  1017. base::MakeRefCounted<HEVCBitstreamConverter>(std::move(hevcConfig));
  1018. DVLOG(2) << __func__ << " reading DolbyVisionConfiguration (dvcC/dvvC)";
  1019. auto dv_config = ParseDOVIConfig(reader);
  1020. RCHECK(dv_config.has_value());
  1021. video_codec = VideoCodec::kDolbyVision;
  1022. video_codec_profile = dv_config->codec_profile;
  1023. video_codec_level = dv_config->dv_level;
  1024. break;
  1025. }
  1026. #endif // BUILDFLAG(ENABLE_PLATFORM_HEVC)
  1027. #endif // BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  1028. #endif // BUILDFLAG(USE_PROPRIETARY_CODECS)
  1029. case FOURCC_VP09: {
  1030. DVLOG(2) << __func__ << " parsing VPCodecConfigurationRecord (vpcC)";
  1031. std::unique_ptr<VPCodecConfigurationRecord> vp_config(
  1032. new VPCodecConfigurationRecord());
  1033. RCHECK(reader->ReadChild(vp_config.get()));
  1034. frame_bitstream_converter = nullptr;
  1035. video_codec = VideoCodec::kVP9;
  1036. video_codec_profile = vp_config->profile;
  1037. video_color_space = vp_config->color_space;
  1038. video_codec_level = vp_config->level;
  1039. SMPTE2086MasteringDisplayMetadataBox color_volume;
  1040. if (reader->HasChild(&color_volume)) {
  1041. RCHECK(reader->ReadChild(&color_volume));
  1042. mastering_display_color_volume = color_volume;
  1043. }
  1044. ContentLightLevel level_information;
  1045. if (reader->HasChild(&level_information)) {
  1046. RCHECK(reader->ReadChild(&level_information));
  1047. content_light_level_information = level_information;
  1048. }
  1049. break;
  1050. }
  1051. #if BUILDFLAG(ENABLE_AV1_DECODER)
  1052. case FOURCC_AV01: {
  1053. DVLOG(2) << __func__ << " reading AV1 configuration.";
  1054. AV1CodecConfigurationRecord av1_config;
  1055. RCHECK(reader->ReadChild(&av1_config));
  1056. frame_bitstream_converter = nullptr;
  1057. video_codec = VideoCodec::kAV1;
  1058. video_codec_profile = av1_config.profile;
  1059. break;
  1060. }
  1061. #endif
  1062. default:
  1063. // Unknown/unsupported format
  1064. MEDIA_LOG(ERROR, reader->media_log())
  1065. << "Unsupported VisualSampleEntry type "
  1066. << FourCCToString(actual_format);
  1067. return false;
  1068. }
  1069. ColorParameterInformation color_parameter_information;
  1070. if (reader->HasChild(&color_parameter_information)) {
  1071. RCHECK(reader->ReadChild(&color_parameter_information));
  1072. if (color_parameter_information.fully_parsed) {
  1073. video_color_space = ConvertColorParameterInformationToColorSpace(
  1074. color_parameter_information);
  1075. }
  1076. }
  1077. MasteringDisplayColorVolume color_volume;
  1078. if (reader->HasChild(&color_volume)) {
  1079. RCHECK(reader->ReadChild(&color_volume));
  1080. mastering_display_color_volume = color_volume;
  1081. }
  1082. ContentLightLevelInformation level_information;
  1083. if (reader->HasChild(&level_information)) {
  1084. RCHECK(reader->ReadChild(&level_information));
  1085. content_light_level_information = level_information;
  1086. }
  1087. if (video_codec_profile == VIDEO_CODEC_PROFILE_UNKNOWN) {
  1088. MEDIA_LOG(ERROR, reader->media_log()) << "Unrecognized video codec profile";
  1089. return false;
  1090. }
  1091. return true;
  1092. }
  1093. bool VideoSampleEntry::IsFormatValid() const {
  1094. const FourCC actual_format =
  1095. format == FOURCC_ENCV ? sinf.format.format : format;
  1096. switch (actual_format) {
  1097. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  1098. case FOURCC_AVC1:
  1099. case FOURCC_AVC3:
  1100. #if BUILDFLAG(ENABLE_PLATFORM_HEVC)
  1101. case FOURCC_HEV1:
  1102. case FOURCC_HVC1:
  1103. #if BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  1104. case FOURCC_DVH1:
  1105. case FOURCC_DVHE:
  1106. #endif // BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  1107. #endif // BUILDFLAG(ENABLE_PLATFORM_HEVC)
  1108. #if BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  1109. case FOURCC_DVA1:
  1110. case FOURCC_DVAV:
  1111. #endif // BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  1112. #endif // BUILDFLAG(USE_PROPRIETARY_CODECS)
  1113. case FOURCC_VP09:
  1114. return true;
  1115. #if BUILDFLAG(ENABLE_AV1_DECODER)
  1116. case FOURCC_AV01:
  1117. return true;
  1118. #endif
  1119. default:
  1120. return false;
  1121. }
  1122. }
  1123. ElementaryStreamDescriptor::ElementaryStreamDescriptor()
  1124. : object_type(kForbidden) {}
  1125. ElementaryStreamDescriptor::ElementaryStreamDescriptor(
  1126. const ElementaryStreamDescriptor& other) = default;
  1127. ElementaryStreamDescriptor::~ElementaryStreamDescriptor() = default;
  1128. FourCC ElementaryStreamDescriptor::BoxType() const {
  1129. return FOURCC_ESDS;
  1130. }
  1131. bool ElementaryStreamDescriptor::Parse(BoxReader* reader) {
  1132. std::vector<uint8_t> data;
  1133. ESDescriptor es_desc;
  1134. RCHECK(reader->ReadFullBoxHeader());
  1135. RCHECK(reader->ReadVec(&data, reader->box_size() - reader->pos()));
  1136. RCHECK(es_desc.Parse(data));
  1137. object_type = es_desc.object_type();
  1138. if (es_desc.IsAAC(object_type)) {
  1139. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  1140. RCHECK(aac.Parse(es_desc.decoder_specific_info(), reader->media_log()));
  1141. #else
  1142. return false;
  1143. #endif
  1144. }
  1145. return true;
  1146. }
  1147. FlacSpecificBox::FlacSpecificBox()
  1148. : sample_rate(0), channel_count(0), bits_per_sample(0) {}
  1149. FlacSpecificBox::FlacSpecificBox(const FlacSpecificBox& other) = default;
  1150. FlacSpecificBox::~FlacSpecificBox() = default;
  1151. FourCC FlacSpecificBox::BoxType() const {
  1152. return FOURCC_DFLA;
  1153. }
  1154. bool FlacSpecificBox::Parse(BoxReader* reader) {
  1155. RCHECK(reader->ReadFullBoxHeader());
  1156. RCHECK_MEDIA_LOGGED(reader->version() == 0, reader->media_log(),
  1157. "Only version 0 FLACSpecificBox (dfLa) is supported.");
  1158. RCHECK_MEDIA_LOGGED(reader->flags() == 0, reader->media_log(),
  1159. "Only 0 flags in FLACSpecificBox (dfLa) is supported.");
  1160. // From https://github.com/xiph/flac/blob/master/doc/isoflac.txt, a
  1161. // FLACMetadataBlock is formatted as:
  1162. // unsigned int(1) LastMetadataBlockFlag;
  1163. // unsigned int(7) BlockType;
  1164. // unsigned int(24) Length;
  1165. // unsigned int(8) BlockData[Length];
  1166. // We only care about the first block, which must exist, and must be
  1167. // STREAMINFO.
  1168. uint32_t metadata_framing;
  1169. RCHECK_MEDIA_LOGGED(reader->Read4(&metadata_framing), reader->media_log(),
  1170. "Missing STREAMINFO block in FLACSpecificBox (dfLa).");
  1171. uint8_t block_type = (metadata_framing >> 24) & 0x7f;
  1172. RCHECK_MEDIA_LOGGED(block_type == 0, reader->media_log(),
  1173. "FLACSpecificBox metadata must begin with STREAMINFO.");
  1174. uint32_t block_length = metadata_framing & 0x00ffffff;
  1175. RCHECK_MEDIA_LOGGED(
  1176. block_length == kFlacMetadataBlockStreaminfoSize, reader->media_log(),
  1177. "STREAMINFO block in FLACSpecificBox (dfLa) has incorrect size.");
  1178. // See https://xiph.org/flac/format.html#metadata_block_streaminfo for
  1179. // STREAMINFO structure format and semantics. We only care about
  1180. // |sample_rate|, |channel_count|, and |bits_per_sample|,
  1181. // though we also copy the STREAMINFO block for use later in audio decoder
  1182. // configuration. See also the FLAC AudioSampleEntry logic: the |sample_rate|
  1183. // here is used instead of that in the AudioSampleEntry per
  1184. // https://github.com/xiph/flac/blob/master/doc/isoflac.txt.
  1185. RCHECK(reader->ReadVec(&stream_info, kFlacMetadataBlockStreaminfoSize));
  1186. // Bytes 0-9 (min/max block and frame sizes) are ignored here.
  1187. sample_rate = stream_info[10] << 12;
  1188. sample_rate += stream_info[11] << 4;
  1189. sample_rate += (stream_info[12] >> 4) & 0xf;
  1190. RCHECK_MEDIA_LOGGED(sample_rate > 0, reader->media_log(),
  1191. "STREAMINFO block in FLACSpecificBox (dfLa) must have "
  1192. "nonzero sample rate.");
  1193. channel_count = (stream_info[12] >> 1) & 0x7;
  1194. channel_count++;
  1195. bits_per_sample = (stream_info[12] & 1) << 4;
  1196. bits_per_sample += (stream_info[13] >> 4) & 0xf;
  1197. bits_per_sample++;
  1198. // The lower 4 bits of byte 13 and all of bytes 14-17 (number of samples in
  1199. // stream) are ignored here.
  1200. // Bytes 18-33 (hash of the unencoded audio data) are ignored here.
  1201. return true;
  1202. }
  1203. OpusSpecificBox::OpusSpecificBox()
  1204. : seek_preroll(base::Milliseconds(80)), codec_delay_in_frames(0) {}
  1205. OpusSpecificBox::OpusSpecificBox(const OpusSpecificBox& other) = default;
  1206. OpusSpecificBox::~OpusSpecificBox() = default;
  1207. FourCC OpusSpecificBox::BoxType() const {
  1208. return FOURCC_DOPS;
  1209. }
  1210. bool OpusSpecificBox::Parse(BoxReader* reader) {
  1211. // Extradata must start with "OpusHead" magic.
  1212. extradata.insert(extradata.end(),
  1213. {0x4f, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64});
  1214. // The opus specific box must be present and at least OPUS_EXTRADATA_SIZE - 8
  1215. // bytes in length. The -8 is for the missing "OpusHead" magic signature that
  1216. // is required at the start of the extradata we give to the codec.
  1217. const size_t headerless_extradata_size = reader->box_size() - reader->pos();
  1218. RCHECK(headerless_extradata_size >= OPUS_EXTRADATA_SIZE - extradata.size());
  1219. extradata.resize(extradata.size() + headerless_extradata_size);
  1220. int16_t gain_db;
  1221. RCHECK(reader->Read1(&extradata[OPUS_EXTRADATA_VERSION_OFFSET]));
  1222. RCHECK(reader->Read1(&extradata[OPUS_EXTRADATA_CHANNELS_OFFSET]));
  1223. RCHECK(reader->Read2(&codec_delay_in_frames /* PreSkip */));
  1224. RCHECK(reader->Read4(&sample_rate));
  1225. RCHECK(reader->Read2s(&gain_db));
  1226. #if !defined(ARCH_CPU_LITTLE_ENDIAN)
  1227. #error The code below assumes little-endianness.
  1228. #endif
  1229. memcpy(&extradata[OPUS_EXTRADATA_SKIP_SAMPLES_OFFSET], &codec_delay_in_frames,
  1230. sizeof(codec_delay_in_frames));
  1231. memcpy(&extradata[OPUS_EXTRADATA_SAMPLE_RATE_OFFSET], &sample_rate,
  1232. sizeof(sample_rate));
  1233. memcpy(&extradata[OPUS_EXTRADATA_GAIN_OFFSET], &gain_db, sizeof(gain_db));
  1234. channel_count = extradata[OPUS_EXTRADATA_CHANNELS_OFFSET];
  1235. // Any remaining data is 1-byte data, so copy it over as is, there should
  1236. // only be a handful of these entries, so reading byte by byte is okay.
  1237. for (size_t i = OPUS_EXTRADATA_CHANNEL_MAPPING_OFFSET; i < extradata.size();
  1238. ++i) {
  1239. RCHECK(reader->Read1(&extradata[i]));
  1240. }
  1241. return true;
  1242. }
  1243. #if BUILDFLAG(USE_PROPRIETARY_CODECS) && BUILDFLAG(ENABLE_PLATFORM_DTS_AUDIO)
  1244. DtsSpecificBox::DtsSpecificBox() {}
  1245. DtsSpecificBox::DtsSpecificBox(const DtsSpecificBox& other) = default;
  1246. DtsSpecificBox::~DtsSpecificBox() = default;
  1247. FourCC DtsSpecificBox::BoxType() const {
  1248. return FOURCC_DDTS;
  1249. }
  1250. bool DtsSpecificBox::Parse(BoxReader* reader) {
  1251. // Read ddts into buffer.
  1252. std::vector<uint8_t> dts_data;
  1253. RCHECK(reader->ReadVec(&dts_data, reader->box_size() - reader->pos()));
  1254. RCHECK(dts.Parse(dts_data, reader->media_log()));
  1255. return true;
  1256. }
  1257. DtsUhdSpecificBox::DtsUhdSpecificBox() {}
  1258. DtsUhdSpecificBox::DtsUhdSpecificBox(const DtsUhdSpecificBox& other) = default;
  1259. DtsUhdSpecificBox::~DtsUhdSpecificBox() = default;
  1260. FourCC DtsUhdSpecificBox::BoxType() const {
  1261. return FOURCC_UDTS;
  1262. }
  1263. bool DtsUhdSpecificBox::Parse(BoxReader* reader) {
  1264. // Read udts into buffer.
  1265. std::vector<uint8_t> dtsx_data;
  1266. RCHECK(reader->ReadVec(&dtsx_data, reader->box_size() - reader->pos()));
  1267. RCHECK(dtsx.Parse(dtsx_data, reader->media_log()));
  1268. return true;
  1269. }
  1270. #endif // BUILDFLAG(USE_PROPRIETARY_CODECS) &&
  1271. // BUILDFLAG(ENABLE_PLATFORM_DTS_AUDIO)
  1272. AudioSampleEntry::AudioSampleEntry()
  1273. : format(FOURCC_NULL),
  1274. data_reference_index(0),
  1275. channelcount(0),
  1276. samplesize(0),
  1277. samplerate(0) {}
  1278. AudioSampleEntry::AudioSampleEntry(const AudioSampleEntry& other) = default;
  1279. AudioSampleEntry::~AudioSampleEntry() = default;
  1280. FourCC AudioSampleEntry::BoxType() const {
  1281. DCHECK(false) << "AudioSampleEntry should be parsed according to the "
  1282. << "handler type recovered in its Media ancestor.";
  1283. return FOURCC_NULL;
  1284. }
  1285. bool AudioSampleEntry::Parse(BoxReader* reader) {
  1286. format = reader->type();
  1287. RCHECK(reader->SkipBytes(6) &&
  1288. reader->Read2(&data_reference_index) &&
  1289. reader->SkipBytes(8) &&
  1290. reader->Read2(&channelcount) &&
  1291. reader->Read2(&samplesize) &&
  1292. reader->SkipBytes(4) &&
  1293. reader->Read4(&samplerate));
  1294. // Convert from 16.16 fixed point to integer
  1295. samplerate >>= 16;
  1296. RCHECK(reader->ScanChildren());
  1297. if (format == FOURCC_ENCA) {
  1298. // Continue scanning until a recognized protection scheme is found, or until
  1299. // we run out of protection schemes.
  1300. while (!sinf.HasSupportedScheme()) {
  1301. if (!reader->ReadChild(&sinf))
  1302. return false;
  1303. }
  1304. }
  1305. if (format == FOURCC_OPUS ||
  1306. (format == FOURCC_ENCA && sinf.format.format == FOURCC_OPUS)) {
  1307. RCHECK_MEDIA_LOGGED(reader->ReadChild(&dops), reader->media_log(),
  1308. "Failure parsing OpusSpecificBox (dOps)");
  1309. RCHECK_MEDIA_LOGGED(channelcount == dops.channel_count, reader->media_log(),
  1310. "Opus AudioSampleEntry channel count mismatches "
  1311. "OpusSpecificBox STREAMINFO channel count");
  1312. RCHECK_MEDIA_LOGGED(samplerate == dops.sample_rate, reader->media_log(),
  1313. "Opus AudioSampleEntry sample rate mismatches "
  1314. "OpusSpecificBox STREAMINFO channel count");
  1315. }
  1316. #if BUILDFLAG(USE_PROPRIETARY_CODECS) && BUILDFLAG(ENABLE_PLATFORM_DTS_AUDIO)
  1317. if (format == FOURCC_DTSC) {
  1318. RCHECK_MEDIA_LOGGED(reader->ReadChild(&ddts), reader->media_log(),
  1319. "Failure parsing DtsSpecificBox (ddts)");
  1320. } else if (format == FOURCC_DTSX) {
  1321. RCHECK_MEDIA_LOGGED(reader->ReadChild(&udts), reader->media_log(),
  1322. "Failure parsing DtsUhdSpecificBox (udts)");
  1323. }
  1324. #endif // BUILDFLAG(USE_PROPRIETARY_CODECS) &&
  1325. // BUILDFLAG(ENABLE_PLATFORM_DTS_AUDIO)
  1326. // Read the FLACSpecificBox, even if CENC is signalled.
  1327. if (format == FOURCC_FLAC ||
  1328. (format == FOURCC_ENCA && sinf.format.format == FOURCC_FLAC)) {
  1329. RCHECK_MEDIA_LOGGED(reader->ReadChild(&dfla), reader->media_log(),
  1330. "Failure parsing FLACSpecificBox (dfLa)");
  1331. // AudioSampleEntry is constrained to max 65535Hz. Instead, use the sample
  1332. // rate from the FlacSpecificBox per
  1333. // https://github.com/xiph/flac/blob/master/doc/isoflac.txt
  1334. if (samplerate != dfla.sample_rate) {
  1335. MEDIA_LOG(INFO, reader->media_log())
  1336. << "FLAC AudioSampleEntry sample rate " << samplerate
  1337. << " overridden by rate " << dfla.sample_rate
  1338. << " from FLACSpecificBox's STREAMINFO metadata";
  1339. samplerate = dfla.sample_rate;
  1340. }
  1341. RCHECK_MEDIA_LOGGED(channelcount == dfla.channel_count, reader->media_log(),
  1342. "FLAC AudioSampleEntry channel count mismatches "
  1343. "FLACSpecificBox STREAMINFO channel count");
  1344. RCHECK_MEDIA_LOGGED(samplesize == dfla.bits_per_sample, reader->media_log(),
  1345. "FLAC AudioSampleEntry sample size mismatches "
  1346. "FLACSpecificBox STREAMINFO sample size");
  1347. } else {
  1348. RCHECK_MEDIA_LOGGED(!reader->HasChild(&dfla), reader->media_log(),
  1349. "FLACSpecificBox (dfLa) must only be used with FLAC "
  1350. "AudioSampleEntry or CENC AudioSampleEntry wrapping "
  1351. "FLAC");
  1352. }
  1353. // ESDS is not valid in case of EAC3.
  1354. RCHECK(reader->MaybeReadChild(&esds));
  1355. return true;
  1356. }
  1357. MediaHeader::MediaHeader()
  1358. : creation_time(0),
  1359. modification_time(0),
  1360. timescale(0),
  1361. duration(0),
  1362. language_code(0) {}
  1363. MediaHeader::MediaHeader(const MediaHeader& other) = default;
  1364. MediaHeader::~MediaHeader() = default;
  1365. FourCC MediaHeader::BoxType() const { return FOURCC_MDHD; }
  1366. bool MediaHeader::Parse(BoxReader* reader) {
  1367. RCHECK(reader->ReadFullBoxHeader());
  1368. if (reader->version() == 1) {
  1369. RCHECK(reader->Read8(&creation_time) && reader->Read8(&modification_time) &&
  1370. reader->Read4(&timescale) && reader->Read8(&duration) &&
  1371. reader->Read2(&language_code));
  1372. } else {
  1373. RCHECK(reader->Read4Into8(&creation_time) &&
  1374. reader->Read4Into8(&modification_time) &&
  1375. reader->Read4(&timescale) && reader->Read4Into8(&duration) &&
  1376. reader->Read2(&language_code));
  1377. }
  1378. RCHECK_MEDIA_LOGGED(timescale > 0, reader->media_log(),
  1379. "Track media header's timescale must not be 0");
  1380. // ISO 639-2/T language code only uses 15 lower bits, so reset the 16th bit.
  1381. language_code &= 0x7fff;
  1382. // Skip playback quality information
  1383. return reader->SkipBytes(2);
  1384. }
  1385. std::string MediaHeader::language() const {
  1386. if (language_code == 0x7fff || language_code < 0x400) {
  1387. return "und";
  1388. }
  1389. char lang_chars[4];
  1390. lang_chars[3] = 0;
  1391. lang_chars[2] = 0x60 + (language_code & 0x1f);
  1392. lang_chars[1] = 0x60 + ((language_code >> 5) & 0x1f);
  1393. lang_chars[0] = 0x60 + ((language_code >> 10) & 0x1f);
  1394. if (lang_chars[0] < 'a' || lang_chars[0] > 'z' || lang_chars[1] < 'a' ||
  1395. lang_chars[1] > 'z' || lang_chars[2] < 'a' || lang_chars[2] > 'z') {
  1396. // Got unexpected characteds in ISO 639-2/T language code. Something must be
  1397. // wrong with the input file, report 'und' language to be safe.
  1398. DVLOG(2) << "Ignoring MDHD language_code (non ISO 639-2 compliant): "
  1399. << lang_chars;
  1400. lang_chars[0] = 'u';
  1401. lang_chars[1] = 'n';
  1402. lang_chars[2] = 'd';
  1403. }
  1404. return lang_chars;
  1405. }
  1406. MediaInformation::MediaInformation() = default;
  1407. MediaInformation::MediaInformation(const MediaInformation& other) = default;
  1408. MediaInformation::~MediaInformation() = default;
  1409. FourCC MediaInformation::BoxType() const { return FOURCC_MINF; }
  1410. bool MediaInformation::Parse(BoxReader* reader) {
  1411. return reader->ScanChildren() &&
  1412. reader->ReadChild(&sample_table);
  1413. }
  1414. Media::Media() = default;
  1415. Media::Media(const Media& other) = default;
  1416. Media::~Media() = default;
  1417. FourCC Media::BoxType() const { return FOURCC_MDIA; }
  1418. bool Media::Parse(BoxReader* reader) {
  1419. RCHECK(reader->ScanChildren() &&
  1420. reader->ReadChild(&header) &&
  1421. reader->ReadChild(&handler));
  1422. // Maddeningly, the HandlerReference box specifies how to parse the
  1423. // SampleDescription box, making the latter the only box (of those that we
  1424. // support) which cannot be parsed correctly on its own (or even with
  1425. // information from its strict ancestor tree). We thus copy the handler type
  1426. // to the sample description box *before* parsing it to provide this
  1427. // information while parsing.
  1428. information.sample_table.description.type = handler.type;
  1429. RCHECK(reader->ReadChild(&information));
  1430. return true;
  1431. }
  1432. Track::Track() = default;
  1433. Track::Track(const Track& other) = default;
  1434. Track::~Track() = default;
  1435. FourCC Track::BoxType() const { return FOURCC_TRAK; }
  1436. bool Track::Parse(BoxReader* reader) {
  1437. RCHECK(reader->ScanChildren() &&
  1438. reader->ReadChild(&header) &&
  1439. reader->ReadChild(&media) &&
  1440. reader->MaybeReadChild(&edit));
  1441. return true;
  1442. }
  1443. MovieExtendsHeader::MovieExtendsHeader() : fragment_duration(0) {}
  1444. MovieExtendsHeader::MovieExtendsHeader(const MovieExtendsHeader& other) =
  1445. default;
  1446. MovieExtendsHeader::~MovieExtendsHeader() = default;
  1447. FourCC MovieExtendsHeader::BoxType() const { return FOURCC_MEHD; }
  1448. bool MovieExtendsHeader::Parse(BoxReader* reader) {
  1449. RCHECK(reader->ReadFullBoxHeader());
  1450. if (reader->version() == 1) {
  1451. RCHECK(reader->Read8(&fragment_duration));
  1452. } else {
  1453. RCHECK(reader->Read4Into8(&fragment_duration));
  1454. }
  1455. return true;
  1456. }
  1457. TrackExtends::TrackExtends()
  1458. : track_id(0),
  1459. default_sample_description_index(0),
  1460. default_sample_duration(0),
  1461. default_sample_size(0),
  1462. default_sample_flags(0) {}
  1463. TrackExtends::TrackExtends(const TrackExtends& other) = default;
  1464. TrackExtends::~TrackExtends() = default;
  1465. FourCC TrackExtends::BoxType() const { return FOURCC_TREX; }
  1466. bool TrackExtends::Parse(BoxReader* reader) {
  1467. RCHECK(reader->ReadFullBoxHeader() &&
  1468. reader->Read4(&track_id) &&
  1469. reader->Read4(&default_sample_description_index) &&
  1470. reader->Read4(&default_sample_duration) &&
  1471. reader->Read4(&default_sample_size) &&
  1472. reader->Read4(&default_sample_flags));
  1473. return true;
  1474. }
  1475. MovieExtends::MovieExtends() = default;
  1476. MovieExtends::MovieExtends(const MovieExtends& other) = default;
  1477. MovieExtends::~MovieExtends() = default;
  1478. FourCC MovieExtends::BoxType() const { return FOURCC_MVEX; }
  1479. bool MovieExtends::Parse(BoxReader* reader) {
  1480. header.fragment_duration = 0;
  1481. return reader->ScanChildren() &&
  1482. reader->MaybeReadChild(&header) &&
  1483. reader->ReadChildren(&tracks);
  1484. }
  1485. Movie::Movie() : fragmented(false) {}
  1486. Movie::Movie(const Movie& other) = default;
  1487. Movie::~Movie() = default;
  1488. FourCC Movie::BoxType() const { return FOURCC_MOOV; }
  1489. bool Movie::Parse(BoxReader* reader) {
  1490. RCHECK(reader->ScanChildren() && reader->ReadChild(&header) &&
  1491. reader->ReadChildren(&tracks));
  1492. RCHECK_MEDIA_LOGGED(reader->ReadChild(&extends), reader->media_log(),
  1493. "Detected unfragmented MP4. Media Source Extensions "
  1494. "require ISO BMFF moov to contain mvex to indicate that "
  1495. "Movie Fragments are to be expected.");
  1496. return reader->MaybeReadChildren(&pssh);
  1497. }
  1498. TrackFragmentDecodeTime::TrackFragmentDecodeTime() : decode_time(0) {}
  1499. TrackFragmentDecodeTime::TrackFragmentDecodeTime(
  1500. const TrackFragmentDecodeTime& other) = default;
  1501. TrackFragmentDecodeTime::~TrackFragmentDecodeTime() = default;
  1502. FourCC TrackFragmentDecodeTime::BoxType() const { return FOURCC_TFDT; }
  1503. bool TrackFragmentDecodeTime::Parse(BoxReader* reader) {
  1504. RCHECK(reader->ReadFullBoxHeader());
  1505. if (reader->version() == 1)
  1506. return reader->Read8(&decode_time);
  1507. else
  1508. return reader->Read4Into8(&decode_time);
  1509. }
  1510. MovieFragmentHeader::MovieFragmentHeader() : sequence_number(0) {}
  1511. MovieFragmentHeader::MovieFragmentHeader(const MovieFragmentHeader& other) =
  1512. default;
  1513. MovieFragmentHeader::~MovieFragmentHeader() = default;
  1514. FourCC MovieFragmentHeader::BoxType() const { return FOURCC_MFHD; }
  1515. bool MovieFragmentHeader::Parse(BoxReader* reader) {
  1516. return reader->SkipBytes(4) && reader->Read4(&sequence_number);
  1517. }
  1518. TrackFragmentHeader::TrackFragmentHeader()
  1519. : track_id(0),
  1520. sample_description_index(0),
  1521. default_sample_duration(0),
  1522. default_sample_size(0),
  1523. default_sample_flags(0),
  1524. has_default_sample_flags(false) {}
  1525. TrackFragmentHeader::TrackFragmentHeader(const TrackFragmentHeader& other) =
  1526. default;
  1527. TrackFragmentHeader::~TrackFragmentHeader() = default;
  1528. FourCC TrackFragmentHeader::BoxType() const { return FOURCC_TFHD; }
  1529. bool TrackFragmentHeader::Parse(BoxReader* reader) {
  1530. RCHECK(reader->ReadFullBoxHeader() && reader->Read4(&track_id));
  1531. // Media Source specific: reject tracks that set 'base-data-offset-present'.
  1532. // Although the Media Source requires that 'default-base-is-moof' (14496-12
  1533. // Amendment 2) be set, we omit this check as many otherwise-valid files in
  1534. // the wild don't set it.
  1535. //
  1536. // RCHECK((flags & 0x020000) && !(flags & 0x1));
  1537. RCHECK_MEDIA_LOGGED(!(reader->flags() & 0x1), reader->media_log(),
  1538. "TFHD base-data-offset not allowed by MSE. See "
  1539. "https://www.w3.org/TR/mse-byte-stream-format-isobmff/"
  1540. "#movie-fragment-relative-addressing");
  1541. if (reader->flags() & 0x2) {
  1542. RCHECK(reader->Read4(&sample_description_index));
  1543. } else {
  1544. sample_description_index = 0;
  1545. }
  1546. if (reader->flags() & 0x8) {
  1547. RCHECK(reader->Read4(&default_sample_duration));
  1548. } else {
  1549. default_sample_duration = 0;
  1550. }
  1551. if (reader->flags() & 0x10) {
  1552. RCHECK(reader->Read4(&default_sample_size));
  1553. } else {
  1554. default_sample_size = 0;
  1555. }
  1556. if (reader->flags() & 0x20) {
  1557. RCHECK(reader->Read4(&default_sample_flags));
  1558. has_default_sample_flags = true;
  1559. } else {
  1560. has_default_sample_flags = false;
  1561. }
  1562. return true;
  1563. }
  1564. TrackFragmentRun::TrackFragmentRun()
  1565. : sample_count(0), data_offset(0) {}
  1566. TrackFragmentRun::TrackFragmentRun(const TrackFragmentRun& other) = default;
  1567. TrackFragmentRun::~TrackFragmentRun() = default;
  1568. FourCC TrackFragmentRun::BoxType() const { return FOURCC_TRUN; }
  1569. bool TrackFragmentRun::Parse(BoxReader* reader) {
  1570. RCHECK(reader->ReadFullBoxHeader() &&
  1571. reader->Read4(&sample_count));
  1572. const uint32_t flags = reader->flags();
  1573. bool data_offset_present = (flags & 0x1) != 0;
  1574. bool first_sample_flags_present = (flags & 0x4) != 0;
  1575. bool sample_duration_present = (flags & 0x100) != 0;
  1576. bool sample_size_present = (flags & 0x200) != 0;
  1577. bool sample_flags_present = (flags & 0x400) != 0;
  1578. bool sample_composition_time_offsets_present = (flags & 0x800) != 0;
  1579. if (data_offset_present) {
  1580. RCHECK(reader->Read4(&data_offset));
  1581. } else {
  1582. data_offset = 0;
  1583. }
  1584. uint32_t first_sample_flags = 0;
  1585. if (first_sample_flags_present)
  1586. RCHECK(reader->Read4(&first_sample_flags));
  1587. int fields = sample_duration_present + sample_size_present +
  1588. sample_flags_present + sample_composition_time_offsets_present;
  1589. const size_t bytes_per_field = 4;
  1590. // Cast |sample_count| to size_t before multiplying to support maximum
  1591. // platform size.
  1592. base::CheckedNumeric<size_t> bytes_needed = base::CheckMul(
  1593. fields, bytes_per_field, static_cast<size_t>(sample_count));
  1594. RCHECK_MEDIA_LOGGED(
  1595. bytes_needed.IsValid(), reader->media_log(),
  1596. "Extreme TRUN sample count exceeds implementation limit.");
  1597. RCHECK(reader->HasBytes(bytes_needed.ValueOrDie()));
  1598. if (sample_duration_present) {
  1599. RCHECK(sample_count <= sample_durations.max_size());
  1600. sample_durations.resize(sample_count);
  1601. }
  1602. if (sample_size_present) {
  1603. RCHECK(sample_count <= sample_sizes.max_size());
  1604. sample_sizes.resize(sample_count);
  1605. }
  1606. if (sample_flags_present) {
  1607. RCHECK(sample_count <= sample_flags.max_size());
  1608. sample_flags.resize(sample_count);
  1609. }
  1610. if (sample_composition_time_offsets_present) {
  1611. RCHECK(sample_count <= sample_composition_time_offsets.max_size());
  1612. sample_composition_time_offsets.resize(sample_count);
  1613. }
  1614. if (sample_duration_present || sample_size_present || sample_flags_present ||
  1615. sample_composition_time_offsets_present) {
  1616. for (uint32_t i = 0; i < sample_count; ++i) {
  1617. if (sample_duration_present)
  1618. RCHECK(reader->Read4(&sample_durations[i]));
  1619. if (sample_size_present)
  1620. RCHECK(reader->Read4(&sample_sizes[i]));
  1621. if (sample_flags_present)
  1622. RCHECK(reader->Read4(&sample_flags[i]));
  1623. if (sample_composition_time_offsets_present)
  1624. RCHECK(reader->Read4s(&sample_composition_time_offsets[i]));
  1625. }
  1626. }
  1627. if (first_sample_flags_present) {
  1628. if (sample_flags.size() == 0) {
  1629. sample_flags.push_back(first_sample_flags);
  1630. } else {
  1631. sample_flags[0] = first_sample_flags;
  1632. }
  1633. }
  1634. return true;
  1635. }
  1636. SampleToGroup::SampleToGroup() : grouping_type(0), grouping_type_parameter(0) {}
  1637. SampleToGroup::SampleToGroup(const SampleToGroup& other) = default;
  1638. SampleToGroup::~SampleToGroup() = default;
  1639. FourCC SampleToGroup::BoxType() const { return FOURCC_SBGP; }
  1640. bool SampleToGroup::Parse(BoxReader* reader) {
  1641. RCHECK(reader->ReadFullBoxHeader() &&
  1642. reader->Read4(&grouping_type));
  1643. if (reader->version() == 1)
  1644. RCHECK(reader->Read4(&grouping_type_parameter));
  1645. if (grouping_type != FOURCC_SEIG) {
  1646. DLOG(WARNING) << "SampleToGroup box with grouping_type '" << grouping_type
  1647. << "' is not supported.";
  1648. return true;
  1649. }
  1650. uint32_t count;
  1651. RCHECK(reader->Read4(&count));
  1652. const size_t bytes_per_entry = 8;
  1653. // Cast |count| to size_t before multiplying to support maximum platform size.
  1654. base::CheckedNumeric<size_t> bytes_needed =
  1655. base::CheckMul(bytes_per_entry, static_cast<size_t>(count));
  1656. RCHECK_MEDIA_LOGGED(bytes_needed.IsValid(), reader->media_log(),
  1657. "Extreme SBGP count exceeds implementation limit.");
  1658. RCHECK(reader->HasBytes(bytes_needed.ValueOrDie()));
  1659. RCHECK(count <= entries.max_size());
  1660. entries.resize(count);
  1661. for (uint32_t i = 0; i < count; ++i) {
  1662. RCHECK(reader->Read4(&entries[i].sample_count) &&
  1663. reader->Read4(&entries[i].group_description_index));
  1664. }
  1665. return true;
  1666. }
  1667. CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry()
  1668. : is_encrypted(false),
  1669. iv_size(0),
  1670. crypt_byte_block(0),
  1671. skip_byte_block(0),
  1672. constant_iv_size(0) {}
  1673. CencSampleEncryptionInfoEntry::CencSampleEncryptionInfoEntry(
  1674. const CencSampleEncryptionInfoEntry& other) = default;
  1675. CencSampleEncryptionInfoEntry::~CencSampleEncryptionInfoEntry() = default;
  1676. bool CencSampleEncryptionInfoEntry::Parse(BoxReader* reader) {
  1677. uint8_t flag;
  1678. uint8_t possible_pattern_info;
  1679. RCHECK(reader->SkipBytes(1) && // reserved.
  1680. reader->Read1(&possible_pattern_info) && reader->Read1(&flag) &&
  1681. reader->Read1(&iv_size) && reader->ReadVec(&key_id, kKeyIdSize));
  1682. is_encrypted = (flag != 0);
  1683. if (is_encrypted) {
  1684. crypt_byte_block = (possible_pattern_info >> 4) & 0x0f;
  1685. skip_byte_block = possible_pattern_info & 0x0f;
  1686. if (iv_size == 0) {
  1687. RCHECK(reader->Read1(&constant_iv_size));
  1688. RCHECK(constant_iv_size == 8 || constant_iv_size == 16);
  1689. memset(constant_iv, 0, sizeof(constant_iv));
  1690. for (uint8_t i = 0; i < constant_iv_size; i++)
  1691. RCHECK(reader->Read1(constant_iv + i));
  1692. } else {
  1693. RCHECK(iv_size == 8 || iv_size == 16);
  1694. }
  1695. } else {
  1696. RCHECK(iv_size == 0);
  1697. }
  1698. return true;
  1699. }
  1700. SampleGroupDescription::SampleGroupDescription() : grouping_type(0) {}
  1701. SampleGroupDescription::SampleGroupDescription(
  1702. const SampleGroupDescription& other) = default;
  1703. SampleGroupDescription::~SampleGroupDescription() = default;
  1704. FourCC SampleGroupDescription::BoxType() const { return FOURCC_SGPD; }
  1705. bool SampleGroupDescription::Parse(BoxReader* reader) {
  1706. RCHECK(reader->ReadFullBoxHeader() &&
  1707. reader->Read4(&grouping_type));
  1708. if (grouping_type != FOURCC_SEIG) {
  1709. DLOG(WARNING) << "SampleGroupDescription box with grouping_type '"
  1710. << grouping_type << "' is not supported.";
  1711. return true;
  1712. }
  1713. const uint8_t version = reader->version();
  1714. const size_t kEntrySize = sizeof(uint32_t) + kKeyIdSize;
  1715. uint32_t default_length = 0;
  1716. if (version == 1) {
  1717. RCHECK(reader->Read4(&default_length));
  1718. RCHECK(default_length == 0 || default_length >= kEntrySize);
  1719. }
  1720. uint32_t count;
  1721. RCHECK(reader->Read4(&count));
  1722. // Check that we have at least two bytes for each entry before allocating a
  1723. // potentially huge entries vector. In reality each entry will require a
  1724. // variable number of bytes in excess of 2.
  1725. const int bytes_per_entry = 2;
  1726. // Cast |count| to size_t before multiplying to support maximum platform size.
  1727. base::CheckedNumeric<size_t> bytes_needed =
  1728. base::CheckMul(bytes_per_entry, static_cast<size_t>(count));
  1729. RCHECK_MEDIA_LOGGED(bytes_needed.IsValid(), reader->media_log(),
  1730. "Extreme SGPD count exceeds implementation limit.");
  1731. RCHECK(reader->HasBytes(bytes_needed.ValueOrDie()));
  1732. RCHECK(count <= entries.max_size());
  1733. entries.resize(count);
  1734. for (uint32_t i = 0; i < count; ++i) {
  1735. if (version == 1) {
  1736. if (default_length == 0) {
  1737. uint32_t description_length = 0;
  1738. RCHECK(reader->Read4(&description_length));
  1739. RCHECK(description_length >= kEntrySize);
  1740. }
  1741. }
  1742. RCHECK(entries[i].Parse(reader));
  1743. }
  1744. return true;
  1745. }
  1746. TrackFragment::TrackFragment() = default;
  1747. TrackFragment::TrackFragment(const TrackFragment& other) = default;
  1748. TrackFragment::~TrackFragment() = default;
  1749. FourCC TrackFragment::BoxType() const { return FOURCC_TRAF; }
  1750. bool TrackFragment::Parse(BoxReader* reader) {
  1751. RCHECK(reader->ScanChildren() && reader->ReadChild(&header) &&
  1752. // Media Source specific: 'tfdt' required
  1753. reader->ReadChild(&decode_time) && reader->MaybeReadChildren(&runs) &&
  1754. reader->MaybeReadChild(&auxiliary_offset) &&
  1755. reader->MaybeReadChild(&auxiliary_size) &&
  1756. reader->MaybeReadChild(&sdtp) &&
  1757. reader->MaybeReadChild(&sample_encryption));
  1758. // There could be multiple SampleGroupDescription and SampleToGroup boxes with
  1759. // different grouping types. For common encryption, the relevant grouping type
  1760. // is 'seig'. Continue reading until 'seig' is found, or until running out of
  1761. // child boxes.
  1762. while (reader->HasChild(&sample_group_description)) {
  1763. RCHECK(reader->ReadChild(&sample_group_description));
  1764. if (sample_group_description.grouping_type == FOURCC_SEIG)
  1765. break;
  1766. sample_group_description.entries.clear();
  1767. }
  1768. while (reader->HasChild(&sample_to_group)) {
  1769. RCHECK(reader->ReadChild(&sample_to_group));
  1770. if (sample_to_group.grouping_type == FOURCC_SEIG)
  1771. break;
  1772. sample_to_group.entries.clear();
  1773. }
  1774. return true;
  1775. }
  1776. MovieFragment::MovieFragment() = default;
  1777. MovieFragment::MovieFragment(const MovieFragment& other) = default;
  1778. MovieFragment::~MovieFragment() = default;
  1779. FourCC MovieFragment::BoxType() const { return FOURCC_MOOF; }
  1780. bool MovieFragment::Parse(BoxReader* reader) {
  1781. RCHECK(reader->ScanChildren() &&
  1782. reader->ReadChild(&header) &&
  1783. reader->ReadChildren(&tracks) &&
  1784. reader->MaybeReadChildren(&pssh));
  1785. return true;
  1786. }
  1787. IndependentAndDisposableSamples::IndependentAndDisposableSamples() = default;
  1788. IndependentAndDisposableSamples::IndependentAndDisposableSamples(
  1789. const IndependentAndDisposableSamples& other) = default;
  1790. IndependentAndDisposableSamples::~IndependentAndDisposableSamples() = default;
  1791. FourCC IndependentAndDisposableSamples::BoxType() const { return FOURCC_SDTP; }
  1792. bool IndependentAndDisposableSamples::Parse(BoxReader* reader) {
  1793. RCHECK(reader->ReadFullBoxHeader());
  1794. RCHECK(reader->version() == 0);
  1795. RCHECK(reader->flags() == 0);
  1796. size_t sample_count = reader->box_size() - reader->pos();
  1797. RCHECK(sample_count <= sample_depends_on_.max_size());
  1798. sample_depends_on_.resize(sample_count);
  1799. for (size_t i = 0; i < sample_count; ++i) {
  1800. uint8_t sample_info;
  1801. RCHECK(reader->Read1(&sample_info));
  1802. sample_depends_on_[i] =
  1803. static_cast<SampleDependsOn>((sample_info >> 4) & 0x3);
  1804. RCHECK(sample_depends_on_[i] != kSampleDependsOnReserved);
  1805. }
  1806. return true;
  1807. }
  1808. SampleDependsOn IndependentAndDisposableSamples::sample_depends_on(
  1809. size_t i) const {
  1810. if (i >= sample_depends_on_.size())
  1811. return kSampleDependsOnUnknown;
  1812. return sample_depends_on_[i];
  1813. }
  1814. } // namespace mp4
  1815. } // namespace media