1 // Copyright 2016 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 #include "vp9_uncompressed_header_parser.h"
6
7 #include "base/logging.h"
8
9 namespace media {
10
11 namespace {
12
13 // 10.5 Default probability tables
14 Vp9FrameContext kVp9DefaultFrameContext = {
15 // tx_probs_8x8
16 {{100}, {66}},
17 // tx_probs_16x16
18 {{20, 152}, {15, 101}},
19 // tx_probs_32x32
20 {{3, 136, 37}, {5, 52, 13}},
21 // coef_probs
22 {// 4x4
23 {{{{{195, 29, 183}, {84, 49, 136}, {8, 42, 71}},
24 {{31, 107, 169},
25 {35, 99, 159},
26 {17, 82, 140},
27 {8, 66, 114},
28 {2, 44, 76},
29 {1, 19, 32}},
30 {{40, 132, 201},
31 {29, 114, 187},
32 {13, 91, 157},
33 {7, 75, 127},
34 {3, 58, 95},
35 {1, 28, 47}},
36 {{69, 142, 221},
37 {42, 122, 201},
38 {15, 91, 159},
39 {6, 67, 121},
40 {1, 42, 77},
41 {1, 17, 31}},
42 {{102, 148, 228},
43 {67, 117, 204},
44 {17, 82, 154},
45 {6, 59, 114},
46 {2, 39, 75},
47 {1, 15, 29}},
48 {{156, 57, 233},
49 {119, 57, 212},
50 {58, 48, 163},
51 {29, 40, 124},
52 {12, 30, 81},
53 {3, 12, 31}}},
54 {{{191, 107, 226}, {124, 117, 204}, {25, 99, 155}},
55 {{29, 148, 210},
56 {37, 126, 194},
57 {8, 93, 157},
58 {2, 68, 118},
59 {1, 39, 69},
60 {1, 17, 33}},
61 {{41, 151, 213},
62 {27, 123, 193},
63 {3, 82, 144},
64 {1, 58, 105},
65 {1, 32, 60},
66 {1, 13, 26}},
67 {{59, 159, 220},
68 {23, 126, 198},
69 {4, 88, 151},
70 {1, 66, 114},
71 {1, 38, 71},
72 {1, 18, 34}},
73 {{114, 136, 232},
74 {51, 114, 207},
75 {11, 83, 155},
76 {3, 56, 105},
77 {1, 33, 65},
78 {1, 17, 34}},
79 {{149, 65, 234},
80 {121, 57, 215},
81 {61, 49, 166},
82 {28, 36, 114},
83 {12, 25, 76},
84 {3, 16, 42}}}},
85 {{{{214, 49, 220}, {132, 63, 188}, {42, 65, 137}},
86 {{85, 137, 221},
87 {104, 131, 216},
88 {49, 111, 192},
89 {21, 87, 155},
90 {2, 49, 87},
91 {1, 16, 28}},
92 {{89, 163, 230},
93 {90, 137, 220},
94 {29, 100, 183},
95 {10, 70, 135},
96 {2, 42, 81},
97 {1, 17, 33}},
98 {{108, 167, 237},
99 {55, 133, 222},
100 {15, 97, 179},
101 {4, 72, 135},
102 {1, 45, 85},
103 {1, 19, 38}},
104 {{124, 146, 240},
105 {66, 124, 224},
106 {17, 88, 175},
107 {4, 58, 122},
108 {1, 36, 75},
109 {1, 18, 37}},
110 {{141, 79, 241},
111 {126, 70, 227},
112 {66, 58, 182},
113 {30, 44, 136},
114 {12, 34, 96},
115 {2, 20, 47}}},
116 {{{229, 99, 249}, {143, 111, 235}, {46, 109, 192}},
117 {{82, 158, 236},
118 {94, 146, 224},
119 {25, 117, 191},
120 {9, 87, 149},
121 {3, 56, 99},
122 {1, 33, 57}},
123 {{83, 167, 237},
124 {68, 145, 222},
125 {10, 103, 177},
126 {2, 72, 131},
127 {1, 41, 79},
128 {1, 20, 39}},
129 {{99, 167, 239},
130 {47, 141, 224},
131 {10, 104, 178},
132 {2, 73, 133},
133 {1, 44, 85},
134 {1, 22, 47}},
135 {{127, 145, 243},
136 {71, 129, 228},
137 {17, 93, 177},
138 {3, 61, 124},
139 {1, 41, 84},
140 {1, 21, 52}},
141 {{157, 78, 244},
142 {140, 72, 231},
143 {69, 58, 184},
144 {31, 44, 137},
145 {14, 38, 105},
146 {8, 23, 61}}}}},
147 // 8x8
148 {{{{{125, 34, 187}, {52, 41, 133}, {6, 31, 56}},
149 {{37, 109, 153},
150 {51, 102, 147},
151 {23, 87, 128},
152 {8, 67, 101},
153 {1, 41, 63},
154 {1, 19, 29}},
155 {{31, 154, 185},
156 {17, 127, 175},
157 {6, 96, 145},
158 {2, 73, 114},
159 {1, 51, 82},
160 {1, 28, 45}},
161 {{23, 163, 200},
162 {10, 131, 185},
163 {2, 93, 148},
164 {1, 67, 111},
165 {1, 41, 69},
166 {1, 14, 24}},
167 {{29, 176, 217},
168 {12, 145, 201},
169 {3, 101, 156},
170 {1, 69, 111},
171 {1, 39, 63},
172 {1, 14, 23}},
173 {{57, 192, 233},
174 {25, 154, 215},
175 {6, 109, 167},
176 {3, 78, 118},
177 {1, 48, 69},
178 {1, 21, 29}}},
179 {{{202, 105, 245}, {108, 106, 216}, {18, 90, 144}},
180 {{33, 172, 219},
181 {64, 149, 206},
182 {14, 117, 177},
183 {5, 90, 141},
184 {2, 61, 95},
185 {1, 37, 57}},
186 {{33, 179, 220},
187 {11, 140, 198},
188 {1, 89, 148},
189 {1, 60, 104},
190 {1, 33, 57},
191 {1, 12, 21}},
192 {{30, 181, 221},
193 {8, 141, 198},
194 {1, 87, 145},
195 {1, 58, 100},
196 {1, 31, 55},
197 {1, 12, 20}},
198 {{32, 186, 224},
199 {7, 142, 198},
200 {1, 86, 143},
201 {1, 58, 100},
202 {1, 31, 55},
203 {1, 12, 22}},
204 {{57, 192, 227},
205 {20, 143, 204},
206 {3, 96, 154},
207 {1, 68, 112},
208 {1, 42, 69},
209 {1, 19, 32}}}},
210 {{{{212, 35, 215}, {113, 47, 169}, {29, 48, 105}},
211 {{74, 129, 203},
212 {106, 120, 203},
213 {49, 107, 178},
214 {19, 84, 144},
215 {4, 50, 84},
216 {1, 15, 25}},
217 {{71, 172, 217},
218 {44, 141, 209},
219 {15, 102, 173},
220 {6, 76, 133},
221 {2, 51, 89},
222 {1, 24, 42}},
223 {{64, 185, 231},
224 {31, 148, 216},
225 {8, 103, 175},
226 {3, 74, 131},
227 {1, 46, 81},
228 {1, 18, 30}},
229 {{65, 196, 235},
230 {25, 157, 221},
231 {5, 105, 174},
232 {1, 67, 120},
233 {1, 38, 69},
234 {1, 15, 30}},
235 {{65, 204, 238},
236 {30, 156, 224},
237 {7, 107, 177},
238 {2, 70, 124},
239 {1, 42, 73},
240 {1, 18, 34}}},
241 {{{225, 86, 251}, {144, 104, 235}, {42, 99, 181}},
242 {{85, 175, 239},
243 {112, 165, 229},
244 {29, 136, 200},
245 {12, 103, 162},
246 {6, 77, 123},
247 {2, 53, 84}},
248 {{75, 183, 239},
249 {30, 155, 221},
250 {3, 106, 171},
251 {1, 74, 128},
252 {1, 44, 76},
253 {1, 17, 28}},
254 {{73, 185, 240},
255 {27, 159, 222},
256 {2, 107, 172},
257 {1, 75, 127},
258 {1, 42, 73},
259 {1, 17, 29}},
260 {{62, 190, 238},
261 {21, 159, 222},
262 {2, 107, 172},
263 {1, 72, 122},
264 {1, 40, 71},
265 {1, 18, 32}},
266 {{61, 199, 240},
267 {27, 161, 226},
268 {4, 113, 180},
269 {1, 76, 129},
270 {1, 46, 80},
271 {1, 23, 41}}}}},
272 // 16x16
273 {{{{{7, 27, 153}, {5, 30, 95}, {1, 16, 30}},
274 {{50, 75, 127},
275 {57, 75, 124},
276 {27, 67, 108},
277 {10, 54, 86},
278 {1, 33, 52},
279 {1, 12, 18}},
280 {{43, 125, 151},
281 {26, 108, 148},
282 {7, 83, 122},
283 {2, 59, 89},
284 {1, 38, 60},
285 {1, 17, 27}},
286 {{23, 144, 163},
287 {13, 112, 154},
288 {2, 75, 117},
289 {1, 50, 81},
290 {1, 31, 51},
291 {1, 14, 23}},
292 {{18, 162, 185},
293 {6, 123, 171},
294 {1, 78, 125},
295 {1, 51, 86},
296 {1, 31, 54},
297 {1, 14, 23}},
298 {{15, 199, 227},
299 {3, 150, 204},
300 {1, 91, 146},
301 {1, 55, 95},
302 {1, 30, 53},
303 {1, 11, 20}}},
304 {{{19, 55, 240}, {19, 59, 196}, {3, 52, 105}},
305 {{41, 166, 207},
306 {104, 153, 199},
307 {31, 123, 181},
308 {14, 101, 152},
309 {5, 72, 106},
310 {1, 36, 52}},
311 {{35, 176, 211},
312 {12, 131, 190},
313 {2, 88, 144},
314 {1, 60, 101},
315 {1, 36, 60},
316 {1, 16, 28}},
317 {{28, 183, 213},
318 {8, 134, 191},
319 {1, 86, 142},
320 {1, 56, 96},
321 {1, 30, 53},
322 {1, 12, 20}},
323 {{20, 190, 215},
324 {4, 135, 192},
325 {1, 84, 139},
326 {1, 53, 91},
327 {1, 28, 49},
328 {1, 11, 20}},
329 {{13, 196, 216},
330 {2, 137, 192},
331 {1, 86, 143},
332 {1, 57, 99},
333 {1, 32, 56},
334 {1, 13, 24}}}},
335 {{{{211, 29, 217}, {96, 47, 156}, {22, 43, 87}},
336 {{78, 120, 193},
337 {111, 116, 186},
338 {46, 102, 164},
339 {15, 80, 128},
340 {2, 49, 76},
341 {1, 18, 28}},
342 {{71, 161, 203},
343 {42, 132, 192},
344 {10, 98, 150},
345 {3, 69, 109},
346 {1, 44, 70},
347 {1, 18, 29}},
348 {{57, 186, 211},
349 {30, 140, 196},
350 {4, 93, 146},
351 {1, 62, 102},
352 {1, 38, 65},
353 {1, 16, 27}},
354 {{47, 199, 217},
355 {14, 145, 196},
356 {1, 88, 142},
357 {1, 57, 98},
358 {1, 36, 62},
359 {1, 15, 26}},
360 {{26, 219, 229},
361 {5, 155, 207},
362 {1, 94, 151},
363 {1, 60, 104},
364 {1, 36, 62},
365 {1, 16, 28}}},
366 {{{233, 29, 248}, {146, 47, 220}, {43, 52, 140}},
367 {{100, 163, 232},
368 {179, 161, 222},
369 {63, 142, 204},
370 {37, 113, 174},
371 {26, 89, 137},
372 {18, 68, 97}},
373 {{85, 181, 230},
374 {32, 146, 209},
375 {7, 100, 164},
376 {3, 71, 121},
377 {1, 45, 77},
378 {1, 18, 30}},
379 {{65, 187, 230},
380 {20, 148, 207},
381 {2, 97, 159},
382 {1, 68, 116},
383 {1, 40, 70},
384 {1, 14, 29}},
385 {{40, 194, 227},
386 {8, 147, 204},
387 {1, 94, 155},
388 {1, 65, 112},
389 {1, 39, 66},
390 {1, 14, 26}},
391 {{16, 208, 228},
392 {3, 151, 207},
393 {1, 98, 160},
394 {1, 67, 117},
395 {1, 41, 74},
396 {1, 17, 31}}}}},
397 // 32x32
398 {{{{{17, 38, 140}, {7, 34, 80}, {1, 17, 29}},
399 {{37, 75, 128},
400 {41, 76, 128},
401 {26, 66, 116},
402 {12, 52, 94},
403 {2, 32, 55},
404 {1, 10, 16}},
405 {{50, 127, 154},
406 {37, 109, 152},
407 {16, 82, 121},
408 {5, 59, 85},
409 {1, 35, 54},
410 {1, 13, 20}},
411 {{40, 142, 167},
412 {17, 110, 157},
413 {2, 71, 112},
414 {1, 44, 72},
415 {1, 27, 45},
416 {1, 11, 17}},
417 {{30, 175, 188},
418 {9, 124, 169},
419 {1, 74, 116},
420 {1, 48, 78},
421 {1, 30, 49},
422 {1, 11, 18}},
423 {{10, 222, 223},
424 {2, 150, 194},
425 {1, 83, 128},
426 {1, 48, 79},
427 {1, 27, 45},
428 {1, 11, 17}}},
429 {{{36, 41, 235}, {29, 36, 193}, {10, 27, 111}},
430 {{85, 165, 222},
431 {177, 162, 215},
432 {110, 135, 195},
433 {57, 113, 168},
434 {23, 83, 120},
435 {10, 49, 61}},
436 {{85, 190, 223},
437 {36, 139, 200},
438 {5, 90, 146},
439 {1, 60, 103},
440 {1, 38, 65},
441 {1, 18, 30}},
442 {{72, 202, 223},
443 {23, 141, 199},
444 {2, 86, 140},
445 {1, 56, 97},
446 {1, 36, 61},
447 {1, 16, 27}},
448 {{55, 218, 225},
449 {13, 145, 200},
450 {1, 86, 141},
451 {1, 57, 99},
452 {1, 35, 61},
453 {1, 13, 22}},
454 {{15, 235, 212},
455 {1, 132, 184},
456 {1, 84, 139},
457 {1, 57, 97},
458 {1, 34, 56},
459 {1, 14, 23}}}},
460 {{{{181, 21, 201}, {61, 37, 123}, {10, 38, 71}},
461 {{47, 106, 172},
462 {95, 104, 173},
463 {42, 93, 159},
464 {18, 77, 131},
465 {4, 50, 81},
466 {1, 17, 23}},
467 {{62, 147, 199},
468 {44, 130, 189},
469 {28, 102, 154},
470 {18, 75, 115},
471 {2, 44, 65},
472 {1, 12, 19}},
473 {{55, 153, 210},
474 {24, 130, 194},
475 {3, 93, 146},
476 {1, 61, 97},
477 {1, 31, 50},
478 {1, 10, 16}},
479 {{49, 186, 223},
480 {17, 148, 204},
481 {1, 96, 142},
482 {1, 53, 83},
483 {1, 26, 44},
484 {1, 11, 17}},
485 {{13, 217, 212},
486 {2, 136, 180},
487 {1, 78, 124},
488 {1, 50, 83},
489 {1, 29, 49},
490 {1, 14, 23}}},
491 {{{197, 13, 247}, {82, 17, 222}, {25, 17, 162}},
492 {{126, 186, 247},
493 {234, 191, 243},
494 {176, 177, 234},
495 {104, 158, 220},
496 {66, 128, 186},
497 {55, 90, 137}},
498 {{111, 197, 242},
499 {46, 158, 219},
500 {9, 104, 171},
501 {2, 65, 125},
502 {1, 44, 80},
503 {1, 17, 91}},
504 {{104, 208, 245},
505 {39, 168, 224},
506 {3, 109, 162},
507 {1, 79, 124},
508 {1, 50, 102},
509 {1, 43, 102}},
510 {{84, 220, 246},
511 {31, 177, 231},
512 {2, 115, 180},
513 {1, 79, 134},
514 {1, 55, 77},
515 {1, 60, 79}},
516 {{43, 243, 240},
517 {8, 180, 217},
518 {1, 115, 166},
519 {1, 84, 121},
520 {1, 51, 67},
521 {1, 16, 6}}}}}},
522 // skip_prob
523 {192, 128, 64},
524 // inter_mode_probs
525 {{2, 173, 34},
526 {7, 145, 85},
527 {7, 166, 63},
528 {7, 94, 66},
529 {8, 64, 46},
530 {17, 81, 31},
531 {25, 29, 30}},
532 // interp_filter_probs
533 {{235, 162}, {36, 255}, {34, 3}, {149, 144}},
534 // is_inter_prob
535 {9, 102, 187, 225},
536 // comp_mode_prob
537 {239, 183, 119, 96, 41},
538 // single_ref_prob
539 {{33, 16}, {77, 74}, {142, 142}, {172, 170}, {238, 247}},
540 // comp_ref_prob
541 {50, 126, 123, 221, 226},
542 // y_mode_probs
543 {{65, 32, 18, 144, 162, 194, 41, 51, 98},
544 {132, 68, 18, 165, 217, 196, 45, 40, 78},
545 {173, 80, 19, 176, 240, 193, 64, 35, 46},
546 {221, 135, 38, 194, 248, 121, 96, 85, 29}},
547 // uv_mode_probs
548 {{120, 7, 76, 176, 208, 126, 28, 54, 103},
549 {48, 12, 154, 155, 139, 90, 34, 117, 119},
550 {67, 6, 25, 204, 243, 158, 13, 21, 96},
551 {97, 5, 44, 131, 176, 139, 48, 68, 97},
552 {83, 5, 42, 156, 111, 152, 26, 49, 152},
553 {80, 5, 58, 178, 74, 83, 33, 62, 145},
554 {86, 5, 32, 154, 192, 168, 14, 22, 163},
555 {85, 5, 32, 156, 216, 148, 19, 29, 73},
556 {77, 7, 64, 116, 132, 122, 37, 126, 120},
557 {101, 21, 107, 181, 192, 103, 19, 67, 125}},
558 // partition_probs
559 {{199, 122, 141},
560 {147, 63, 159},
561 {148, 133, 118},
562 {121, 104, 114},
563 {174, 73, 87},
564 {92, 41, 83},
565 {82, 99, 50},
566 {53, 39, 39},
567 {177, 58, 59},
568 {68, 26, 63},
569 {52, 79, 25},
570 {17, 14, 12},
571 {222, 34, 30},
572 {72, 16, 44},
573 {58, 32, 12},
574 {10, 7, 6}},
575 // mv_joint_probs
576 {32, 64, 96},
577 // mv_sign_prob
578 {128, 128},
579 // mv_class_probs
580 {{224, 144, 192, 168, 192, 176, 192, 198, 198, 245},
581 {216, 128, 176, 160, 176, 176, 192, 198, 198, 208}},
582 // mv_class0_bit_prob
583 {216, 208},
584 // mv_bits_prob
585 {{136, 140, 148, 160, 176, 192, 224, 234, 234, 240},
586 {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}},
587 // mv_class0_fr_probs
588 {{{128, 128, 64}, {96, 112, 64}}, {{128, 128, 64}, {96, 112, 64}}},
589 // mv_fr_probs
590 {{64, 96, 64}, {64, 96, 64}},
591 // mv_class0_hp_prob
592 {160, 160},
593 // mv_hp_prob
594 {128, 128},
595 };
596
597 // Helper function for Vp9Parser::ReadTileInfo. Defined as
598 // calc_min_log2_tile_cols in spec 6.2.14 Tile size calculation.
GetMinLog2TileCols(int sb64_cols)599 int GetMinLog2TileCols(int sb64_cols) {
600 const int kMaxTileWidthB64 = 64;
601 int min_log2 = 0;
602 while ((kMaxTileWidthB64 << min_log2) < sb64_cols)
603 min_log2++;
604 return min_log2;
605 }
606
607 // Helper function for Vp9Parser::ReadTileInfo. Defined as
608 // calc_max_log2_tile_cols in spec 6.2.14 Tile size calculation.
GetMaxLog2TileCols(int sb64_cols)609 int GetMaxLog2TileCols(int sb64_cols) {
610 const int kMinTileWidthB64 = 4;
611 int max_log2 = 1;
612 while ((sb64_cols >> max_log2) >= kMinTileWidthB64)
613 max_log2++;
614 return max_log2 - 1;
615 }
616
617 } // namespace
618
Vp9UncompressedHeaderParser(Vp9Parser::Context * context)619 Vp9UncompressedHeaderParser::Vp9UncompressedHeaderParser(
620 Vp9Parser::Context* context)
621 : context_(context) {}
622
ReadProfile()623 uint8_t Vp9UncompressedHeaderParser::ReadProfile() {
624 uint8_t profile = 0;
625
626 // LSB first.
627 if (reader_.ReadBool())
628 profile |= 1;
629 if (reader_.ReadBool())
630 profile |= 2;
631 if (profile > 2 && reader_.ReadBool())
632 profile += 1;
633 return profile;
634 }
635
636 // 6.2.1 Frame sync syntax
VerifySyncCode()637 bool Vp9UncompressedHeaderParser::VerifySyncCode() {
638 const int kSyncCode = 0x498342;
639 if (reader_.ReadLiteral(8 * 3) != kSyncCode) {
640 DVLOG(1) << "Invalid frame sync code";
641 return false;
642 }
643 return true;
644 }
645
646 // 6.2.2 Color config syntax
ReadColorConfig(Vp9FrameHeader * fhdr)647 bool Vp9UncompressedHeaderParser::ReadColorConfig(Vp9FrameHeader* fhdr) {
648 if (fhdr->profile == 2 || fhdr->profile == 3) {
649 fhdr->bit_depth = reader_.ReadBool() ? 12 : 10;
650 } else {
651 fhdr->bit_depth = 8;
652 }
653
654 fhdr->color_space = static_cast<Vp9ColorSpace>(reader_.ReadLiteral(3));
655 if (fhdr->color_space != Vp9ColorSpace::SRGB) {
656 fhdr->color_range = reader_.ReadBool();
657 if (fhdr->profile == 1 || fhdr->profile == 3) {
658 fhdr->subsampling_x = reader_.ReadBool() ? 1 : 0;
659 fhdr->subsampling_y = reader_.ReadBool() ? 1 : 0;
660 if (fhdr->subsampling_x == 1 && fhdr->subsampling_y == 1) {
661 DVLOG(1) << "4:2:0 color not supported in profile 1 or 3";
662 return false;
663 }
664 bool reserved = reader_.ReadBool();
665 if (reserved) {
666 DVLOG(1) << "reserved bit set";
667 return false;
668 }
669 } else {
670 fhdr->subsampling_x = fhdr->subsampling_y = 1;
671 }
672 } else {
673 fhdr->color_range = true;
674 if (fhdr->profile == 1 || fhdr->profile == 3) {
675 fhdr->subsampling_x = fhdr->subsampling_y = 0;
676
677 bool reserved = reader_.ReadBool();
678 if (reserved) {
679 DVLOG(1) << "reserved bit set";
680 return false;
681 }
682 } else {
683 DVLOG(1) << "4:4:4 color not supported in profile 0 or 2";
684 return false;
685 }
686 }
687
688 return true;
689 }
690
691 // 6.2.3 Frame size syntax
ReadFrameSize(Vp9FrameHeader * fhdr)692 void Vp9UncompressedHeaderParser::ReadFrameSize(Vp9FrameHeader* fhdr) {
693 fhdr->frame_width = reader_.ReadLiteral(16) + 1;
694 fhdr->frame_height = reader_.ReadLiteral(16) + 1;
695 }
696
697 // 6.2.4 Render size syntax
ReadRenderSize(Vp9FrameHeader * fhdr)698 void Vp9UncompressedHeaderParser::ReadRenderSize(Vp9FrameHeader* fhdr) {
699 if (reader_.ReadBool()) {
700 fhdr->render_width = reader_.ReadLiteral(16) + 1;
701 fhdr->render_height = reader_.ReadLiteral(16) + 1;
702 } else {
703 fhdr->render_width = fhdr->frame_width;
704 fhdr->render_height = fhdr->frame_height;
705 }
706 }
707
708 // 6.2.5 Frame size with refs syntax
ReadFrameSizeFromRefs(Vp9FrameHeader * fhdr)709 bool Vp9UncompressedHeaderParser::ReadFrameSizeFromRefs(Vp9FrameHeader* fhdr) {
710 bool found_ref = false;
711 for (const auto& idx : fhdr->ref_frame_idx) {
712 found_ref = reader_.ReadBool();
713 if (found_ref) {
714 const Vp9Parser::ReferenceSlot& ref = context_->GetRefSlot(idx);
715 DCHECK(ref.initialized);
716 fhdr->frame_width = ref.frame_width;
717 fhdr->frame_height = ref.frame_height;
718
719 const unsigned kMaxDimension = 1u << 16;
720 DCHECK_LE(fhdr->frame_width, kMaxDimension);
721 DCHECK_LE(fhdr->frame_height, kMaxDimension);
722 break;
723 }
724 }
725
726 if (!found_ref)
727 ReadFrameSize(fhdr);
728
729 // 7.2.5 Frame size with refs semantics
730 bool has_valid_ref_frame = false;
731 for (const auto& idx : fhdr->ref_frame_idx) {
732 const Vp9Parser::ReferenceSlot& ref = context_->GetRefSlot(idx);
733 if (2 * fhdr->frame_width >= ref.frame_width &&
734 2 * fhdr->frame_height >= ref.frame_height &&
735 fhdr->frame_width <= 16 * ref.frame_width &&
736 fhdr->frame_height <= 16 * ref.frame_height) {
737 has_valid_ref_frame = true;
738 break;
739 }
740 }
741 if (!has_valid_ref_frame) {
742 DVLOG(1) << "There should be at least one reference frame meeting "
743 << "size conditions.";
744 return false;
745 }
746
747 ReadRenderSize(fhdr);
748 return true;
749 }
750
751 // 6.2.7 Interpolation filter syntax
ReadInterpolationFilter()752 Vp9InterpolationFilter Vp9UncompressedHeaderParser::ReadInterpolationFilter() {
753 if (reader_.ReadBool())
754 return Vp9InterpolationFilter::SWITCHABLE;
755
756 // The mapping table for next two bits.
757 const Vp9InterpolationFilter table[] = {
758 Vp9InterpolationFilter::EIGHTTAP_SMOOTH, Vp9InterpolationFilter::EIGHTTAP,
759 Vp9InterpolationFilter::EIGHTTAP_SHARP, Vp9InterpolationFilter::BILINEAR,
760 };
761 return table[reader_.ReadLiteral(2)];
762 }
763
SetupPastIndependence(Vp9FrameHeader * fhdr)764 void Vp9UncompressedHeaderParser::SetupPastIndependence(Vp9FrameHeader* fhdr) {
765 memset(&context_->segmentation_, 0, sizeof(context_->segmentation_));
766 ResetLoopfilter();
767 fhdr->frame_context = kVp9DefaultFrameContext;
768 DCHECK(fhdr->frame_context.IsValid());
769 }
770
771 // 6.2.8 Loop filter params syntax
ReadLoopFilterParams()772 void Vp9UncompressedHeaderParser::ReadLoopFilterParams() {
773 Vp9LoopFilterParams& loop_filter = context_->loop_filter_;
774
775 loop_filter.level = reader_.ReadLiteral(6);
776 loop_filter.sharpness = reader_.ReadLiteral(3);
777 loop_filter.delta_update = false;
778
779 loop_filter.delta_enabled = reader_.ReadBool();
780 if (loop_filter.delta_enabled) {
781 loop_filter.delta_update = reader_.ReadBool();
782 if (loop_filter.delta_update) {
783 for (size_t i = 0; i < Vp9RefType::VP9_FRAME_MAX; i++) {
784 loop_filter.update_ref_deltas[i] = reader_.ReadBool();
785 if (loop_filter.update_ref_deltas[i])
786 loop_filter.ref_deltas[i] = reader_.ReadSignedLiteral(6);
787 }
788
789 for (size_t i = 0; i < Vp9LoopFilterParams::kNumModeDeltas; i++) {
790 loop_filter.update_mode_deltas[i] = reader_.ReadBool();
791 if (loop_filter.update_mode_deltas[i])
792 loop_filter.mode_deltas[i] = reader_.ReadLiteral(6);
793 }
794 }
795 }
796 }
797
798 // 6.2.9 Quantization params syntax
ReadQuantizationParams(Vp9QuantizationParams * quants)799 void Vp9UncompressedHeaderParser::ReadQuantizationParams(
800 Vp9QuantizationParams* quants) {
801 quants->base_q_idx = reader_.ReadLiteral(8);
802
803 quants->delta_q_y_dc = ReadDeltaQ();
804 quants->delta_q_uv_dc = ReadDeltaQ();
805 quants->delta_q_uv_ac = ReadDeltaQ();
806 }
807
808 // 6.2.10 Delta quantizer syntax
ReadDeltaQ()809 int8_t Vp9UncompressedHeaderParser::ReadDeltaQ() {
810 if (reader_.ReadBool())
811 return reader_.ReadSignedLiteral(4);
812 return 0;
813 }
814
815 // 6.2.11 Segmentation params syntax
ReadSegmentationParams()816 bool Vp9UncompressedHeaderParser::ReadSegmentationParams() {
817 Vp9SegmentationParams& segmentation = context_->segmentation_;
818 segmentation.update_map = false;
819 segmentation.update_data = false;
820
821 segmentation.enabled = reader_.ReadBool();
822 if (!segmentation.enabled)
823 return true;
824
825 segmentation.update_map = reader_.ReadBool();
826 if (segmentation.update_map) {
827 for (auto& tree_prob : segmentation.tree_probs) {
828 tree_prob = ReadProb();
829 }
830
831 segmentation.temporal_update = reader_.ReadBool();
832 for (auto& pred_prob : segmentation.pred_probs) {
833 pred_prob = segmentation.temporal_update ? ReadProb() : kVp9MaxProb;
834 }
835 }
836
837 segmentation.update_data = reader_.ReadBool();
838 if (segmentation.update_data) {
839 segmentation.abs_or_delta_update = reader_.ReadBool();
840
841 const int kFeatureDataBits[] = {8, 6, 2, 0};
842 const bool kFeatureDataSigned[] = {true, true, false, false};
843
844 for (size_t i = 0; i < Vp9SegmentationParams::kNumSegments; i++) {
845 for (size_t j = 0; j < Vp9SegmentationParams::SEG_LVL_MAX; j++) {
846 int16_t data = 0;
847 segmentation.feature_enabled[i][j] = reader_.ReadBool();
848 if (segmentation.feature_enabled[i][j]) {
849 data = reader_.ReadLiteral(kFeatureDataBits[j]);
850 if (kFeatureDataSigned[j])
851 if (reader_.ReadBool()) {
852 // 7.2.9
853 if (segmentation.abs_or_delta_update) {
854 DVLOG(1) << "feature_sign should be 0"
855 << " if abs_or_delta_update is 1";
856 return false;
857 }
858 data = -data;
859 }
860 }
861 segmentation.feature_data[i][j] = data;
862 }
863 }
864 }
865 return true;
866 }
867
868 // 6.2.12 Probability syntax
ReadProb()869 uint8_t Vp9UncompressedHeaderParser::ReadProb() {
870 return reader_.ReadBool() ? reader_.ReadLiteral(8) : kVp9MaxProb;
871 }
872
873 // 6.2.13 Tile info syntax
ReadTileInfo(Vp9FrameHeader * fhdr)874 bool Vp9UncompressedHeaderParser::ReadTileInfo(Vp9FrameHeader* fhdr) {
875 int sb64_cols = (fhdr->frame_width + 63) / 64;
876
877 int min_log2_tile_cols = GetMinLog2TileCols(sb64_cols);
878 int max_log2_tile_cols = GetMaxLog2TileCols(sb64_cols);
879
880 int max_ones = max_log2_tile_cols - min_log2_tile_cols;
881 fhdr->tile_cols_log2 = min_log2_tile_cols;
882 while (max_ones-- && reader_.ReadBool())
883 fhdr->tile_cols_log2++;
884
885 fhdr->tile_rows_log2 = reader_.ReadBool() ? 1 : 0;
886 if (fhdr->tile_rows_log2 > 0 && reader_.ReadBool())
887 fhdr->tile_rows_log2++;
888
889 // 7.2.11 Tile info semantics
890 if (fhdr->tile_cols_log2 > 6) {
891 DVLOG(1) << "tile_cols_log2 should be <= 6";
892 return false;
893 }
894
895 return true;
896 }
897
ResetLoopfilter()898 void Vp9UncompressedHeaderParser::ResetLoopfilter() {
899 Vp9LoopFilterParams& loop_filter = context_->loop_filter_;
900
901 loop_filter.delta_enabled = true;
902 loop_filter.delta_update = true;
903
904 loop_filter.ref_deltas[VP9_FRAME_INTRA] = 1;
905 loop_filter.ref_deltas[VP9_FRAME_LAST] = 0;
906 loop_filter.ref_deltas[VP9_FRAME_GOLDEN] = -1;
907 loop_filter.ref_deltas[VP9_FRAME_ALTREF] = -1;
908
909 memset(loop_filter.mode_deltas, 0, sizeof(loop_filter.mode_deltas));
910 }
911
912 // 6.2 Uncompressed header syntax
Parse(const uint8_t * stream,off_t frame_size,Vp9FrameHeader * fhdr)913 bool Vp9UncompressedHeaderParser::Parse(const uint8_t* stream,
914 off_t frame_size,
915 Vp9FrameHeader* fhdr) {
916 DVLOG(2) << "Vp9UncompressedHeaderParser::Parse";
917 reader_.Initialize(stream, frame_size);
918
919 fhdr->data = stream;
920 fhdr->frame_size = frame_size;
921
922 // frame marker
923 if (reader_.ReadLiteral(2) != 0x2) {
924 DVLOG(1) << "frame marker shall be equal to 2";
925 return false;
926 }
927
928 fhdr->profile = ReadProfile();
929 if (fhdr->profile >= kVp9MaxProfile) {
930 DVLOG(1) << "Unsupported bitstream profile";
931 return false;
932 }
933
934 fhdr->show_existing_frame = reader_.ReadBool();
935 if (fhdr->show_existing_frame) {
936 fhdr->frame_to_show_map_idx = reader_.ReadLiteral(3);
937 fhdr->show_frame = true;
938
939 if (!reader_.ConsumeTrailingBits()) {
940 DVLOG(1) << "trailing bits are not zero";
941 return false;
942 }
943 if (!reader_.IsValid()) {
944 DVLOG(1) << "parser reads beyond the end of buffer";
945 return false;
946 }
947 fhdr->uncompressed_header_size = reader_.GetBytesRead();
948 fhdr->header_size_in_bytes = 0;
949 return true;
950 }
951
952 fhdr->frame_type = static_cast<Vp9FrameHeader::FrameType>(reader_.ReadBool());
953 fhdr->show_frame = reader_.ReadBool();
954 fhdr->error_resilient_mode = reader_.ReadBool();
955
956 if (fhdr->IsKeyframe()) {
957 if (!VerifySyncCode())
958 return false;
959
960 if (!ReadColorConfig(fhdr))
961 return false;
962
963 ReadFrameSize(fhdr);
964 ReadRenderSize(fhdr);
965 fhdr->refresh_frame_flags = 0xff;
966 } else {
967 if (!fhdr->show_frame)
968 fhdr->intra_only = reader_.ReadBool();
969
970 if (!fhdr->error_resilient_mode)
971 fhdr->reset_frame_context = reader_.ReadLiteral(2);
972
973 if (fhdr->intra_only) {
974 if (!VerifySyncCode())
975 return false;
976
977 if (fhdr->profile > 0) {
978 if (!ReadColorConfig(fhdr))
979 return false;
980 } else {
981 fhdr->bit_depth = 8;
982 fhdr->color_space = Vp9ColorSpace::BT_601;
983 fhdr->subsampling_x = fhdr->subsampling_y = 1;
984 }
985
986 fhdr->refresh_frame_flags = reader_.ReadLiteral(8);
987
988 ReadFrameSize(fhdr);
989 ReadRenderSize(fhdr);
990 } else {
991 fhdr->refresh_frame_flags = reader_.ReadLiteral(8);
992
993 static_assert(arraysize(fhdr->ref_frame_sign_bias) >=
994 Vp9RefType::VP9_FRAME_LAST + kVp9NumRefsPerFrame,
995 "ref_frame_sign_bias is not big enough");
996 for (size_t i = 0; i < kVp9NumRefsPerFrame; i++) {
997 fhdr->ref_frame_idx[i] = reader_.ReadLiteral(kVp9NumRefFramesLog2);
998 fhdr->ref_frame_sign_bias[Vp9RefType::VP9_FRAME_LAST + i] =
999 reader_.ReadBool();
1000
1001 // 8.2 Frame order constraints
1002 // ref_frame_idx[i] refers to an earlier decoded frame.
1003 const Vp9Parser::ReferenceSlot& ref =
1004 context_->GetRefSlot(fhdr->ref_frame_idx[i]);
1005 if (!ref.initialized) {
1006 DVLOG(1) << "ref_frame_idx[" << i
1007 << "]=" << static_cast<int>(fhdr->ref_frame_idx[i])
1008 << " refers to unused frame";
1009 return false;
1010 }
1011
1012 // 7.2 Uncompressed header semantics
1013 // the selected reference frames match the current frame in bit depth,
1014 // profile, chroma subsampling, and color space.
1015 if (ref.profile != fhdr->profile) {
1016 DVLOG(1) << "profile of referenced frame mismatch";
1017 return false;
1018 }
1019 if (i == 0) {
1020 // Below fields are not specified for inter-frame in header, so copy
1021 // them from referenced frame.
1022 fhdr->bit_depth = ref.bit_depth;
1023 fhdr->color_space = ref.color_space;
1024 fhdr->subsampling_x = ref.subsampling_x;
1025 fhdr->subsampling_y = ref.subsampling_y;
1026 } else {
1027 if (fhdr->bit_depth != ref.bit_depth) {
1028 DVLOG(1) << "bit_depth of referenced frame mismatch";
1029 return false;
1030 }
1031 if (fhdr->color_space != ref.color_space) {
1032 DVLOG(1) << "color_space of referenced frame mismatch";
1033 return false;
1034 }
1035 if (fhdr->subsampling_x != ref.subsampling_x ||
1036 fhdr->subsampling_y != ref.subsampling_y) {
1037 DVLOG(1) << "chroma subsampling of referenced frame mismatch";
1038 return false;
1039 }
1040 }
1041 }
1042
1043 if (!ReadFrameSizeFromRefs(fhdr))
1044 return false;
1045
1046 fhdr->allow_high_precision_mv = reader_.ReadBool();
1047 fhdr->interpolation_filter = ReadInterpolationFilter();
1048 }
1049 }
1050
1051 if (fhdr->error_resilient_mode) {
1052 fhdr->refresh_frame_context = false;
1053 fhdr->frame_parallel_decoding_mode = true;
1054 } else {
1055 fhdr->refresh_frame_context = reader_.ReadBool();
1056 fhdr->frame_parallel_decoding_mode = reader_.ReadBool();
1057 }
1058
1059 fhdr->frame_context_idx_to_save_probs = fhdr->frame_context_idx =
1060 reader_.ReadLiteral(kVp9NumFrameContextsLog2);
1061
1062 if (fhdr->IsIntra()) {
1063 SetupPastIndependence(fhdr);
1064 if (fhdr->IsKeyframe() || fhdr->error_resilient_mode ||
1065 fhdr->reset_frame_context == 3) {
1066 for (size_t i = 0; i < kVp9NumFrameContexts; ++i)
1067 context_->UpdateFrameContext(i, fhdr->frame_context);
1068 } else if (fhdr->reset_frame_context == 2) {
1069 context_->UpdateFrameContext(fhdr->frame_context_idx,
1070 fhdr->frame_context);
1071 }
1072 fhdr->frame_context_idx = 0;
1073 }
1074
1075 ReadLoopFilterParams();
1076 ReadQuantizationParams(&fhdr->quant_params);
1077 if (!ReadSegmentationParams())
1078 return false;
1079
1080 if (!ReadTileInfo(fhdr))
1081 return false;
1082
1083 fhdr->header_size_in_bytes = reader_.ReadLiteral(16);
1084 if (fhdr->header_size_in_bytes == 0) {
1085 DVLOG(1) << "invalid header size";
1086 return false;
1087 }
1088
1089 if (!reader_.ConsumeTrailingBits()) {
1090 DVLOG(1) << "trailing bits are not zero";
1091 return false;
1092 }
1093 if (!reader_.IsValid()) {
1094 DVLOG(1) << "parser reads beyond the end of buffer";
1095 return false;
1096 }
1097 fhdr->uncompressed_header_size = reader_.GetBytesRead();
1098
1099 return true;
1100 }
1101
1102 } // namespace media
1103