track_run_iterator.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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/track_run_iterator.h"
  5. #include <algorithm>
  6. #include <iomanip>
  7. #include <limits>
  8. #include <memory>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/metrics/histogram_macros.h"
  11. #include "base/numerics/checked_math.h"
  12. #include "base/numerics/safe_conversions.h"
  13. #include "build/chromecast_buildflags.h"
  14. #include "media/base/decrypt_config.h"
  15. #include "media/base/demuxer.h"
  16. #include "media/base/demuxer_memory_limit.h"
  17. #include "media/base/encryption_pattern.h"
  18. #include "media/base/encryption_scheme.h"
  19. #include "media/base/media_util.h"
  20. #include "media/base/timestamp_constants.h"
  21. #include "media/formats/mp4/rcheck.h"
  22. #include "media/formats/mp4/sample_to_group_iterator.h"
  23. #include "media/media_buildflags.h"
  24. namespace media {
  25. namespace mp4 {
  26. struct SampleInfo {
  27. uint32_t size;
  28. uint32_t duration;
  29. int64_t cts_offset;
  30. bool is_keyframe;
  31. uint32_t cenc_group_description_index;
  32. };
  33. struct TrackRunInfo {
  34. uint32_t track_id;
  35. std::vector<SampleInfo> samples;
  36. int64_t timescale;
  37. int64_t start_dts;
  38. int64_t sample_start_offset;
  39. bool is_audio;
  40. raw_ptr<const AudioSampleEntry> audio_description;
  41. raw_ptr<const VideoSampleEntry> video_description;
  42. raw_ptr<const SampleGroupDescription> track_sample_encryption_group;
  43. // Stores sample encryption entries, which is populated from 'senc' box if it
  44. // is available, otherwise will try to load from cenc auxiliary information.
  45. std::vector<SampleEncryptionEntry> sample_encryption_entries;
  46. // These variables are useful to load |sample_encryption_entries| from cenc
  47. // auxiliary information when 'senc' box is not available.
  48. int64_t aux_info_start_offset; // Only valid if aux_info_total_size > 0.
  49. int aux_info_default_size;
  50. std::vector<uint8_t> aux_info_sizes; // Populated if default_size == 0.
  51. int aux_info_total_size;
  52. EncryptionScheme encryption_scheme = EncryptionScheme::kUnencrypted;
  53. EncryptionPattern encryption_pattern;
  54. std::vector<CencSampleEncryptionInfoEntry> fragment_sample_encryption_info;
  55. TrackRunInfo();
  56. ~TrackRunInfo();
  57. };
  58. TrackRunInfo::TrackRunInfo()
  59. : track_id(0),
  60. timescale(-1),
  61. start_dts(-1),
  62. sample_start_offset(-1),
  63. is_audio(false),
  64. aux_info_start_offset(-1),
  65. aux_info_default_size(-1),
  66. aux_info_total_size(-1) {
  67. }
  68. TrackRunInfo::~TrackRunInfo() = default;
  69. base::TimeDelta TimeDeltaFromRational(int64_t numer, int64_t denom) {
  70. // TODO(sandersd): Change all callers to pass a |denom| as a uint32_t. This is
  71. // the correct (and sufficient) type in all cases, but some intermediaries
  72. // currently store -1 as a default value.
  73. // TODO(sandersd): Change all callers to pass |numer| as a uint64_t. The few
  74. // cases that could theoretically be negative would result in negative PTS
  75. // anyway, and there are cases where an int64_t is not sufficient to store the
  76. // entire representable range.
  77. DCHECK_GT(denom, 0);
  78. DCHECK_LE(denom, std::numeric_limits<uint32_t>::max());
  79. // The maximum number of seconds that a TimeDelta can hold (about 300,000
  80. // years worth). There is a (t ~= 0.775)-second fraction that is ignored.
  81. const int64_t max_seconds =
  82. std::numeric_limits<int64_t>::max() / base::Time::kMicrosecondsPerSecond;
  83. // The integer part of the result, in seconds. There is a (0 <= f < 1)-second
  84. // fraction that is not computed. (Also true for negative |numer|, since
  85. // rounding of integer division is towards zero in C++.)
  86. const int64_t result_seconds = numer / denom;
  87. // Reject |actual_seconds == max_seconds| under the assumption that f > t.
  88. // This rejects valid times that are within t seconds of the limit.
  89. if (result_seconds >= max_seconds || result_seconds <= -max_seconds)
  90. return kNoTimestamp;
  91. // Since (denom <= 2 ** 32), the multiplication fits in 52 bits.
  92. // Note: When |numer| is negative, (numer % denom) is also negative. C++
  93. // guarantees that ((numer / denom) * denom + (numer % denom) == numer).
  94. // TODO(sandersd): Is round-toward-zero the best possible computation here?
  95. const int64_t result_microseconds =
  96. base::Time::kMicrosecondsPerSecond * (numer % denom) / denom;
  97. const int64_t total_microseconds =
  98. base::Time::kMicrosecondsPerSecond * result_seconds + result_microseconds;
  99. return base::Microseconds(total_microseconds);
  100. }
  101. DecodeTimestamp DecodeTimestampFromRational(int64_t numer, int64_t denom) {
  102. return DecodeTimestamp::FromPresentationTime(
  103. TimeDeltaFromRational(numer, denom));
  104. }
  105. TrackRunIterator::TrackRunIterator(const Movie* moov, MediaLog* media_log)
  106. : moov_(moov),
  107. media_log_(media_log),
  108. sample_dts_(0),
  109. sample_cts_(0),
  110. sample_offset_(0) {
  111. CHECK(moov);
  112. }
  113. TrackRunIterator::~TrackRunIterator() = default;
  114. static std::string HexFlags(uint32_t flags) {
  115. std::stringstream stream;
  116. stream << std::setfill('0') << std::setw(sizeof(flags)*2) << std::hex
  117. << flags;
  118. return stream.str();
  119. }
  120. static bool PopulateSampleInfo(const TrackExtends& trex,
  121. const TrackFragmentHeader& tfhd,
  122. const TrackFragmentRun& trun,
  123. const int64_t edit_list_offset,
  124. const uint32_t i,
  125. SampleInfo* sample_info,
  126. const SampleDependsOn sdtp_sample_depends_on,
  127. bool is_audio,
  128. MediaLog* media_log) {
  129. if (i < trun.sample_sizes.size()) {
  130. sample_info->size = trun.sample_sizes[i];
  131. } else if (tfhd.default_sample_size > 0) {
  132. sample_info->size = tfhd.default_sample_size;
  133. } else {
  134. sample_info->size = trex.default_sample_size;
  135. }
  136. if (i < trun.sample_durations.size()) {
  137. sample_info->duration = trun.sample_durations[i];
  138. } else if (tfhd.default_sample_duration > 0) {
  139. sample_info->duration = tfhd.default_sample_duration;
  140. } else {
  141. sample_info->duration = trex.default_sample_duration;
  142. }
  143. auto cts_offset = -base::CheckedNumeric<int64_t>(edit_list_offset);
  144. if (i < trun.sample_composition_time_offsets.size())
  145. cts_offset += trun.sample_composition_time_offsets[i];
  146. if (!cts_offset.AssignIfValid(&sample_info->cts_offset)) {
  147. MEDIA_LOG(ERROR, media_log) << "PTS offset exceeds representable range.";
  148. return false;
  149. }
  150. uint32_t flags;
  151. if (i < trun.sample_flags.size()) {
  152. flags = trun.sample_flags[i];
  153. DVLOG(4) << __func__ << " trun sample flags " << HexFlags(flags);
  154. } else if (tfhd.has_default_sample_flags) {
  155. flags = tfhd.default_sample_flags;
  156. DVLOG(4) << __func__ << " tfhd sample flags " << HexFlags(flags);
  157. } else {
  158. flags = trex.default_sample_flags;
  159. DVLOG(4) << __func__ << " trex sample flags " << HexFlags(flags);
  160. }
  161. SampleDependsOn sample_depends_on =
  162. static_cast<SampleDependsOn>((flags >> 24) & 0x3);
  163. if (sample_depends_on == kSampleDependsOnUnknown) {
  164. sample_depends_on = sdtp_sample_depends_on;
  165. }
  166. DVLOG(4) << __func__ << " sample_depends_on " << sample_depends_on;
  167. if (sample_depends_on == kSampleDependsOnReserved) {
  168. MEDIA_LOG(ERROR, media_log) << "Reserved value used in sample dependency"
  169. " info.";
  170. return false;
  171. }
  172. // Per spec (ISO 14496-12:2012), the definition for a "sync sample" is
  173. // equivalent to the downstream code's "is keyframe" concept. But media exists
  174. // that marks non-key video frames as sync samples (http://crbug.com/507916
  175. // and http://crbug.com/310712). Hence, for video we additionally check that
  176. // the sample does not depend on others (FFmpeg does too, see mov_read_trun).
  177. // Sample dependency is ignored for audio because encoded audio samples can
  178. // depend on other samples and still be used for random access. Generally all
  179. // audio samples are expected to be sync samples, but we prefer to check the
  180. // flags to catch badly muxed audio (for now anyway ;P). History of attempts
  181. // to get this right discussed in http://crrev.com/1319813002
  182. bool sample_is_sync_sample = !(flags & kSampleIsNonSyncSample);
  183. bool sample_depends_on_others = sample_depends_on == kSampleDependsOnOthers;
  184. sample_info->is_keyframe = sample_is_sync_sample &&
  185. (!sample_depends_on_others || is_audio);
  186. DVLOG(4) << __func__ << " is_kf:" << sample_info->is_keyframe
  187. << " is_sync:" << sample_is_sync_sample
  188. << " deps:" << sample_depends_on_others << " audio:" << is_audio;
  189. return true;
  190. }
  191. static const CencSampleEncryptionInfoEntry* GetSampleEncryptionInfoEntry(
  192. const TrackRunInfo& run_info,
  193. uint32_t group_description_index) {
  194. const std::vector<CencSampleEncryptionInfoEntry>* entries = nullptr;
  195. // ISO-14496-12 Section 8.9.2.3 and 8.9.4 : group description index
  196. // (1) ranges from 1 to the number of sample group entries in the track
  197. // level SampleGroupDescription Box, or (2) takes the value 0 to
  198. // indicate that this sample is a member of no group, in this case, the
  199. // sample is associated with the default values specified in
  200. // TrackEncryption Box, or (3) starts at 0x10001, i.e. the index value
  201. // 1, with the value 1 in the top 16 bits, to reference fragment-local
  202. // SampleGroupDescription Box.
  203. // Case (2) is not supported here. The caller must handle it externally
  204. // before invoking this function.
  205. DCHECK_NE(group_description_index, 0u);
  206. if (group_description_index >
  207. SampleToGroupEntry::kFragmentGroupDescriptionIndexBase) {
  208. group_description_index -=
  209. SampleToGroupEntry::kFragmentGroupDescriptionIndexBase;
  210. entries = &run_info.fragment_sample_encryption_info;
  211. } else {
  212. entries = &run_info.track_sample_encryption_group->entries;
  213. }
  214. // |group_description_index| is 1-based.
  215. return (group_description_index > entries->size())
  216. ? nullptr
  217. : &(*entries)[group_description_index - 1];
  218. }
  219. // In well-structured encrypted media, each track run will be immediately
  220. // preceded by its auxiliary information; this is the only optimal storage
  221. // pattern in terms of minimum number of bytes from a serial stream needed to
  222. // begin playback. It also allows us to optimize caching on memory-constrained
  223. // architectures, because we can cache the relatively small auxiliary
  224. // information for an entire run and then discard data from the input stream,
  225. // instead of retaining the entire 'mdat' box.
  226. //
  227. // We optimize for this situation (with no loss of generality) by sorting track
  228. // runs during iteration in order of their first data offset (either sample data
  229. // or auxiliary data).
  230. class CompareMinTrackRunDataOffset {
  231. public:
  232. bool operator()(const TrackRunInfo& a, const TrackRunInfo& b) {
  233. int64_t a_aux = a.aux_info_total_size ? a.aux_info_start_offset
  234. : std::numeric_limits<int64_t>::max();
  235. int64_t b_aux = b.aux_info_total_size ? b.aux_info_start_offset
  236. : std::numeric_limits<int64_t>::max();
  237. int64_t a_lesser = std::min(a_aux, a.sample_start_offset);
  238. int64_t a_greater = std::max(a_aux, a.sample_start_offset);
  239. int64_t b_lesser = std::min(b_aux, b.sample_start_offset);
  240. int64_t b_greater = std::max(b_aux, b.sample_start_offset);
  241. if (a_lesser == b_lesser) return a_greater < b_greater;
  242. return a_lesser < b_lesser;
  243. }
  244. };
  245. bool TrackRunIterator::Init(const MovieFragment& moof) {
  246. runs_.clear();
  247. for (size_t i = 0; i < moof.tracks.size(); i++) {
  248. const TrackFragment& traf = moof.tracks[i];
  249. const Track* trak = NULL;
  250. for (size_t t = 0; t < moov_->tracks.size(); t++) {
  251. if (moov_->tracks[t].header.track_id == traf.header.track_id)
  252. trak = &moov_->tracks[t];
  253. }
  254. RCHECK(trak);
  255. const TrackExtends* trex = NULL;
  256. for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
  257. if (moov_->extends.tracks[t].track_id == traf.header.track_id)
  258. trex = &moov_->extends.tracks[t];
  259. }
  260. RCHECK(trex);
  261. const SampleDescription& stsd =
  262. trak->media.information.sample_table.description;
  263. if (stsd.type != kAudio && stsd.type != kVideo) {
  264. DVLOG(1) << "Skipping unhandled track type";
  265. continue;
  266. }
  267. size_t desc_idx = traf.header.sample_description_index;
  268. if (!desc_idx) desc_idx = trex->default_sample_description_index;
  269. RCHECK(desc_idx > 0); // Descriptions are one-indexed in the file
  270. desc_idx -= 1;
  271. const std::vector<uint8_t>& sample_encryption_data =
  272. traf.sample_encryption.sample_encryption_data;
  273. std::unique_ptr<BufferReader> sample_encryption_reader;
  274. uint32_t sample_encryption_entries_count = 0;
  275. if (!sample_encryption_data.empty()) {
  276. sample_encryption_reader = std::make_unique<BufferReader>(
  277. sample_encryption_data.data(), sample_encryption_data.size());
  278. RCHECK(sample_encryption_reader->Read4(&sample_encryption_entries_count));
  279. }
  280. // Process edit list to remove CTS offset introduced in the presence of
  281. // B-frames (those that contain a single edit with a nonnegative media
  282. // time). Other uses of edit lists are not supported, as they are
  283. // both uncommon and better served by higher-level protocols.
  284. int64_t edit_list_offset = 0;
  285. const std::vector<EditListEntry>& edits = trak->edit.list.edits;
  286. if (!edits.empty()) {
  287. if (edits.size() > 1)
  288. DVLOG(1) << "Multi-entry edit box detected; some components ignored.";
  289. if (edits[0].media_time < 0) {
  290. DVLOG(1) << "Empty edit list entry ignored.";
  291. } else {
  292. edit_list_offset = edits[0].media_time;
  293. }
  294. }
  295. SampleToGroupIterator sample_to_group_itr(traf.sample_to_group);
  296. bool is_sample_to_group_valid = sample_to_group_itr.IsValid();
  297. int64_t run_start_dts = traf.decode_time.decode_time;
  298. uint64_t sample_count_sum = 0;
  299. for (size_t j = 0; j < traf.runs.size(); j++) {
  300. const TrackFragmentRun& trun = traf.runs[j];
  301. TrackRunInfo tri;
  302. tri.track_id = traf.header.track_id;
  303. tri.timescale = trak->media.header.timescale;
  304. tri.start_dts = run_start_dts;
  305. tri.sample_start_offset = trun.data_offset;
  306. tri.track_sample_encryption_group =
  307. &trak->media.information.sample_table.sample_group_description;
  308. tri.fragment_sample_encryption_info =
  309. traf.sample_group_description.entries;
  310. const TrackEncryption* track_encryption;
  311. const ProtectionSchemeInfo* sinf;
  312. tri.is_audio = (stsd.type == kAudio);
  313. if (tri.is_audio) {
  314. RCHECK(!stsd.audio_entries.empty());
  315. if (desc_idx >= stsd.audio_entries.size())
  316. desc_idx = 0;
  317. tri.audio_description = &stsd.audio_entries[desc_idx];
  318. sinf = &tri.audio_description->sinf;
  319. track_encryption = &tri.audio_description->sinf.info.track_encryption;
  320. } else {
  321. RCHECK(!stsd.video_entries.empty());
  322. if (desc_idx >= stsd.video_entries.size())
  323. desc_idx = 0;
  324. tri.video_description = &stsd.video_entries[desc_idx];
  325. sinf = &tri.video_description->sinf;
  326. track_encryption = &tri.video_description->sinf.info.track_encryption;
  327. }
  328. if (!sinf->HasSupportedScheme()) {
  329. tri.encryption_scheme = EncryptionScheme::kUnencrypted;
  330. } else {
  331. tri.encryption_scheme = sinf->IsCbcsEncryptionScheme()
  332. ? EncryptionScheme::kCbcs
  333. : EncryptionScheme::kCenc;
  334. tri.encryption_pattern =
  335. EncryptionPattern(track_encryption->default_crypt_byte_block,
  336. track_encryption->default_skip_byte_block);
  337. }
  338. // Initialize aux_info variables only if no sample encryption entries.
  339. if (sample_encryption_entries_count == 0 &&
  340. traf.auxiliary_offset.offsets.size() > j) {
  341. // Collect information from the auxiliary_offset entry with the same
  342. // index in the 'saiz' container as the current run's index in the
  343. // 'trun' container, if it is present.
  344. // There should be an auxiliary info entry corresponding to each sample
  345. // in the auxiliary offset entry's corresponding track run.
  346. RCHECK(traf.auxiliary_size.sample_count >=
  347. sample_count_sum + trun.sample_count);
  348. tri.aux_info_start_offset = traf.auxiliary_offset.offsets[j];
  349. tri.aux_info_default_size =
  350. traf.auxiliary_size.default_sample_info_size;
  351. if (tri.aux_info_default_size == 0) {
  352. const std::vector<uint8_t>& sizes =
  353. traf.auxiliary_size.sample_info_sizes;
  354. tri.aux_info_sizes.insert(
  355. tri.aux_info_sizes.begin(), sizes.begin() + sample_count_sum,
  356. sizes.begin() + sample_count_sum + trun.sample_count);
  357. }
  358. // If the default info size is positive, find the total size of the aux
  359. // info block from it, otherwise sum over the individual sizes of each
  360. // aux info entry in the aux_offset entry.
  361. if (tri.aux_info_default_size) {
  362. tri.aux_info_total_size =
  363. tri.aux_info_default_size * trun.sample_count;
  364. } else {
  365. tri.aux_info_total_size = 0;
  366. for (size_t k = 0; k < trun.sample_count; k++) {
  367. tri.aux_info_total_size += tri.aux_info_sizes[k];
  368. }
  369. }
  370. } else {
  371. tri.aux_info_start_offset = -1;
  372. tri.aux_info_total_size = 0;
  373. }
  374. // Avoid allocating insane sample counts for invalid media.
  375. const size_t max_sample_count =
  376. GetDemuxerMemoryLimit(Demuxer::DemuxerTypes::kChunkDemuxer) /
  377. sizeof(decltype(tri.samples)::value_type);
  378. RCHECK_MEDIA_LOGGED(
  379. base::strict_cast<size_t>(trun.sample_count) <= max_sample_count,
  380. media_log_, "Metadata overhead exceeds storage limit.");
  381. tri.samples.resize(trun.sample_count);
  382. int empty_sample_count = 0;
  383. int empty_samples_in_sequence_count = 0;
  384. UMA_HISTOGRAM_COUNTS_1M("Media.MSE.Mp4TrunSampleCount",
  385. trun.sample_count);
  386. for (size_t k = 0; k < trun.sample_count; k++) {
  387. if (!PopulateSampleInfo(*trex, traf.header, trun, edit_list_offset, k,
  388. &tri.samples[k], traf.sdtp.sample_depends_on(k),
  389. tri.is_audio, media_log_)) {
  390. return false;
  391. }
  392. UMA_HISTOGRAM_COUNTS_1M("Media.MSE.Mp4SampleSize", tri.samples[k].size);
  393. if (tri.samples[k].size == 0) {
  394. empty_sample_count++;
  395. empty_samples_in_sequence_count++;
  396. }
  397. // Report the number of consecutive zero-sized samples seen in a
  398. // sequence. Can report counts for 1 or more such sequences within the
  399. // same trun, and a sequence can be as short as just 1 empty sample.
  400. if (empty_samples_in_sequence_count &&
  401. (tri.samples[k].size != 0 || k == trun.sample_count - 1)) {
  402. UMA_HISTOGRAM_COUNTS_1M("Media.MSE.Mp4ConsecutiveEmptySamples",
  403. empty_samples_in_sequence_count);
  404. empty_samples_in_sequence_count = 0;
  405. }
  406. RCHECK(std::numeric_limits<int64_t>::max() - tri.samples[k].duration >
  407. run_start_dts);
  408. run_start_dts += tri.samples[k].duration;
  409. if (!is_sample_to_group_valid) {
  410. // Set group description index to 0 to read encryption information
  411. // from TrackEncryption Box.
  412. tri.samples[k].cenc_group_description_index = 0;
  413. continue;
  414. }
  415. uint32_t index = sample_to_group_itr.group_description_index();
  416. tri.samples[k].cenc_group_description_index = index;
  417. if (index != 0)
  418. RCHECK(GetSampleEncryptionInfoEntry(tri, index));
  419. is_sample_to_group_valid = sample_to_group_itr.Advance();
  420. }
  421. UMA_HISTOGRAM_COUNTS_1M("Media.MSE.Mp4EmptySamplesInTRun",
  422. empty_sample_count);
  423. if (sample_encryption_entries_count > 0) {
  424. RCHECK(sample_encryption_entries_count >=
  425. sample_count_sum + trun.sample_count);
  426. tri.sample_encryption_entries.resize(trun.sample_count);
  427. for (size_t k = 0; k < trun.sample_count; k++) {
  428. uint32_t index = tri.samples[k].cenc_group_description_index;
  429. const CencSampleEncryptionInfoEntry* info_entry =
  430. index == 0 ? nullptr : GetSampleEncryptionInfoEntry(tri, index);
  431. const uint8_t iv_size = index == 0 ? track_encryption->default_iv_size
  432. : info_entry->iv_size;
  433. SampleEncryptionEntry& entry = tri.sample_encryption_entries[k];
  434. RCHECK(entry.Parse(sample_encryption_reader.get(), iv_size,
  435. traf.sample_encryption.use_subsample_encryption));
  436. // If we don't have a per-sample IV, get the constant IV.
  437. bool is_encrypted = index == 0 ? track_encryption->is_encrypted
  438. : info_entry->is_encrypted;
  439. // TODO(crbug.com/1336055): Investigate if this is a hardware or
  440. // cast-related limitation.
  441. #if BUILDFLAG(IS_CASTOS)
  442. // On Chromecast, we only support setting the pattern values in the
  443. // 'tenc' box for the track (not varying on per sample group basis).
  444. // Thus we need to verify that the settings in the sample group
  445. // match those in the 'tenc'.
  446. if (is_encrypted && index != 0) {
  447. RCHECK_MEDIA_LOGGED(info_entry->crypt_byte_block ==
  448. track_encryption->default_crypt_byte_block,
  449. media_log_,
  450. "Pattern value (crypt byte block) for the "
  451. "sample group does not match that in the tenc "
  452. "box . This is not currently supported.");
  453. RCHECK_MEDIA_LOGGED(info_entry->skip_byte_block ==
  454. track_encryption->default_skip_byte_block,
  455. media_log_,
  456. "Pattern value (skip byte block) for the "
  457. "sample group does not match that in the tenc "
  458. "box . This is not currently supported.");
  459. }
  460. #endif // BUILDFLAG(IS_CASTOS)
  461. if (is_encrypted && !iv_size) {
  462. const uint8_t constant_iv_size =
  463. index == 0 ? track_encryption->default_constant_iv_size
  464. : info_entry->constant_iv_size;
  465. RCHECK(constant_iv_size != 0);
  466. const uint8_t* constant_iv =
  467. index == 0 ? track_encryption->default_constant_iv
  468. : info_entry->constant_iv;
  469. memcpy(entry.initialization_vector, constant_iv, constant_iv_size);
  470. }
  471. }
  472. }
  473. runs_.push_back(tri);
  474. sample_count_sum += trun.sample_count;
  475. }
  476. // We should have iterated through all samples in SampleToGroup Box.
  477. RCHECK(!sample_to_group_itr.IsValid());
  478. }
  479. std::sort(runs_.begin(), runs_.end(), CompareMinTrackRunDataOffset());
  480. run_itr_ = runs_.begin();
  481. return ResetRun();
  482. }
  483. bool TrackRunIterator::UpdateCts() {
  484. // TODO(sandersd): Should |sample_cts_| be cleared in this case?
  485. if (!IsSampleValid())
  486. return true;
  487. auto cts = base::CheckAdd(sample_dts_, sample_itr_->cts_offset);
  488. if (!cts.AssignIfValid(&sample_cts_)) {
  489. MEDIA_LOG(ERROR, media_log_) << "Sample PTS exceeds representable range.";
  490. return false;
  491. }
  492. return true;
  493. }
  494. bool TrackRunIterator::AdvanceRun() {
  495. ++run_itr_;
  496. return ResetRun();
  497. }
  498. bool TrackRunIterator::ResetRun() {
  499. // TODO(sandersd): Should we clear all the values if the run is not valid?
  500. if (!IsRunValid())
  501. return true;
  502. sample_dts_ = run_itr_->start_dts;
  503. sample_offset_ = run_itr_->sample_start_offset;
  504. sample_itr_ = run_itr_->samples.begin();
  505. // UpdateCts() must run after |sample_itr_| is updated to the current run.
  506. return UpdateCts();
  507. }
  508. bool TrackRunIterator::AdvanceSample() {
  509. DCHECK(IsSampleValid());
  510. auto dts = base::CheckAdd(sample_dts_, sample_itr_->duration);
  511. if (!dts.AssignIfValid(&sample_dts_)) {
  512. MEDIA_LOG(ERROR, media_log_) << "Sample DTS exceeds representable range.";
  513. return false;
  514. }
  515. sample_offset_ += sample_itr_->size;
  516. ++sample_itr_;
  517. // UpdateCts() must run after |sample_itr_| is updated to the current sample.
  518. return UpdateCts();
  519. }
  520. // This implementation only indicates a need for caching if CENC auxiliary
  521. // info is available in the stream.
  522. bool TrackRunIterator::AuxInfoNeedsToBeCached() {
  523. DCHECK(IsRunValid());
  524. return is_encrypted() && aux_info_size() > 0 &&
  525. run_itr_->sample_encryption_entries.size() == 0;
  526. }
  527. // This implementation currently only caches CENC auxiliary info.
  528. bool TrackRunIterator::CacheAuxInfo(const uint8_t* buf, int buf_size) {
  529. RCHECK(AuxInfoNeedsToBeCached() && buf_size >= aux_info_size());
  530. std::vector<SampleEncryptionEntry>& sample_encryption_entries =
  531. runs_[run_itr_ - runs_.begin()].sample_encryption_entries;
  532. sample_encryption_entries.resize(run_itr_->samples.size());
  533. int64_t pos = 0;
  534. for (size_t i = 0; i < run_itr_->samples.size(); i++) {
  535. int info_size = run_itr_->aux_info_default_size;
  536. if (!info_size)
  537. info_size = run_itr_->aux_info_sizes[i];
  538. if (IsSampleEncrypted(i)) {
  539. BufferReader reader(buf + pos, info_size);
  540. const uint8_t iv_size = GetIvSize(i);
  541. const bool has_subsamples = info_size > iv_size;
  542. SampleEncryptionEntry& entry = sample_encryption_entries[i];
  543. RCHECK_MEDIA_LOGGED(
  544. entry.Parse(&reader, iv_size, has_subsamples), media_log_,
  545. "SampleEncryptionEntry parse failed when caching aux info");
  546. // if we don't have a per-sample IV, get the constant IV.
  547. if (!iv_size) {
  548. RCHECK(ApplyConstantIv(i, &entry));
  549. }
  550. }
  551. pos += info_size;
  552. }
  553. return true;
  554. }
  555. bool TrackRunIterator::IsRunValid() const {
  556. return run_itr_ != runs_.end();
  557. }
  558. bool TrackRunIterator::IsSampleValid() const {
  559. return IsRunValid() && (sample_itr_ != run_itr_->samples.end());
  560. }
  561. // Because tracks are in sorted order and auxiliary information is cached when
  562. // returning samples, it is guaranteed that no data will be required before the
  563. // lesser of the minimum data offset of this track and the next in sequence.
  564. // (The stronger condition - that no data is required before the minimum data
  565. // offset of this track alone - is not guaranteed, because the BMFF spec does
  566. // not have any inter-run ordering restrictions.)
  567. int64_t TrackRunIterator::GetMaxClearOffset() {
  568. int64_t offset = std::numeric_limits<int64_t>::max();
  569. if (IsSampleValid()) {
  570. offset = std::min(offset, sample_offset_);
  571. if (AuxInfoNeedsToBeCached())
  572. offset = std::min(offset, aux_info_offset());
  573. }
  574. if (run_itr_ != runs_.end()) {
  575. auto next_run = run_itr_ + 1;
  576. if (next_run != runs_.end()) {
  577. offset = std::min(offset, next_run->sample_start_offset);
  578. if (next_run->aux_info_total_size)
  579. offset = std::min(offset, next_run->aux_info_start_offset);
  580. }
  581. }
  582. if (offset == std::numeric_limits<int64_t>::max())
  583. return 0;
  584. return offset;
  585. }
  586. uint32_t TrackRunIterator::track_id() const {
  587. DCHECK(IsRunValid());
  588. return run_itr_->track_id;
  589. }
  590. bool TrackRunIterator::is_encrypted() const {
  591. DCHECK(IsSampleValid());
  592. return IsSampleEncrypted(sample_itr_ - run_itr_->samples.begin());
  593. }
  594. int64_t TrackRunIterator::aux_info_offset() const {
  595. return run_itr_->aux_info_start_offset;
  596. }
  597. int TrackRunIterator::aux_info_size() const {
  598. return run_itr_->aux_info_total_size;
  599. }
  600. bool TrackRunIterator::is_audio() const {
  601. DCHECK(IsRunValid());
  602. return run_itr_->is_audio;
  603. }
  604. const AudioSampleEntry& TrackRunIterator::audio_description() const {
  605. DCHECK(is_audio());
  606. DCHECK(run_itr_->audio_description);
  607. return *run_itr_->audio_description;
  608. }
  609. const VideoSampleEntry& TrackRunIterator::video_description() const {
  610. DCHECK(!is_audio());
  611. DCHECK(run_itr_->video_description);
  612. return *run_itr_->video_description;
  613. }
  614. int64_t TrackRunIterator::sample_offset() const {
  615. DCHECK(IsSampleValid());
  616. return sample_offset_;
  617. }
  618. uint32_t TrackRunIterator::sample_size() const {
  619. DCHECK(IsSampleValid());
  620. return sample_itr_->size;
  621. }
  622. DecodeTimestamp TrackRunIterator::dts() const {
  623. DCHECK(IsSampleValid());
  624. return DecodeTimestampFromRational(sample_dts_, run_itr_->timescale);
  625. }
  626. base::TimeDelta TrackRunIterator::cts() const {
  627. DCHECK(IsSampleValid());
  628. return TimeDeltaFromRational(sample_cts_, run_itr_->timescale);
  629. }
  630. base::TimeDelta TrackRunIterator::duration() const {
  631. DCHECK(IsSampleValid());
  632. return TimeDeltaFromRational(sample_itr_->duration, run_itr_->timescale);
  633. }
  634. bool TrackRunIterator::is_keyframe() const {
  635. DCHECK(IsSampleValid());
  636. return sample_itr_->is_keyframe;
  637. }
  638. const ProtectionSchemeInfo& TrackRunIterator::protection_scheme_info() const {
  639. if (is_audio())
  640. return audio_description().sinf;
  641. return video_description().sinf;
  642. }
  643. const TrackEncryption& TrackRunIterator::track_encryption() const {
  644. return protection_scheme_info().info.track_encryption;
  645. }
  646. std::unique_ptr<DecryptConfig> TrackRunIterator::GetDecryptConfig() {
  647. DCHECK(is_encrypted());
  648. size_t sample_idx = sample_itr_ - run_itr_->samples.begin();
  649. const std::vector<uint8_t>& kid = GetKeyId(sample_idx);
  650. std::string key_id(kid.begin(), kid.end());
  651. if (run_itr_->sample_encryption_entries.empty()) {
  652. DCHECK_EQ(0, aux_info_size());
  653. // The 'cbcs' scheme allows empty aux info when a constant IV is in use
  654. // with full sample encryption. That case will fall through to here.
  655. SampleEncryptionEntry sample_encryption_entry;
  656. if (ApplyConstantIv(sample_idx, &sample_encryption_entry)) {
  657. std::string iv(reinterpret_cast<const char*>(
  658. sample_encryption_entry.initialization_vector),
  659. std::size(sample_encryption_entry.initialization_vector));
  660. switch (run_itr_->encryption_scheme) {
  661. case EncryptionScheme::kUnencrypted:
  662. return nullptr;
  663. case EncryptionScheme::kCenc:
  664. return DecryptConfig::CreateCencConfig(
  665. key_id, iv, sample_encryption_entry.subsamples);
  666. case EncryptionScheme::kCbcs:
  667. return DecryptConfig::CreateCbcsConfig(
  668. key_id, iv, sample_encryption_entry.subsamples,
  669. run_itr_->encryption_pattern);
  670. }
  671. }
  672. MEDIA_LOG(ERROR, media_log_) << "Sample encryption info is not available.";
  673. return nullptr;
  674. }
  675. DCHECK_LT(sample_idx, run_itr_->sample_encryption_entries.size());
  676. const SampleEncryptionEntry& sample_encryption_entry =
  677. run_itr_->sample_encryption_entries[sample_idx];
  678. std::string iv(reinterpret_cast<const char*>(
  679. sample_encryption_entry.initialization_vector),
  680. std::size(sample_encryption_entry.initialization_vector));
  681. size_t total_size = 0;
  682. if (!sample_encryption_entry.subsamples.empty() &&
  683. (!sample_encryption_entry.GetTotalSizeOfSubsamples(&total_size) ||
  684. total_size != static_cast<size_t>(sample_size()))) {
  685. MEDIA_LOG(ERROR, media_log_) << "Incorrect CENC subsample size.";
  686. return nullptr;
  687. }
  688. if (protection_scheme_info().IsCbcsEncryptionScheme()) {
  689. uint32_t index = GetGroupDescriptionIndex(sample_idx);
  690. uint32_t encrypt_blocks =
  691. (index == 0)
  692. ? track_encryption().default_crypt_byte_block
  693. : GetSampleEncryptionInfoEntry(*run_itr_, index)->crypt_byte_block;
  694. uint32_t skip_blocks =
  695. (index == 0)
  696. ? track_encryption().default_skip_byte_block
  697. : GetSampleEncryptionInfoEntry(*run_itr_, index)->skip_byte_block;
  698. return DecryptConfig::CreateCbcsConfig(
  699. key_id, iv, sample_encryption_entry.subsamples,
  700. EncryptionPattern(encrypt_blocks, skip_blocks));
  701. }
  702. return DecryptConfig::CreateCencConfig(key_id, iv,
  703. sample_encryption_entry.subsamples);
  704. }
  705. uint32_t TrackRunIterator::GetGroupDescriptionIndex(
  706. uint32_t sample_index) const {
  707. DCHECK(IsRunValid());
  708. DCHECK_LT(sample_index, run_itr_->samples.size());
  709. return run_itr_->samples[sample_index].cenc_group_description_index;
  710. }
  711. bool TrackRunIterator::IsSampleEncrypted(size_t sample_index) const {
  712. uint32_t index = GetGroupDescriptionIndex(sample_index);
  713. return (index == 0)
  714. ? track_encryption().is_encrypted
  715. : GetSampleEncryptionInfoEntry(*run_itr_, index)->is_encrypted;
  716. }
  717. const std::vector<uint8_t>& TrackRunIterator::GetKeyId(
  718. size_t sample_index) const {
  719. uint32_t index = GetGroupDescriptionIndex(sample_index);
  720. return (index == 0) ? track_encryption().default_kid
  721. : GetSampleEncryptionInfoEntry(*run_itr_, index)->key_id;
  722. }
  723. uint8_t TrackRunIterator::GetIvSize(size_t sample_index) const {
  724. uint32_t index = GetGroupDescriptionIndex(sample_index);
  725. return (index == 0) ? track_encryption().default_iv_size
  726. : GetSampleEncryptionInfoEntry(*run_itr_, index)->iv_size;
  727. }
  728. bool TrackRunIterator::ApplyConstantIv(size_t sample_index,
  729. SampleEncryptionEntry* entry) const {
  730. DCHECK(IsSampleEncrypted(sample_index));
  731. uint32_t index = GetGroupDescriptionIndex(sample_index);
  732. const uint8_t constant_iv_size =
  733. index == 0
  734. ? track_encryption().default_constant_iv_size
  735. : GetSampleEncryptionInfoEntry(*run_itr_, index)->constant_iv_size;
  736. RCHECK(constant_iv_size != 0);
  737. const uint8_t* constant_iv =
  738. index == 0 ? track_encryption().default_constant_iv
  739. : GetSampleEncryptionInfoEntry(*run_itr_, index)->constant_iv;
  740. RCHECK(constant_iv != nullptr);
  741. memcpy(entry->initialization_vector, constant_iv, kInitializationVectorSize);
  742. return true;
  743. }
  744. } // namespace mp4
  745. } // namespace media