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