vp8_parser.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // Copyright 2015 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. //
  5. // This file contains an implementation of a VP8 raw stream parser,
  6. // as defined in RFC 6386.
  7. #include "media/parsers/vp8_parser.h"
  8. #include <cstring>
  9. #include "base/check_op.h"
  10. #include "base/logging.h"
  11. namespace media {
  12. #define ERROR_RETURN(what) \
  13. do { \
  14. DVLOG(1) << "Error while trying to read " #what; \
  15. return false; \
  16. } while (0)
  17. #define BD_READ_BOOL_OR_RETURN(out) \
  18. do { \
  19. if (!bd_.ReadBool(out)) \
  20. ERROR_RETURN(out); \
  21. } while (0)
  22. #define BD_READ_BOOL_WITH_PROB_OR_RETURN(out, prob) \
  23. do { \
  24. if (!bd_.ReadBool(out, prob)) \
  25. ERROR_RETURN(out); \
  26. } while (0)
  27. #define BD_READ_UNSIGNED_OR_RETURN(num_bits, out) \
  28. do { \
  29. int _out; \
  30. if (!bd_.ReadLiteral(num_bits, &_out)) \
  31. ERROR_RETURN(out); \
  32. *out = _out; \
  33. } while (0)
  34. #define BD_READ_SIGNED_OR_RETURN(num_bits, out) \
  35. do { \
  36. int _out; \
  37. if (!bd_.ReadLiteralWithSign(num_bits, &_out)) \
  38. ERROR_RETURN(out); \
  39. *out = _out; \
  40. } while (0)
  41. Vp8FrameHeader::Vp8FrameHeader() {
  42. memset(this, 0, sizeof(*this));
  43. }
  44. Vp8FrameHeader::~Vp8FrameHeader() = default;
  45. Vp8FrameHeader::Vp8FrameHeader(const Vp8FrameHeader& fhdr) {
  46. memcpy(this, &fhdr, sizeof(*this));
  47. }
  48. Vp8FrameHeader& Vp8FrameHeader::operator=(const Vp8FrameHeader& fhdr) {
  49. memcpy(this, &fhdr, sizeof(*this));
  50. return *this;
  51. }
  52. Vp8Parser::Vp8Parser() : stream_(nullptr), bytes_left_(0) {
  53. }
  54. Vp8Parser::~Vp8Parser() = default;
  55. bool Vp8Parser::ParseFrame(const uint8_t* ptr,
  56. size_t frame_size,
  57. Vp8FrameHeader* fhdr) {
  58. stream_ = ptr;
  59. bytes_left_ = frame_size;
  60. memset(fhdr, 0, sizeof(*fhdr));
  61. fhdr->data = stream_;
  62. fhdr->frame_size = bytes_left_;
  63. if (!ParseFrameTag(fhdr))
  64. return false;
  65. fhdr->first_part_offset = stream_ - fhdr->data;
  66. if (!ParseFrameHeader(fhdr))
  67. return false;
  68. if (!ParsePartitions(fhdr))
  69. return false;
  70. DVLOG(4) << "Frame parsed, start: " << static_cast<const void*>(ptr)
  71. << ", size: " << frame_size
  72. << ", offsets: to first_part=" << fhdr->first_part_offset
  73. << ", to macroblock data (in bits)=" << fhdr->macroblock_bit_offset;
  74. return true;
  75. }
  76. static inline uint32_t GetBitsAt(uint32_t data, size_t shift, size_t num_bits) {
  77. return ((data >> shift) & ((1 << num_bits) - 1));
  78. }
  79. bool Vp8Parser::ParseFrameTag(Vp8FrameHeader* fhdr) {
  80. const size_t kFrameTagSize = 3;
  81. if (bytes_left_ < kFrameTagSize)
  82. return false;
  83. uint32_t frame_tag = (stream_[2] << 16) | (stream_[1] << 8) | stream_[0];
  84. fhdr->frame_type =
  85. static_cast<Vp8FrameHeader::FrameType>(GetBitsAt(frame_tag, 0, 1));
  86. fhdr->version = GetBitsAt(frame_tag, 1, 2);
  87. fhdr->is_experimental = !!GetBitsAt(frame_tag, 3, 1);
  88. fhdr->show_frame =!!GetBitsAt(frame_tag, 4, 1);
  89. fhdr->first_part_size = GetBitsAt(frame_tag, 5, 19);
  90. stream_ += kFrameTagSize;
  91. bytes_left_ -= kFrameTagSize;
  92. if (fhdr->IsKeyframe()) {
  93. const size_t kKeyframeTagSize = 7;
  94. if (bytes_left_ < kKeyframeTagSize)
  95. return false;
  96. static const uint8_t kVp8StartCode[] = {0x9d, 0x01, 0x2a};
  97. if (memcmp(stream_, kVp8StartCode, sizeof(kVp8StartCode)) != 0)
  98. return false;
  99. stream_ += sizeof(kVp8StartCode);
  100. bytes_left_ -= sizeof(kVp8StartCode);
  101. uint16_t data = (stream_[1] << 8) | stream_[0];
  102. fhdr->width = data & 0x3fff;
  103. fhdr->horizontal_scale = data >> 14;
  104. data = (stream_[3] << 8) | stream_[2];
  105. fhdr->height = data & 0x3fff;
  106. fhdr->vertical_scale = data >> 14;
  107. stream_ += 4;
  108. bytes_left_ -= 4;
  109. }
  110. return true;
  111. }
  112. bool Vp8Parser::ParseFrameHeader(Vp8FrameHeader* fhdr) {
  113. if (!bd_.Initialize(stream_, bytes_left_))
  114. return false;
  115. bool keyframe = fhdr->IsKeyframe();
  116. if (keyframe) {
  117. unsigned int data;
  118. BD_READ_UNSIGNED_OR_RETURN(1, &data); // color_space
  119. BD_READ_UNSIGNED_OR_RETURN(1, &data); // clamping_type
  120. fhdr->is_full_range = data == 1;
  121. } else {
  122. fhdr->is_full_range = false;
  123. }
  124. if (!ParseSegmentationHeader(keyframe))
  125. return false;
  126. fhdr->segmentation_hdr = curr_segmentation_hdr_;
  127. if (!ParseLoopFilterHeader(keyframe))
  128. return false;
  129. fhdr->loopfilter_hdr = curr_loopfilter_hdr_;
  130. int log2_nbr_of_dct_partitions;
  131. BD_READ_UNSIGNED_OR_RETURN(2, &log2_nbr_of_dct_partitions);
  132. fhdr->num_of_dct_partitions = static_cast<size_t>(1)
  133. << log2_nbr_of_dct_partitions;
  134. if (!ParseQuantizationHeader(&fhdr->quantization_hdr))
  135. return false;
  136. if (keyframe) {
  137. BD_READ_BOOL_OR_RETURN(&fhdr->refresh_entropy_probs);
  138. } else {
  139. BD_READ_BOOL_OR_RETURN(&fhdr->refresh_golden_frame);
  140. BD_READ_BOOL_OR_RETURN(&fhdr->refresh_alternate_frame);
  141. int refresh_mode;
  142. if (!fhdr->refresh_golden_frame) {
  143. BD_READ_UNSIGNED_OR_RETURN(2, &refresh_mode);
  144. fhdr->copy_buffer_to_golden =
  145. static_cast<Vp8FrameHeader::GoldenRefreshMode>(refresh_mode);
  146. }
  147. if (!fhdr->refresh_alternate_frame) {
  148. BD_READ_UNSIGNED_OR_RETURN(2, &refresh_mode);
  149. fhdr->copy_buffer_to_alternate =
  150. static_cast<Vp8FrameHeader::AltRefreshMode>(refresh_mode);
  151. }
  152. BD_READ_UNSIGNED_OR_RETURN(1, &fhdr->sign_bias_golden);
  153. BD_READ_UNSIGNED_OR_RETURN(1, &fhdr->sign_bias_alternate);
  154. BD_READ_BOOL_OR_RETURN(&fhdr->refresh_entropy_probs);
  155. BD_READ_BOOL_OR_RETURN(&fhdr->refresh_last);
  156. }
  157. if (keyframe)
  158. ResetProbs();
  159. fhdr->entropy_hdr = curr_entropy_hdr_;
  160. if (!ParseTokenProbs(&fhdr->entropy_hdr, fhdr->refresh_entropy_probs))
  161. return false;
  162. BD_READ_BOOL_OR_RETURN(&fhdr->mb_no_skip_coeff);
  163. if (fhdr->mb_no_skip_coeff)
  164. BD_READ_UNSIGNED_OR_RETURN(8, &fhdr->prob_skip_false);
  165. if (!keyframe) {
  166. BD_READ_UNSIGNED_OR_RETURN(8, &fhdr->prob_intra);
  167. BD_READ_UNSIGNED_OR_RETURN(8, &fhdr->prob_last);
  168. BD_READ_UNSIGNED_OR_RETURN(8, &fhdr->prob_gf);
  169. }
  170. if (!ParseIntraProbs(&fhdr->entropy_hdr, fhdr->refresh_entropy_probs,
  171. keyframe))
  172. return false;
  173. if (!keyframe) {
  174. if (!ParseMVProbs(&fhdr->entropy_hdr, fhdr->refresh_entropy_probs))
  175. return false;
  176. }
  177. fhdr->macroblock_bit_offset = bd_.BitOffset();
  178. fhdr->bool_dec_range = bd_.GetRange();
  179. fhdr->bool_dec_value = bd_.GetBottom();
  180. fhdr->bool_dec_count = 7 - (bd_.BitOffset() + 7) % 8;
  181. return true;
  182. }
  183. bool Vp8Parser::ParseSegmentationHeader(bool keyframe) {
  184. Vp8SegmentationHeader* shdr = &curr_segmentation_hdr_;
  185. if (keyframe)
  186. memset(shdr, 0, sizeof(*shdr));
  187. BD_READ_BOOL_OR_RETURN(&shdr->segmentation_enabled);
  188. if (!shdr->segmentation_enabled)
  189. return true;
  190. BD_READ_BOOL_OR_RETURN(&shdr->update_mb_segmentation_map);
  191. BD_READ_BOOL_OR_RETURN(&shdr->update_segment_feature_data);
  192. if (shdr->update_segment_feature_data) {
  193. int mode;
  194. BD_READ_UNSIGNED_OR_RETURN(1, &mode);
  195. shdr->segment_feature_mode =
  196. static_cast<Vp8SegmentationHeader::SegmentFeatureMode>(mode);
  197. for (size_t i = 0; i < kMaxMBSegments; ++i) {
  198. bool quantizer_update;
  199. BD_READ_BOOL_OR_RETURN(&quantizer_update);
  200. if (quantizer_update)
  201. BD_READ_SIGNED_OR_RETURN(7, &shdr->quantizer_update_value[i]);
  202. else
  203. shdr->quantizer_update_value[i] = 0;
  204. }
  205. for (size_t i = 0; i < kMaxMBSegments; ++i) {
  206. bool loop_filter_update;
  207. BD_READ_BOOL_OR_RETURN(&loop_filter_update);
  208. if (loop_filter_update)
  209. BD_READ_SIGNED_OR_RETURN(6, &shdr->lf_update_value[i]);
  210. else
  211. shdr->lf_update_value[i] = 0;
  212. }
  213. }
  214. if (shdr->update_mb_segmentation_map) {
  215. for (size_t i = 0; i < kNumMBFeatureTreeProbs; ++i) {
  216. bool segment_prob_update;
  217. BD_READ_BOOL_OR_RETURN(&segment_prob_update);
  218. if (segment_prob_update)
  219. BD_READ_UNSIGNED_OR_RETURN(8, &shdr->segment_prob[i]);
  220. else
  221. shdr->segment_prob[i] = Vp8SegmentationHeader::kDefaultSegmentProb;
  222. }
  223. }
  224. return true;
  225. }
  226. bool Vp8Parser::ParseLoopFilterHeader(bool keyframe) {
  227. Vp8LoopFilterHeader* lfhdr = &curr_loopfilter_hdr_;
  228. if (keyframe)
  229. memset(lfhdr, 0, sizeof(*lfhdr));
  230. int type;
  231. BD_READ_UNSIGNED_OR_RETURN(1, &type);
  232. lfhdr->type = static_cast<Vp8LoopFilterHeader::Type>(type);
  233. BD_READ_UNSIGNED_OR_RETURN(6, &lfhdr->level);
  234. BD_READ_UNSIGNED_OR_RETURN(3, &lfhdr->sharpness_level);
  235. BD_READ_BOOL_OR_RETURN(&lfhdr->loop_filter_adj_enable);
  236. if (lfhdr->loop_filter_adj_enable) {
  237. BD_READ_BOOL_OR_RETURN(&lfhdr->mode_ref_lf_delta_update);
  238. if (lfhdr->mode_ref_lf_delta_update) {
  239. for (size_t i = 0; i < kNumBlockContexts; ++i) {
  240. bool ref_frame_delta_update_flag;
  241. BD_READ_BOOL_OR_RETURN(&ref_frame_delta_update_flag);
  242. if (ref_frame_delta_update_flag)
  243. BD_READ_SIGNED_OR_RETURN(6, &lfhdr->ref_frame_delta[i]);
  244. }
  245. for (size_t i = 0; i < kNumBlockContexts; ++i) {
  246. bool mb_mode_delta_update_flag;
  247. BD_READ_BOOL_OR_RETURN(&mb_mode_delta_update_flag);
  248. if (mb_mode_delta_update_flag)
  249. BD_READ_SIGNED_OR_RETURN(6, &lfhdr->mb_mode_delta[i]);
  250. }
  251. }
  252. }
  253. return true;
  254. }
  255. bool Vp8Parser::ParseQuantizationHeader(Vp8QuantizationHeader* qhdr) {
  256. // If any of the delta values is not present, the delta should be zero.
  257. memset(qhdr, 0, sizeof(*qhdr));
  258. BD_READ_UNSIGNED_OR_RETURN(7, &qhdr->y_ac_qi);
  259. bool delta_present;
  260. BD_READ_BOOL_OR_RETURN(&delta_present);
  261. if (delta_present)
  262. BD_READ_SIGNED_OR_RETURN(4, &qhdr->y_dc_delta);
  263. BD_READ_BOOL_OR_RETURN(&delta_present);
  264. if (delta_present)
  265. BD_READ_SIGNED_OR_RETURN(4, &qhdr->y2_dc_delta);
  266. BD_READ_BOOL_OR_RETURN(&delta_present);
  267. if (delta_present)
  268. BD_READ_SIGNED_OR_RETURN(4, &qhdr->y2_ac_delta);
  269. BD_READ_BOOL_OR_RETURN(&delta_present);
  270. if (delta_present)
  271. BD_READ_SIGNED_OR_RETURN(4, &qhdr->uv_dc_delta);
  272. BD_READ_BOOL_OR_RETURN(&delta_present);
  273. if (delta_present)
  274. BD_READ_SIGNED_OR_RETURN(4, &qhdr->uv_ac_delta);
  275. return true;
  276. }
  277. // See spec for details on these values.
  278. const uint8_t kCoeffUpdateProbs[kNumBlockTypes][kNumCoeffBands]
  279. [kNumPrevCoeffContexts][kNumEntropyNodes] = {
  280. {
  281. {
  282. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  283. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  284. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  285. },
  286. {
  287. {176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  288. {223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255},
  289. {249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255},
  290. },
  291. {
  292. {255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255},
  293. {234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  294. {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  295. },
  296. {
  297. {255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  298. {239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  299. {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  300. },
  301. {
  302. {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  303. {251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  304. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  305. },
  306. {
  307. {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  308. {251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  309. {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  310. },
  311. {
  312. {255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255},
  313. {250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255},
  314. {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  315. },
  316. {
  317. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  318. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  319. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  320. },
  321. },
  322. {
  323. {
  324. {217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  325. {225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255},
  326. {234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255},
  327. },
  328. {
  329. {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  330. {223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  331. {238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255},
  332. },
  333. {
  334. {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  335. {249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  336. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  337. },
  338. {
  339. {255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  340. {247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  341. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  342. },
  343. {
  344. {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  345. {252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  346. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  347. },
  348. {
  349. {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  350. {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  351. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  352. },
  353. {
  354. {255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255},
  355. {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  356. {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  357. },
  358. {
  359. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  360. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  361. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  362. },
  363. },
  364. {
  365. {
  366. {186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255},
  367. {234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255},
  368. {251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255},
  369. },
  370. {
  371. {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  372. {236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  373. {251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255},
  374. },
  375. {
  376. {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  377. {254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  378. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  379. },
  380. {
  381. {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  382. {254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  383. {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  384. },
  385. {
  386. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  387. {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  388. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  389. },
  390. {
  391. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  392. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  393. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  394. },
  395. {
  396. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  397. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  398. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  399. },
  400. {
  401. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  402. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  403. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  404. },
  405. },
  406. {
  407. {
  408. {248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  409. {250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255},
  410. {248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255},
  411. },
  412. {
  413. {255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255},
  414. {246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255},
  415. {252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255},
  416. },
  417. {
  418. {255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255},
  419. {248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255},
  420. {253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255},
  421. },
  422. {
  423. {255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  424. {245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  425. {253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  426. },
  427. {
  428. {255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255},
  429. {252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  430. {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  431. },
  432. {
  433. {255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  434. {249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  435. {255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
  436. },
  437. {
  438. {255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255},
  439. {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  440. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  441. },
  442. {
  443. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  444. {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  445. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  446. },
  447. },
  448. };
  449. const uint8_t kKeyframeYModeProbs[kNumYModeProbs] = {145, 156, 163, 128};
  450. const uint8_t kKeyframeUVModeProbs[kNumUVModeProbs] = {142, 114, 183};
  451. const uint8_t kDefaultYModeProbs[kNumYModeProbs] = {112, 86, 140, 37};
  452. const uint8_t kDefaultUVModeProbs[kNumUVModeProbs] = {162, 101, 204};
  453. const uint8_t kDefaultCoeffProbs[kNumBlockTypes][kNumCoeffBands]
  454. [kNumPrevCoeffContexts][kNumEntropyNodes] = {
  455. {
  456. {
  457. {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
  458. {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
  459. {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
  460. },
  461. {
  462. {253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128},
  463. {189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128},
  464. {106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128},
  465. },
  466. {
  467. { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128},
  468. {181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128},
  469. { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128},
  470. },
  471. {
  472. { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128},
  473. {184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128},
  474. { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128},
  475. },
  476. {
  477. { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128},
  478. {170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128},
  479. { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128},
  480. },
  481. {
  482. { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128},
  483. {207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128},
  484. {102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128},
  485. },
  486. {
  487. { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128},
  488. {177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128},
  489. { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128},
  490. },
  491. {
  492. { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  493. {246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  494. {255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
  495. }
  496. },
  497. {
  498. {
  499. {198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62},
  500. {131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1},
  501. { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128},
  502. },
  503. {
  504. { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128},
  505. {184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128},
  506. { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128},
  507. },
  508. {
  509. { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128},
  510. { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128},
  511. { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128},
  512. },
  513. {
  514. { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128},
  515. {109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128},
  516. { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128},
  517. },
  518. {
  519. { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128},
  520. { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128},
  521. { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128},
  522. },
  523. {
  524. { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128},
  525. {124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128},
  526. { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128},
  527. },
  528. {
  529. { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128},
  530. {121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128},
  531. { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128},
  532. },
  533. {
  534. { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128},
  535. {203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128},
  536. {137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128},
  537. }
  538. },
  539. {
  540. {
  541. {253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128},
  542. {175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128},
  543. { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128},
  544. },
  545. {
  546. { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128},
  547. {239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128},
  548. {155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128},
  549. },
  550. {
  551. { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128},
  552. {201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128},
  553. { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128},
  554. },
  555. {
  556. { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128},
  557. {223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128},
  558. {141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128},
  559. },
  560. {
  561. { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128},
  562. {190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128},
  563. {149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  564. },
  565. {
  566. { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  567. {247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  568. {240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  569. },
  570. {
  571. { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128},
  572. {213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128},
  573. { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  574. },
  575. {
  576. {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
  577. {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
  578. {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
  579. }
  580. },
  581. {
  582. {
  583. {202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255},
  584. {126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128},
  585. { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128},
  586. },
  587. {
  588. { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128},
  589. {166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128},
  590. { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128},
  591. },
  592. {
  593. { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128},
  594. {124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128},
  595. { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128},
  596. },
  597. {
  598. { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128},
  599. {149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128},
  600. { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128}
  601. },
  602. {
  603. { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128},
  604. {123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128},
  605. { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128},
  606. },
  607. {
  608. { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128},
  609. {168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128},
  610. { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128},
  611. },
  612. {
  613. { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128},
  614. {141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128},
  615. { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128},
  616. },
  617. {
  618. { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  619. {244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  620. {238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
  621. },
  622. },
  623. };
  624. const uint8_t kMVUpdateProbs[kNumMVContexts][kNumMVProbs] =
  625. {
  626. {
  627. 237, 246, 253, 253, 254, 254, 254, 254, 254,
  628. 254, 254, 254, 254, 254, 250, 250, 252, 254, 254,
  629. },
  630. {
  631. 231, 243, 245, 253, 254, 254, 254, 254, 254,
  632. 254, 254, 254, 254, 254, 251, 251, 254, 254, 254,
  633. },
  634. };
  635. const uint8_t kDefaultMVProbs[kNumMVContexts][kNumMVProbs] = {
  636. {
  637. 162, 128, 225, 146, 172, 147, 214, 39, 156,
  638. 128, 129, 132, 75, 145, 178, 206, 239, 254, 254,
  639. },
  640. {
  641. 164, 128, 204, 170, 119, 235, 140, 230, 228,
  642. 128, 130, 130, 74, 148, 180, 203, 236, 254, 254,
  643. },
  644. };
  645. void Vp8Parser::ResetProbs() {
  646. static_assert(
  647. sizeof(curr_entropy_hdr_.coeff_probs) == sizeof(kDefaultCoeffProbs),
  648. "coeff_probs_arrays_must_be_of_correct_size");
  649. memcpy(curr_entropy_hdr_.coeff_probs, kDefaultCoeffProbs,
  650. sizeof(curr_entropy_hdr_.coeff_probs));
  651. static_assert(sizeof(curr_entropy_hdr_.mv_probs) == sizeof(kDefaultMVProbs),
  652. "mv_probs_arrays_must_be_of_correct_size");
  653. memcpy(curr_entropy_hdr_.mv_probs, kDefaultMVProbs,
  654. sizeof(curr_entropy_hdr_.mv_probs));
  655. static_assert(
  656. sizeof(curr_entropy_hdr_.y_mode_probs) == sizeof(kDefaultYModeProbs),
  657. "y_probs_arrays_must_be_of_correct_size");
  658. memcpy(curr_entropy_hdr_.y_mode_probs, kDefaultYModeProbs,
  659. sizeof(curr_entropy_hdr_.y_mode_probs));
  660. static_assert(
  661. sizeof(curr_entropy_hdr_.uv_mode_probs) == sizeof(kDefaultUVModeProbs),
  662. "uv_probs_arrays_must_be_of_correct_size");
  663. memcpy(curr_entropy_hdr_.uv_mode_probs, kDefaultUVModeProbs,
  664. sizeof(curr_entropy_hdr_.uv_mode_probs));
  665. }
  666. bool Vp8Parser::ParseTokenProbs(Vp8EntropyHeader* ehdr,
  667. bool update_curr_probs) {
  668. for (size_t i = 0; i < kNumBlockTypes; ++i) {
  669. for (size_t j = 0; j < kNumCoeffBands; ++j) {
  670. for (size_t k = 0; k < kNumPrevCoeffContexts; ++k) {
  671. for (size_t l = 0; l < kNumEntropyNodes; ++l) {
  672. bool coeff_prob_update_flag;
  673. BD_READ_BOOL_WITH_PROB_OR_RETURN(&coeff_prob_update_flag,
  674. kCoeffUpdateProbs[i][j][k][l]);
  675. if (coeff_prob_update_flag)
  676. BD_READ_UNSIGNED_OR_RETURN(8, &ehdr->coeff_probs[i][j][k][l]);
  677. }
  678. }
  679. }
  680. }
  681. if (update_curr_probs) {
  682. memcpy(curr_entropy_hdr_.coeff_probs, ehdr->coeff_probs,
  683. sizeof(curr_entropy_hdr_.coeff_probs));
  684. }
  685. return true;
  686. }
  687. bool Vp8Parser::ParseIntraProbs(Vp8EntropyHeader* ehdr,
  688. bool update_curr_probs,
  689. bool keyframe) {
  690. if (keyframe) {
  691. static_assert(
  692. sizeof(ehdr->y_mode_probs) == sizeof(kKeyframeYModeProbs),
  693. "y_probs_arrays_must_be_of_correct_size");
  694. memcpy(ehdr->y_mode_probs, kKeyframeYModeProbs,
  695. sizeof(ehdr->y_mode_probs));
  696. static_assert(
  697. sizeof(ehdr->uv_mode_probs) == sizeof(kKeyframeUVModeProbs),
  698. "uv_probs_arrays_must_be_of_correct_size");
  699. memcpy(ehdr->uv_mode_probs, kKeyframeUVModeProbs,
  700. sizeof(ehdr->uv_mode_probs));
  701. } else {
  702. bool intra_16x16_prob_update_flag;
  703. BD_READ_BOOL_OR_RETURN(&intra_16x16_prob_update_flag);
  704. if (intra_16x16_prob_update_flag) {
  705. for (size_t i = 0; i < kNumYModeProbs; ++i)
  706. BD_READ_UNSIGNED_OR_RETURN(8, &ehdr->y_mode_probs[i]);
  707. if (update_curr_probs) {
  708. memcpy(curr_entropy_hdr_.y_mode_probs, ehdr->y_mode_probs,
  709. sizeof(curr_entropy_hdr_.y_mode_probs));
  710. }
  711. }
  712. bool intra_chroma_prob_update_flag;
  713. BD_READ_BOOL_OR_RETURN(&intra_chroma_prob_update_flag);
  714. if (intra_chroma_prob_update_flag) {
  715. for (size_t i = 0; i < kNumUVModeProbs; ++i)
  716. BD_READ_UNSIGNED_OR_RETURN(8, &ehdr->uv_mode_probs[i]);
  717. if (update_curr_probs) {
  718. memcpy(curr_entropy_hdr_.uv_mode_probs, ehdr->uv_mode_probs,
  719. sizeof(curr_entropy_hdr_.uv_mode_probs));
  720. }
  721. }
  722. }
  723. return true;
  724. }
  725. bool Vp8Parser::ParseMVProbs(Vp8EntropyHeader* ehdr, bool update_curr_probs) {
  726. for (size_t mv_ctx = 0; mv_ctx < kNumMVContexts; ++mv_ctx) {
  727. for (size_t p = 0; p < kNumMVProbs; ++p) {
  728. bool mv_prob_update_flag;
  729. BD_READ_BOOL_WITH_PROB_OR_RETURN(&mv_prob_update_flag,
  730. kMVUpdateProbs[mv_ctx][p]);
  731. if (mv_prob_update_flag) {
  732. uint8_t prob;
  733. BD_READ_UNSIGNED_OR_RETURN(7, &prob);
  734. ehdr->mv_probs[mv_ctx][p] = prob ? (prob << 1) : 1;
  735. }
  736. }
  737. }
  738. if (update_curr_probs) {
  739. memcpy(curr_entropy_hdr_.mv_probs, ehdr->mv_probs,
  740. sizeof(curr_entropy_hdr_.mv_probs));
  741. }
  742. return true;
  743. }
  744. bool Vp8Parser::ParsePartitions(Vp8FrameHeader* fhdr) {
  745. CHECK_GE(fhdr->num_of_dct_partitions, 1u);
  746. CHECK_LE(fhdr->num_of_dct_partitions, kMaxDCTPartitions);
  747. // DCT partitions start after the first partition and partition size values
  748. // that follow it. There are num_of_dct_partitions - 1 sizes stored in the
  749. // stream after the first partition, each 3 bytes long. The size of last
  750. // DCT partition is not stored in the stream, but is instead calculated by
  751. // taking the remainder of the frame size after the penultimate DCT partition.
  752. size_t first_dct_pos = fhdr->first_part_offset + fhdr->first_part_size +
  753. (fhdr->num_of_dct_partitions - 1) * 3;
  754. // Make sure we have enough data for the first partition and partition sizes.
  755. if (fhdr->frame_size < first_dct_pos)
  756. return false;
  757. // Total size of all DCT partitions.
  758. size_t bytes_left = fhdr->frame_size - first_dct_pos;
  759. // Position ourselves at the beginning of partition size values.
  760. const uint8_t* ptr =
  761. fhdr->data + fhdr->first_part_offset + fhdr->first_part_size;
  762. // Read sizes from the stream (if present).
  763. for (size_t i = 0; i < fhdr->num_of_dct_partitions - 1; ++i) {
  764. fhdr->dct_partition_sizes[i] = (ptr[2] << 16) | (ptr[1] << 8) | ptr[0];
  765. // Make sure we have enough data in the stream for ith partition and
  766. // subtract its size from total.
  767. if (bytes_left < fhdr->dct_partition_sizes[i])
  768. return false;
  769. bytes_left -= fhdr->dct_partition_sizes[i];
  770. // Move to the position of the next partition size value.
  771. ptr += 3;
  772. }
  773. // The remainder of the data belongs to the last DCT partition.
  774. fhdr->dct_partition_sizes[fhdr->num_of_dct_partitions - 1] = bytes_left;
  775. DVLOG(4) << "Control part size: " << fhdr->first_part_size;
  776. for (size_t i = 0; i < fhdr->num_of_dct_partitions; ++i)
  777. DVLOG(4) << "DCT part " << i << " size: " << fhdr->dct_partition_sizes[i];
  778. return true;
  779. }
  780. } // namespace media