Home | History | Annotate | Download | only in vda
      1 // Copyright 2015 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 //
      5 // 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 // Note: ported from Chromium commit head: 2de6929
     12 
     13 #include "vp9_parser.h"
     14 
     15 #include <algorithm>
     16 
     17 #include "base/bind.h"
     18 #include "base/logging.h"
     19 #include "base/macros.h"
     20 #include "base/numerics/safe_conversions.h"
     21 #include "vp9_compressed_header_parser.h"
     22 #include "vp9_uncompressed_header_parser.h"
     23 
     24 namespace media {
     25 
     26 namespace {
     27 
     28 // Coefficients extracted verbatim from "VP9 Bitstream & Decoding Process
     29 // Specification" Version 0.6, Sec 8.6.1 Dequantization functions, see:
     30 // https://www.webmproject.org/vp9/#draft-vp9-bitstream-and-decoding-process-specification
     31 constexpr size_t kQIndexRange = 256;
     32 // clang-format off
     33 // libva is the only user of high bit depth VP9 formats and only supports
     34 // 10 bits per component, see https://github.com/01org/libva/issues/137.
     35 // TODO(mcasas): Add the 12 bit versions of these tables.
     36 const int16_t kDcQLookup[][kQIndexRange] = {
     37     {
     38         4,    8,    8,    9,    10,   11,   12,   12,  13,   14,   15,   16,
     39         17,   18,   19,   19,   20,   21,   22,   23,  24,   25,   26,   26,
     40         27,   28,   29,   30,   31,   32,   32,   33,  34,   35,   36,   37,
     41         38,   38,   39,   40,   41,   42,   43,   43,  44,   45,   46,   47,
     42         48,   48,   49,   50,   51,   52,   53,   53,  54,   55,   56,   57,
     43         57,   58,   59,   60,   61,   62,   62,   63,  64,   65,   66,   66,
     44         67,   68,   69,   70,   70,   71,   72,   73,  74,   74,   75,   76,
     45         77,   78,   78,   79,   80,   81,   81,   82,  83,   84,   85,   85,
     46         87,   88,   90,   92,   93,   95,   96,   98,  99,   101,  102,  104,
     47         105,  107,  108,  110,  111,  113,  114,  116, 117,  118,  120,  121,
     48         123,  125,  127,  129,  131,  134,  136,  138, 140,  142,  144,  146,
     49         148,  150,  152,  154,  156,  158,  161,  164, 166,  169,  172,  174,
     50         177,  180,  182,  185,  187,  190,  192,  195, 199,  202,  205,  208,
     51         211,  214,  217,  220,  223,  226,  230,  233, 237,  240,  243,  247,
     52         250,  253,  257,  261,  265,  269,  272,  276, 280,  284,  288,  292,
     53         296,  300,  304,  309,  313,  317,  322,  326, 330,  335,  340,  344,
     54         349,  354,  359,  364,  369,  374,  379,  384, 389,  395,  400,  406,
     55         411,  417,  423,  429,  435,  441,  447,  454, 461,  467,  475,  482,
     56         489,  497,  505,  513,  522,  530,  539,  549, 559,  569,  579,  590,
     57         602,  614,  626,  640,  654,  668,  684,  700, 717,  736,  755,  775,
     58         796,  819,  843,  869,  896,  925,  955,  988, 1022, 1058, 1098, 1139,
     59         1184, 1232, 1282, 1336,
     60     },
     61     {
     62         4,    9,    10,   13,   15,   17,   20,   22,   25,   28,   31,   34,
     63         37,   40,   43,   47,   50,   53,   57,   60,   64,   68,   71,   75,
     64         78,   82,   86,   90,   93,   97,   101,  105,  109,  113,  116,  120,
     65         124,  128,  132,  136,  140,  143,  147,  151,  155,  159,  163,  166,
     66         170,  174,  178,  182,  185,  189,  193,  197,  200,  204,  208,  212,
     67         215,  219,  223,  226,  230,  233,  237,  241,  244,  248,  251,  255,
     68         259,  262,  266,  269,  273,  276,  280,  283,  287,  290,  293,  297,
     69         300,  304,  307,  310,  314,  317,  321,  324,  327,  331,  334,  337,
     70         343,  350,  356,  362,  369,  375,  381,  387,  394,  400,  406,  412,
     71         418,  424,  430,  436,  442,  448,  454,  460,  466,  472,  478,  484,
     72         490,  499,  507,  516,  525,  533,  542,  550,  559,  567,  576,  584,
     73         592,  601,  609,  617,  625,  634,  644,  655,  666,  676,  687,  698,
     74         708,  718,  729,  739,  749,  759,  770,  782,  795,  807,  819,  831,
     75         844,  856,  868,  880,  891,  906,  920,  933,  947,  961,  975,  988,
     76         1001, 1015, 1030, 1045, 1061, 1076, 1090, 1105, 1120, 1137, 1153, 1170,
     77         1186, 1202, 1218, 1236, 1253, 1271, 1288, 1306, 1323, 1342, 1361, 1379,
     78         1398, 1416, 1436, 1456, 1476, 1496, 1516, 1537, 1559, 1580, 1601, 1624,
     79         1647, 1670, 1692, 1717, 1741, 1766, 1791, 1817, 1844, 1871, 1900, 1929,
     80         1958, 1990, 2021, 2054, 2088, 2123, 2159, 2197, 2236, 2276, 2319, 2363,
     81         2410, 2458, 2508, 2561, 2616, 2675, 2737, 2802, 2871, 2944, 3020, 3102,
     82         3188, 3280, 3375, 3478, 3586, 3702, 3823, 3953, 4089, 4236, 4394, 4559,
     83         4737, 4929, 5130, 5347
     84    }
     85 };
     86 
     87 const int16_t kAcQLookup[][kQIndexRange] = {
     88     {
     89         4,    8,    9,    10,   11,   12,   13,   14,   15,   16,   17,   18,
     90         19,   20,   21,   22,   23,   24,   25,   26,   27,   28,   29,   30,
     91         31,   32,   33,   34,   35,   36,   37,   38,   39,   40,   41,   42,
     92         43,   44,   45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
     93         55,   56,   57,   58,   59,   60,   61,   62,   63,   64,   65,   66,
     94         67,   68,   69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
     95         79,   80,   81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
     96         91,   92,   93,   94,   95,   96,   97,   98,   99,   100,  101,  102,
     97         104,  106,  108,  110,  112,  114,  116,  118,  120,  122,  124,  126,
     98         128,  130,  132,  134,  136,  138,  140,  142,  144,  146,  148,  150,
     99         152,  155,  158,  161,  164,  167,  170,  173,  176,  179,  182,  185,
    100         188,  191,  194,  197,  200,  203,  207,  211,  215,  219,  223,  227,
    101         231,  235,  239,  243,  247,  251,  255,  260,  265,  270,  275,  280,
    102         285,  290,  295,  300,  305,  311,  317,  323,  329,  335,  341,  347,
    103         353,  359,  366,  373,  380,  387,  394,  401,  408,  416,  424,  432,
    104         440,  448,  456,  465,  474,  483,  492,  501,  510,  520,  530,  540,
    105         550,  560,  571,  582,  593,  604,  615,  627,  639,  651,  663,  676,
    106         689,  702,  715,  729,  743,  757,  771,  786,  801,  816,  832,  848,
    107         864,  881,  898,  915,  933,  951,  969,  988,  1007, 1026, 1046, 1066,
    108         1087, 1108, 1129, 1151, 1173, 1196, 1219, 1243, 1267, 1292, 1317, 1343,
    109         1369, 1396, 1423, 1451, 1479, 1508, 1537, 1567, 1597, 1628, 1660, 1692,
    110         1725, 1759, 1793, 1828,
    111     },
    112     {
    113         4,    9,    11,   13,   16,   18,   21,   24,   27,   30,   33,   37,
    114         40,   44,   48,   51,   55,   59,   63,   67,   71,   75,   79,   83,
    115         88,   92,   96,   100,  105,  109,  114,  118,  122,  127,  131,  136,
    116         140,  145,  149,  154,  158,  163,  168,  172,  177,  181,  186,  190,
    117         195,  199,  204,  208,  213,  217,  222,  226,  231,  235,  240,  244,
    118         249,  253,  258,  262,  267,  271,  275,  280,  284,  289,  293,  297,
    119         302,  306,  311,  315,  319,  324,  328,  332,  337,  341,  345,  349,
    120         354,  358,  362,  367,  371,  375,  379,  384,  388,  392,  396,  401,
    121         409,  417,  425,  433,  441,  449,  458,  466,  474,  482,  490,  498,
    122         506,  514,  523,  531,  539,  547,  555,  563,  571,  579,  588,  596,
    123         604,  616,  628,  640,  652,  664,  676,  688,  700,  713,  725,  737,
    124         749,  761,  773,  785,  797,  809,  825,  841,  857,  873,  889,  905,
    125         922,  938,  954,  970,  986,  1002, 1018, 1038, 1058, 1078, 1098, 1118,
    126         1138, 1158, 1178, 1198, 1218, 1242, 1266, 1290, 1314, 1338, 1362, 1386,
    127         1411, 1435, 1463, 1491, 1519, 1547, 1575, 1603, 1631, 1663, 1695, 1727,
    128         1759, 1791, 1823, 1859, 1895, 1931, 1967, 2003, 2039, 2079, 2119, 2159,
    129         2199, 2239, 2283, 2327, 2371, 2415, 2459, 2507, 2555, 2603, 2651, 2703,
    130         2755, 2807, 2859, 2915, 2971, 3027, 3083, 3143, 3203, 3263, 3327, 3391,
    131         3455, 3523, 3591, 3659, 3731, 3803, 3876, 3952, 4028, 4104, 4184, 4264,
    132         4348, 4432, 4516, 4604, 4692, 4784, 4876, 4972, 5068, 5168, 5268, 5372,
    133         5476, 5584, 5692, 5804, 5916, 6032, 6148, 6268, 6388, 6512, 6640, 6768,
    134         6900, 7036, 7172, 7312
    135    }
    136 };
    137 // clang-format on
    138 
    139 static_assert(arraysize(kDcQLookup[0]) == arraysize(kAcQLookup[0]),
    140               "quantizer lookup arrays of incorrect size");
    141 
    142 size_t ClampQ(size_t q) {
    143   return std::min(q, kQIndexRange - 1);
    144 }
    145 
    146 int ClampLf(int lf) {
    147   const int kMaxLoopFilterLevel = 63;
    148   return std::min(std::max(0, lf), kMaxLoopFilterLevel);
    149 }
    150 
    151 }  // namespace
    152 
    153 bool Vp9FrameHeader::IsKeyframe() const {
    154   // When show_existing_frame is true, the frame header does not precede an
    155   // actual frame to be decoded, so frame_type does not apply (and is not read
    156   // from the stream).
    157   return !show_existing_frame && frame_type == KEYFRAME;
    158 }
    159 
    160 bool Vp9FrameHeader::IsIntra() const {
    161   return !show_existing_frame && (frame_type == KEYFRAME || intra_only);
    162 }
    163 
    164 Vp9Parser::FrameInfo::FrameInfo(const uint8_t* ptr, off_t size)
    165     : ptr(ptr), size(size) {}
    166 
    167 bool Vp9FrameContext::IsValid() const {
    168   // probs should be in [1, 255] range.
    169   static_assert(sizeof(Vp9Prob) == 1,
    170                 "following checks assuming Vp9Prob is single byte");
    171   if (memchr(tx_probs_8x8, 0, sizeof(tx_probs_8x8)))
    172     return false;
    173   if (memchr(tx_probs_16x16, 0, sizeof(tx_probs_16x16)))
    174     return false;
    175   if (memchr(tx_probs_32x32, 0, sizeof(tx_probs_32x32)))
    176     return false;
    177 
    178   for (auto& a : coef_probs) {
    179     for (auto& ai : a) {
    180       for (auto& aj : ai) {
    181         for (auto& ak : aj) {
    182           int max_l = (ak == aj[0]) ? 3 : 6;
    183           for (int l = 0; l < max_l; l++) {
    184             for (auto& x : ak[l]) {
    185               if (x == 0)
    186                 return false;
    187             }
    188           }
    189         }
    190       }
    191     }
    192   }
    193   if (memchr(skip_prob, 0, sizeof(skip_prob)))
    194     return false;
    195   if (memchr(inter_mode_probs, 0, sizeof(inter_mode_probs)))
    196     return false;
    197   if (memchr(interp_filter_probs, 0, sizeof(interp_filter_probs)))
    198     return false;
    199   if (memchr(is_inter_prob, 0, sizeof(is_inter_prob)))
    200     return false;
    201   if (memchr(comp_mode_prob, 0, sizeof(comp_mode_prob)))
    202     return false;
    203   if (memchr(single_ref_prob, 0, sizeof(single_ref_prob)))
    204     return false;
    205   if (memchr(comp_ref_prob, 0, sizeof(comp_ref_prob)))
    206     return false;
    207   if (memchr(y_mode_probs, 0, sizeof(y_mode_probs)))
    208     return false;
    209   if (memchr(uv_mode_probs, 0, sizeof(uv_mode_probs)))
    210     return false;
    211   if (memchr(partition_probs, 0, sizeof(partition_probs)))
    212     return false;
    213   if (memchr(mv_joint_probs, 0, sizeof(mv_joint_probs)))
    214     return false;
    215   if (memchr(mv_sign_prob, 0, sizeof(mv_sign_prob)))
    216     return false;
    217   if (memchr(mv_class_probs, 0, sizeof(mv_class_probs)))
    218     return false;
    219   if (memchr(mv_class0_bit_prob, 0, sizeof(mv_class0_bit_prob)))
    220     return false;
    221   if (memchr(mv_bits_prob, 0, sizeof(mv_bits_prob)))
    222     return false;
    223   if (memchr(mv_class0_fr_probs, 0, sizeof(mv_class0_fr_probs)))
    224     return false;
    225   if (memchr(mv_fr_probs, 0, sizeof(mv_fr_probs)))
    226     return false;
    227   if (memchr(mv_class0_hp_prob, 0, sizeof(mv_class0_hp_prob)))
    228     return false;
    229   if (memchr(mv_hp_prob, 0, sizeof(mv_hp_prob)))
    230     return false;
    231 
    232   return true;
    233 }
    234 
    235 Vp9Parser::Context::Vp9FrameContextManager::Vp9FrameContextManager()
    236     : weak_ptr_factory_(this) {}
    237 
    238 Vp9Parser::Context::Vp9FrameContextManager::~Vp9FrameContextManager() = default;
    239 
    240 const Vp9FrameContext&
    241 Vp9Parser::Context::Vp9FrameContextManager::frame_context() const {
    242   DCHECK(initialized_);
    243   DCHECK(!needs_client_update_);
    244   return frame_context_;
    245 }
    246 
    247 void Vp9Parser::Context::Vp9FrameContextManager::Reset() {
    248   initialized_ = false;
    249   needs_client_update_ = false;
    250   weak_ptr_factory_.InvalidateWeakPtrs();
    251 }
    252 
    253 void Vp9Parser::Context::Vp9FrameContextManager::SetNeedsClientUpdate() {
    254   DCHECK(!needs_client_update_);
    255   initialized_ = true;
    256   needs_client_update_ = true;
    257 }
    258 
    259 Vp9Parser::ContextRefreshCallback
    260 Vp9Parser::Context::Vp9FrameContextManager::GetUpdateCb() {
    261   if (needs_client_update_)
    262     return base::Bind(&Vp9FrameContextManager::UpdateFromClient,
    263                       weak_ptr_factory_.GetWeakPtr());
    264   else
    265     return Vp9Parser::ContextRefreshCallback();
    266 }
    267 
    268 void Vp9Parser::Context::Vp9FrameContextManager::Update(
    269     const Vp9FrameContext& frame_context) {
    270   // DCHECK because we can trust values from our parser.
    271   DCHECK(frame_context.IsValid());
    272   initialized_ = true;
    273   frame_context_ = frame_context;
    274 
    275   // For frame context we are updating, it may be still awaiting previous
    276   // ContextRefreshCallback. Because we overwrite the value of context here and
    277   // previous ContextRefreshCallback no longer matters, invalidate the weak ptr
    278   // to prevent previous ContextRefreshCallback run.
    279   // With this optimization, we may be able to parse more frames while previous
    280   // are still decoding.
    281   weak_ptr_factory_.InvalidateWeakPtrs();
    282   needs_client_update_ = false;
    283 }
    284 
    285 void Vp9Parser::Context::Vp9FrameContextManager::UpdateFromClient(
    286     const Vp9FrameContext& frame_context) {
    287   DVLOG(2) << "Got external frame_context update";
    288   DCHECK(needs_client_update_);
    289   if (!frame_context.IsValid()) {
    290     DLOG(ERROR) << "Invalid prob value in frame_context";
    291     return;
    292   }
    293   needs_client_update_ = false;
    294   initialized_ = true;
    295   frame_context_ = frame_context;
    296 }
    297 
    298 void Vp9Parser::Context::Reset() {
    299   memset(&segmentation_, 0, sizeof(segmentation_));
    300   memset(&loop_filter_, 0, sizeof(loop_filter_));
    301   memset(&ref_slots_, 0, sizeof(ref_slots_));
    302   for (auto& manager : frame_context_managers_)
    303     manager.Reset();
    304 }
    305 
    306 void Vp9Parser::Context::MarkFrameContextForUpdate(size_t frame_context_idx) {
    307   DCHECK_LT(frame_context_idx, arraysize(frame_context_managers_));
    308   frame_context_managers_[frame_context_idx].SetNeedsClientUpdate();
    309 }
    310 
    311 void Vp9Parser::Context::UpdateFrameContext(
    312     size_t frame_context_idx,
    313     const Vp9FrameContext& frame_context) {
    314   DCHECK_LT(frame_context_idx, arraysize(frame_context_managers_));
    315   frame_context_managers_[frame_context_idx].Update(frame_context);
    316 }
    317 
    318 const Vp9Parser::ReferenceSlot& Vp9Parser::Context::GetRefSlot(
    319     size_t ref_type) const {
    320   DCHECK_LT(ref_type, arraysize(ref_slots_));
    321   return ref_slots_[ref_type];
    322 }
    323 
    324 void Vp9Parser::Context::UpdateRefSlot(
    325     size_t ref_type,
    326     const Vp9Parser::ReferenceSlot& ref_slot) {
    327   DCHECK_LT(ref_type, arraysize(ref_slots_));
    328   ref_slots_[ref_type] = ref_slot;
    329 }
    330 
    331 Vp9Parser::Vp9Parser(bool parsing_compressed_header)
    332     : parsing_compressed_header_(parsing_compressed_header) {
    333   Reset();
    334 }
    335 
    336 Vp9Parser::~Vp9Parser() = default;
    337 
    338 void Vp9Parser::SetStream(const uint8_t* stream, off_t stream_size) {
    339   DCHECK(stream);
    340   stream_ = stream;
    341   bytes_left_ = stream_size;
    342   frames_.clear();
    343 }
    344 
    345 void Vp9Parser::Reset() {
    346   stream_ = nullptr;
    347   bytes_left_ = 0;
    348   frames_.clear();
    349   curr_frame_info_.Reset();
    350 
    351   context_.Reset();
    352 }
    353 
    354 bool Vp9Parser::ParseUncompressedHeader(const FrameInfo& frame_info,
    355                                         Vp9FrameHeader* fhdr,
    356                                         Result* result) {
    357   memset(&curr_frame_header_, 0, sizeof(curr_frame_header_));
    358   *result = kInvalidStream;
    359 
    360   Vp9UncompressedHeaderParser uncompressed_parser(&context_);
    361   if (!uncompressed_parser.Parse(frame_info.ptr, frame_info.size,
    362                                  &curr_frame_header_)) {
    363     *result = kInvalidStream;
    364     return true;
    365   }
    366 
    367   if (curr_frame_header_.header_size_in_bytes == 0) {
    368     // Verify padding bits are zero.
    369     for (off_t i = curr_frame_header_.uncompressed_header_size;
    370          i < frame_info.size; i++) {
    371       if (frame_info.ptr[i] != 0) {
    372         DVLOG(1) << "Padding bits are not zeros.";
    373         *result = kInvalidStream;
    374         return true;
    375       }
    376     }
    377     *fhdr = curr_frame_header_;
    378     *result = kOk;
    379     return true;
    380   }
    381   if (curr_frame_header_.uncompressed_header_size +
    382           curr_frame_header_.header_size_in_bytes >
    383       base::checked_cast<size_t>(frame_info.size)) {
    384     DVLOG(1) << "header_size_in_bytes="
    385              << curr_frame_header_.header_size_in_bytes
    386              << " is larger than bytes left in buffer: "
    387              << frame_info.size - curr_frame_header_.uncompressed_header_size;
    388     *result = kInvalidStream;
    389     return true;
    390   }
    391 
    392   return false;
    393 }
    394 
    395 bool Vp9Parser::ParseCompressedHeader(const FrameInfo& frame_info,
    396                                       Result* result) {
    397   *result = kInvalidStream;
    398   size_t frame_context_idx = curr_frame_header_.frame_context_idx;
    399   const Context::Vp9FrameContextManager& context_to_load =
    400       context_.frame_context_managers_[frame_context_idx];
    401   if (!context_to_load.initialized()) {
    402     // 8.2 Frame order constraints
    403     // must load an initialized set of probabilities.
    404     DVLOG(1) << "loading uninitialized frame context, index="
    405              << frame_context_idx;
    406     *result = kInvalidStream;
    407     return true;
    408   }
    409   if (context_to_load.needs_client_update()) {
    410     DVLOG(3) << "waiting frame_context_idx=" << frame_context_idx
    411              << " to update";
    412     curr_frame_info_ = frame_info;
    413     *result = kAwaitingRefresh;
    414     return true;
    415   }
    416   curr_frame_header_.initial_frame_context = curr_frame_header_.frame_context =
    417       context_to_load.frame_context();
    418 
    419   Vp9CompressedHeaderParser compressed_parser;
    420   if (!compressed_parser.Parse(
    421           frame_info.ptr + curr_frame_header_.uncompressed_header_size,
    422           curr_frame_header_.header_size_in_bytes, &curr_frame_header_)) {
    423     *result = kInvalidStream;
    424     return true;
    425   }
    426 
    427   if (curr_frame_header_.refresh_frame_context) {
    428     // In frame parallel mode, we can refresh the context without decoding
    429     // tile data.
    430     if (curr_frame_header_.frame_parallel_decoding_mode) {
    431       context_.UpdateFrameContext(frame_context_idx,
    432                                   curr_frame_header_.frame_context);
    433     } else {
    434       context_.MarkFrameContextForUpdate(frame_context_idx);
    435     }
    436   }
    437   return false;
    438 }
    439 
    440 Vp9Parser::Result Vp9Parser::ParseNextFrame(Vp9FrameHeader* fhdr) {
    441   DCHECK(fhdr);
    442   DVLOG(2) << "ParseNextFrame";
    443   FrameInfo frame_info;
    444   Result result;
    445 
    446   // If |curr_frame_info_| is valid, uncompressed header was parsed into
    447   // |curr_frame_header_| and we are awaiting context update to proceed with
    448   // compressed header parsing.
    449   if (curr_frame_info_.IsValid()) {
    450     DCHECK(parsing_compressed_header_);
    451     frame_info = curr_frame_info_;
    452     curr_frame_info_.Reset();
    453   } else {
    454     if (frames_.empty()) {
    455       // No frames to be decoded, if there is no more stream, request more.
    456       if (!stream_)
    457         return kEOStream;
    458 
    459       // New stream to be parsed, parse it and fill frames_.
    460       frames_ = ParseSuperframe();
    461       if (frames_.empty()) {
    462         DVLOG(1) << "Failed parsing superframes";
    463         return kInvalidStream;
    464       }
    465     }
    466 
    467     frame_info = frames_.front();
    468     frames_.pop_front();
    469 
    470     if (ParseUncompressedHeader(frame_info, fhdr, &result))
    471       return result;
    472   }
    473 
    474   if (parsing_compressed_header_) {
    475     if (ParseCompressedHeader(frame_info, &result)) {
    476       DCHECK(result != kAwaitingRefresh || curr_frame_info_.IsValid());
    477       return result;
    478     }
    479   }
    480 
    481   if (!SetupSegmentationDequant())
    482     return kInvalidStream;
    483   SetupLoopFilter();
    484   UpdateSlots();
    485 
    486   *fhdr = curr_frame_header_;
    487   return kOk;
    488 }
    489 
    490 Vp9Parser::ContextRefreshCallback Vp9Parser::GetContextRefreshCb(
    491     size_t frame_context_idx) {
    492   DCHECK_LT(frame_context_idx, arraysize(context_.frame_context_managers_));
    493   auto& frame_context_manager =
    494       context_.frame_context_managers_[frame_context_idx];
    495 
    496   return frame_context_manager.GetUpdateCb();
    497 }
    498 
    499 // Annex B Superframes
    500 std::deque<Vp9Parser::FrameInfo> Vp9Parser::ParseSuperframe() {
    501   const uint8_t* stream = stream_;
    502   off_t bytes_left = bytes_left_;
    503 
    504   // Make sure we don't parse stream_ more than once.
    505   stream_ = nullptr;
    506   bytes_left_ = 0;
    507 
    508   if (bytes_left < 1)
    509     return std::deque<FrameInfo>();
    510 
    511   // If this is a superframe, the last byte in the stream will contain the
    512   // superframe marker. If not, the whole buffer contains a single frame.
    513   uint8_t marker = *(stream + bytes_left - 1);
    514   if ((marker & 0xe0) != 0xc0) {
    515     return {FrameInfo(stream, bytes_left)};
    516   }
    517 
    518   DVLOG(1) << "Parsing a superframe";
    519 
    520   // The bytes immediately before the superframe marker constitute superframe
    521   // index, which stores information about sizes of each frame in it.
    522   // Calculate its size and set index_ptr to the beginning of it.
    523   size_t num_frames = (marker & 0x7) + 1;
    524   size_t mag = ((marker >> 3) & 0x3) + 1;
    525   off_t index_size = 2 + mag * num_frames;
    526 
    527   if (bytes_left < index_size)
    528     return std::deque<FrameInfo>();
    529 
    530   const uint8_t* index_ptr = stream + bytes_left - index_size;
    531   if (marker != *index_ptr)
    532     return std::deque<FrameInfo>();
    533 
    534   ++index_ptr;
    535   bytes_left -= index_size;
    536 
    537   // Parse frame information contained in the index and add a pointer to and
    538   // size of each frame to frames.
    539   std::deque<FrameInfo> frames;
    540   for (size_t i = 0; i < num_frames; ++i) {
    541     uint32_t size = 0;
    542     for (size_t j = 0; j < mag; ++j) {
    543       size |= *index_ptr << (j * 8);
    544       ++index_ptr;
    545     }
    546 
    547     if (base::checked_cast<off_t>(size) > bytes_left) {
    548       DVLOG(1) << "Not enough data in the buffer for frame " << i;
    549       return std::deque<FrameInfo>();
    550     }
    551 
    552     frames.push_back(FrameInfo(stream, size));
    553     stream += size;
    554     bytes_left -= size;
    555 
    556     DVLOG(1) << "Frame " << i << ", size: " << size;
    557   }
    558 
    559   return frames;
    560 }
    561 
    562 // 8.6.1 Dequantization functions
    563 size_t Vp9Parser::GetQIndex(const Vp9QuantizationParams& quant,
    564                             size_t segid) const {
    565   const Vp9SegmentationParams& segmentation = context_.segmentation();
    566 
    567   if (segmentation.FeatureEnabled(segid,
    568                                   Vp9SegmentationParams::SEG_LVL_ALT_Q)) {
    569     int16_t feature_data =
    570         segmentation.FeatureData(segid, Vp9SegmentationParams::SEG_LVL_ALT_Q);
    571     size_t q_index = segmentation.abs_or_delta_update
    572                          ? feature_data
    573                          : quant.base_q_idx + feature_data;
    574     return ClampQ(q_index);
    575   }
    576 
    577   return quant.base_q_idx;
    578 }
    579 
    580 // 8.6.1 Dequantization functions
    581 bool Vp9Parser::SetupSegmentationDequant() {
    582   const Vp9QuantizationParams& quant = curr_frame_header_.quant_params;
    583   Vp9SegmentationParams& segmentation = context_.segmentation_;
    584 
    585   if (curr_frame_header_.bit_depth > 10) {
    586     DLOG(ERROR) << "bit_depth > 10 is not supported yet, kDcQLookup and "
    587                    "kAcQLookup need to be extended";
    588     return false;
    589   }
    590   const size_t bit_depth_index = (curr_frame_header_.bit_depth == 8) ? 0 : 1;
    591 
    592   if (segmentation.enabled) {
    593     for (size_t i = 0; i < Vp9SegmentationParams::kNumSegments; ++i) {
    594       const size_t q_index = GetQIndex(quant, i);
    595       segmentation.y_dequant[i][0] =
    596           kDcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_y_dc)];
    597       segmentation.y_dequant[i][1] =
    598           kAcQLookup[bit_depth_index][ClampQ(q_index)];
    599       segmentation.uv_dequant[i][0] =
    600           kDcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_uv_dc)];
    601       segmentation.uv_dequant[i][1] =
    602           kAcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_uv_ac)];
    603     }
    604   } else {
    605     const size_t q_index = quant.base_q_idx;
    606     segmentation.y_dequant[0][0] =
    607         kDcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_y_dc)];
    608     segmentation.y_dequant[0][1] = kAcQLookup[bit_depth_index][ClampQ(q_index)];
    609     segmentation.uv_dequant[0][0] =
    610         kDcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_uv_dc)];
    611     segmentation.uv_dequant[0][1] =
    612         kAcQLookup[bit_depth_index][ClampQ(q_index + quant.delta_q_uv_ac)];
    613   }
    614   return true;
    615 }
    616 
    617 // 8.8.1 Loop filter frame init process
    618 void Vp9Parser::SetupLoopFilter() {
    619   Vp9LoopFilterParams& loop_filter = context_.loop_filter_;
    620   if (!loop_filter.level)
    621     return;
    622 
    623   int scale = loop_filter.level < 32 ? 1 : 2;
    624 
    625   for (size_t i = 0; i < Vp9SegmentationParams::kNumSegments; ++i) {
    626     int level = loop_filter.level;
    627     const Vp9SegmentationParams& segmentation = context_.segmentation();
    628 
    629     if (segmentation.FeatureEnabled(i, Vp9SegmentationParams::SEG_LVL_ALT_LF)) {
    630       int feature_data =
    631           segmentation.FeatureData(i, Vp9SegmentationParams::SEG_LVL_ALT_LF);
    632       level = ClampLf(segmentation.abs_or_delta_update ? feature_data
    633                                                        : level + feature_data);
    634     }
    635 
    636     if (!loop_filter.delta_enabled) {
    637       memset(loop_filter.lvl[i], level, sizeof(loop_filter.lvl[i]));
    638     } else {
    639       loop_filter.lvl[i][Vp9RefType::VP9_FRAME_INTRA][0] = ClampLf(
    640           level + loop_filter.ref_deltas[Vp9RefType::VP9_FRAME_INTRA] * scale);
    641       loop_filter.lvl[i][Vp9RefType::VP9_FRAME_INTRA][1] = 0;
    642 
    643       for (size_t type = Vp9RefType::VP9_FRAME_LAST;
    644            type < Vp9RefType::VP9_FRAME_MAX; ++type) {
    645         for (size_t mode = 0; mode < Vp9LoopFilterParams::kNumModeDeltas;
    646              ++mode) {
    647           loop_filter.lvl[i][type][mode] =
    648               ClampLf(level + loop_filter.ref_deltas[type] * scale +
    649                       loop_filter.mode_deltas[mode] * scale);
    650         }
    651       }
    652     }
    653   }
    654 }
    655 
    656 void Vp9Parser::UpdateSlots() {
    657   // 8.10 Reference frame update process
    658   for (size_t i = 0; i < kVp9NumRefFrames; i++) {
    659     if (curr_frame_header_.RefreshFlag(i)) {
    660       ReferenceSlot ref_slot;
    661       ref_slot.initialized = true;
    662 
    663       ref_slot.frame_width = curr_frame_header_.frame_width;
    664       ref_slot.frame_height = curr_frame_header_.frame_height;
    665       ref_slot.subsampling_x = curr_frame_header_.subsampling_x;
    666       ref_slot.subsampling_y = curr_frame_header_.subsampling_y;
    667       ref_slot.bit_depth = curr_frame_header_.bit_depth;
    668 
    669       ref_slot.profile = curr_frame_header_.profile;
    670       ref_slot.color_space = curr_frame_header_.color_space;
    671       context_.UpdateRefSlot(i, ref_slot);
    672     }
    673   }
    674 }
    675 
    676 }  // namespace media
    677