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