Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / vp9_parser.cc
1 // Copyright 2015 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 // This file contains an implementation of a VP9 bitstream parser.
6 //
7 // VERBOSE level:
8 //  1 something wrong in bitstream
9 //  2 parsing steps
10 //  3 parsed values (selected)
11
12 #include "media/filters/vp9_parser.h"
13
14 #include <algorithm>
15
16 #include "base/bind.h"
17 #include "base/callback_helpers.h"
18 #include "base/containers/circular_deque.h"
19 #include "base/cxx17_backports.h"
20 #include "base/logging.h"
21 #include "base/numerics/safe_conversions.h"
22 #include "base/sys_byteorder.h"
23 #include "media/filters/vp9_compressed_header_parser.h"
24 #include "media/filters/vp9_uncompressed_header_parser.h"
25
26 namespace media {
27
28 namespace {
29
30 // Coefficients extracted verbatim from "VP9 Bitstream & Decoding Process
31 // Specification" Version 0.6, Sec 8.6.1 Dequantization functions, see:
32 // https://www.webmproject.org/vp9/#draft-vp9-bitstream-and-decoding-process-specification
33 constexpr size_t kQIndexRange = 256;
34 // clang-format off
35 // libva is the only user of high bit depth VP9 formats and only supports
36 // 10 bits per component, see https://github.com/01org/libva/issues/137.
37 // TODO(mcasas): Add the 12 bit versions of these tables.
38 const int16_t kDcQLookup[][kQIndexRange] = {
39     {
40         4,    8,    8,    9,    10,   11,   12,   12,  13,   14,   15,   16,
41         17,   18,   19,   19,   20,   21,   22,   23,  24,   25,   26,   26,
42         27,   28,   29,   30,   31,   32,   32,   33,  34,   35,   36,   37,
43         38,   38,   39,   40,   41,   42,   43,   43,  44,   45,   46,   47,
44         48,   48,   49,   50,   51,   52,   53,   53,  54,   55,   56,   57,
45         57,   58,   59,   60,   61,   62,   62,   63,  64,   65,   66,   66,
46         67,   68,   69,   70,   70,   71,   72,   73,  74,   74,   75,   76,
47         77,   78,   78,   79,   80,   81,   81,   82,  83,   84,   85,   85,
48         87,   88,   90,   92,   93,   95,   96,   98,  99,   101,  102,  104,
49         105,  107,  108,  110,  111,  113,  114,  116, 117,  118,  120,  121,
50         123,  125,  127,  129,  131,  134,  136,  138, 140,  142,  144,  146,
51         148,  150,  152,  154,  156,  158,  161,  164, 166,  169,  172,  174,
52         177,  180,  182,  185,  187,  190,  192,  195, 199,  202,  205,  208,
53         211,  214,  217,  220,  223,  226,  230,  233, 237,  240,  243,  247,
54         250,  253,  257,  261,  265,  269,  272,  276, 280,  284,  288,  292,
55         296,  300,  304,  309,  313,  317,  322,  326, 330,  335,  340,  344,
56         349,  354,  359,  364,  369,  374,  379,  384, 389,  395,  400,  406,
57         411,  417,  423,  429,  435,  441,  447,  454, 461,  467,  475,  482,
58         489,  497,  505,  513,  522,  530,  539,  549, 559,  569,  579,  590,
59         602,  614,  626,  640,  654,  668,  684,  700, 717,  736,  755,  775,
60         796,  819,  843,  869,  896,  925,  955,  988, 1022, 1058, 1098, 1139,
61         1184, 1232, 1282, 1336,
62     },
63     {
64         4,    9,    10,   13,   15,   17,   20,   22,   25,   28,   31,   34,
65         37,   40,   43,   47,   50,   53,   57,   60,   64,   68,   71,   75,
66         78,   82,   86,   90,   93,   97,   101,  105,  109,  113,  116,  120,
67         124,  128,  132,  136,  140,  143,  147,  151,  155,  159,  163,  166,
68         170,  174,  178,  182,  185,  189,  193,  197,  200,  204,  208,  212,
69         215,  219,  223,  226,  230,  233,  237,  241,  244,  248,  251,  255,
70         259,  262,  266,  269,  273,  276,  280,  283,  287,  290,  293,  297,
71         300,  304,  307,  310,  314,  317,  321,  324,  327,  331,  334,  337,
72         343,  350,  356,  362,  369,  375,  381,  387,  394,  400,  406,  412,
73         418,  424,  430,  436,  442,  448,  454,  460,  466,  472,  478,  484,
74         490,  499,  507,  516,  525,  533,  542,  550,  559,  567,  576,  584,
75         592,  601,  609,  617,  625,  634,  644,  655,  666,  676,  687,  698,
76         708,  718,  729,  739,  749,  759,  770,  782,  795,  807,  819,  831,
77         844,  856,  868,  880,  891,  906,  920,  933,  947,  961,  975,  988,
78         1001, 1015, 1030, 1045, 1061, 1076, 1090, 1105, 1120, 1137, 1153, 1170,
79         1186, 1202, 1218, 1236, 1253, 1271, 1288, 1306, 1323, 1342, 1361, 1379,
80         1398, 1416, 1436, 1456, 1476, 1496, 1516, 1537, 1559, 1580, 1601, 1624,
81         1647, 1670, 1692, 1717, 1741, 1766, 1791, 1817, 1844, 1871, 1900, 1929,
82         1958, 1990, 2021, 2054, 2088, 2123, 2159, 2197, 2236, 2276, 2319, 2363,
83         2410, 2458, 2508, 2561, 2616, 2675, 2737, 2802, 2871, 2944, 3020, 3102,
84         3188, 3280, 3375, 3478, 3586, 3702, 3823, 3953, 4089, 4236, 4394, 4559,
85         4737, 4929, 5130, 5347
86    }
87 };
88
89 const int16_t kAcQLookup[][kQIndexRange] = {
90     {
91         4,    8,    9,    10,   11,   12,   13,   14,   15,   16,   17,   18,
92         19,   20,   21,   22,   23,   24,   25,   26,   27,   28,   29,   30,
93         31,   32,   33,   34,   35,   36,   37,   38,   39,   40,   41,   42,
94         43,   44,   45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
95         55,   56,   57,   58,   59,   60,   61,   62,   63,   64,   65,   66,
96         67,   68,   69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
97         79,   80,   81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
98         91,   92,   93,   94,   95,   96,   97,   98,   99,   100,  101,  102,
99         104,  106,  108,  110,  112,  114,  116,  118,  120,  122,  124,  126,
100         128,  130,  132,  134,  136,  138,  140,  142,  144,  146,  148,  150,
101         152,  155,  158,  161,  164,  167,  170,  173,  176,  179,  182,  185,
102         188,  191,  194,  197,  200,  203,  207,  211,  215,  219,  223,  227,
103         231,  235,  239,  243,  247,  251,  255,  260,  265,  270,  275,  280,
104         285,  290,  295,  300,  305,  311,  317,  323,  329,  335,  341,  347,
105         353,  359,  366,  373,  380,  387,  394,  401,  408,  416,  424,  432,
106         440,  448,  456,  465,  474,  483,  492,  501,  510,  520,  530,  540,
107         550,  560,  571,  582,  593,  604,  615,  627,  639,  651,  663,  676,
108         689,  702,  715,  729,  743,  757,  771,  786,  801,  816,  832,  848,
109         864,  881,  898,  915,  933,  951,  969,  988,  1007, 1026, 1046, 1066,
110         1087, 1108, 1129, 1151, 1173, 1196, 1219, 1243, 1267, 1292, 1317, 1343,
111         1369, 1396, 1423, 1451, 1479, 1508, 1537, 1567, 1597, 1628, 1660, 1692,
112         1725, 1759, 1793, 1828,
113     },
114     {
115         4,    9,    11,   13,   16,   18,   21,   24,   27,   30,   33,   37,
116         40,   44,   48,   51,   55,   59,   63,   67,   71,   75,   79,   83,
117         88,   92,   96,   100,  105,  109,  114,  118,  122,  127,  131,  136,
118         140,  145,  149,  154,  158,  163,  168,  172,  177,  181,  186,  190,
119         195,  199,  204,  208,  213,  217,  222,  226,  231,  235,  240,  244,
120         249,  253,  258,  262,  267,  271,  275,  280,  284,  289,  293,  297,
121         302,  306,  311,  315,  319,  324,  328,  332,  337,  341,  345,  349,
122         354,  358,  362,  367,  371,  375,  379,  384,  388,  392,  396,  401,
123         409,  417,  425,  433,  441,  449,  458,  466,  474,  482,  490,  498,
124         506,  514,  523,  531,  539,  547,  555,  563,  571,  579,  588,  596,
125         604,  616,  628,  640,  652,  664,  676,  688,  700,  713,  725,  737,
126         749,  761,  773,  785,  797,  809,  825,  841,  857,  873,  889,  905,
127         922,  938,  954,  970,  986,  1002, 1018, 1038, 1058, 1078, 1098, 1118,
128         1138, 1158, 1178, 1198, 1218, 1242, 1266, 1290, 1314, 1338, 1362, 1386,
129         1411, 1435, 1463, 1491, 1519, 1547, 1575, 1603, 1631, 1663, 1695, 1727,
130         1759, 1791, 1823, 1859, 1895, 1931, 1967, 2003, 2039, 2079, 2119, 2159,
131         2199, 2239, 2283, 2327, 2371, 2415, 2459, 2507, 2555, 2603, 2651, 2703,
132         2755, 2807, 2859, 2915, 2971, 3027, 3083, 3143, 3203, 3263, 3327, 3391,
133         3455, 3523, 3591, 3659, 3731, 3803, 3876, 3952, 4028, 4104, 4184, 4264,
134         4348, 4432, 4516, 4604, 4692, 4784, 4876, 4972, 5068, 5168, 5268, 5372,
135         5476, 5584, 5692, 5804, 5916, 6032, 6148, 6268, 6388, 6512, 6640, 6768,
136         6900, 7036, 7172, 7312
137    }
138 };
139 // clang-format on
140
141 static_assert(std::size(kDcQLookup[0]) == std::size(kAcQLookup[0]),
142               "quantizer lookup arrays of incorrect size");
143
144 size_t ClampQ(int64_t q) {
145   return q < 0 ? 0
146                : base::checked_cast<size_t>(
147                      std::min(q, static_cast<int64_t>(kQIndexRange - 1)));
148 }
149
150 int ClampLf(int lf) {
151   constexpr int kMaxLoopFilterLevel = 63;
152   return base::clamp(lf, 0, kMaxLoopFilterLevel);
153 }
154
155 std::string IncrementIV(const std::string& iv, uint32_t by) {
156   // What we call the 'IV' value is actually somewhat of a misnomer:
157   // "IV" = 0xFFFFFFFFFFFFFFFF0000000000000000
158   //          â””──actual IV───┘└─block counter┘
159   // When we want to 'increment' this structure, we treat them both
160   // as big-endian 64 bit unsigned integers, then increment _only_ the
161   // block counter, then combine them back into a big-endian bytestring.
162   // |by| is usually going to be the number of blocks (aka 16 byte chunks)
163   //      of cipher data.
164   DCHECK_EQ(iv.size(), 16u);
165   uint64_t integral_data[2];
166   memcpy(integral_data, reinterpret_cast<const uint8_t*>(iv.data()), 16);
167   uint64_t block_counter = base::NetToHost64(integral_data[1]) + by;
168   integral_data[1] = base::HostToNet64(block_counter);
169   uint8_t new_iv[16];
170   memcpy(new_iv, integral_data, 16);
171   return std::string(reinterpret_cast<char*>(new_iv), 16);
172 }
173
174 // |frame_size|: The size of the current frame; this controls how long we
175 //               loop through the subsamples.
176 // |current_subsample_index|: An index into the |subsamples| vector, we need
177 //                            to have this saved between function calls.
178 // |extra_clear_bytes|: The previous call may have set this variable to show
179 //                      that a subsample mey have already started being parsed
180 //                      and that only X bytes of free data are left in it.
181 // |base_decrypt_config|: Not an output parameter, it is just a raw ptr from a
182 //                        unique_ptr.
183 // |subsamples|: A vector of subsamples.
184 // |iv|: The initialization vector (128bit number stored as std::string). This
185 //       gets incremented by (cipher_bytes % 16) for each frame, and must be
186 //       preserved across function calls.
187 std::unique_ptr<DecryptConfig> SplitSubsamples(
188     uint32_t frame_size,
189     size_t* current_subsample_index,
190     size_t* extra_clear_subsample_bytes,
191     DecryptConfig* base_decrypt_config,
192     const std::vector<SubsampleEntry>& subsamples,
193     std::string* iv) {
194   // We copy iv so that we can use the starting value in our
195   // new config while still incrementing IV for the next frame.
196   std::string frame_dc_iv = *iv;
197   std::vector<SubsampleEntry> frame_dc_subsamples;
198   do {
199     if (*current_subsample_index >= subsamples.size()) {
200       DVLOG(1) << "Not enough subsamples in the superframe decrypt config";
201       return nullptr;
202     }
203
204     uint32_t subsample_clear = subsamples[*current_subsample_index].clear_bytes;
205     uint32_t subsample_cipher =
206         subsamples[*current_subsample_index].cypher_bytes;
207
208     // if clear+cipher bytes would be over the max of uint32_t, we need to
209     // quit immediatly, to prevent malicious overflowing.
210     if (0xFFFFFFFF - subsample_clear < subsample_cipher) {
211       DVLOG(1) << "Invalid subsample alignment";
212       return nullptr;
213     }
214
215     // It's possible that the previous frame didn't use all the clear bytes
216     // in this subsample, in which case we have to start from midway through
217     // the clear section.
218     if (*extra_clear_subsample_bytes) {
219       subsample_clear = *extra_clear_subsample_bytes;
220     }
221
222     if (subsample_clear > frame_size) {
223       // Support scenario where clear section is larger than our frame:
224       // The entire length is clear. If |subsample_clear| is the same length,
225       // we handle it below.
226       frame_dc_subsamples.push_back(SubsampleEntry(frame_size, 0));
227       *extra_clear_subsample_bytes = subsample_clear - frame_size;
228       frame_size = 0;
229     } else if (subsample_clear + subsample_cipher > frame_size) {
230       // Only a clear section can cross over a frame boundary, otherwise
231       // the frame header for the next frame would be encrypted, which is not
232       // spec compliant.
233       DVLOG(1) << "Invalid subsample alignment";
234       return nullptr;
235     } else if (subsample_clear + subsample_cipher <= frame_size) {
236       // In this case a subsample is less than or equal to a whole frame
237       // This is the most likely case for almost all encrypted media.
238       // note that |subsample_cipher| can be 0.
239       frame_dc_subsamples.push_back(
240           SubsampleEntry(subsample_clear, subsample_cipher));
241       frame_size -= (subsample_clear + subsample_cipher);
242       *extra_clear_subsample_bytes = 0;
243
244       // IV gets incremented by 1 for every 16 bytes of cypher
245       *iv = IncrementIV(*iv, subsample_cipher >> 4);  // uint32 logical shift.
246     }
247
248     // Don't go to the next subsample if there are more clear bytes.
249     if (!*extra_clear_subsample_bytes) {
250       (*current_subsample_index)++;
251     }
252
253     // It is possible for there to be more than one subsample associated
254     // with a single frame, so we need to try again if there are more bytes
255     // left unaccounted for in this frame.
256   } while (frame_size);
257
258   return base_decrypt_config->CopyNewSubsamplesIV(frame_dc_subsamples,
259                                                   frame_dc_iv);
260 }
261
262 bool IsByteNEncrypted(off_t byte,
263                       const std::vector<SubsampleEntry>& subsamples) {
264   off_t original_byte = byte;
265   for (const SubsampleEntry& subsample : subsamples) {
266     if (byte < 0) {
267       return false;
268     }
269     if (static_cast<uint32_t>(byte) < subsample.clear_bytes) {
270       return false;
271     }
272     byte -= subsample.clear_bytes;
273     if (static_cast<uint32_t>(byte) < subsample.cypher_bytes) {
274       return true;
275     }
276     byte -= subsample.cypher_bytes;
277   }
278   DVLOG(3) << "Subsamples do not extend to cover offset " << original_byte;
279   return false;
280 }
281
282 }  // namespace
283
284 bool Vp9FrameHeader::IsKeyframe() const {
285   // When show_existing_frame is true, the frame header does not precede an
286   // actual frame to be decoded, so frame_type does not apply (and is not read
287   // from the stream).
288   return !show_existing_frame && frame_type == KEYFRAME;
289 }
290
291 bool Vp9FrameHeader::IsIntra() const {
292   return !show_existing_frame && (frame_type == KEYFRAME || intra_only);
293 }
294
295 VideoColorSpace Vp9FrameHeader::GetColorSpace() const {
296   VideoColorSpace ret;
297   ret.range = color_range ? gfx::ColorSpace::RangeID::FULL
298                           : gfx::ColorSpace::RangeID::LIMITED;
299   switch (color_space) {
300     case Vp9ColorSpace::RESERVED:
301     case Vp9ColorSpace::UNKNOWN:
302       break;
303     case Vp9ColorSpace::BT_601:
304     case Vp9ColorSpace::SMPTE_170:
305       ret.primaries = VideoColorSpace::PrimaryID::SMPTE170M;
306       ret.transfer = VideoColorSpace::TransferID::SMPTE170M;
307       ret.matrix = VideoColorSpace::MatrixID::SMPTE170M;
308       break;
309     case Vp9ColorSpace::BT_709:
310       ret.primaries = VideoColorSpace::PrimaryID::BT709;
311       ret.transfer = VideoColorSpace::TransferID::BT709;
312       ret.matrix = VideoColorSpace::MatrixID::BT709;
313       break;
314     case Vp9ColorSpace::SMPTE_240:
315       ret.primaries = VideoColorSpace::PrimaryID::SMPTE240M;
316       ret.transfer = VideoColorSpace::TransferID::SMPTE240M;
317       ret.matrix = VideoColorSpace::MatrixID::SMPTE240M;
318       break;
319     case Vp9ColorSpace::BT_2020:
320       ret.primaries = VideoColorSpace::PrimaryID::BT2020;
321       ret.transfer = VideoColorSpace::TransferID::BT2020_10;
322       ret.matrix = VideoColorSpace::MatrixID::BT2020_NCL;
323       break;
324     case Vp9ColorSpace::SRGB:
325       ret.primaries = VideoColorSpace::PrimaryID::BT709;
326       ret.transfer = VideoColorSpace::TransferID::IEC61966_2_1;
327       ret.matrix = VideoColorSpace::MatrixID::BT709;
328       break;
329   }
330   return ret;
331 }
332
333 Vp9Parser::FrameInfo::FrameInfo() = default;
334
335 Vp9Parser::FrameInfo::~FrameInfo() = default;
336
337 Vp9Parser::FrameInfo::FrameInfo(const uint8_t* ptr, off_t size)
338     : ptr(ptr), size(size) {}
339
340 Vp9Parser::FrameInfo::FrameInfo(const FrameInfo& copy_from)
341     : ptr(copy_from.ptr),
342       size(copy_from.size),
343       allocate_size(copy_from.allocate_size),
344       decrypt_config(copy_from.decrypt_config
345                          ? copy_from.decrypt_config->Clone()
346                          : nullptr) {}
347
348 Vp9Parser::FrameInfo& Vp9Parser::FrameInfo::operator=(
349     const FrameInfo& copy_from) {
350   this->ptr = copy_from.ptr;
351   this->size = copy_from.size;
352   this->allocate_size = copy_from.allocate_size;
353   this->decrypt_config =
354       copy_from.decrypt_config ? copy_from.decrypt_config->Clone() : nullptr;
355   return *this;
356 }
357
358 bool Vp9FrameContext::IsValid() const {
359   // probs should be in [1, 255] range.
360   static_assert(sizeof(Vp9Prob) == 1,
361                 "following checks assuming Vp9Prob is single byte");
362   if (memchr(tx_probs_8x8, 0, sizeof(tx_probs_8x8))) {
363     return false;
364   }
365   if (memchr(tx_probs_16x16, 0, sizeof(tx_probs_16x16))) {
366     return false;
367   }
368   if (memchr(tx_probs_32x32, 0, sizeof(tx_probs_32x32))) {
369     return false;
370   }
371
372   for (auto& a : coef_probs) {
373     for (auto& ai : a) {
374       for (auto& aj : ai) {
375         for (auto& ak : aj) {
376           int max_l = (+ak == +aj[0]) ? 3 : 6;
377           for (int l = 0; l < max_l; l++) {
378             for (auto& x : ak[l]) {
379               if (x == 0) {
380                 return false;
381               }
382             }
383           }
384         }
385       }
386     }
387   }
388   if (memchr(skip_prob, 0, sizeof(skip_prob))) {
389     return false;
390   }
391   if (memchr(inter_mode_probs, 0, sizeof(inter_mode_probs))) {
392     return false;
393   }
394   if (memchr(interp_filter_probs, 0, sizeof(interp_filter_probs))) {
395     return false;
396   }
397   if (memchr(is_inter_prob, 0, sizeof(is_inter_prob))) {
398     return false;
399   }
400   if (memchr(comp_mode_prob, 0, sizeof(comp_mode_prob))) {
401     return false;
402   }
403   if (memchr(single_ref_prob, 0, sizeof(single_ref_prob))) {
404     return false;
405   }
406   if (memchr(comp_ref_prob, 0, sizeof(comp_ref_prob))) {
407     return false;
408   }
409   if (memchr(y_mode_probs, 0, sizeof(y_mode_probs))) {
410     return false;
411   }
412   if (memchr(uv_mode_probs, 0, sizeof(uv_mode_probs))) {
413     return false;
414   }
415   if (memchr(partition_probs, 0, sizeof(partition_probs))) {
416     return false;
417   }
418   if (memchr(mv_joint_probs, 0, sizeof(mv_joint_probs))) {
419     return false;
420   }
421   if (memchr(mv_sign_prob, 0, sizeof(mv_sign_prob))) {
422     return false;
423   }
424   if (memchr(mv_class_probs, 0, sizeof(mv_class_probs))) {
425     return false;
426   }
427   if (memchr(mv_class0_bit_prob, 0, sizeof(mv_class0_bit_prob))) {
428     return false;
429   }
430   if (memchr(mv_bits_prob, 0, sizeof(mv_bits_prob))) {
431     return false;
432   }
433   if (memchr(mv_class0_fr_probs, 0, sizeof(mv_class0_fr_probs))) {
434     return false;
435   }
436   if (memchr(mv_fr_probs, 0, sizeof(mv_fr_probs))) {
437     return false;
438   }
439   if (memchr(mv_class0_hp_prob, 0, sizeof(mv_class0_hp_prob))) {
440     return false;
441   }
442   if (memchr(mv_hp_prob, 0, sizeof(mv_hp_prob))) {
443     return false;
444   }
445
446   return true;
447 }
448
449 Vp9Parser::Context::Vp9FrameContextManager::Vp9FrameContextManager() {}
450
451 Vp9Parser::Context::Vp9FrameContextManager::~Vp9FrameContextManager() = default;
452
453 const Vp9FrameContext&
454 Vp9Parser::Context::Vp9FrameContextManager::frame_context() const {
455   DCHECK(initialized_);
456   DCHECK(!needs_client_update_);
457   return frame_context_;
458 }
459
460 void Vp9Parser::Context::Vp9FrameContextManager::Reset() {
461   initialized_ = false;
462   needs_client_update_ = false;
463   weak_ptr_factory_.InvalidateWeakPtrs();
464 }
465
466 void Vp9Parser::Context::Vp9FrameContextManager::SetNeedsClientUpdate() {
467   DCHECK(!needs_client_update_);
468   initialized_ = true;
469   needs_client_update_ = true;
470 }
471
472 Vp9Parser::ContextRefreshCallback
473 Vp9Parser::Context::Vp9FrameContextManager::GetUpdateCb() {
474   if (needs_client_update_) {
475     return base::BindOnce(&Vp9FrameContextManager::UpdateFromClient,
476                           weak_ptr_factory_.GetWeakPtr());
477   }
478
479   return {};
480 }
481
482 void Vp9Parser::Context::Vp9FrameContextManager::Update(
483     const Vp9FrameContext& frame_context) {
484   // DCHECK because we can trust values from our parser.
485   DCHECK(frame_context.IsValid());
486   initialized_ = true;
487   frame_context_ = frame_context;
488
489   // For frame context we are updating, it may be still awaiting previous
490   // ContextRefreshCallback. Because we overwrite the value of context here and
491   // previous ContextRefreshCallback no longer matters, invalidate the weak ptr
492   // to prevent previous ContextRefreshCallback run.
493   // With this optimization, we may be able to parse more frames while previous
494   // are still decoding.
495   weak_ptr_factory_.InvalidateWeakPtrs();
496   needs_client_update_ = false;
497 }
498
499 void Vp9Parser::Context::Vp9FrameContextManager::UpdateFromClient(
500     const Vp9FrameContext& frame_context) {
501   DVLOG(2) << "Got external frame_context update";
502   DCHECK(needs_client_update_);
503   if (!frame_context.IsValid()) {
504     DLOG(ERROR) << "Invalid prob value in frame_context";
505     return;
506   }
507   needs_client_update_ = false;
508   initialized_ = true;
509   frame_context_ = frame_context;
510 }
511
512 void Vp9Parser::Context::Reset() {
513   memset(&segmentation_, 0, sizeof(segmentation_));
514   memset(&loop_filter_, 0, sizeof(loop_filter_));
515   memset(&ref_slots_, 0, sizeof(ref_slots_));
516   for (auto& manager : frame_context_managers_)
517     manager.Reset();
518 }
519
520 void Vp9Parser::Context::MarkFrameContextForUpdate(size_t frame_context_idx) {
521   DCHECK_LT(frame_context_idx, std::size(frame_context_managers_));
522   frame_context_managers_[frame_context_idx].SetNeedsClientUpdate();
523 }
524
525 void Vp9Parser::Context::UpdateFrameContext(
526     size_t frame_context_idx,
527     const Vp9FrameContext& frame_context) {
528   DCHECK_LT(frame_context_idx, std::size(frame_context_managers_));
529   frame_context_managers_[frame_context_idx].Update(frame_context);
530 }
531
532 const Vp9Parser::ReferenceSlot& Vp9Parser::Context::GetRefSlot(
533     size_t ref_type) const {
534   DCHECK_LT(ref_type, std::size(ref_slots_));
535   return ref_slots_[ref_type];
536 }
537
538 void Vp9Parser::Context::UpdateRefSlot(
539     size_t ref_type,
540     const Vp9Parser::ReferenceSlot& ref_slot) {
541   DCHECK_LT(ref_type, std::size(ref_slots_));
542   ref_slots_[ref_type] = ref_slot;
543 }
544
545 Vp9Parser::Vp9Parser(bool parsing_compressed_header)
546     : Vp9Parser(parsing_compressed_header,
547                 /*needs_external_context_update=*/false) {}
548
549 Vp9Parser::Vp9Parser(bool parsing_compressed_header,
550                      bool needs_external_context_update)
551     : parsing_compressed_header_(parsing_compressed_header),
552       needs_external_context_update_(needs_external_context_update) {
553   Reset();
554 }
555
556 Vp9Parser::~Vp9Parser() = default;
557
558 void Vp9Parser::SetStream(const uint8_t* stream,
559                           off_t stream_size,
560                           const std::vector<uint32_t>& spatial_layer_frame_size,
561                           std::unique_ptr<DecryptConfig> stream_config) {
562   DCHECK(stream);
563   stream_ = stream;
564   bytes_left_ = stream_size;
565   frames_.clear();
566   spatial_layer_frame_size_ = spatial_layer_frame_size;
567   stream_decrypt_config_ = std::move(stream_config);
568 }
569
570 void Vp9Parser::SetStream(const uint8_t* stream,
571                           off_t stream_size,
572                           std::unique_ptr<DecryptConfig> stream_config) {
573   SetStream(stream, stream_size, {}, std::move(stream_config));
574 }
575
576 void Vp9Parser::Reset() {
577   stream_ = nullptr;
578   bytes_left_ = 0;
579   frames_.clear();
580   spatial_layer_frame_size_.clear();
581   curr_frame_info_.Reset();
582
583   context_.Reset();
584 }
585
586 bool Vp9Parser::ParseUncompressedHeader(const FrameInfo& frame_info,
587                                         Vp9FrameHeader* fhdr,
588                                         Result* result,
589                                         Vp9Parser::Context* context) {
590   memset(&curr_frame_header_, 0, sizeof(curr_frame_header_));
591   *result = kInvalidStream;
592
593   Vp9UncompressedHeaderParser uncompressed_parser(context);
594   if (!uncompressed_parser.Parse(frame_info.ptr, frame_info.size,
595                                  &curr_frame_header_)) {
596     *result = kInvalidStream;
597     return true;
598   }
599
600   if (curr_frame_header_.header_size_in_bytes == 0) {
601     // Verify padding bits are zero.
602     for (off_t i = curr_frame_header_.uncompressed_header_size;
603          i < frame_info.size; i++) {
604       if (frame_info.ptr[i] != 0) {
605         DVLOG(1) << "Padding bits are not zeros.";
606         *result = kInvalidStream;
607         return true;
608       }
609     }
610     *fhdr = curr_frame_header_;
611     *result = kOk;
612     return true;
613   }
614   if (curr_frame_header_.uncompressed_header_size +
615           curr_frame_header_.header_size_in_bytes >
616       base::checked_cast<size_t>(frame_info.size)) {
617     DVLOG(1) << "header_size_in_bytes="
618              << curr_frame_header_.header_size_in_bytes
619              << " is larger than bytes left in buffer: "
620              << frame_info.size - curr_frame_header_.uncompressed_header_size;
621     *result = kInvalidStream;
622     return true;
623   }
624
625   return false;
626 }
627
628 bool Vp9Parser::ParseCompressedHeader(const FrameInfo& frame_info,
629                                       Result* result) {
630   *result = kInvalidStream;
631   size_t frame_context_idx = curr_frame_header_.frame_context_idx;
632   const Context::Vp9FrameContextManager& context_to_load =
633       context_.frame_context_managers_[frame_context_idx];
634   if (!context_to_load.initialized()) {
635     // 8.2 Frame order constraints
636     // must load an initialized set of probabilities.
637     DVLOG(1) << "loading uninitialized frame context, index="
638              << frame_context_idx;
639     *result = kInvalidStream;
640     return true;
641   }
642   if (context_to_load.needs_client_update()) {
643     DVLOG(3) << "waiting frame_context_idx=" << frame_context_idx
644              << " to update";
645     curr_frame_info_ = frame_info;
646     *result = kAwaitingRefresh;
647     return true;
648   }
649   curr_frame_header_.initial_frame_context = curr_frame_header_.frame_context =
650       context_to_load.frame_context();
651
652   Vp9CompressedHeaderParser compressed_parser;
653   bool parse_success;
654   if (!needs_external_context_update_) {
655     parse_success = compressed_parser.ParseNoContext(
656         frame_info.ptr + curr_frame_header_.uncompressed_header_size,
657         curr_frame_header_.header_size_in_bytes, &curr_frame_header_);
658   } else {
659     parse_success = compressed_parser.Parse(
660         frame_info.ptr + curr_frame_header_.uncompressed_header_size,
661         curr_frame_header_.header_size_in_bytes, &curr_frame_header_);
662   }
663   if (!parse_success) {
664     *result = kInvalidStream;
665     return true;
666   }
667
668   if (curr_frame_header_.refresh_frame_context) {
669     // In frame parallel mode, we can refresh the context without decoding
670     // tile data.
671     if (curr_frame_header_.frame_parallel_decoding_mode) {
672       context_.UpdateFrameContext(frame_context_idx,
673                                   curr_frame_header_.frame_context);
674     } else {
675       if (needs_external_context_update_)
676         context_.MarkFrameContextForUpdate(frame_context_idx);
677     }
678   }
679   return false;
680 }
681
682 Vp9Parser::Result Vp9Parser::ParseNextFrame(
683     Vp9FrameHeader* fhdr,
684     gfx::Size* allocate_size,
685     std::unique_ptr<DecryptConfig>* frame_decrypt_config) {
686   DCHECK(fhdr);
687   DCHECK(allocate_size);
688   DVLOG(2) << "ParseNextFrame";
689   FrameInfo frame_info;
690   Result result;
691
692   // If |curr_frame_info_| is valid, uncompressed header was parsed into
693   // |curr_frame_header_| and we are awaiting context update to proceed with
694   // compressed header parsing.
695   if (curr_frame_info_.IsValid()) {
696     DCHECK(parsing_compressed_header_);
697     frame_info = curr_frame_info_;
698     curr_frame_info_.Reset();
699   } else {
700     if (frames_.empty()) {
701       // No frames to be decoded, if there is no more stream, request more.
702       if (!stream_) {
703         return kEOStream;
704       }
705
706       // New stream to be parsed, parse it and fill frames_.
707       if (!spatial_layer_frame_size_.empty()) {
708         // If it is SVC stream, we have to parse the stream with
709         // |spatial_layer_frame_size_|.
710         frames_ = ParseSVCFrame();
711       } else {
712         frames_ = ParseSuperframe();
713       }
714
715       if (frames_.empty()) {
716         DVLOG(1) << "Failed parsing superframes/SVC frame";
717         return kInvalidStream;
718       }
719     }
720
721     frame_info = frames_.front();
722     frames_.pop_front();
723     if (frame_decrypt_config) {
724       if (frame_info.decrypt_config) {
725         *frame_decrypt_config = frame_info.decrypt_config->Clone();
726       } else {
727         *frame_decrypt_config = nullptr;
728       }
729     }
730
731     if (ParseUncompressedHeader(frame_info, fhdr, &result, &context_)) {
732       return result;
733     }
734   }
735
736   if (parsing_compressed_header_) {
737     if (ParseCompressedHeader(frame_info, &result)) {
738       DCHECK(result != kAwaitingRefresh || curr_frame_info_.IsValid());
739       return result;
740     }
741   }
742
743   if (!SetupSegmentationDequant()) {
744     return kInvalidStream;
745   }
746   SetupLoopFilter();
747   UpdateSlots(&context_);
748
749   *fhdr = curr_frame_header_;
750   // show_frame must be true for the last frame, otherwise false in SVC frame.
751   if (!spatial_layer_frame_size_.empty()) {
752     fhdr->show_frame = frames_.empty();
753   }
754
755   if (frame_info.allocate_size.IsEmpty()) {
756     allocate_size->SetSize(fhdr->frame_width, fhdr->frame_height);
757   } else {
758     *allocate_size = frame_info.allocate_size;
759   }
760
761   return kOk;
762 }
763
764 Vp9Parser::ContextRefreshCallback Vp9Parser::GetContextRefreshCb(
765     size_t frame_context_idx) {
766   DCHECK_LT(frame_context_idx, std::size(context_.frame_context_managers_));
767   auto& frame_context_manager =
768       context_.frame_context_managers_[frame_context_idx];
769
770   return frame_context_manager.GetUpdateCb();
771 }
772
773 std::unique_ptr<DecryptConfig> Vp9Parser::NextFrameDecryptContextForTesting() {
774   if (frames_.empty()) {
775     // No frames to be decoded, if there is no more stream, request more.
776     if (!stream_) {
777       return nullptr;
778     }
779
780     // New stream to be parsed, parse it and fill frames_.
781     frames_ = ParseSuperframe();
782     if (frames_.empty()) {
783       return nullptr;
784     }
785   }
786   FrameInfo frame_info = std::move(frames_.front());
787   frames_.pop_front();
788   return std::move(frame_info.decrypt_config);
789 }
790
791 std::string Vp9Parser::IncrementIVForTesting(const std::string& iv,
792                                              uint32_t by) {
793   return IncrementIV(iv, by);
794 }
795
796 // Annex B Superframes
797 base::circular_deque<Vp9Parser::FrameInfo> Vp9Parser::ParseSuperframe() {
798   const uint8_t* stream = stream_;
799   off_t bytes_left = bytes_left_;
800
801   // Make sure we don't parse stream_ more than once.
802   stream_ = nullptr;
803   bytes_left_ = 0;
804
805   base::circular_deque<FrameInfo> frames;
806
807   if (bytes_left < 1) {
808     return frames;
809   }
810
811   // The marker byte might be encrypted, in which case we should treat
812   // the stream as a single frame.
813   off_t marker_offset = bytes_left - 1;
814   if (stream_decrypt_config_) {
815     if (IsByteNEncrypted(marker_offset, stream_decrypt_config_->subsamples())) {
816       frames.push_back(FrameInfo(stream, bytes_left));
817       frames[0].decrypt_config = stream_decrypt_config_->Clone();
818       return frames;
819     }
820   }
821
822   // If this is a superframe, the last byte in the stream will contain the
823   // superframe marker. If not, the whole buffer contains a single frame.
824   uint8_t marker = *(stream + marker_offset);
825   if ((marker & 0xe0) != 0xc0) {
826     frames.push_back(FrameInfo(stream, bytes_left));
827     if (stream_decrypt_config_) {
828       frames[0].decrypt_config = stream_decrypt_config_->Clone();
829     }
830     return frames;
831   }
832
833   DVLOG(1) << "Parsing a superframe";
834
835   // The bytes immediately before the superframe marker constitute superframe
836   // index, which stores information about sizes of each frame in it.
837   // Calculate its size and set index_ptr to the beginning of it.
838   size_t num_frames = (marker & 0x7) + 1;
839   size_t mag = ((marker >> 3) & 0x3) + 1;
840   off_t index_size = 2 + mag * num_frames;
841
842   if (bytes_left < index_size) {
843     return base::circular_deque<FrameInfo>();
844   }
845
846   const uint8_t* index_ptr = stream + bytes_left - index_size;
847   if (marker != *index_ptr) {
848     return base::circular_deque<FrameInfo>();
849   }
850
851   ++index_ptr;
852   bytes_left -= index_size;
853
854   // Parse frame information contained in the index and add a pointer to and
855   // size of each frame to frames.
856
857   // Use this to calculate the per-frame IV value.
858   std::string iv;
859   std::vector<SubsampleEntry> subsamples;
860   size_t current_subsample = 0;
861   size_t extra_clear_subsample_bytes = 0;
862   if (stream_decrypt_config_) {
863     iv = stream_decrypt_config_->iv();
864     subsamples = stream_decrypt_config_->subsamples();
865   }
866
867   for (size_t i = 0; i < num_frames; ++i) {
868     uint32_t size = 0;
869     for (size_t j = 0; j < mag; ++j) {
870       size |= *index_ptr << (j * 8);
871       ++index_ptr;
872     }
873
874     if (!base::IsValueInRangeForNumericType<off_t>(size) ||
875         static_cast<off_t>(size) > bytes_left) {
876       DVLOG(1) << "Not enough data in the buffer for frame " << i;
877       frames.clear();
878       return frames;
879     }
880
881     FrameInfo frame = FrameInfo(stream, size);
882     if (subsamples.size()) {
883       std::unique_ptr<DecryptConfig> frame_dc = SplitSubsamples(
884           size, &current_subsample, &extra_clear_subsample_bytes,
885           stream_decrypt_config_.get(), subsamples, &iv);
886       if (!frame_dc) {
887         DVLOG(1) << "Failed to calculate decrypt config for frame " << i;
888         frames.clear();
889         return frames;
890       }
891
892       frame.decrypt_config = std::move(frame_dc);
893     }
894
895     frames.push_back(std::move(frame));
896     stream += size;
897     bytes_left -= size;
898
899     DVLOG(1) << "Frame " << i << ", size: " << size;
900   }
901
902   return frames;
903 }
904
905 base::circular_deque<Vp9Parser::FrameInfo> Vp9Parser::ParseSVCFrame() {
906   if (parsing_compressed_header_) {
907     LOG(ERROR) << "Vp9Parser doesn't support parsing SVC stream when "
908                << "a compressed header needs to be parsed";
909     return {};
910   }
911   if (stream_decrypt_config_) {
912     LOG(ERROR) << "Encrypted frame with SVC stream is not supported";
913     return {};
914   }
915
916   const uint8_t* stream = stream_;
917   off_t bytes_left = bytes_left_;
918
919   // Make sure we don't parse stream_ more than once.
920   stream_ = nullptr;
921   bytes_left_ = 0;
922
923   base::circular_deque<FrameInfo> frames;
924
925   for (size_t i = 0; i < spatial_layer_frame_size_.size(); i++) {
926     const uint32_t size = spatial_layer_frame_size_[i];
927     if (!base::IsValueInRangeForNumericType<off_t>(size) ||
928         static_cast<off_t>(size) > bytes_left) {
929       DVLOG(1) << "Not enough data in the buffer for frame " << i;
930       return {};
931     }
932
933     frames.emplace_back(stream, size);
934     stream += size;
935     bytes_left -= size;
936     DVLOG(1) << "Frame " << i << ", size: " << size;
937   }
938
939   DCHECK(!frames.empty());
940
941   gfx::Size max_frame_size;
942
943   // Context is not copyable because it has base::WeakPtrFactory. The weak
944   // pointer is necessary to update context for compressed header. To parse
945   // uncompressed header, |segmentation_|, |loop_filter| and |ref_slots_| are
946   // sufficient. Copy the variables manually here.
947   Context tmp_context;
948   tmp_context.segmentation_ = context_.segmentation_;
949   tmp_context.loop_filter_ = context_.loop_filter_;
950   memcpy(tmp_context.ref_slots_, context_.ref_slots_,
951          sizeof(context_.ref_slots_));
952   for (const auto& frame_info : frames) {
953     // |curr_frame_header_| is used safely because it is reset every
954     // ParseUncompressedHeader().
955     Vp9FrameHeader dummy_fhdr;
956     Result result;
957     if (ParseUncompressedHeader(frame_info, &dummy_fhdr, &result,
958                                 &tmp_context) &&
959         result != kOk) {
960       return {};
961     }
962     UpdateSlots(&tmp_context);
963     max_frame_size.SetToMax(gfx::Size(curr_frame_header_.frame_width,
964                                       curr_frame_header_.frame_height));
965   }
966
967   for (auto& frame_info : frames)
968     frame_info.allocate_size = max_frame_size;
969   return frames;
970 }
971
972 // 8.6.1 Dequantization functions
973 int64_t Vp9Parser::GetQIndex(const Vp9QuantizationParams& quant,
974                              size_t segid) const {
975   const Vp9SegmentationParams& segmentation = context_.segmentation();
976
977   if (segmentation.FeatureEnabled(segid,
978                                   Vp9SegmentationParams::SEG_LVL_ALT_Q)) {
979     int16_t feature_data =
980         segmentation.FeatureData(segid, Vp9SegmentationParams::SEG_LVL_ALT_Q);
981     int64_t q_index = segmentation.abs_or_delta_update
982                           ? feature_data
983                           : quant.base_q_idx + feature_data;
984     return ClampQ(q_index);
985   }
986   return quant.base_q_idx;
987 }
988
989 // 8.6.1 Dequantization functions
990 bool Vp9Parser::SetupSegmentationDequant() {
991   const Vp9QuantizationParams& quant = curr_frame_header_.quant_params;
992   Vp9SegmentationParams& segmentation = context_.segmentation_;
993
994   if (curr_frame_header_.bit_depth > 10) {
995     DLOG(ERROR) << "bit_depth > 10 is not supported yet, kDcQLookup and "
996                    "kAcQLookup need to be extended";
997     return false;
998   }
999   const size_t bit_depth_index = (curr_frame_header_.bit_depth == 8) ? 0 : 1;
1000
1001   if (segmentation.enabled) {
1002     for (size_t i = 0; i < Vp9SegmentationParams::kNumSegments; ++i) {
1003       const int64_t q_index = GetQIndex(quant, i);
1004       segmentation.y_dequant[i][0] =
1005           kDcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_y_dc)];
1006       segmentation.y_dequant[i][1] =
1007           kAcQLookup[bit_depth_index][ClampQ(q_index)];
1008       segmentation.uv_dequant[i][0] =
1009           kDcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_uv_dc)];
1010       segmentation.uv_dequant[i][1] =
1011           kAcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_uv_ac)];
1012     }
1013   } else {
1014     const int64_t q_index = quant.base_q_idx;
1015     segmentation.y_dequant[0][0] =
1016         kDcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_y_dc)];
1017     segmentation.y_dequant[0][1] = kAcQLookup[bit_depth_index][ClampQ(q_index)];
1018     segmentation.uv_dequant[0][0] =
1019         kDcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_uv_dc)];
1020     segmentation.uv_dequant[0][1] =
1021         kAcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_uv_ac)];
1022   }
1023   return true;
1024 }
1025
1026 // 8.8.1 Loop filter frame init process
1027 void Vp9Parser::SetupLoopFilter() {
1028   Vp9LoopFilterParams& loop_filter = context_.loop_filter_;
1029   if (!loop_filter.level) {
1030     return;
1031   }
1032
1033   int scale = loop_filter.level < 32 ? 1 : 2;
1034
1035   for (size_t i = 0; i < Vp9SegmentationParams::kNumSegments; ++i) {
1036     int level = loop_filter.level;
1037     const Vp9SegmentationParams& segmentation = context_.segmentation();
1038
1039     if (segmentation.FeatureEnabled(i, Vp9SegmentationParams::SEG_LVL_ALT_LF)) {
1040       int feature_data =
1041           segmentation.FeatureData(i, Vp9SegmentationParams::SEG_LVL_ALT_LF);
1042       level = ClampLf(segmentation.abs_or_delta_update ? feature_data
1043                                                        : level + feature_data);
1044     }
1045
1046     if (!loop_filter.delta_enabled) {
1047       memset(loop_filter.lvl[i], level, sizeof(loop_filter.lvl[i]));
1048     } else {
1049       loop_filter.lvl[i][Vp9RefType::VP9_FRAME_INTRA][0] = ClampLf(
1050           level + loop_filter.ref_deltas[Vp9RefType::VP9_FRAME_INTRA] * scale);
1051       loop_filter.lvl[i][Vp9RefType::VP9_FRAME_INTRA][1] = 0;
1052
1053       for (size_t type = Vp9RefType::VP9_FRAME_LAST;
1054            type < Vp9RefType::VP9_FRAME_MAX; ++type) {
1055         for (size_t mode = 0; mode < Vp9LoopFilterParams::kNumModeDeltas;
1056              ++mode) {
1057           loop_filter.lvl[i][type][mode] =
1058               ClampLf(level + loop_filter.ref_deltas[type] * scale +
1059                       loop_filter.mode_deltas[mode] * scale);
1060         }
1061       }
1062     }
1063   }
1064 }
1065
1066 void Vp9Parser::UpdateSlots(Vp9Parser::Context* context) {
1067   // 8.10 Reference frame update process
1068   for (size_t i = 0; i < kVp9NumRefFrames; i++) {
1069     if (curr_frame_header_.RefreshFlag(i)) {
1070       ReferenceSlot ref_slot;
1071       ref_slot.initialized = true;
1072
1073       ref_slot.frame_width = curr_frame_header_.frame_width;
1074       ref_slot.frame_height = curr_frame_header_.frame_height;
1075       ref_slot.subsampling_x = curr_frame_header_.subsampling_x;
1076       ref_slot.subsampling_y = curr_frame_header_.subsampling_y;
1077       ref_slot.bit_depth = curr_frame_header_.bit_depth;
1078
1079       ref_slot.profile = curr_frame_header_.profile;
1080       ref_slot.color_space = curr_frame_header_.color_space;
1081       context->UpdateRefSlot(i, ref_slot);
1082     }
1083   }
1084 }
1085
1086 }  // namespace media