h264_parser.cc 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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/video/h264_parser.h"
  5. #include <cstring>
  6. #include <limits>
  7. #include <memory>
  8. #include "base/logging.h"
  9. #include "base/notreached.h"
  10. #include "base/numerics/safe_math.h"
  11. #include "media/base/subsample_entry.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. #include "ui/gfx/geometry/size.h"
  14. namespace media {
  15. namespace {
  16. // Converts [|start|, |end|) range with |encrypted_ranges| into a vector of
  17. // SubsampleEntry. |encrypted_ranges| must be with in the range defined by
  18. // |start| and |end|.
  19. // It is OK to pass in empty |encrypted_ranges|; this will return a vector
  20. // with single SubsampleEntry with clear_bytes set to the size of the buffer.
  21. std::vector<SubsampleEntry> EncryptedRangesToSubsampleEntry(
  22. const uint8_t* start,
  23. const uint8_t* end,
  24. const Ranges<const uint8_t*>& encrypted_ranges) {
  25. std::vector<SubsampleEntry> subsamples;
  26. const uint8_t* cur = start;
  27. for (size_t i = 0; i < encrypted_ranges.size(); ++i) {
  28. SubsampleEntry subsample = {};
  29. const uint8_t* encrypted_start = encrypted_ranges.start(i);
  30. DCHECK_GE(encrypted_start, cur)
  31. << "Encrypted range started before the current buffer pointer.";
  32. subsample.clear_bytes = encrypted_start - cur;
  33. const uint8_t* encrypted_end = encrypted_ranges.end(i);
  34. subsample.cypher_bytes = encrypted_end - encrypted_start;
  35. subsamples.push_back(subsample);
  36. cur = encrypted_end;
  37. DCHECK_LE(cur, end) << "Encrypted range is outside the buffer range.";
  38. }
  39. // If there is more data in the buffer but not covered by encrypted_ranges,
  40. // then it must be in the clear.
  41. if (cur < end) {
  42. SubsampleEntry subsample = {};
  43. subsample.clear_bytes = end - cur;
  44. subsamples.push_back(subsample);
  45. }
  46. return subsamples;
  47. }
  48. } // namespace
  49. bool H264SliceHeader::IsPSlice() const {
  50. return (slice_type % 5 == kPSlice);
  51. }
  52. bool H264SliceHeader::IsBSlice() const {
  53. return (slice_type % 5 == kBSlice);
  54. }
  55. bool H264SliceHeader::IsISlice() const {
  56. return (slice_type % 5 == kISlice);
  57. }
  58. bool H264SliceHeader::IsSPSlice() const {
  59. return (slice_type % 5 == kSPSlice);
  60. }
  61. bool H264SliceHeader::IsSISlice() const {
  62. return (slice_type % 5 == kSISlice);
  63. }
  64. H264NALU::H264NALU() {
  65. memset(this, 0, sizeof(*this));
  66. }
  67. // static
  68. void H264SPS::GetLevelConfigFromProfileLevel(VideoCodecProfile profile,
  69. uint8_t level,
  70. int* level_idc,
  71. bool* constraint_set3_flag) {
  72. // Spec A.3.1.
  73. // Note: we always use h264_output_level = 9 to indicate Level 1b in
  74. // VideoEncodeAccelerator::Config, in order to tell apart from Level 1.1
  75. // which level IDC is also 11.
  76. // For Baseline and Main profile, if requested level is Level 1b, set
  77. // level_idc to 11 and constraint_set3_flag to true. Otherwise, set level_idc
  78. // to 9 for Level 1b, and ten times level number for others.
  79. if ((profile == H264PROFILE_BASELINE || profile == H264PROFILE_MAIN) &&
  80. level == kLevelIDC1B) {
  81. *level_idc = 11;
  82. *constraint_set3_flag = true;
  83. } else {
  84. *level_idc = level;
  85. }
  86. }
  87. H264SPS::H264SPS() {
  88. memset(this, 0, sizeof(*this));
  89. }
  90. // Based on T-REC-H.264 7.4.2.1.1, "Sequence parameter set data semantics",
  91. // available from http://www.itu.int/rec/T-REC-H.264.
  92. absl::optional<gfx::Size> H264SPS::GetCodedSize() const {
  93. // Interlaced frames are twice the height of each field.
  94. const int mb_unit = 16;
  95. int map_unit = frame_mbs_only_flag ? 16 : 32;
  96. // Verify that the values are not too large before multiplying them.
  97. // TODO(sandersd): These limits could be much smaller. The currently-largest
  98. // specified limit (excluding SVC, multiview, etc., which I didn't bother to
  99. // read) is 543 macroblocks (section A.3.1).
  100. int max_mb_minus1 = std::numeric_limits<int>::max() / mb_unit - 1;
  101. int max_map_units_minus1 = std::numeric_limits<int>::max() / map_unit - 1;
  102. if (pic_width_in_mbs_minus1 > max_mb_minus1 ||
  103. pic_height_in_map_units_minus1 > max_map_units_minus1) {
  104. DVLOG(1) << "Coded size is too large.";
  105. return absl::nullopt;
  106. }
  107. return gfx::Size(mb_unit * (pic_width_in_mbs_minus1 + 1),
  108. map_unit * (pic_height_in_map_units_minus1 + 1));
  109. }
  110. // Also based on section 7.4.2.1.1.
  111. absl::optional<gfx::Rect> H264SPS::GetVisibleRect() const {
  112. absl::optional<gfx::Size> coded_size = GetCodedSize();
  113. if (!coded_size)
  114. return absl::nullopt;
  115. if (!frame_cropping_flag)
  116. return gfx::Rect(coded_size.value());
  117. int crop_unit_x;
  118. int crop_unit_y;
  119. if (chroma_array_type == 0) {
  120. crop_unit_x = 1;
  121. crop_unit_y = frame_mbs_only_flag ? 1 : 2;
  122. } else {
  123. // Section 6.2.
  124. // |chroma_format_idc| may be:
  125. // 1 => 4:2:0
  126. // 2 => 4:2:2
  127. // 3 => 4:4:4
  128. // Everything else has |chroma_array_type| == 0.
  129. int sub_width_c = chroma_format_idc > 2 ? 1 : 2;
  130. int sub_height_c = chroma_format_idc > 1 ? 1 : 2;
  131. crop_unit_x = sub_width_c;
  132. crop_unit_y = sub_height_c * (frame_mbs_only_flag ? 1 : 2);
  133. }
  134. // Verify that the values are not too large before multiplying.
  135. if (coded_size->width() / crop_unit_x < frame_crop_left_offset ||
  136. coded_size->width() / crop_unit_x < frame_crop_right_offset ||
  137. coded_size->height() / crop_unit_y < frame_crop_top_offset ||
  138. coded_size->height() / crop_unit_y < frame_crop_bottom_offset) {
  139. DVLOG(1) << "Frame cropping exceeds coded size.";
  140. return absl::nullopt;
  141. }
  142. int crop_left = crop_unit_x * frame_crop_left_offset;
  143. int crop_right = crop_unit_x * frame_crop_right_offset;
  144. int crop_top = crop_unit_y * frame_crop_top_offset;
  145. int crop_bottom = crop_unit_y * frame_crop_bottom_offset;
  146. // Verify that the values are sane. Note that some decoders also require that
  147. // crops are smaller than a macroblock and/or that crops must be adjacent to
  148. // at least one corner of the coded frame.
  149. if (coded_size->width() - crop_left <= crop_right ||
  150. coded_size->height() - crop_top <= crop_bottom) {
  151. DVLOG(1) << "Frame cropping excludes entire frame.";
  152. return absl::nullopt;
  153. }
  154. return gfx::Rect(crop_left, crop_top,
  155. coded_size->width() - crop_left - crop_right,
  156. coded_size->height() - crop_top - crop_bottom);
  157. }
  158. // Based on T-REC-H.264 E.2.1, "VUI parameters semantics",
  159. // available from http://www.itu.int/rec/T-REC-H.264.
  160. VideoColorSpace H264SPS::GetColorSpace() const {
  161. if (colour_description_present_flag) {
  162. return VideoColorSpace(
  163. colour_primaries, transfer_characteristics, matrix_coefficients,
  164. video_full_range_flag ? gfx::ColorSpace::RangeID::FULL
  165. : gfx::ColorSpace::RangeID::LIMITED);
  166. } else {
  167. return VideoColorSpace();
  168. }
  169. }
  170. VideoChromaSampling H264SPS::GetChromaSampling() const {
  171. // Spec section 6.2
  172. switch (chroma_format_idc) {
  173. case 0:
  174. return VideoChromaSampling::k400;
  175. case 1:
  176. return VideoChromaSampling::k420;
  177. case 2:
  178. return VideoChromaSampling::k422;
  179. case 3:
  180. return VideoChromaSampling::k444;
  181. default:
  182. DVLOG(1) << "Unknown chroma subsampling format.";
  183. return VideoChromaSampling::kUnknown;
  184. }
  185. }
  186. uint8_t H264SPS::GetIndicatedLevel() const {
  187. // Spec A.3.1 and A.3.2
  188. // For Baseline, Constrained Baseline and Main profile, the indicated level is
  189. // Level 1b if level_idc is equal to 11 and constraint_set3_flag is true.
  190. if ((profile_idc == H264SPS::kProfileIDCBaseline ||
  191. profile_idc == H264SPS::kProfileIDCConstrainedBaseline ||
  192. profile_idc == H264SPS::kProfileIDCMain) &&
  193. level_idc == 11 && constraint_set3_flag) {
  194. return kLevelIDC1B; // Level 1b
  195. }
  196. // Otherwise, the level_idc is equal to 9 for Level 1b, and others are equal
  197. // to values of ten times the level numbers.
  198. return base::checked_cast<uint8_t>(level_idc);
  199. }
  200. bool H264SPS::CheckIndicatedLevelWithinTarget(uint8_t target_level) const {
  201. // See table A-1 in spec.
  202. // Level 1.0 < 1b < 1.1 < 1.2 .... (in numeric order).
  203. uint8_t level = GetIndicatedLevel();
  204. if (target_level == kLevelIDC1p0)
  205. return level == kLevelIDC1p0;
  206. if (target_level == kLevelIDC1B)
  207. return level == kLevelIDC1p0 || level == kLevelIDC1B;
  208. return level <= target_level;
  209. }
  210. H264PPS::H264PPS() {
  211. memset(this, 0, sizeof(*this));
  212. }
  213. H264SliceHeader::H264SliceHeader() {
  214. memset(this, 0, sizeof(*this));
  215. }
  216. H264SEIMessage::H264SEIMessage() {
  217. memset(this, 0, sizeof(*this));
  218. }
  219. #define READ_BITS_OR_RETURN(num_bits, out) \
  220. do { \
  221. int _out; \
  222. if (!br_.ReadBits(num_bits, &_out)) { \
  223. DVLOG(1) \
  224. << "Error in stream: unexpected EOS while trying to read " #out; \
  225. return kInvalidStream; \
  226. } \
  227. *out = _out; \
  228. } while (0)
  229. #define READ_BOOL_OR_RETURN(out) \
  230. do { \
  231. int _out; \
  232. if (!br_.ReadBits(1, &_out)) { \
  233. DVLOG(1) \
  234. << "Error in stream: unexpected EOS while trying to read " #out; \
  235. return kInvalidStream; \
  236. } \
  237. *out = _out != 0; \
  238. } while (0)
  239. #define READ_UE_OR_RETURN(out) \
  240. do { \
  241. if (ReadUE(out) != kOk) { \
  242. DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
  243. return kInvalidStream; \
  244. } \
  245. } while (0)
  246. #define READ_SE_OR_RETURN(out) \
  247. do { \
  248. if (ReadSE(out) != kOk) { \
  249. DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
  250. return kInvalidStream; \
  251. } \
  252. } while (0)
  253. #define IN_RANGE_OR_RETURN(val, min, max) \
  254. do { \
  255. if ((val) < (min) || (val) > (max)) { \
  256. DVLOG(1) << "Error in stream: invalid value, expected " #val " to be" \
  257. << " in range [" << (min) << ":" << (max) << "]" \
  258. << " found " << (val) << " instead"; \
  259. return kInvalidStream; \
  260. } \
  261. } while (0)
  262. #define TRUE_OR_RETURN(a) \
  263. do { \
  264. if (!(a)) { \
  265. DVLOG(1) << "Error in stream: invalid value, expected " << #a; \
  266. return kInvalidStream; \
  267. } \
  268. } while (0)
  269. // ISO 14496 part 10
  270. // VUI parameters: Table E-1 "Meaning of sample aspect ratio indicator"
  271. static const int kTableSarWidth[] = {0, 1, 12, 10, 16, 40, 24, 20, 32,
  272. 80, 18, 15, 64, 160, 4, 3, 2};
  273. static const int kTableSarHeight[] = {0, 1, 11, 11, 11, 33, 11, 11, 11,
  274. 33, 11, 11, 33, 99, 3, 2, 1};
  275. static_assert(std::size(kTableSarWidth) == std::size(kTableSarHeight),
  276. "sar tables must have the same size");
  277. H264Parser::H264Parser() {
  278. Reset();
  279. }
  280. H264Parser::~H264Parser() = default;
  281. void H264Parser::Reset() {
  282. stream_ = nullptr;
  283. bytes_left_ = 0;
  284. encrypted_ranges_.clear();
  285. previous_nalu_range_.clear();
  286. }
  287. void H264Parser::SetStream(const uint8_t* stream, off_t stream_size) {
  288. std::vector<SubsampleEntry> subsamples;
  289. SetEncryptedStream(stream, stream_size, subsamples);
  290. }
  291. void H264Parser::SetEncryptedStream(
  292. const uint8_t* stream,
  293. off_t stream_size,
  294. const std::vector<SubsampleEntry>& subsamples) {
  295. DCHECK(stream);
  296. DCHECK_GT(stream_size, 0);
  297. stream_ = stream;
  298. bytes_left_ = stream_size;
  299. previous_nalu_range_.clear();
  300. encrypted_ranges_.clear();
  301. const uint8_t* start = stream;
  302. const uint8_t* stream_end = stream_ + bytes_left_;
  303. for (size_t i = 0; i < subsamples.size() && start < stream_end; ++i) {
  304. start += subsamples[i].clear_bytes;
  305. const uint8_t* end =
  306. std::min(start + subsamples[i].cypher_bytes, stream_end);
  307. encrypted_ranges_.Add(start, end);
  308. start = end;
  309. }
  310. }
  311. const H264PPS* H264Parser::GetPPS(int pps_id) const {
  312. auto it = active_PPSes_.find(pps_id);
  313. if (it == active_PPSes_.end()) {
  314. DVLOG(1) << "Requested a nonexistent PPS id " << pps_id;
  315. return nullptr;
  316. }
  317. return it->second.get();
  318. }
  319. const H264SPS* H264Parser::GetSPS(int sps_id) const {
  320. auto it = active_SPSes_.find(sps_id);
  321. if (it == active_SPSes_.end()) {
  322. DVLOG(1) << "Requested a nonexistent SPS id " << sps_id;
  323. return nullptr;
  324. }
  325. return it->second.get();
  326. }
  327. static inline bool IsStartCode(const uint8_t* data) {
  328. return data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01;
  329. }
  330. // static
  331. bool H264Parser::FindStartCode(const uint8_t* data,
  332. off_t data_size,
  333. off_t* offset,
  334. off_t* start_code_size) {
  335. DCHECK_GE(data_size, 0);
  336. off_t bytes_left = data_size;
  337. while (bytes_left >= 3) {
  338. // The start code is "\0\0\1", ones are more unusual than zeroes, so let's
  339. // search for it first.
  340. const uint8_t* tmp =
  341. reinterpret_cast<const uint8_t*>(memchr(data + 2, 1, bytes_left - 2));
  342. if (!tmp) {
  343. data += bytes_left - 2;
  344. bytes_left = 2;
  345. break;
  346. }
  347. tmp -= 2;
  348. bytes_left -= tmp - data;
  349. data = tmp;
  350. if (IsStartCode(data)) {
  351. // Found three-byte start code, set pointer at its beginning.
  352. *offset = data_size - bytes_left;
  353. *start_code_size = 3;
  354. // If there is a zero byte before this start code,
  355. // then it's actually a four-byte start code, so backtrack one byte.
  356. if (*offset > 0 && *(data - 1) == 0x00) {
  357. --(*offset);
  358. ++(*start_code_size);
  359. }
  360. return true;
  361. }
  362. ++data;
  363. --bytes_left;
  364. }
  365. // End of data: offset is pointing to the first byte that was not considered
  366. // as a possible start of a start code.
  367. // Note: there is no security issue when receiving a negative |data_size|
  368. // since in this case, |bytes_left| is equal to |data_size| and thus
  369. // |*offset| is equal to 0 (valid offset).
  370. *offset = data_size - bytes_left;
  371. *start_code_size = 0;
  372. return false;
  373. }
  374. bool H264Parser::LocateNALU(off_t* nalu_size, off_t* start_code_size) {
  375. // Find the start code of next NALU.
  376. off_t nalu_start_off = 0;
  377. off_t annexb_start_code_size = 0;
  378. if (!FindStartCodeInClearRanges(stream_, bytes_left_, encrypted_ranges_,
  379. &nalu_start_off, &annexb_start_code_size)) {
  380. DVLOG(4) << "Could not find start code, end of stream?";
  381. return false;
  382. }
  383. // Move the stream to the beginning of the NALU (pointing at the start code).
  384. stream_ += nalu_start_off;
  385. bytes_left_ -= nalu_start_off;
  386. const uint8_t* nalu_data = stream_ + annexb_start_code_size;
  387. off_t max_nalu_data_size = bytes_left_ - annexb_start_code_size;
  388. if (max_nalu_data_size <= 0) {
  389. DVLOG(3) << "End of stream";
  390. return false;
  391. }
  392. // Find the start code of next NALU;
  393. // if successful, |nalu_size_without_start_code| is the number of bytes from
  394. // after previous start code to before this one;
  395. // if next start code is not found, it is still a valid NALU since there
  396. // are some bytes left after the first start code: all the remaining bytes
  397. // belong to the current NALU.
  398. off_t next_start_code_size = 0;
  399. off_t nalu_size_without_start_code = 0;
  400. if (!FindStartCodeInClearRanges(
  401. nalu_data, max_nalu_data_size, encrypted_ranges_,
  402. &nalu_size_without_start_code, &next_start_code_size)) {
  403. nalu_size_without_start_code = max_nalu_data_size;
  404. }
  405. *nalu_size = nalu_size_without_start_code + annexb_start_code_size;
  406. *start_code_size = annexb_start_code_size;
  407. return true;
  408. }
  409. // static
  410. bool H264Parser::FindStartCodeInClearRanges(
  411. const uint8_t* data,
  412. off_t data_size,
  413. const Ranges<const uint8_t*>& encrypted_ranges,
  414. off_t* offset,
  415. off_t* start_code_size) {
  416. if (encrypted_ranges.size() == 0)
  417. return FindStartCode(data, data_size, offset, start_code_size);
  418. DCHECK_GE(data_size, 0);
  419. const uint8_t* start = data;
  420. do {
  421. off_t bytes_left = data_size - (start - data);
  422. if (!FindStartCode(start, bytes_left, offset, start_code_size))
  423. return false;
  424. // Construct a Ranges object that represents the region occupied
  425. // by the start code and the 1 byte needed to read the NAL unit type.
  426. const uint8_t* start_code = start + *offset;
  427. const uint8_t* start_code_end = start_code + *start_code_size;
  428. Ranges<const uint8_t*> start_code_range;
  429. start_code_range.Add(start_code, start_code_end + 1);
  430. if (encrypted_ranges.IntersectionWith(start_code_range).size() > 0) {
  431. // The start code is inside an encrypted section so we need to scan
  432. // for another start code.
  433. *start_code_size = 0;
  434. start += std::min(*offset + 1, bytes_left);
  435. }
  436. } while (*start_code_size == 0);
  437. // Update |*offset| to include the data we skipped over.
  438. *offset += start - data;
  439. return true;
  440. }
  441. // static
  442. VideoCodecProfile H264Parser::ProfileIDCToVideoCodecProfile(int profile_idc) {
  443. switch (profile_idc) {
  444. case H264SPS::kProfileIDCBaseline:
  445. return H264PROFILE_BASELINE;
  446. case H264SPS::kProfileIDCMain:
  447. return H264PROFILE_MAIN;
  448. case H264SPS::kProfileIDCHigh:
  449. return H264PROFILE_HIGH;
  450. case H264SPS::kProfileIDHigh10:
  451. return H264PROFILE_HIGH10PROFILE;
  452. case H264SPS::kProfileIDHigh422:
  453. return H264PROFILE_HIGH422PROFILE;
  454. case H264SPS::kProfileIDHigh444Predictive:
  455. return H264PROFILE_HIGH444PREDICTIVEPROFILE;
  456. case H264SPS::kProfileIDScalableBaseline:
  457. return H264PROFILE_SCALABLEBASELINE;
  458. case H264SPS::kProfileIDScalableHigh:
  459. return H264PROFILE_SCALABLEHIGH;
  460. case H264SPS::kProfileIDStereoHigh:
  461. return H264PROFILE_STEREOHIGH;
  462. case H264SPS::kProfileIDSMultiviewHigh:
  463. return H264PROFILE_MULTIVIEWHIGH;
  464. }
  465. DVLOG(1) << "unknown video profile: " << profile_idc;
  466. return VIDEO_CODEC_PROFILE_UNKNOWN;
  467. }
  468. // static
  469. bool H264Parser::ParseNALUs(const uint8_t* stream,
  470. size_t stream_size,
  471. std::vector<H264NALU>* nalus) {
  472. DCHECK(nalus);
  473. H264Parser parser;
  474. parser.SetStream(stream, stream_size);
  475. while (true) {
  476. H264NALU nalu;
  477. const H264Parser::Result result = parser.AdvanceToNextNALU(&nalu);
  478. if (result == H264Parser::kOk) {
  479. nalus->push_back(nalu);
  480. } else if (result == media::H264Parser::kEOStream) {
  481. return true;
  482. } else {
  483. DLOG(ERROR) << "Unexpected H264 parser result";
  484. return false;
  485. }
  486. }
  487. NOTREACHED();
  488. return false;
  489. }
  490. H264Parser::Result H264Parser::ReadUE(int* val) {
  491. int num_bits = -1;
  492. int bit;
  493. int rest;
  494. // Count the number of contiguous zero bits.
  495. do {
  496. READ_BITS_OR_RETURN(1, &bit);
  497. num_bits++;
  498. } while (bit == 0);
  499. if (num_bits > 31)
  500. return kInvalidStream;
  501. // Calculate exp-Golomb code value of size num_bits.
  502. // Special case for |num_bits| == 31 to avoid integer overflow. The only
  503. // valid representation as an int is 2^31 - 1, so the remaining bits must
  504. // be 0 or else the number is too large.
  505. *val = (1u << num_bits) - 1u;
  506. if (num_bits == 31) {
  507. READ_BITS_OR_RETURN(num_bits, &rest);
  508. return (rest == 0) ? kOk : kInvalidStream;
  509. }
  510. if (num_bits > 0) {
  511. READ_BITS_OR_RETURN(num_bits, &rest);
  512. *val += rest;
  513. }
  514. return kOk;
  515. }
  516. H264Parser::Result H264Parser::ReadSE(int* val) {
  517. int ue;
  518. Result res;
  519. // See Chapter 9 in the spec.
  520. res = ReadUE(&ue);
  521. if (res != kOk)
  522. return res;
  523. if (ue % 2 == 0)
  524. *val = -(ue / 2);
  525. else
  526. *val = ue / 2 + 1;
  527. return kOk;
  528. }
  529. H264Parser::Result H264Parser::AdvanceToNextNALU(H264NALU* nalu) {
  530. off_t start_code_size;
  531. off_t nalu_size_with_start_code;
  532. if (!LocateNALU(&nalu_size_with_start_code, &start_code_size)) {
  533. DVLOG(4) << "Could not find next NALU, bytes left in stream: "
  534. << bytes_left_;
  535. stream_ = nullptr;
  536. bytes_left_ = 0;
  537. return kEOStream;
  538. }
  539. nalu->data = stream_ + start_code_size;
  540. nalu->size = nalu_size_with_start_code - start_code_size;
  541. DVLOG(4) << "NALU found: size=" << nalu_size_with_start_code;
  542. // Initialize bit reader at the start of found NALU.
  543. if (!br_.Initialize(nalu->data, nalu->size)) {
  544. stream_ = nullptr;
  545. bytes_left_ = 0;
  546. return kEOStream;
  547. }
  548. // Move parser state to after this NALU, so next time AdvanceToNextNALU
  549. // is called, we will effectively be skipping it;
  550. // other parsing functions will use the position saved
  551. // in bit reader for parsing, so we don't have to remember it here.
  552. stream_ += nalu_size_with_start_code;
  553. bytes_left_ -= nalu_size_with_start_code;
  554. // Read NALU header, skip the forbidden_zero_bit, but check for it.
  555. int data;
  556. READ_BITS_OR_RETURN(1, &data);
  557. TRUE_OR_RETURN(data == 0);
  558. READ_BITS_OR_RETURN(2, &nalu->nal_ref_idc);
  559. READ_BITS_OR_RETURN(5, &nalu->nal_unit_type);
  560. DVLOG(4) << "NALU type: " << static_cast<int>(nalu->nal_unit_type)
  561. << " at: " << reinterpret_cast<const void*>(nalu->data)
  562. << " size: " << nalu->size
  563. << " ref: " << static_cast<int>(nalu->nal_ref_idc);
  564. previous_nalu_range_.clear();
  565. previous_nalu_range_.Add(nalu->data, nalu->data + nalu->size);
  566. return kOk;
  567. }
  568. // Default scaling lists (per spec).
  569. static const int kDefault4x4Intra[kH264ScalingList4x4Length] = {
  570. 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42,
  571. };
  572. static const int kDefault4x4Inter[kH264ScalingList4x4Length] = {
  573. 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34,
  574. };
  575. static const int kDefault8x8Intra[kH264ScalingList8x8Length] = {
  576. 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, 18, 18, 18, 23,
  577. 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27,
  578. 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31,
  579. 31, 33, 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42,
  580. };
  581. static const int kDefault8x8Inter[kH264ScalingList8x8Length] = {
  582. 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, 19, 19, 19, 21,
  583. 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, 24,
  584. 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27,
  585. 27, 28, 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35,
  586. };
  587. static inline void DefaultScalingList4x4(
  588. int i,
  589. int scaling_list4x4[][kH264ScalingList4x4Length]) {
  590. DCHECK_LT(i, 6);
  591. if (i < 3)
  592. memcpy(scaling_list4x4[i], kDefault4x4Intra, sizeof(kDefault4x4Intra));
  593. else if (i < 6)
  594. memcpy(scaling_list4x4[i], kDefault4x4Inter, sizeof(kDefault4x4Inter));
  595. }
  596. static inline void DefaultScalingList8x8(
  597. int i,
  598. int scaling_list8x8[][kH264ScalingList8x8Length]) {
  599. DCHECK_LT(i, 6);
  600. if (i % 2 == 0)
  601. memcpy(scaling_list8x8[i], kDefault8x8Intra, sizeof(kDefault8x8Intra));
  602. else
  603. memcpy(scaling_list8x8[i], kDefault8x8Inter, sizeof(kDefault8x8Inter));
  604. }
  605. static void FallbackScalingList4x4(
  606. int i,
  607. const int default_scaling_list_intra[],
  608. const int default_scaling_list_inter[],
  609. int scaling_list4x4[][kH264ScalingList4x4Length]) {
  610. static const int kScalingList4x4ByteSize =
  611. sizeof(scaling_list4x4[0][0]) * kH264ScalingList4x4Length;
  612. switch (i) {
  613. case 0:
  614. memcpy(scaling_list4x4[i], default_scaling_list_intra,
  615. kScalingList4x4ByteSize);
  616. break;
  617. case 1:
  618. memcpy(scaling_list4x4[i], scaling_list4x4[0], kScalingList4x4ByteSize);
  619. break;
  620. case 2:
  621. memcpy(scaling_list4x4[i], scaling_list4x4[1], kScalingList4x4ByteSize);
  622. break;
  623. case 3:
  624. memcpy(scaling_list4x4[i], default_scaling_list_inter,
  625. kScalingList4x4ByteSize);
  626. break;
  627. case 4:
  628. memcpy(scaling_list4x4[i], scaling_list4x4[3], kScalingList4x4ByteSize);
  629. break;
  630. case 5:
  631. memcpy(scaling_list4x4[i], scaling_list4x4[4], kScalingList4x4ByteSize);
  632. break;
  633. default:
  634. NOTREACHED();
  635. break;
  636. }
  637. }
  638. static void FallbackScalingList8x8(
  639. int i,
  640. const int default_scaling_list_intra[],
  641. const int default_scaling_list_inter[],
  642. int scaling_list8x8[][kH264ScalingList8x8Length]) {
  643. static const int kScalingList8x8ByteSize =
  644. sizeof(scaling_list8x8[0][0]) * kH264ScalingList8x8Length;
  645. switch (i) {
  646. case 0:
  647. memcpy(scaling_list8x8[i], default_scaling_list_intra,
  648. kScalingList8x8ByteSize);
  649. break;
  650. case 1:
  651. memcpy(scaling_list8x8[i], default_scaling_list_inter,
  652. kScalingList8x8ByteSize);
  653. break;
  654. case 2:
  655. memcpy(scaling_list8x8[i], scaling_list8x8[0], kScalingList8x8ByteSize);
  656. break;
  657. case 3:
  658. memcpy(scaling_list8x8[i], scaling_list8x8[1], kScalingList8x8ByteSize);
  659. break;
  660. case 4:
  661. memcpy(scaling_list8x8[i], scaling_list8x8[2], kScalingList8x8ByteSize);
  662. break;
  663. case 5:
  664. memcpy(scaling_list8x8[i], scaling_list8x8[3], kScalingList8x8ByteSize);
  665. break;
  666. default:
  667. NOTREACHED();
  668. break;
  669. }
  670. }
  671. H264Parser::Result H264Parser::ParseScalingList(int size,
  672. int* scaling_list,
  673. bool* use_default) {
  674. // See chapter 7.3.2.1.1.1.
  675. int last_scale = 8;
  676. int next_scale = 8;
  677. int delta_scale;
  678. *use_default = false;
  679. for (int j = 0; j < size; ++j) {
  680. if (next_scale != 0) {
  681. READ_SE_OR_RETURN(&delta_scale);
  682. IN_RANGE_OR_RETURN(delta_scale, -128, 127);
  683. next_scale = (last_scale + delta_scale + 256) & 0xff;
  684. if (j == 0 && next_scale == 0) {
  685. *use_default = true;
  686. return kOk;
  687. }
  688. }
  689. scaling_list[j] = (next_scale == 0) ? last_scale : next_scale;
  690. last_scale = scaling_list[j];
  691. }
  692. return kOk;
  693. }
  694. H264Parser::Result H264Parser::ParseSPSScalingLists(H264SPS* sps) {
  695. // See 7.4.2.1.1.
  696. bool seq_scaling_list_present_flag;
  697. bool use_default;
  698. Result res;
  699. // Parse scaling_list4x4.
  700. for (int i = 0; i < 6; ++i) {
  701. READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
  702. if (seq_scaling_list_present_flag) {
  703. res = ParseScalingList(std::size(sps->scaling_list4x4[i]),
  704. sps->scaling_list4x4[i], &use_default);
  705. if (res != kOk)
  706. return res;
  707. if (use_default)
  708. DefaultScalingList4x4(i, sps->scaling_list4x4);
  709. } else {
  710. FallbackScalingList4x4(i, kDefault4x4Intra, kDefault4x4Inter,
  711. sps->scaling_list4x4);
  712. }
  713. }
  714. // Parse scaling_list8x8.
  715. for (int i = 0; i < ((sps->chroma_format_idc != 3) ? 2 : 6); ++i) {
  716. READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
  717. if (seq_scaling_list_present_flag) {
  718. res = ParseScalingList(std::size(sps->scaling_list8x8[i]),
  719. sps->scaling_list8x8[i], &use_default);
  720. if (res != kOk)
  721. return res;
  722. if (use_default)
  723. DefaultScalingList8x8(i, sps->scaling_list8x8);
  724. } else {
  725. FallbackScalingList8x8(i, kDefault8x8Intra, kDefault8x8Inter,
  726. sps->scaling_list8x8);
  727. }
  728. }
  729. return kOk;
  730. }
  731. H264Parser::Result H264Parser::ParsePPSScalingLists(const H264SPS& sps,
  732. H264PPS* pps) {
  733. // See 7.4.2.2.
  734. bool pic_scaling_list_present_flag;
  735. bool use_default;
  736. Result res;
  737. for (int i = 0; i < 6; ++i) {
  738. READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
  739. if (pic_scaling_list_present_flag) {
  740. res = ParseScalingList(std::size(pps->scaling_list4x4[i]),
  741. pps->scaling_list4x4[i], &use_default);
  742. if (res != kOk)
  743. return res;
  744. if (use_default)
  745. DefaultScalingList4x4(i, pps->scaling_list4x4);
  746. } else {
  747. if (!sps.seq_scaling_matrix_present_flag) {
  748. // Table 7-2 fallback rule A in spec.
  749. FallbackScalingList4x4(i, kDefault4x4Intra, kDefault4x4Inter,
  750. pps->scaling_list4x4);
  751. } else {
  752. // Table 7-2 fallback rule B in spec.
  753. FallbackScalingList4x4(i, sps.scaling_list4x4[0],
  754. sps.scaling_list4x4[3], pps->scaling_list4x4);
  755. }
  756. }
  757. }
  758. if (pps->transform_8x8_mode_flag) {
  759. for (int i = 0; i < ((sps.chroma_format_idc != 3) ? 2 : 6); ++i) {
  760. READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
  761. if (pic_scaling_list_present_flag) {
  762. res = ParseScalingList(std::size(pps->scaling_list8x8[i]),
  763. pps->scaling_list8x8[i], &use_default);
  764. if (res != kOk)
  765. return res;
  766. if (use_default)
  767. DefaultScalingList8x8(i, pps->scaling_list8x8);
  768. } else {
  769. if (!sps.seq_scaling_matrix_present_flag) {
  770. // Table 7-2 fallback rule A in spec.
  771. FallbackScalingList8x8(i, kDefault8x8Intra, kDefault8x8Inter,
  772. pps->scaling_list8x8);
  773. } else {
  774. // Table 7-2 fallback rule B in spec.
  775. FallbackScalingList8x8(i, sps.scaling_list8x8[0],
  776. sps.scaling_list8x8[1], pps->scaling_list8x8);
  777. }
  778. }
  779. }
  780. }
  781. return kOk;
  782. }
  783. H264Parser::Result H264Parser::ParseAndIgnoreHRDParameters(
  784. bool* hrd_parameters_present) {
  785. int data;
  786. READ_BOOL_OR_RETURN(&data); // {nal,vcl}_hrd_parameters_present_flag
  787. if (!data)
  788. return kOk;
  789. *hrd_parameters_present = true;
  790. int cpb_cnt_minus1;
  791. READ_UE_OR_RETURN(&cpb_cnt_minus1);
  792. IN_RANGE_OR_RETURN(cpb_cnt_minus1, 0, 31);
  793. READ_BITS_OR_RETURN(8, &data); // bit_rate_scale, cpb_size_scale
  794. for (int i = 0; i <= cpb_cnt_minus1; ++i) {
  795. READ_UE_OR_RETURN(&data); // bit_rate_value_minus1[i]
  796. READ_UE_OR_RETURN(&data); // cpb_size_value_minus1[i]
  797. READ_BOOL_OR_RETURN(&data); // cbr_flag
  798. }
  799. READ_BITS_OR_RETURN(20, &data); // cpb/dpb delays, etc.
  800. return kOk;
  801. }
  802. H264Parser::Result H264Parser::ParseVUIParameters(H264SPS* sps) {
  803. bool aspect_ratio_info_present_flag;
  804. READ_BOOL_OR_RETURN(&aspect_ratio_info_present_flag);
  805. if (aspect_ratio_info_present_flag) {
  806. int aspect_ratio_idc;
  807. READ_BITS_OR_RETURN(8, &aspect_ratio_idc);
  808. if (aspect_ratio_idc == H264SPS::kExtendedSar) {
  809. READ_BITS_OR_RETURN(16, &sps->sar_width);
  810. READ_BITS_OR_RETURN(16, &sps->sar_height);
  811. } else {
  812. const int max_aspect_ratio_idc = std::size(kTableSarWidth) - 1;
  813. IN_RANGE_OR_RETURN(aspect_ratio_idc, 0, max_aspect_ratio_idc);
  814. sps->sar_width = kTableSarWidth[aspect_ratio_idc];
  815. sps->sar_height = kTableSarHeight[aspect_ratio_idc];
  816. }
  817. }
  818. int data;
  819. // Read and ignore overscan and video signal type info.
  820. READ_BOOL_OR_RETURN(&data); // overscan_info_present_flag
  821. if (data)
  822. READ_BOOL_OR_RETURN(&data); // overscan_appropriate_flag
  823. READ_BOOL_OR_RETURN(&sps->video_signal_type_present_flag);
  824. if (sps->video_signal_type_present_flag) {
  825. READ_BITS_OR_RETURN(3, &sps->video_format);
  826. READ_BOOL_OR_RETURN(&sps->video_full_range_flag);
  827. READ_BOOL_OR_RETURN(&sps->colour_description_present_flag);
  828. if (sps->colour_description_present_flag) {
  829. // color description syntax elements
  830. READ_BITS_OR_RETURN(8, &sps->colour_primaries);
  831. READ_BITS_OR_RETURN(8, &sps->transfer_characteristics);
  832. READ_BITS_OR_RETURN(8, &sps->matrix_coefficients);
  833. }
  834. }
  835. READ_BOOL_OR_RETURN(&data); // chroma_loc_info_present_flag
  836. if (data) {
  837. READ_UE_OR_RETURN(&data); // chroma_sample_loc_type_top_field
  838. READ_UE_OR_RETURN(&data); // chroma_sample_loc_type_bottom_field
  839. }
  840. // Read and ignore timing info.
  841. READ_BOOL_OR_RETURN(&data); // timing_info_present_flag
  842. if (data) {
  843. READ_BITS_OR_RETURN(16, &data); // num_units_in_tick
  844. READ_BITS_OR_RETURN(16, &data); // num_units_in_tick
  845. READ_BITS_OR_RETURN(16, &data); // time_scale
  846. READ_BITS_OR_RETURN(16, &data); // time_scale
  847. READ_BOOL_OR_RETURN(&data); // fixed_frame_rate_flag
  848. }
  849. // Read and ignore NAL HRD parameters, if present.
  850. bool hrd_parameters_present = false;
  851. Result res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
  852. if (res != kOk)
  853. return res;
  854. // Read and ignore VCL HRD parameters, if present.
  855. res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
  856. if (res != kOk)
  857. return res;
  858. if (hrd_parameters_present) // One of NAL or VCL params present is enough.
  859. READ_BOOL_OR_RETURN(&data); // low_delay_hrd_flag
  860. READ_BOOL_OR_RETURN(&data); // pic_struct_present_flag
  861. READ_BOOL_OR_RETURN(&sps->bitstream_restriction_flag);
  862. if (sps->bitstream_restriction_flag) {
  863. READ_BOOL_OR_RETURN(&data); // motion_vectors_over_pic_boundaries_flag
  864. READ_UE_OR_RETURN(&data); // max_bytes_per_pic_denom
  865. READ_UE_OR_RETURN(&data); // max_bits_per_mb_denom
  866. READ_UE_OR_RETURN(&data); // log2_max_mv_length_horizontal
  867. READ_UE_OR_RETURN(&data); // log2_max_mv_length_vertical
  868. READ_UE_OR_RETURN(&sps->max_num_reorder_frames);
  869. READ_UE_OR_RETURN(&sps->max_dec_frame_buffering);
  870. TRUE_OR_RETURN(sps->max_dec_frame_buffering >= sps->max_num_ref_frames);
  871. IN_RANGE_OR_RETURN(sps->max_num_reorder_frames, 0,
  872. sps->max_dec_frame_buffering);
  873. }
  874. return kOk;
  875. }
  876. static void FillDefaultSeqScalingLists(H264SPS* sps) {
  877. for (int i = 0; i < 6; ++i)
  878. for (int j = 0; j < kH264ScalingList4x4Length; ++j)
  879. sps->scaling_list4x4[i][j] = 16;
  880. for (int i = 0; i < 6; ++i)
  881. for (int j = 0; j < kH264ScalingList8x8Length; ++j)
  882. sps->scaling_list8x8[i][j] = 16;
  883. }
  884. H264Parser::Result H264Parser::ParseSPS(int* sps_id) {
  885. // See 7.4.2.1.
  886. int data;
  887. Result res;
  888. *sps_id = -1;
  889. std::unique_ptr<H264SPS> sps(new H264SPS());
  890. READ_BITS_OR_RETURN(8, &sps->profile_idc);
  891. READ_BOOL_OR_RETURN(&sps->constraint_set0_flag);
  892. READ_BOOL_OR_RETURN(&sps->constraint_set1_flag);
  893. READ_BOOL_OR_RETURN(&sps->constraint_set2_flag);
  894. READ_BOOL_OR_RETURN(&sps->constraint_set3_flag);
  895. READ_BOOL_OR_RETURN(&sps->constraint_set4_flag);
  896. READ_BOOL_OR_RETURN(&sps->constraint_set5_flag);
  897. READ_BITS_OR_RETURN(2, &data); // reserved_zero_2bits
  898. READ_BITS_OR_RETURN(8, &sps->level_idc);
  899. READ_UE_OR_RETURN(&sps->seq_parameter_set_id);
  900. TRUE_OR_RETURN(sps->seq_parameter_set_id < 32);
  901. if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
  902. sps->profile_idc == 122 || sps->profile_idc == 244 ||
  903. sps->profile_idc == 44 || sps->profile_idc == 83 ||
  904. sps->profile_idc == 86 || sps->profile_idc == 118 ||
  905. sps->profile_idc == 128) {
  906. READ_UE_OR_RETURN(&sps->chroma_format_idc);
  907. TRUE_OR_RETURN(sps->chroma_format_idc < 4);
  908. if (sps->chroma_format_idc == 3)
  909. READ_BOOL_OR_RETURN(&sps->separate_colour_plane_flag);
  910. READ_UE_OR_RETURN(&sps->bit_depth_luma_minus8);
  911. TRUE_OR_RETURN(sps->bit_depth_luma_minus8 < 7);
  912. READ_UE_OR_RETURN(&sps->bit_depth_chroma_minus8);
  913. TRUE_OR_RETURN(sps->bit_depth_chroma_minus8 < 7);
  914. READ_BOOL_OR_RETURN(&sps->qpprime_y_zero_transform_bypass_flag);
  915. READ_BOOL_OR_RETURN(&sps->seq_scaling_matrix_present_flag);
  916. if (sps->seq_scaling_matrix_present_flag) {
  917. DVLOG(4) << "Scaling matrix present";
  918. res = ParseSPSScalingLists(sps.get());
  919. if (res != kOk)
  920. return res;
  921. } else {
  922. FillDefaultSeqScalingLists(sps.get());
  923. }
  924. } else {
  925. sps->chroma_format_idc = 1;
  926. FillDefaultSeqScalingLists(sps.get());
  927. }
  928. if (sps->separate_colour_plane_flag)
  929. sps->chroma_array_type = 0;
  930. else
  931. sps->chroma_array_type = sps->chroma_format_idc;
  932. READ_UE_OR_RETURN(&sps->log2_max_frame_num_minus4);
  933. TRUE_OR_RETURN(sps->log2_max_frame_num_minus4 < 13);
  934. READ_UE_OR_RETURN(&sps->pic_order_cnt_type);
  935. TRUE_OR_RETURN(sps->pic_order_cnt_type < 3);
  936. if (sps->pic_order_cnt_type == 0) {
  937. READ_UE_OR_RETURN(&sps->log2_max_pic_order_cnt_lsb_minus4);
  938. TRUE_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 < 13);
  939. sps->expected_delta_per_pic_order_cnt_cycle = 0;
  940. } else if (sps->pic_order_cnt_type == 1) {
  941. READ_BOOL_OR_RETURN(&sps->delta_pic_order_always_zero_flag);
  942. READ_SE_OR_RETURN(&sps->offset_for_non_ref_pic);
  943. READ_SE_OR_RETURN(&sps->offset_for_top_to_bottom_field);
  944. READ_UE_OR_RETURN(&sps->num_ref_frames_in_pic_order_cnt_cycle);
  945. TRUE_OR_RETURN(sps->num_ref_frames_in_pic_order_cnt_cycle < 255);
  946. base::CheckedNumeric<int> offset_acc = 0;
  947. for (int i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; ++i) {
  948. READ_SE_OR_RETURN(&sps->offset_for_ref_frame[i]);
  949. offset_acc += sps->offset_for_ref_frame[i];
  950. }
  951. if (!offset_acc.IsValid())
  952. return kInvalidStream;
  953. sps->expected_delta_per_pic_order_cnt_cycle = offset_acc.ValueOrDefault(0);
  954. }
  955. READ_UE_OR_RETURN(&sps->max_num_ref_frames);
  956. READ_BOOL_OR_RETURN(&sps->gaps_in_frame_num_value_allowed_flag);
  957. READ_UE_OR_RETURN(&sps->pic_width_in_mbs_minus1);
  958. READ_UE_OR_RETURN(&sps->pic_height_in_map_units_minus1);
  959. READ_BOOL_OR_RETURN(&sps->frame_mbs_only_flag);
  960. if (!sps->frame_mbs_only_flag)
  961. READ_BOOL_OR_RETURN(&sps->mb_adaptive_frame_field_flag);
  962. READ_BOOL_OR_RETURN(&sps->direct_8x8_inference_flag);
  963. READ_BOOL_OR_RETURN(&sps->frame_cropping_flag);
  964. if (sps->frame_cropping_flag) {
  965. READ_UE_OR_RETURN(&sps->frame_crop_left_offset);
  966. READ_UE_OR_RETURN(&sps->frame_crop_right_offset);
  967. READ_UE_OR_RETURN(&sps->frame_crop_top_offset);
  968. READ_UE_OR_RETURN(&sps->frame_crop_bottom_offset);
  969. }
  970. READ_BOOL_OR_RETURN(&sps->vui_parameters_present_flag);
  971. if (sps->vui_parameters_present_flag) {
  972. DVLOG(4) << "VUI parameters present";
  973. res = ParseVUIParameters(sps.get());
  974. if (res != kOk)
  975. return res;
  976. }
  977. // If an SPS with the same id already exists, replace it.
  978. *sps_id = sps->seq_parameter_set_id;
  979. active_SPSes_[*sps_id] = std::move(sps);
  980. return kOk;
  981. }
  982. H264Parser::Result H264Parser::ParsePPS(int* pps_id) {
  983. // See 7.4.2.2.
  984. const H264SPS* sps;
  985. Result res;
  986. *pps_id = -1;
  987. std::unique_ptr<H264PPS> pps(new H264PPS());
  988. READ_UE_OR_RETURN(&pps->pic_parameter_set_id);
  989. READ_UE_OR_RETURN(&pps->seq_parameter_set_id);
  990. TRUE_OR_RETURN(pps->seq_parameter_set_id < 32);
  991. if (active_SPSes_.find(pps->seq_parameter_set_id) == active_SPSes_.end()) {
  992. DVLOG(1) << "Invalid stream, no SPS id: " << pps->seq_parameter_set_id;
  993. return kInvalidStream;
  994. }
  995. sps = GetSPS(pps->seq_parameter_set_id);
  996. TRUE_OR_RETURN(sps);
  997. READ_BOOL_OR_RETURN(&pps->entropy_coding_mode_flag);
  998. READ_BOOL_OR_RETURN(&pps->bottom_field_pic_order_in_frame_present_flag);
  999. READ_UE_OR_RETURN(&pps->num_slice_groups_minus1);
  1000. if (pps->num_slice_groups_minus1 > 1) {
  1001. DVLOG(1) << "Slice groups not supported";
  1002. return kUnsupportedStream;
  1003. }
  1004. READ_UE_OR_RETURN(&pps->num_ref_idx_l0_default_active_minus1);
  1005. TRUE_OR_RETURN(pps->num_ref_idx_l0_default_active_minus1 < 32);
  1006. READ_UE_OR_RETURN(&pps->num_ref_idx_l1_default_active_minus1);
  1007. TRUE_OR_RETURN(pps->num_ref_idx_l1_default_active_minus1 < 32);
  1008. READ_BOOL_OR_RETURN(&pps->weighted_pred_flag);
  1009. READ_BITS_OR_RETURN(2, &pps->weighted_bipred_idc);
  1010. TRUE_OR_RETURN(pps->weighted_bipred_idc < 3);
  1011. READ_SE_OR_RETURN(&pps->pic_init_qp_minus26);
  1012. IN_RANGE_OR_RETURN(pps->pic_init_qp_minus26, -26, 25);
  1013. READ_SE_OR_RETURN(&pps->pic_init_qs_minus26);
  1014. IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25);
  1015. READ_SE_OR_RETURN(&pps->chroma_qp_index_offset);
  1016. IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12);
  1017. pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
  1018. READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag);
  1019. READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag);
  1020. READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag);
  1021. bool pps_remainder_unencrypted = true;
  1022. if (encrypted_ranges_.size()) {
  1023. Ranges<const uint8_t*> pps_range;
  1024. // Only check that the next byte is unencrypted, not the rest of the NALU.
  1025. const uint8_t* next_byte =
  1026. previous_nalu_range_.end(0) - br_.NumBitsLeft() / 8;
  1027. pps_range.Add(next_byte, next_byte + 1);
  1028. pps_remainder_unencrypted =
  1029. (encrypted_ranges_.IntersectionWith(pps_range).size() == 0);
  1030. }
  1031. if (pps_remainder_unencrypted && br_.HasMoreRBSPData()) {
  1032. READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag);
  1033. READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag);
  1034. if (pps->pic_scaling_matrix_present_flag) {
  1035. DVLOG(4) << "Picture scaling matrix present";
  1036. res = ParsePPSScalingLists(*sps, pps.get());
  1037. if (res != kOk)
  1038. return res;
  1039. }
  1040. READ_SE_OR_RETURN(&pps->second_chroma_qp_index_offset);
  1041. }
  1042. // If a PPS with the same id already exists, replace it.
  1043. *pps_id = pps->pic_parameter_set_id;
  1044. active_PPSes_[*pps_id] = std::move(pps);
  1045. return kOk;
  1046. }
  1047. H264Parser::Result H264Parser::ParseSPSExt(int* sps_id) {
  1048. // See 7.4.2.1.
  1049. int local_sps_id = -1;
  1050. *sps_id = -1;
  1051. READ_UE_OR_RETURN(&local_sps_id);
  1052. TRUE_OR_RETURN(local_sps_id < 32);
  1053. *sps_id = local_sps_id;
  1054. return kOk;
  1055. }
  1056. H264Parser::Result H264Parser::ParseRefPicListModification(
  1057. int num_ref_idx_active_minus1,
  1058. H264ModificationOfPicNum* ref_list_mods) {
  1059. H264ModificationOfPicNum* pic_num_mod;
  1060. if (num_ref_idx_active_minus1 >= 32)
  1061. return kInvalidStream;
  1062. for (int i = 0; i < 32; ++i) {
  1063. pic_num_mod = &ref_list_mods[i];
  1064. READ_UE_OR_RETURN(&pic_num_mod->modification_of_pic_nums_idc);
  1065. TRUE_OR_RETURN(pic_num_mod->modification_of_pic_nums_idc < 4);
  1066. switch (pic_num_mod->modification_of_pic_nums_idc) {
  1067. case 0:
  1068. case 1:
  1069. READ_UE_OR_RETURN(&pic_num_mod->abs_diff_pic_num_minus1);
  1070. break;
  1071. case 2:
  1072. READ_UE_OR_RETURN(&pic_num_mod->long_term_pic_num);
  1073. break;
  1074. case 3:
  1075. // Per spec, list cannot be empty.
  1076. if (i == 0)
  1077. return kInvalidStream;
  1078. return kOk;
  1079. default:
  1080. return kInvalidStream;
  1081. }
  1082. }
  1083. // If we got here, we didn't get loop end marker prematurely,
  1084. // so make sure it is there for our client.
  1085. int modification_of_pic_nums_idc;
  1086. READ_UE_OR_RETURN(&modification_of_pic_nums_idc);
  1087. TRUE_OR_RETURN(modification_of_pic_nums_idc == 3);
  1088. return kOk;
  1089. }
  1090. H264Parser::Result H264Parser::ParseRefPicListModifications(
  1091. H264SliceHeader* shdr) {
  1092. Result res;
  1093. if (!shdr->IsISlice() && !shdr->IsSISlice()) {
  1094. READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l0);
  1095. if (shdr->ref_pic_list_modification_flag_l0) {
  1096. res = ParseRefPicListModification(shdr->num_ref_idx_l0_active_minus1,
  1097. shdr->ref_list_l0_modifications);
  1098. if (res != kOk)
  1099. return res;
  1100. }
  1101. }
  1102. if (shdr->IsBSlice()) {
  1103. READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l1);
  1104. if (shdr->ref_pic_list_modification_flag_l1) {
  1105. res = ParseRefPicListModification(shdr->num_ref_idx_l1_active_minus1,
  1106. shdr->ref_list_l1_modifications);
  1107. if (res != kOk)
  1108. return res;
  1109. }
  1110. }
  1111. return kOk;
  1112. }
  1113. H264Parser::Result H264Parser::ParseWeightingFactors(
  1114. int num_ref_idx_active_minus1,
  1115. int chroma_array_type,
  1116. int luma_log2_weight_denom,
  1117. int chroma_log2_weight_denom,
  1118. H264WeightingFactors* w_facts) {
  1119. int def_luma_weight = 1 << luma_log2_weight_denom;
  1120. int def_chroma_weight = 1 << chroma_log2_weight_denom;
  1121. for (int i = 0; i < num_ref_idx_active_minus1 + 1; ++i) {
  1122. READ_BOOL_OR_RETURN(&w_facts->luma_weight_flag);
  1123. if (w_facts->luma_weight_flag) {
  1124. READ_SE_OR_RETURN(&w_facts->luma_weight[i]);
  1125. IN_RANGE_OR_RETURN(w_facts->luma_weight[i], -128, 127);
  1126. READ_SE_OR_RETURN(&w_facts->luma_offset[i]);
  1127. IN_RANGE_OR_RETURN(w_facts->luma_offset[i], -128, 127);
  1128. } else {
  1129. w_facts->luma_weight[i] = def_luma_weight;
  1130. w_facts->luma_offset[i] = 0;
  1131. }
  1132. if (chroma_array_type != 0) {
  1133. READ_BOOL_OR_RETURN(&w_facts->chroma_weight_flag);
  1134. if (w_facts->chroma_weight_flag) {
  1135. for (int j = 0; j < 2; ++j) {
  1136. READ_SE_OR_RETURN(&w_facts->chroma_weight[i][j]);
  1137. IN_RANGE_OR_RETURN(w_facts->chroma_weight[i][j], -128, 127);
  1138. READ_SE_OR_RETURN(&w_facts->chroma_offset[i][j]);
  1139. IN_RANGE_OR_RETURN(w_facts->chroma_offset[i][j], -128, 127);
  1140. }
  1141. } else {
  1142. for (int j = 0; j < 2; ++j) {
  1143. w_facts->chroma_weight[i][j] = def_chroma_weight;
  1144. w_facts->chroma_offset[i][j] = 0;
  1145. }
  1146. }
  1147. }
  1148. }
  1149. return kOk;
  1150. }
  1151. H264Parser::Result H264Parser::ParsePredWeightTable(const H264SPS& sps,
  1152. H264SliceHeader* shdr) {
  1153. READ_UE_OR_RETURN(&shdr->luma_log2_weight_denom);
  1154. TRUE_OR_RETURN(shdr->luma_log2_weight_denom < 8);
  1155. if (sps.chroma_array_type != 0)
  1156. READ_UE_OR_RETURN(&shdr->chroma_log2_weight_denom);
  1157. TRUE_OR_RETURN(shdr->chroma_log2_weight_denom < 8);
  1158. Result res = ParseWeightingFactors(
  1159. shdr->num_ref_idx_l0_active_minus1, sps.chroma_array_type,
  1160. shdr->luma_log2_weight_denom, shdr->chroma_log2_weight_denom,
  1161. &shdr->pred_weight_table_l0);
  1162. if (res != kOk)
  1163. return res;
  1164. if (shdr->IsBSlice()) {
  1165. res = ParseWeightingFactors(
  1166. shdr->num_ref_idx_l1_active_minus1, sps.chroma_array_type,
  1167. shdr->luma_log2_weight_denom, shdr->chroma_log2_weight_denom,
  1168. &shdr->pred_weight_table_l1);
  1169. if (res != kOk)
  1170. return res;
  1171. }
  1172. return kOk;
  1173. }
  1174. H264Parser::Result H264Parser::ParseDecRefPicMarking(H264SliceHeader* shdr) {
  1175. size_t bits_left_at_start = br_.NumBitsLeft();
  1176. if (shdr->idr_pic_flag) {
  1177. READ_BOOL_OR_RETURN(&shdr->no_output_of_prior_pics_flag);
  1178. READ_BOOL_OR_RETURN(&shdr->long_term_reference_flag);
  1179. } else {
  1180. READ_BOOL_OR_RETURN(&shdr->adaptive_ref_pic_marking_mode_flag);
  1181. H264DecRefPicMarking* marking;
  1182. if (shdr->adaptive_ref_pic_marking_mode_flag) {
  1183. size_t i;
  1184. for (i = 0; i < std::size(shdr->ref_pic_marking); ++i) {
  1185. marking = &shdr->ref_pic_marking[i];
  1186. READ_UE_OR_RETURN(&marking->memory_mgmnt_control_operation);
  1187. if (marking->memory_mgmnt_control_operation == 0)
  1188. break;
  1189. if (marking->memory_mgmnt_control_operation == 1 ||
  1190. marking->memory_mgmnt_control_operation == 3)
  1191. READ_UE_OR_RETURN(&marking->difference_of_pic_nums_minus1);
  1192. if (marking->memory_mgmnt_control_operation == 2)
  1193. READ_UE_OR_RETURN(&marking->long_term_pic_num);
  1194. if (marking->memory_mgmnt_control_operation == 3 ||
  1195. marking->memory_mgmnt_control_operation == 6)
  1196. READ_UE_OR_RETURN(&marking->long_term_frame_idx);
  1197. if (marking->memory_mgmnt_control_operation == 4)
  1198. READ_UE_OR_RETURN(&marking->max_long_term_frame_idx_plus1);
  1199. if (marking->memory_mgmnt_control_operation > 6)
  1200. return kInvalidStream;
  1201. }
  1202. if (i == std::size(shdr->ref_pic_marking)) {
  1203. DVLOG(1) << "Ran out of dec ref pic marking fields";
  1204. return kUnsupportedStream;
  1205. }
  1206. }
  1207. }
  1208. shdr->dec_ref_pic_marking_bit_size = bits_left_at_start - br_.NumBitsLeft();
  1209. return kOk;
  1210. }
  1211. H264Parser::Result H264Parser::ParseSliceHeader(const H264NALU& nalu,
  1212. H264SliceHeader* shdr) {
  1213. // See 7.4.3.
  1214. const H264SPS* sps;
  1215. const H264PPS* pps;
  1216. Result res;
  1217. memset(shdr, 0, sizeof(*shdr));
  1218. shdr->idr_pic_flag = (nalu.nal_unit_type == 5);
  1219. shdr->nal_ref_idc = nalu.nal_ref_idc;
  1220. shdr->nalu_data = nalu.data;
  1221. shdr->nalu_size = nalu.size;
  1222. READ_UE_OR_RETURN(&shdr->first_mb_in_slice);
  1223. READ_UE_OR_RETURN(&shdr->slice_type);
  1224. TRUE_OR_RETURN(shdr->slice_type < 10);
  1225. READ_UE_OR_RETURN(&shdr->pic_parameter_set_id);
  1226. pps = GetPPS(shdr->pic_parameter_set_id);
  1227. TRUE_OR_RETURN(pps);
  1228. sps = GetSPS(pps->seq_parameter_set_id);
  1229. TRUE_OR_RETURN(sps);
  1230. if (sps->separate_colour_plane_flag) {
  1231. DVLOG(1) << "Interlaced streams not supported";
  1232. return kUnsupportedStream;
  1233. }
  1234. READ_BITS_OR_RETURN(sps->log2_max_frame_num_minus4 + 4, &shdr->frame_num);
  1235. if (!sps->frame_mbs_only_flag) {
  1236. READ_BOOL_OR_RETURN(&shdr->field_pic_flag);
  1237. if (shdr->field_pic_flag) {
  1238. DVLOG(1) << "Interlaced streams not supported";
  1239. return kUnsupportedStream;
  1240. }
  1241. }
  1242. if (shdr->idr_pic_flag)
  1243. READ_UE_OR_RETURN(&shdr->idr_pic_id);
  1244. size_t bits_left_at_pic_order_cnt_start = br_.NumBitsLeft();
  1245. if (sps->pic_order_cnt_type == 0) {
  1246. READ_BITS_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
  1247. &shdr->pic_order_cnt_lsb);
  1248. if (pps->bottom_field_pic_order_in_frame_present_flag &&
  1249. !shdr->field_pic_flag)
  1250. READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt_bottom);
  1251. }
  1252. if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
  1253. READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt0);
  1254. if (pps->bottom_field_pic_order_in_frame_present_flag &&
  1255. !shdr->field_pic_flag)
  1256. READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt1);
  1257. }
  1258. shdr->pic_order_cnt_bit_size =
  1259. bits_left_at_pic_order_cnt_start - br_.NumBitsLeft();
  1260. if (pps->redundant_pic_cnt_present_flag) {
  1261. READ_UE_OR_RETURN(&shdr->redundant_pic_cnt);
  1262. TRUE_OR_RETURN(shdr->redundant_pic_cnt < 128);
  1263. }
  1264. if (shdr->IsBSlice())
  1265. READ_BOOL_OR_RETURN(&shdr->direct_spatial_mv_pred_flag);
  1266. if (shdr->IsPSlice() || shdr->IsSPSlice() || shdr->IsBSlice()) {
  1267. READ_BOOL_OR_RETURN(&shdr->num_ref_idx_active_override_flag);
  1268. if (shdr->num_ref_idx_active_override_flag) {
  1269. READ_UE_OR_RETURN(&shdr->num_ref_idx_l0_active_minus1);
  1270. if (shdr->IsBSlice())
  1271. READ_UE_OR_RETURN(&shdr->num_ref_idx_l1_active_minus1);
  1272. } else {
  1273. shdr->num_ref_idx_l0_active_minus1 =
  1274. pps->num_ref_idx_l0_default_active_minus1;
  1275. if (shdr->IsBSlice()) {
  1276. shdr->num_ref_idx_l1_active_minus1 =
  1277. pps->num_ref_idx_l1_default_active_minus1;
  1278. }
  1279. }
  1280. }
  1281. if (shdr->field_pic_flag) {
  1282. TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 32);
  1283. TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 32);
  1284. } else {
  1285. TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 16);
  1286. TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 16);
  1287. }
  1288. if (nalu.nal_unit_type == H264NALU::kCodedSliceExtension) {
  1289. return kUnsupportedStream;
  1290. } else {
  1291. res = ParseRefPicListModifications(shdr);
  1292. if (res != kOk)
  1293. return res;
  1294. }
  1295. if ((pps->weighted_pred_flag && (shdr->IsPSlice() || shdr->IsSPSlice())) ||
  1296. (pps->weighted_bipred_idc == 1 && shdr->IsBSlice())) {
  1297. res = ParsePredWeightTable(*sps, shdr);
  1298. if (res != kOk)
  1299. return res;
  1300. }
  1301. if (nalu.nal_ref_idc != 0) {
  1302. res = ParseDecRefPicMarking(shdr);
  1303. if (res != kOk)
  1304. return res;
  1305. }
  1306. if (pps->entropy_coding_mode_flag && !shdr->IsISlice() &&
  1307. !shdr->IsSISlice()) {
  1308. READ_UE_OR_RETURN(&shdr->cabac_init_idc);
  1309. TRUE_OR_RETURN(shdr->cabac_init_idc < 3);
  1310. }
  1311. READ_SE_OR_RETURN(&shdr->slice_qp_delta);
  1312. if (shdr->IsSPSlice() || shdr->IsSISlice()) {
  1313. if (shdr->IsSPSlice())
  1314. READ_BOOL_OR_RETURN(&shdr->sp_for_switch_flag);
  1315. READ_SE_OR_RETURN(&shdr->slice_qs_delta);
  1316. }
  1317. if (pps->deblocking_filter_control_present_flag) {
  1318. READ_UE_OR_RETURN(&shdr->disable_deblocking_filter_idc);
  1319. TRUE_OR_RETURN(shdr->disable_deblocking_filter_idc < 3);
  1320. if (shdr->disable_deblocking_filter_idc != 1) {
  1321. READ_SE_OR_RETURN(&shdr->slice_alpha_c0_offset_div2);
  1322. IN_RANGE_OR_RETURN(shdr->slice_alpha_c0_offset_div2, -6, 6);
  1323. READ_SE_OR_RETURN(&shdr->slice_beta_offset_div2);
  1324. IN_RANGE_OR_RETURN(shdr->slice_beta_offset_div2, -6, 6);
  1325. }
  1326. }
  1327. if (pps->num_slice_groups_minus1 > 0) {
  1328. DVLOG(1) << "Slice groups not supported";
  1329. return kUnsupportedStream;
  1330. }
  1331. size_t epb = br_.NumEmulationPreventionBytesRead();
  1332. shdr->header_bit_size = (shdr->nalu_size - epb) * 8 - br_.NumBitsLeft();
  1333. return kOk;
  1334. }
  1335. H264Parser::Result H264Parser::ParseSEI(H264SEIMessage* sei_msg) {
  1336. int byte;
  1337. memset(sei_msg, 0, sizeof(*sei_msg));
  1338. READ_BITS_OR_RETURN(8, &byte);
  1339. while (byte == 0xff) {
  1340. sei_msg->type += 255;
  1341. READ_BITS_OR_RETURN(8, &byte);
  1342. }
  1343. sei_msg->type += byte;
  1344. READ_BITS_OR_RETURN(8, &byte);
  1345. while (byte == 0xff) {
  1346. sei_msg->payload_size += 255;
  1347. READ_BITS_OR_RETURN(8, &byte);
  1348. }
  1349. sei_msg->payload_size += byte;
  1350. DVLOG(4) << "Found SEI message type: " << sei_msg->type
  1351. << " payload size: " << sei_msg->payload_size;
  1352. switch (sei_msg->type) {
  1353. case H264SEIMessage::kSEIRecoveryPoint:
  1354. READ_UE_OR_RETURN(&sei_msg->recovery_point.recovery_frame_cnt);
  1355. READ_BOOL_OR_RETURN(&sei_msg->recovery_point.exact_match_flag);
  1356. READ_BOOL_OR_RETURN(&sei_msg->recovery_point.broken_link_flag);
  1357. READ_BITS_OR_RETURN(2, &sei_msg->recovery_point.changing_slice_group_idc);
  1358. break;
  1359. default:
  1360. DVLOG(4) << "Unsupported SEI message";
  1361. break;
  1362. }
  1363. return kOk;
  1364. }
  1365. std::vector<SubsampleEntry> H264Parser::GetCurrentSubsamples() {
  1366. DCHECK_EQ(previous_nalu_range_.size(), 1u)
  1367. << "This should only be called after a "
  1368. "successful call to AdvanceToNextNalu()";
  1369. auto intersection = encrypted_ranges_.IntersectionWith(previous_nalu_range_);
  1370. return EncryptedRangesToSubsampleEntry(
  1371. previous_nalu_range_.start(0), previous_nalu_range_.end(0), intersection);
  1372. }
  1373. } // namespace media