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