Fix Mac GN build
[chromium-blink-merge.git] / media / filters / vp8_parser.cc
blob727f31b55f4a566aeaa4438433d3fab75a55795c
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.
8 #include "base/logging.h"
9 #include "media/filters/vp8_parser.h"
11 namespace media {
13 #define ERROR_RETURN(what) \
14 do { \
15 DVLOG(1) << "Error while trying to read " #what; \
16 return false; \
17 } while (0)
19 #define BD_READ_BOOL_OR_RETURN(out) \
20 do { \
21 if (!bd_.ReadBool(out)) \
22 ERROR_RETURN(out); \
23 } while (0)
25 #define BD_READ_BOOL_WITH_PROB_OR_RETURN(out, prob) \
26 do { \
27 if (!bd_.ReadBool(out, prob)) \
28 ERROR_RETURN(out); \
29 } while (0)
31 #define BD_READ_UNSIGNED_OR_RETURN(num_bits, out) \
32 do { \
33 int _out; \
34 if (!bd_.ReadLiteral(num_bits, &_out)) \
35 ERROR_RETURN(out); \
36 *out = _out; \
37 } while (0)
39 #define BD_READ_SIGNED_OR_RETURN(num_bits, out) \
40 do { \
41 int _out; \
42 if (!bd_.ReadLiteralWithSign(num_bits, &_out)) \
43 ERROR_RETURN(out); \
44 *out = _out; \
45 } while (0)
47 Vp8FrameHeader::Vp8FrameHeader() {
48 memset(this, 0, sizeof(*this));
51 Vp8Parser::Vp8Parser() : stream_(nullptr), bytes_left_(0) {
54 Vp8Parser::~Vp8Parser() {
57 bool Vp8Parser::ParseFrame(const uint8_t* ptr,
58 size_t frame_size,
59 Vp8FrameHeader* fhdr) {
60 stream_ = ptr;
61 bytes_left_ = frame_size;
63 memset(fhdr, 0, sizeof(*fhdr));
64 fhdr->data = stream_;
65 fhdr->frame_size = bytes_left_;
67 if (!ParseFrameTag(fhdr))
68 return false;
70 fhdr->first_part_offset = stream_ - fhdr->data;
72 if (!ParseFrameHeader(fhdr))
73 return false;
75 if (!ParsePartitions(fhdr))
76 return false;
78 DVLOG(4) << "Frame parsed, start: " << ptr << " size: " << frame_size
79 << ", offsets: to first_part=" << fhdr->first_part_offset
80 << " macroblocks (in bits)=" << fhdr->macroblock_bit_offset;
82 return true;
85 static inline uint32_t GetBitsAt(uint32_t data, size_t shift, size_t num_bits) {
86 return ((data >> shift) & ((1 << num_bits) - 1));
89 bool Vp8Parser::ParseFrameTag(Vp8FrameHeader* fhdr) {
90 const size_t kFrameTagSize = 3;
91 if (bytes_left_ < kFrameTagSize)
92 return false;
94 uint32_t frame_tag = (stream_[2] << 16) | (stream_[1] << 8) | stream_[0];
95 fhdr->key_frame =
96 static_cast<Vp8FrameHeader::FrameType>(GetBitsAt(frame_tag, 0, 1));
97 fhdr->version = GetBitsAt(frame_tag, 1, 2);
98 fhdr->is_experimental = !!GetBitsAt(frame_tag, 3, 1);
99 fhdr->show_frame =!!GetBitsAt(frame_tag, 4, 1);
100 fhdr->first_part_size = GetBitsAt(frame_tag, 5, 19);
102 stream_ += kFrameTagSize;
103 bytes_left_ -= kFrameTagSize;
105 if (fhdr->IsKeyframe()) {
106 const size_t kKeyframeTagSize = 7;
107 if (bytes_left_ < kKeyframeTagSize)
108 return false;
110 static const uint8_t kVp8StartCode[] = {0x9d, 0x01, 0x2a};
111 if (memcmp(stream_, kVp8StartCode, sizeof(kVp8StartCode)) != 0)
112 return false;
114 stream_ += sizeof(kVp8StartCode);
115 bytes_left_ -= sizeof(kVp8StartCode);
117 uint16_t data = (stream_[1] << 8) | stream_[0];
118 fhdr->width = data & 0x3fff;
119 fhdr->horizontal_scale = data >> 14;
121 data = (stream_[3] << 8) | stream_[2];
122 fhdr->height = data & 0x3fff;
123 fhdr->vertical_scale = data >> 14;
125 stream_ += 4;
126 bytes_left_ -= 4;
129 return true;
132 bool Vp8Parser::ParseFrameHeader(Vp8FrameHeader* fhdr) {
133 if (!bd_.Initialize(stream_, bytes_left_))
134 return false;
136 bool keyframe = fhdr->IsKeyframe();
137 if (keyframe) {
138 unsigned int data;
139 BD_READ_UNSIGNED_OR_RETURN(1, &data); // color_space
140 BD_READ_UNSIGNED_OR_RETURN(1, &data); // clamping_type
143 if (!ParseSegmentationHeader(keyframe))
144 return false;
146 fhdr->segmentation_hdr = curr_segmentation_hdr_;
148 if (!ParseLoopFilterHeader(keyframe))
149 return false;
151 fhdr->loopfilter_hdr = curr_loopfilter_hdr_;
153 int log2_nbr_of_dct_partitions;
154 BD_READ_UNSIGNED_OR_RETURN(2, &log2_nbr_of_dct_partitions);
155 fhdr->num_of_dct_partitions =
156 static_cast<size_t>(1 << log2_nbr_of_dct_partitions);
158 if (!ParseQuantizationHeader(&fhdr->quantization_hdr))
159 return false;
161 if (keyframe) {
162 BD_READ_BOOL_OR_RETURN(&fhdr->refresh_entropy_probs);
163 } else {
164 BD_READ_BOOL_OR_RETURN(&fhdr->refresh_golden_frame);
165 BD_READ_BOOL_OR_RETURN(&fhdr->refresh_alternate_frame);
167 int refresh_mode;
168 if (!fhdr->refresh_golden_frame) {
169 BD_READ_UNSIGNED_OR_RETURN(2, &refresh_mode);
170 fhdr->copy_buffer_to_golden =
171 static_cast<Vp8FrameHeader::GoldenRefreshMode>(refresh_mode);
174 if (!fhdr->refresh_alternate_frame) {
175 BD_READ_UNSIGNED_OR_RETURN(2, &refresh_mode);
176 fhdr->copy_buffer_to_alternate =
177 static_cast<Vp8FrameHeader::AltRefreshMode>(refresh_mode);
180 BD_READ_UNSIGNED_OR_RETURN(1, &fhdr->sign_bias_golden);
181 BD_READ_UNSIGNED_OR_RETURN(1, &fhdr->sign_bias_alternate);
182 BD_READ_BOOL_OR_RETURN(&fhdr->refresh_entropy_probs);
183 BD_READ_BOOL_OR_RETURN(&fhdr->refresh_last);
186 if (keyframe)
187 ResetProbs(&curr_entropy_hdr_);
189 fhdr->entropy_hdr = curr_entropy_hdr_;
191 if (!ParseTokenProbs(&fhdr->entropy_hdr, fhdr->refresh_entropy_probs))
192 return false;
194 BD_READ_BOOL_OR_RETURN(&fhdr->mb_no_skip_coeff);
195 if (fhdr->mb_no_skip_coeff)
196 BD_READ_UNSIGNED_OR_RETURN(8, &fhdr->prob_skip_false);
198 if (!keyframe) {
199 BD_READ_UNSIGNED_OR_RETURN(8, &fhdr->prob_intra);
200 BD_READ_UNSIGNED_OR_RETURN(8, &fhdr->prob_last);
201 BD_READ_UNSIGNED_OR_RETURN(8, &fhdr->prob_gf);
203 if (!ParseIntraProbs(&fhdr->entropy_hdr, fhdr->refresh_entropy_probs))
204 return false;
206 if (!ParseMVProbs(&fhdr->entropy_hdr, fhdr->refresh_entropy_probs))
207 return false;
210 fhdr->macroblock_bit_offset = bd_.BitOffset();
211 fhdr->bool_dec_range = bd_.GetRange();
212 fhdr->bool_dec_value = bd_.GetBottom();
213 fhdr->bool_dec_count = 7 - (bd_.BitOffset() + 7) % 8;
215 return true;
218 bool Vp8Parser::ParseSegmentationHeader(bool keyframe) {
219 Vp8SegmentationHeader* shdr = &curr_segmentation_hdr_;
221 if (keyframe)
222 memset(shdr, 0, sizeof(*shdr));
224 BD_READ_BOOL_OR_RETURN(&shdr->segmentation_enabled);
225 if (!shdr->segmentation_enabled)
226 return true;
228 BD_READ_BOOL_OR_RETURN(&shdr->update_mb_segmentation_map);
229 BD_READ_BOOL_OR_RETURN(&shdr->update_segment_feature_data);
230 if (shdr->update_segment_feature_data) {
231 int mode;
232 BD_READ_UNSIGNED_OR_RETURN(1, &mode);
233 shdr->segment_feature_mode =
234 static_cast<Vp8SegmentationHeader::SegmentFeatureMode>(mode);
236 for (size_t i = 0; i < kMaxMBSegments; ++i) {
237 bool quantizer_update;
238 BD_READ_BOOL_OR_RETURN(&quantizer_update);
239 if (quantizer_update)
240 BD_READ_SIGNED_OR_RETURN(7, &shdr->quantizer_update_value[i]);
243 for (size_t i = 0; i < kMaxMBSegments; ++i) {
244 bool loop_filter_update;
245 BD_READ_BOOL_OR_RETURN(&loop_filter_update);
246 if (loop_filter_update)
247 BD_READ_SIGNED_OR_RETURN(6, &shdr->lf_update_value[i]);
251 if (shdr->update_mb_segmentation_map) {
252 for (size_t i = 0; i < kNumMBFeatureTreeProbs; ++i) {
253 bool segment_prob_update;
254 BD_READ_BOOL_OR_RETURN(&segment_prob_update);
255 if (segment_prob_update)
256 BD_READ_UNSIGNED_OR_RETURN(8, &shdr->segment_prob[i]);
257 else
258 shdr->segment_prob[i] = Vp8SegmentationHeader::kDefaultSegmentProb;
262 return true;
265 bool Vp8Parser::ParseLoopFilterHeader(bool keyframe) {
266 Vp8LoopFilterHeader* lfhdr = &curr_loopfilter_hdr_;
268 if (keyframe)
269 memset(lfhdr, 0, sizeof(*lfhdr));
271 int type;
272 BD_READ_UNSIGNED_OR_RETURN(1, &type);
273 lfhdr->type = static_cast<Vp8LoopFilterHeader::Type>(type);
274 BD_READ_UNSIGNED_OR_RETURN(6, &lfhdr->level);
275 BD_READ_UNSIGNED_OR_RETURN(3, &lfhdr->sharpness_level);
276 BD_READ_BOOL_OR_RETURN(&lfhdr->loop_filter_adj_enable);
278 if (lfhdr->loop_filter_adj_enable) {
279 BD_READ_BOOL_OR_RETURN(&lfhdr->mode_ref_lf_delta_update);
280 if (lfhdr->mode_ref_lf_delta_update) {
281 for (size_t i = 0; i < kNumBlockContexts; ++i) {
282 bool ref_frame_delta_update_flag;
283 BD_READ_BOOL_OR_RETURN(&ref_frame_delta_update_flag);
284 if (ref_frame_delta_update_flag)
285 BD_READ_SIGNED_OR_RETURN(6, &lfhdr->ref_frame_delta[i]);
288 for (size_t i = 0; i < kNumBlockContexts; ++i) {
289 bool mb_mode_delta_update_flag;
290 BD_READ_BOOL_OR_RETURN(&mb_mode_delta_update_flag);
291 if (mb_mode_delta_update_flag)
292 BD_READ_SIGNED_OR_RETURN(6, &lfhdr->mb_mode_delta[i]);
297 return true;
300 bool Vp8Parser::ParseQuantizationHeader(Vp8QuantizationHeader* qhdr) {
301 // If any of the delta values is not present, the delta should be zero.
302 memset(qhdr, 0, sizeof(*qhdr));
304 BD_READ_UNSIGNED_OR_RETURN(7, &qhdr->y_ac_qi);
306 bool delta_present;
308 BD_READ_BOOL_OR_RETURN(&delta_present);
309 if (delta_present)
310 BD_READ_SIGNED_OR_RETURN(4, &qhdr->y_dc_delta);
312 BD_READ_BOOL_OR_RETURN(&delta_present);
313 if (delta_present)
314 BD_READ_SIGNED_OR_RETURN(4, &qhdr->y2_dc_delta);
316 BD_READ_BOOL_OR_RETURN(&delta_present);
317 if (delta_present)
318 BD_READ_SIGNED_OR_RETURN(4, &qhdr->y2_ac_delta);
320 BD_READ_BOOL_OR_RETURN(&delta_present);
321 if (delta_present)
322 BD_READ_SIGNED_OR_RETURN(4, &qhdr->uv_dc_delta);
324 BD_READ_BOOL_OR_RETURN(&delta_present);
325 if (delta_present)
326 BD_READ_SIGNED_OR_RETURN(4, &qhdr->uv_ac_delta);
328 return true;
331 // See spec for details on these values.
332 const uint8_t kCoeffUpdateProbs[kNumBlockTypes][kNumCoeffBands]
333 [kNumPrevCoeffContexts][kNumEntropyNodes] = {
336 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
337 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
338 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
341 {176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255},
342 {223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255},
343 {249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255},
346 {255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255},
347 {234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
348 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
351 {255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255},
352 {239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
353 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
356 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255},
357 {251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
358 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
361 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
362 {251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
363 {254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
366 {255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255},
367 {250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255},
368 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
371 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
372 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
373 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
378 {217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
379 {225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255},
380 {234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255},
383 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
384 {223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
385 {238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255},
388 {255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255},
389 {249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
390 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
393 {255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255},
394 {247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
395 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
398 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
399 {252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
400 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
403 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
404 {253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
405 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
408 {255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255},
409 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
410 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
413 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
414 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
415 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
420 {186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255},
421 {234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255},
422 {251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255},
425 {255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
426 {236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
427 {251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255},
430 {255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
431 {254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255},
432 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
435 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
436 {254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
437 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
440 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
441 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
442 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
445 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
446 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
447 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
450 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
451 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
452 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
455 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
456 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
457 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
462 {248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
463 {250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255},
464 {248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255},
467 {255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255},
468 {246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255},
469 {252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255},
472 {255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255},
473 {248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255},
474 {253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255},
477 {255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255},
478 {245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255},
479 {253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
482 {255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255},
483 {252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255},
484 {255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255},
487 {255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255},
488 {249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
489 {255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255},
492 {255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255},
493 {250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
494 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
497 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
498 {254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
499 {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
504 const uint8_t kDefaultYModeProbs[kNumYModeProbs] = {112, 86, 140, 37};
505 const uint8_t kDefaultUVModeProbs[kNumUVModeProbs] = {162, 101, 204};
507 const uint8_t kDefaultCoeffProbs[kNumBlockTypes][kNumCoeffBands]
508 [kNumPrevCoeffContexts][kNumEntropyNodes] = {
511 {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
512 {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
513 {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
516 {253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128},
517 {189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128},
518 {106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128},
521 { 1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128},
522 {181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128},
523 { 78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128},
526 { 1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128},
527 {184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128},
528 { 77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128},
531 { 1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128},
532 {170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128},
533 { 37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128},
536 { 1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128},
537 {207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128},
538 {102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128},
541 { 1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128},
542 {177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128},
543 { 80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128},
546 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
547 {246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
548 {255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
553 {198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62},
554 {131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1},
555 { 68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128},
558 { 1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128},
559 {184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128},
560 { 81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128},
563 { 1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128},
564 { 99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128},
565 { 23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128},
568 { 1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128},
569 {109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128},
570 { 44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128},
573 { 1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128},
574 { 94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128},
575 { 22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128},
578 { 1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128},
579 {124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128},
580 { 35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128},
583 { 1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128},
584 {121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128},
585 { 45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128},
588 { 1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128},
589 {203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128},
590 {137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128},
595 {253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128},
596 {175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128},
597 { 73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128},
600 { 1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128},
601 {239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128},
602 {155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128},
605 { 1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128},
606 {201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128},
607 { 69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128},
610 { 1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128},
611 {223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128},
612 {141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128},
615 { 1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128},
616 {190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128},
617 {149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
620 { 1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128},
621 {247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128},
622 {240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128},
625 { 1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128},
626 {213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128},
627 { 55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128},
630 {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
631 {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
632 {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
637 {202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255},
638 {126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128},
639 { 61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128},
642 { 1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128},
643 {166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128},
644 { 39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128},
647 { 1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128},
648 {124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128},
649 { 24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128},
652 { 1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128},
653 {149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128},
654 { 28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128}
657 { 1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128},
658 {123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128},
659 { 20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128},
662 { 1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128},
663 {168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128},
664 { 47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128},
667 { 1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128},
668 {141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128},
669 { 42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128},
672 { 1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
673 {244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
674 {238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128},
679 const uint8_t kMVUpdateProbs[kNumMVContexts][kNumMVProbs] =
682 237, 246, 253, 253, 254, 254, 254, 254, 254,
683 254, 254, 254, 254, 254, 250, 250, 252, 254, 254,
686 231, 243, 245, 253, 254, 254, 254, 254, 254,
687 254, 254, 254, 254, 254, 251, 251, 254, 254, 254,
691 const uint8_t kDefaultMVProbs[kNumMVContexts][kNumMVProbs] = {
693 162, 128, 225, 146, 172, 147, 214, 39, 156,
694 128, 129, 132, 75, 145, 178, 206, 239, 254, 254,
697 164, 128, 204, 170, 119, 235, 140, 230, 228,
698 128, 130, 130, 74, 148, 180, 203, 236, 254, 254,
702 void Vp8Parser::ResetProbs(Vp8EntropyHeader* ehdr) {
703 static_assert(sizeof(ehdr->coeff_probs) == sizeof(kDefaultCoeffProbs),
704 "coeff_probs_arrays_must_be_of_correct_size");
705 memcpy(ehdr->coeff_probs, kDefaultCoeffProbs, sizeof(ehdr->coeff_probs));
707 static_assert(sizeof(ehdr->y_mode_probs) == sizeof(kDefaultYModeProbs),
708 "y_probs_arrays_must_be_of_correct_size");
709 memcpy(ehdr->y_mode_probs, kDefaultYModeProbs, sizeof(ehdr->y_mode_probs));
711 static_assert(sizeof(ehdr->uv_mode_probs) == sizeof(kDefaultUVModeProbs),
712 "uv_probs_arrays_must_be_of_correct_size");
713 memcpy(ehdr->uv_mode_probs, kDefaultUVModeProbs, sizeof(ehdr->uv_mode_probs));
715 static_assert(sizeof(ehdr->mv_probs) == sizeof(kDefaultMVProbs),
716 "mv_probs_arrays_must_be_of_correct_size");
717 memcpy(ehdr->mv_probs, kDefaultMVProbs, sizeof(ehdr->mv_probs));
720 bool Vp8Parser::ParseTokenProbs(Vp8EntropyHeader* ehdr,
721 bool update_curr_probs) {
722 for (size_t i = 0; i < kNumBlockTypes; ++i) {
723 for (size_t j = 0; j < kNumCoeffBands; ++j) {
724 for (size_t k = 0; k < kNumPrevCoeffContexts; ++k) {
725 for (size_t l = 0; l < kNumEntropyNodes; ++l) {
726 bool coeff_prob_update_flag;
727 BD_READ_BOOL_WITH_PROB_OR_RETURN(&coeff_prob_update_flag,
728 kCoeffUpdateProbs[i][j][k][l]);
729 if (coeff_prob_update_flag)
730 BD_READ_UNSIGNED_OR_RETURN(8, &ehdr->coeff_probs[i][j][k][l]);
736 if (update_curr_probs) {
737 memcpy(curr_entropy_hdr_.coeff_probs, ehdr->coeff_probs,
738 sizeof(curr_entropy_hdr_.coeff_probs));
741 return true;
744 bool Vp8Parser::ParseIntraProbs(Vp8EntropyHeader* ehdr,
745 bool update_curr_probs) {
746 bool intra_16x16_prob_update_flag;
747 BD_READ_BOOL_OR_RETURN(&intra_16x16_prob_update_flag);
748 if (intra_16x16_prob_update_flag) {
749 for (size_t i = 0; i < kNumYModeProbs; ++i)
750 BD_READ_UNSIGNED_OR_RETURN(8, &ehdr->y_mode_probs[i]);
753 bool intra_chroma_prob_update_flag;
754 BD_READ_BOOL_OR_RETURN(&intra_chroma_prob_update_flag);
755 if (intra_chroma_prob_update_flag) {
756 for (size_t i = 0; i < kNumUVModeProbs; ++i)
757 BD_READ_UNSIGNED_OR_RETURN(8, &ehdr->uv_mode_probs[i]);
760 if (update_curr_probs) {
761 memcpy(curr_entropy_hdr_.y_mode_probs, ehdr->y_mode_probs,
762 sizeof(curr_entropy_hdr_.y_mode_probs));
763 memcpy(curr_entropy_hdr_.uv_mode_probs, ehdr->uv_mode_probs,
764 sizeof(curr_entropy_hdr_.uv_mode_probs));
767 return true;
770 bool Vp8Parser::ParseMVProbs(Vp8EntropyHeader* ehdr, bool update_curr_probs) {
771 for (size_t mv_ctx = 0; mv_ctx < kNumMVContexts; ++mv_ctx) {
772 for (size_t p = 0; p < kNumMVProbs; ++p) {
773 bool mv_prob_update_flag;
774 BD_READ_BOOL_WITH_PROB_OR_RETURN(&mv_prob_update_flag,
775 kMVUpdateProbs[mv_ctx][p]);
776 if (mv_prob_update_flag) {
777 uint8_t prob;
778 BD_READ_UNSIGNED_OR_RETURN(7, &prob);
779 ehdr->mv_probs[mv_ctx][p] = prob ? prob << 1 : 1;
784 if (update_curr_probs) {
785 memcpy(curr_entropy_hdr_.mv_probs, ehdr->mv_probs,
786 sizeof(curr_entropy_hdr_.mv_probs));
789 return true;
792 bool Vp8Parser::ParsePartitions(Vp8FrameHeader* fhdr) {
793 CHECK_GE(fhdr->num_of_dct_partitions, 1u);
794 CHECK_LE(fhdr->num_of_dct_partitions, kMaxDCTPartitions);
796 // DCT partitions start after the first partition and partition size values
797 // that follow it. There are num_of_dct_partitions - 1 sizes stored in the
798 // stream after the first partition, each 3 bytes long. The size of last
799 // DCT partition is not stored in the stream, but is instead calculated by
800 // taking the remainder of the frame size after the penultimate DCT partition.
801 size_t first_dct_pos = fhdr->first_part_offset + fhdr->first_part_size +
802 (fhdr->num_of_dct_partitions - 1) * 3;
804 // Make sure we have enough data for the first partition and partition sizes.
805 if (fhdr->frame_size < first_dct_pos)
806 return false;
808 // Total size of all DCT partitions.
809 size_t bytes_left = fhdr->frame_size - first_dct_pos;
811 // Position ourselves at the beginning of partition size values.
812 const uint8_t* ptr =
813 fhdr->data + fhdr->first_part_offset + fhdr->first_part_size;
815 // Read sizes from the stream (if present).
816 for (size_t i = 0; i < fhdr->num_of_dct_partitions - 1; ++i) {
817 fhdr->dct_partition_sizes[i] = (ptr[2] << 16) | (ptr[1] << 8) | ptr[0];
819 // Make sure we have enough data in the stream for ith partition and
820 // subtract its size from total.
821 if (bytes_left < fhdr->dct_partition_sizes[i])
822 return false;
824 bytes_left -= fhdr->dct_partition_sizes[i];
826 // Move to the position of the next partition size value.
827 ptr += 3;
830 // The remainder of the data belongs to the last DCT partition.
831 fhdr->dct_partition_sizes[fhdr->num_of_dct_partitions - 1] = bytes_left;
833 DVLOG(4) << "Control part size: " << fhdr->first_part_size;
834 for (size_t i = 0; i < fhdr->num_of_dct_partitions; ++i)
835 DVLOG(4) << "DCT part " << i << " size: " << fhdr->dct_partition_sizes[i];
837 return true;
840 } // namespace media