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