Home | History | Annotate | Download | only in dec
      1 // Copyright 2010 Google Inc. All Rights Reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style license
      4 // that can be found in the COPYING file in the root of the source
      5 // tree. An additional intellectual property rights grant can be found
      6 // in the file PATENTS. All contributing project authors may
      7 // be found in the AUTHORS file in the root of the source tree.
      8 // -----------------------------------------------------------------------------
      9 //
     10 // Frame-reconstruction function. Memory allocation.
     11 //
     12 // Author: Skal (pascal.massimino (at) gmail.com)
     13 
     14 #include <stdlib.h>
     15 #include "src/dec/vp8i_dec.h"
     16 #include "src/utils/utils.h"
     17 
     18 //------------------------------------------------------------------------------
     19 // Main reconstruction function.
     20 
     21 static const uint16_t kScan[16] = {
     22   0 +  0 * BPS,  4 +  0 * BPS, 8 +  0 * BPS, 12 +  0 * BPS,
     23   0 +  4 * BPS,  4 +  4 * BPS, 8 +  4 * BPS, 12 +  4 * BPS,
     24   0 +  8 * BPS,  4 +  8 * BPS, 8 +  8 * BPS, 12 +  8 * BPS,
     25   0 + 12 * BPS,  4 + 12 * BPS, 8 + 12 * BPS, 12 + 12 * BPS
     26 };
     27 
     28 static int CheckMode(int mb_x, int mb_y, int mode) {
     29   if (mode == B_DC_PRED) {
     30     if (mb_x == 0) {
     31       return (mb_y == 0) ? B_DC_PRED_NOTOPLEFT : B_DC_PRED_NOLEFT;
     32     } else {
     33       return (mb_y == 0) ? B_DC_PRED_NOTOP : B_DC_PRED;
     34     }
     35   }
     36   return mode;
     37 }
     38 
     39 static void Copy32b(uint8_t* const dst, const uint8_t* const src) {
     40   memcpy(dst, src, 4);
     41 }
     42 
     43 static WEBP_INLINE void DoTransform(uint32_t bits, const int16_t* const src,
     44                                     uint8_t* const dst) {
     45   switch (bits >> 30) {
     46     case 3:
     47       VP8Transform(src, dst, 0);
     48       break;
     49     case 2:
     50       VP8TransformAC3(src, dst);
     51       break;
     52     case 1:
     53       VP8TransformDC(src, dst);
     54       break;
     55     default:
     56       break;
     57   }
     58 }
     59 
     60 static void DoUVTransform(uint32_t bits, const int16_t* const src,
     61                           uint8_t* const dst) {
     62   if (bits & 0xff) {    // any non-zero coeff at all?
     63     if (bits & 0xaa) {  // any non-zero AC coefficient?
     64       VP8TransformUV(src, dst);   // note we don't use the AC3 variant for U/V
     65     } else {
     66       VP8TransformDCUV(src, dst);
     67     }
     68   }
     69 }
     70 
     71 static void ReconstructRow(const VP8Decoder* const dec,
     72                            const VP8ThreadContext* ctx) {
     73   int j;
     74   int mb_x;
     75   const int mb_y = ctx->mb_y_;
     76   const int cache_id = ctx->id_;
     77   uint8_t* const y_dst = dec->yuv_b_ + Y_OFF;
     78   uint8_t* const u_dst = dec->yuv_b_ + U_OFF;
     79   uint8_t* const v_dst = dec->yuv_b_ + V_OFF;
     80 
     81   // Initialize left-most block.
     82   for (j = 0; j < 16; ++j) {
     83     y_dst[j * BPS - 1] = 129;
     84   }
     85   for (j = 0; j < 8; ++j) {
     86     u_dst[j * BPS - 1] = 129;
     87     v_dst[j * BPS - 1] = 129;
     88   }
     89 
     90   // Init top-left sample on left column too.
     91   if (mb_y > 0) {
     92     y_dst[-1 - BPS] = u_dst[-1 - BPS] = v_dst[-1 - BPS] = 129;
     93   } else {
     94     // we only need to do this init once at block (0,0).
     95     // Afterward, it remains valid for the whole topmost row.
     96     memset(y_dst - BPS - 1, 127, 16 + 4 + 1);
     97     memset(u_dst - BPS - 1, 127, 8 + 1);
     98     memset(v_dst - BPS - 1, 127, 8 + 1);
     99   }
    100 
    101   // Reconstruct one row.
    102   for (mb_x = 0; mb_x < dec->mb_w_; ++mb_x) {
    103     const VP8MBData* const block = ctx->mb_data_ + mb_x;
    104 
    105     // Rotate in the left samples from previously decoded block. We move four
    106     // pixels at a time for alignment reason, and because of in-loop filter.
    107     if (mb_x > 0) {
    108       for (j = -1; j < 16; ++j) {
    109         Copy32b(&y_dst[j * BPS - 4], &y_dst[j * BPS + 12]);
    110       }
    111       for (j = -1; j < 8; ++j) {
    112         Copy32b(&u_dst[j * BPS - 4], &u_dst[j * BPS + 4]);
    113         Copy32b(&v_dst[j * BPS - 4], &v_dst[j * BPS + 4]);
    114       }
    115     }
    116     {
    117       // bring top samples into the cache
    118       VP8TopSamples* const top_yuv = dec->yuv_t_ + mb_x;
    119       const int16_t* const coeffs = block->coeffs_;
    120       uint32_t bits = block->non_zero_y_;
    121       int n;
    122 
    123       if (mb_y > 0) {
    124         memcpy(y_dst - BPS, top_yuv[0].y, 16);
    125         memcpy(u_dst - BPS, top_yuv[0].u, 8);
    126         memcpy(v_dst - BPS, top_yuv[0].v, 8);
    127       }
    128 
    129       // predict and add residuals
    130       if (block->is_i4x4_) {   // 4x4
    131         uint32_t* const top_right = (uint32_t*)(y_dst - BPS + 16);
    132 
    133         if (mb_y > 0) {
    134           if (mb_x >= dec->mb_w_ - 1) {    // on rightmost border
    135             memset(top_right, top_yuv[0].y[15], sizeof(*top_right));
    136           } else {
    137             memcpy(top_right, top_yuv[1].y, sizeof(*top_right));
    138           }
    139         }
    140         // replicate the top-right pixels below
    141         top_right[BPS] = top_right[2 * BPS] = top_right[3 * BPS] = top_right[0];
    142 
    143         // predict and add residuals for all 4x4 blocks in turn.
    144         for (n = 0; n < 16; ++n, bits <<= 2) {
    145           uint8_t* const dst = y_dst + kScan[n];
    146           VP8PredLuma4[block->imodes_[n]](dst);
    147           DoTransform(bits, coeffs + n * 16, dst);
    148         }
    149       } else {    // 16x16
    150         const int pred_func = CheckMode(mb_x, mb_y, block->imodes_[0]);
    151         VP8PredLuma16[pred_func](y_dst);
    152         if (bits != 0) {
    153           for (n = 0; n < 16; ++n, bits <<= 2) {
    154             DoTransform(bits, coeffs + n * 16, y_dst + kScan[n]);
    155           }
    156         }
    157       }
    158       {
    159         // Chroma
    160         const uint32_t bits_uv = block->non_zero_uv_;
    161         const int pred_func = CheckMode(mb_x, mb_y, block->uvmode_);
    162         VP8PredChroma8[pred_func](u_dst);
    163         VP8PredChroma8[pred_func](v_dst);
    164         DoUVTransform(bits_uv >> 0, coeffs + 16 * 16, u_dst);
    165         DoUVTransform(bits_uv >> 8, coeffs + 20 * 16, v_dst);
    166       }
    167 
    168       // stash away top samples for next block
    169       if (mb_y < dec->mb_h_ - 1) {
    170         memcpy(top_yuv[0].y, y_dst + 15 * BPS, 16);
    171         memcpy(top_yuv[0].u, u_dst +  7 * BPS,  8);
    172         memcpy(top_yuv[0].v, v_dst +  7 * BPS,  8);
    173       }
    174     }
    175     // Transfer reconstructed samples from yuv_b_ cache to final destination.
    176     {
    177       const int y_offset = cache_id * 16 * dec->cache_y_stride_;
    178       const int uv_offset = cache_id * 8 * dec->cache_uv_stride_;
    179       uint8_t* const y_out = dec->cache_y_ + mb_x * 16 + y_offset;
    180       uint8_t* const u_out = dec->cache_u_ + mb_x * 8 + uv_offset;
    181       uint8_t* const v_out = dec->cache_v_ + mb_x * 8 + uv_offset;
    182       for (j = 0; j < 16; ++j) {
    183         memcpy(y_out + j * dec->cache_y_stride_, y_dst + j * BPS, 16);
    184       }
    185       for (j = 0; j < 8; ++j) {
    186         memcpy(u_out + j * dec->cache_uv_stride_, u_dst + j * BPS, 8);
    187         memcpy(v_out + j * dec->cache_uv_stride_, v_dst + j * BPS, 8);
    188       }
    189     }
    190   }
    191 }
    192 
    193 //------------------------------------------------------------------------------
    194 // Filtering
    195 
    196 // kFilterExtraRows[] = How many extra lines are needed on the MB boundary
    197 // for caching, given a filtering level.
    198 // Simple filter:  up to 2 luma samples are read and 1 is written.
    199 // Complex filter: up to 4 luma samples are read and 3 are written. Same for
    200 //                 U/V, so it's 8 samples total (because of the 2x upsampling).
    201 static const uint8_t kFilterExtraRows[3] = { 0, 2, 8 };
    202 
    203 static void DoFilter(const VP8Decoder* const dec, int mb_x, int mb_y) {
    204   const VP8ThreadContext* const ctx = &dec->thread_ctx_;
    205   const int cache_id = ctx->id_;
    206   const int y_bps = dec->cache_y_stride_;
    207   const VP8FInfo* const f_info = ctx->f_info_ + mb_x;
    208   uint8_t* const y_dst = dec->cache_y_ + cache_id * 16 * y_bps + mb_x * 16;
    209   const int ilevel = f_info->f_ilevel_;
    210   const int limit = f_info->f_limit_;
    211   if (limit == 0) {
    212     return;
    213   }
    214   assert(limit >= 3);
    215   if (dec->filter_type_ == 1) {   // simple
    216     if (mb_x > 0) {
    217       VP8SimpleHFilter16(y_dst, y_bps, limit + 4);
    218     }
    219     if (f_info->f_inner_) {
    220       VP8SimpleHFilter16i(y_dst, y_bps, limit);
    221     }
    222     if (mb_y > 0) {
    223       VP8SimpleVFilter16(y_dst, y_bps, limit + 4);
    224     }
    225     if (f_info->f_inner_) {
    226       VP8SimpleVFilter16i(y_dst, y_bps, limit);
    227     }
    228   } else {    // complex
    229     const int uv_bps = dec->cache_uv_stride_;
    230     uint8_t* const u_dst = dec->cache_u_ + cache_id * 8 * uv_bps + mb_x * 8;
    231     uint8_t* const v_dst = dec->cache_v_ + cache_id * 8 * uv_bps + mb_x * 8;
    232     const int hev_thresh = f_info->hev_thresh_;
    233     if (mb_x > 0) {
    234       VP8HFilter16(y_dst, y_bps, limit + 4, ilevel, hev_thresh);
    235       VP8HFilter8(u_dst, v_dst, uv_bps, limit + 4, ilevel, hev_thresh);
    236     }
    237     if (f_info->f_inner_) {
    238       VP8HFilter16i(y_dst, y_bps, limit, ilevel, hev_thresh);
    239       VP8HFilter8i(u_dst, v_dst, uv_bps, limit, ilevel, hev_thresh);
    240     }
    241     if (mb_y > 0) {
    242       VP8VFilter16(y_dst, y_bps, limit + 4, ilevel, hev_thresh);
    243       VP8VFilter8(u_dst, v_dst, uv_bps, limit + 4, ilevel, hev_thresh);
    244     }
    245     if (f_info->f_inner_) {
    246       VP8VFilter16i(y_dst, y_bps, limit, ilevel, hev_thresh);
    247       VP8VFilter8i(u_dst, v_dst, uv_bps, limit, ilevel, hev_thresh);
    248     }
    249   }
    250 }
    251 
    252 // Filter the decoded macroblock row (if needed)
    253 static void FilterRow(const VP8Decoder* const dec) {
    254   int mb_x;
    255   const int mb_y = dec->thread_ctx_.mb_y_;
    256   assert(dec->thread_ctx_.filter_row_);
    257   for (mb_x = dec->tl_mb_x_; mb_x < dec->br_mb_x_; ++mb_x) {
    258     DoFilter(dec, mb_x, mb_y);
    259   }
    260 }
    261 
    262 //------------------------------------------------------------------------------
    263 // Precompute the filtering strength for each segment and each i4x4/i16x16 mode.
    264 
    265 static void PrecomputeFilterStrengths(VP8Decoder* const dec) {
    266   if (dec->filter_type_ > 0) {
    267     int s;
    268     const VP8FilterHeader* const hdr = &dec->filter_hdr_;
    269     for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
    270       int i4x4;
    271       // First, compute the initial level
    272       int base_level;
    273       if (dec->segment_hdr_.use_segment_) {
    274         base_level = dec->segment_hdr_.filter_strength_[s];
    275         if (!dec->segment_hdr_.absolute_delta_) {
    276           base_level += hdr->level_;
    277         }
    278       } else {
    279         base_level = hdr->level_;
    280       }
    281       for (i4x4 = 0; i4x4 <= 1; ++i4x4) {
    282         VP8FInfo* const info = &dec->fstrengths_[s][i4x4];
    283         int level = base_level;
    284         if (hdr->use_lf_delta_) {
    285           level += hdr->ref_lf_delta_[0];
    286           if (i4x4) {
    287             level += hdr->mode_lf_delta_[0];
    288           }
    289         }
    290         level = (level < 0) ? 0 : (level > 63) ? 63 : level;
    291         if (level > 0) {
    292           int ilevel = level;
    293           if (hdr->sharpness_ > 0) {
    294             if (hdr->sharpness_ > 4) {
    295               ilevel >>= 2;
    296             } else {
    297               ilevel >>= 1;
    298             }
    299             if (ilevel > 9 - hdr->sharpness_) {
    300               ilevel = 9 - hdr->sharpness_;
    301             }
    302           }
    303           if (ilevel < 1) ilevel = 1;
    304           info->f_ilevel_ = ilevel;
    305           info->f_limit_ = 2 * level + ilevel;
    306           info->hev_thresh_ = (level >= 40) ? 2 : (level >= 15) ? 1 : 0;
    307         } else {
    308           info->f_limit_ = 0;  // no filtering
    309         }
    310         info->f_inner_ = i4x4;
    311       }
    312     }
    313   }
    314 }
    315 
    316 //------------------------------------------------------------------------------
    317 // Dithering
    318 
    319 // minimal amp that will provide a non-zero dithering effect
    320 #define MIN_DITHER_AMP 4
    321 
    322 #define DITHER_AMP_TAB_SIZE 12
    323 static const uint8_t kQuantToDitherAmp[DITHER_AMP_TAB_SIZE] = {
    324   // roughly, it's dqm->uv_mat_[1]
    325   8, 7, 6, 4, 4, 2, 2, 2, 1, 1, 1, 1
    326 };
    327 
    328 void VP8InitDithering(const WebPDecoderOptions* const options,
    329                       VP8Decoder* const dec) {
    330   assert(dec != NULL);
    331   if (options != NULL) {
    332     const int d = options->dithering_strength;
    333     const int max_amp = (1 << VP8_RANDOM_DITHER_FIX) - 1;
    334     const int f = (d < 0) ? 0 : (d > 100) ? max_amp : (d * max_amp / 100);
    335     if (f > 0) {
    336       int s;
    337       int all_amp = 0;
    338       for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
    339         VP8QuantMatrix* const dqm = &dec->dqm_[s];
    340         if (dqm->uv_quant_ < DITHER_AMP_TAB_SIZE) {
    341           // TODO(skal): should we specially dither more for uv_quant_ < 0?
    342           const int idx = (dqm->uv_quant_ < 0) ? 0 : dqm->uv_quant_;
    343           dqm->dither_ = (f * kQuantToDitherAmp[idx]) >> 3;
    344         }
    345         all_amp |= dqm->dither_;
    346       }
    347       if (all_amp != 0) {
    348         VP8InitRandom(&dec->dithering_rg_, 1.0f);
    349         dec->dither_ = 1;
    350       }
    351     }
    352     // potentially allow alpha dithering
    353     dec->alpha_dithering_ = options->alpha_dithering_strength;
    354     if (dec->alpha_dithering_ > 100) {
    355       dec->alpha_dithering_ = 100;
    356     } else if (dec->alpha_dithering_ < 0) {
    357       dec->alpha_dithering_ = 0;
    358     }
    359   }
    360 }
    361 
    362 // Convert to range: [-2,2] for dither=50, [-4,4] for dither=100
    363 static void Dither8x8(VP8Random* const rg, uint8_t* dst, int bps, int amp) {
    364   uint8_t dither[64];
    365   int i;
    366   for (i = 0; i < 8 * 8; ++i) {
    367     dither[i] = VP8RandomBits2(rg, VP8_DITHER_AMP_BITS + 1, amp);
    368   }
    369   VP8DitherCombine8x8(dither, dst, bps);
    370 }
    371 
    372 static void DitherRow(VP8Decoder* const dec) {
    373   int mb_x;
    374   assert(dec->dither_);
    375   for (mb_x = dec->tl_mb_x_; mb_x < dec->br_mb_x_; ++mb_x) {
    376     const VP8ThreadContext* const ctx = &dec->thread_ctx_;
    377     const VP8MBData* const data = ctx->mb_data_ + mb_x;
    378     const int cache_id = ctx->id_;
    379     const int uv_bps = dec->cache_uv_stride_;
    380     if (data->dither_ >= MIN_DITHER_AMP) {
    381       uint8_t* const u_dst = dec->cache_u_ + cache_id * 8 * uv_bps + mb_x * 8;
    382       uint8_t* const v_dst = dec->cache_v_ + cache_id * 8 * uv_bps + mb_x * 8;
    383       Dither8x8(&dec->dithering_rg_, u_dst, uv_bps, data->dither_);
    384       Dither8x8(&dec->dithering_rg_, v_dst, uv_bps, data->dither_);
    385     }
    386   }
    387 }
    388 
    389 //------------------------------------------------------------------------------
    390 // This function is called after a row of macroblocks is finished decoding.
    391 // It also takes into account the following restrictions:
    392 //  * In case of in-loop filtering, we must hold off sending some of the bottom
    393 //    pixels as they are yet unfiltered. They will be when the next macroblock
    394 //    row is decoded. Meanwhile, we must preserve them by rotating them in the
    395 //    cache area. This doesn't hold for the very bottom row of the uncropped
    396 //    picture of course.
    397 //  * we must clip the remaining pixels against the cropping area. The VP8Io
    398 //    struct must have the following fields set correctly before calling put():
    399 
    400 #define MACROBLOCK_VPOS(mb_y)  ((mb_y) * 16)    // vertical position of a MB
    401 
    402 // Finalize and transmit a complete row. Return false in case of user-abort.
    403 static int FinishRow(VP8Decoder* const dec, VP8Io* const io) {
    404   int ok = 1;
    405   const VP8ThreadContext* const ctx = &dec->thread_ctx_;
    406   const int cache_id = ctx->id_;
    407   const int extra_y_rows = kFilterExtraRows[dec->filter_type_];
    408   const int ysize = extra_y_rows * dec->cache_y_stride_;
    409   const int uvsize = (extra_y_rows / 2) * dec->cache_uv_stride_;
    410   const int y_offset = cache_id * 16 * dec->cache_y_stride_;
    411   const int uv_offset = cache_id * 8 * dec->cache_uv_stride_;
    412   uint8_t* const ydst = dec->cache_y_ - ysize + y_offset;
    413   uint8_t* const udst = dec->cache_u_ - uvsize + uv_offset;
    414   uint8_t* const vdst = dec->cache_v_ - uvsize + uv_offset;
    415   const int mb_y = ctx->mb_y_;
    416   const int is_first_row = (mb_y == 0);
    417   const int is_last_row = (mb_y >= dec->br_mb_y_ - 1);
    418 
    419   if (dec->mt_method_ == 2) {
    420     ReconstructRow(dec, ctx);
    421   }
    422 
    423   if (ctx->filter_row_) {
    424     FilterRow(dec);
    425   }
    426 
    427   if (dec->dither_) {
    428     DitherRow(dec);
    429   }
    430 
    431   if (io->put != NULL) {
    432     int y_start = MACROBLOCK_VPOS(mb_y);
    433     int y_end = MACROBLOCK_VPOS(mb_y + 1);
    434     if (!is_first_row) {
    435       y_start -= extra_y_rows;
    436       io->y = ydst;
    437       io->u = udst;
    438       io->v = vdst;
    439     } else {
    440       io->y = dec->cache_y_ + y_offset;
    441       io->u = dec->cache_u_ + uv_offset;
    442       io->v = dec->cache_v_ + uv_offset;
    443     }
    444 
    445     if (!is_last_row) {
    446       y_end -= extra_y_rows;
    447     }
    448     if (y_end > io->crop_bottom) {
    449       y_end = io->crop_bottom;    // make sure we don't overflow on last row.
    450     }
    451     io->a = NULL;
    452     if (dec->alpha_data_ != NULL && y_start < y_end) {
    453       // TODO(skal): testing presence of alpha with dec->alpha_data_ is not a
    454       // good idea.
    455       io->a = VP8DecompressAlphaRows(dec, io, y_start, y_end - y_start);
    456       if (io->a == NULL) {
    457         return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
    458                            "Could not decode alpha data.");
    459       }
    460     }
    461     if (y_start < io->crop_top) {
    462       const int delta_y = io->crop_top - y_start;
    463       y_start = io->crop_top;
    464       assert(!(delta_y & 1));
    465       io->y += dec->cache_y_stride_ * delta_y;
    466       io->u += dec->cache_uv_stride_ * (delta_y >> 1);
    467       io->v += dec->cache_uv_stride_ * (delta_y >> 1);
    468       if (io->a != NULL) {
    469         io->a += io->width * delta_y;
    470       }
    471     }
    472     if (y_start < y_end) {
    473       io->y += io->crop_left;
    474       io->u += io->crop_left >> 1;
    475       io->v += io->crop_left >> 1;
    476       if (io->a != NULL) {
    477         io->a += io->crop_left;
    478       }
    479       io->mb_y = y_start - io->crop_top;
    480       io->mb_w = io->crop_right - io->crop_left;
    481       io->mb_h = y_end - y_start;
    482       ok = io->put(io);
    483     }
    484   }
    485   // rotate top samples if needed
    486   if (cache_id + 1 == dec->num_caches_) {
    487     if (!is_last_row) {
    488       memcpy(dec->cache_y_ - ysize, ydst + 16 * dec->cache_y_stride_, ysize);
    489       memcpy(dec->cache_u_ - uvsize, udst + 8 * dec->cache_uv_stride_, uvsize);
    490       memcpy(dec->cache_v_ - uvsize, vdst + 8 * dec->cache_uv_stride_, uvsize);
    491     }
    492   }
    493 
    494   return ok;
    495 }
    496 
    497 #undef MACROBLOCK_VPOS
    498 
    499 //------------------------------------------------------------------------------
    500 
    501 int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io) {
    502   int ok = 1;
    503   VP8ThreadContext* const ctx = &dec->thread_ctx_;
    504   const int filter_row =
    505       (dec->filter_type_ > 0) &&
    506       (dec->mb_y_ >= dec->tl_mb_y_) && (dec->mb_y_ <= dec->br_mb_y_);
    507   if (dec->mt_method_ == 0) {
    508     // ctx->id_ and ctx->f_info_ are already set
    509     ctx->mb_y_ = dec->mb_y_;
    510     ctx->filter_row_ = filter_row;
    511     ReconstructRow(dec, ctx);
    512     ok = FinishRow(dec, io);
    513   } else {
    514     WebPWorker* const worker = &dec->worker_;
    515     // Finish previous job *before* updating context
    516     ok &= WebPGetWorkerInterface()->Sync(worker);
    517     assert(worker->status_ == OK);
    518     if (ok) {   // spawn a new deblocking/output job
    519       ctx->io_ = *io;
    520       ctx->id_ = dec->cache_id_;
    521       ctx->mb_y_ = dec->mb_y_;
    522       ctx->filter_row_ = filter_row;
    523       if (dec->mt_method_ == 2) {  // swap macroblock data
    524         VP8MBData* const tmp = ctx->mb_data_;
    525         ctx->mb_data_ = dec->mb_data_;
    526         dec->mb_data_ = tmp;
    527       } else {
    528         // perform reconstruction directly in main thread
    529         ReconstructRow(dec, ctx);
    530       }
    531       if (filter_row) {            // swap filter info
    532         VP8FInfo* const tmp = ctx->f_info_;
    533         ctx->f_info_ = dec->f_info_;
    534         dec->f_info_ = tmp;
    535       }
    536       // (reconstruct)+filter in parallel
    537       WebPGetWorkerInterface()->Launch(worker);
    538       if (++dec->cache_id_ == dec->num_caches_) {
    539         dec->cache_id_ = 0;
    540       }
    541     }
    542   }
    543   return ok;
    544 }
    545 
    546 //------------------------------------------------------------------------------
    547 // Finish setting up the decoding parameter once user's setup() is called.
    548 
    549 VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io) {
    550   // Call setup() first. This may trigger additional decoding features on 'io'.
    551   // Note: Afterward, we must call teardown() no matter what.
    552   if (io->setup != NULL && !io->setup(io)) {
    553     VP8SetError(dec, VP8_STATUS_USER_ABORT, "Frame setup failed");
    554     return dec->status_;
    555   }
    556 
    557   // Disable filtering per user request
    558   if (io->bypass_filtering) {
    559     dec->filter_type_ = 0;
    560   }
    561   // TODO(skal): filter type / strength / sharpness forcing
    562 
    563   // Define the area where we can skip in-loop filtering, in case of cropping.
    564   //
    565   // 'Simple' filter reads two luma samples outside of the macroblock
    566   // and filters one. It doesn't filter the chroma samples. Hence, we can
    567   // avoid doing the in-loop filtering before crop_top/crop_left position.
    568   // For the 'Complex' filter, 3 samples are read and up to 3 are filtered.
    569   // Means: there's a dependency chain that goes all the way up to the
    570   // top-left corner of the picture (MB #0). We must filter all the previous
    571   // macroblocks.
    572   // TODO(skal): add an 'approximate_decoding' option, that won't produce
    573   // a 1:1 bit-exactness for complex filtering?
    574   {
    575     const int extra_pixels = kFilterExtraRows[dec->filter_type_];
    576     if (dec->filter_type_ == 2) {
    577       // For complex filter, we need to preserve the dependency chain.
    578       dec->tl_mb_x_ = 0;
    579       dec->tl_mb_y_ = 0;
    580     } else {
    581       // For simple filter, we can filter only the cropped region.
    582       // We include 'extra_pixels' on the other side of the boundary, since
    583       // vertical or horizontal filtering of the previous macroblock can
    584       // modify some abutting pixels.
    585       dec->tl_mb_x_ = (io->crop_left - extra_pixels) >> 4;
    586       dec->tl_mb_y_ = (io->crop_top - extra_pixels) >> 4;
    587       if (dec->tl_mb_x_ < 0) dec->tl_mb_x_ = 0;
    588       if (dec->tl_mb_y_ < 0) dec->tl_mb_y_ = 0;
    589     }
    590     // We need some 'extra' pixels on the right/bottom.
    591     dec->br_mb_y_ = (io->crop_bottom + 15 + extra_pixels) >> 4;
    592     dec->br_mb_x_ = (io->crop_right + 15 + extra_pixels) >> 4;
    593     if (dec->br_mb_x_ > dec->mb_w_) {
    594       dec->br_mb_x_ = dec->mb_w_;
    595     }
    596     if (dec->br_mb_y_ > dec->mb_h_) {
    597       dec->br_mb_y_ = dec->mb_h_;
    598     }
    599   }
    600   PrecomputeFilterStrengths(dec);
    601   return VP8_STATUS_OK;
    602 }
    603 
    604 int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io) {
    605   int ok = 1;
    606   if (dec->mt_method_ > 0) {
    607     ok = WebPGetWorkerInterface()->Sync(&dec->worker_);
    608   }
    609 
    610   if (io->teardown != NULL) {
    611     io->teardown(io);
    612   }
    613   return ok;
    614 }
    615 
    616 //------------------------------------------------------------------------------
    617 // For multi-threaded decoding we need to use 3 rows of 16 pixels as delay line.
    618 //
    619 // Reason is: the deblocking filter cannot deblock the bottom horizontal edges
    620 // immediately, and needs to wait for first few rows of the next macroblock to
    621 // be decoded. Hence, deblocking is lagging behind by 4 or 8 pixels (depending
    622 // on strength).
    623 // With two threads, the vertical positions of the rows being decoded are:
    624 // Decode:  [ 0..15][16..31][32..47][48..63][64..79][...
    625 // Deblock:         [ 0..11][12..27][28..43][44..59][...
    626 // If we use two threads and two caches of 16 pixels, the sequence would be:
    627 // Decode:  [ 0..15][16..31][ 0..15!!][16..31][ 0..15][...
    628 // Deblock:         [ 0..11][12..27!!][-4..11][12..27][...
    629 // The problem occurs during row [12..15!!] that both the decoding and
    630 // deblocking threads are writing simultaneously.
    631 // With 3 cache lines, one get a safe write pattern:
    632 // Decode:  [ 0..15][16..31][32..47][ 0..15][16..31][32..47][0..
    633 // Deblock:         [ 0..11][12..27][28..43][-4..11][12..27][28...
    634 // Note that multi-threaded output _without_ deblocking can make use of two
    635 // cache lines of 16 pixels only, since there's no lagging behind. The decoding
    636 // and output process have non-concurrent writing:
    637 // Decode:  [ 0..15][16..31][ 0..15][16..31][...
    638 // io->put:         [ 0..15][16..31][ 0..15][...
    639 
    640 #define MT_CACHE_LINES 3
    641 #define ST_CACHE_LINES 1   // 1 cache row only for single-threaded case
    642 
    643 // Initialize multi/single-thread worker
    644 static int InitThreadContext(VP8Decoder* const dec) {
    645   dec->cache_id_ = 0;
    646   if (dec->mt_method_ > 0) {
    647     WebPWorker* const worker = &dec->worker_;
    648     if (!WebPGetWorkerInterface()->Reset(worker)) {
    649       return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
    650                          "thread initialization failed.");
    651     }
    652     worker->data1 = dec;
    653     worker->data2 = (void*)&dec->thread_ctx_.io_;
    654     worker->hook = (WebPWorkerHook)FinishRow;
    655     dec->num_caches_ =
    656       (dec->filter_type_ > 0) ? MT_CACHE_LINES : MT_CACHE_LINES - 1;
    657   } else {
    658     dec->num_caches_ = ST_CACHE_LINES;
    659   }
    660   return 1;
    661 }
    662 
    663 int VP8GetThreadMethod(const WebPDecoderOptions* const options,
    664                        const WebPHeaderStructure* const headers,
    665                        int width, int height) {
    666   if (options == NULL || options->use_threads == 0) {
    667     return 0;
    668   }
    669   (void)headers;
    670   (void)width;
    671   (void)height;
    672   assert(headers == NULL || !headers->is_lossless);
    673 #if defined(WEBP_USE_THREAD)
    674   if (width < MIN_WIDTH_FOR_THREADS) return 0;
    675   // TODO(skal): tune the heuristic further
    676 #if 0
    677   if (height < 2 * width) return 2;
    678 #endif
    679   return 2;
    680 #else   // !WEBP_USE_THREAD
    681   return 0;
    682 #endif
    683 }
    684 
    685 #undef MT_CACHE_LINES
    686 #undef ST_CACHE_LINES
    687 
    688 //------------------------------------------------------------------------------
    689 // Memory setup
    690 
    691 static int AllocateMemory(VP8Decoder* const dec) {
    692   const int num_caches = dec->num_caches_;
    693   const int mb_w = dec->mb_w_;
    694   // Note: we use 'size_t' when there's no overflow risk, uint64_t otherwise.
    695   const size_t intra_pred_mode_size = 4 * mb_w * sizeof(uint8_t);
    696   const size_t top_size = sizeof(VP8TopSamples) * mb_w;
    697   const size_t mb_info_size = (mb_w + 1) * sizeof(VP8MB);
    698   const size_t f_info_size =
    699       (dec->filter_type_ > 0) ?
    700           mb_w * (dec->mt_method_ > 0 ? 2 : 1) * sizeof(VP8FInfo)
    701         : 0;
    702   const size_t yuv_size = YUV_SIZE * sizeof(*dec->yuv_b_);
    703   const size_t mb_data_size =
    704       (dec->mt_method_ == 2 ? 2 : 1) * mb_w * sizeof(*dec->mb_data_);
    705   const size_t cache_height = (16 * num_caches
    706                             + kFilterExtraRows[dec->filter_type_]) * 3 / 2;
    707   const size_t cache_size = top_size * cache_height;
    708   // alpha_size is the only one that scales as width x height.
    709   const uint64_t alpha_size = (dec->alpha_data_ != NULL) ?
    710       (uint64_t)dec->pic_hdr_.width_ * dec->pic_hdr_.height_ : 0ULL;
    711   const uint64_t needed = (uint64_t)intra_pred_mode_size
    712                         + top_size + mb_info_size + f_info_size
    713                         + yuv_size + mb_data_size
    714                         + cache_size + alpha_size + WEBP_ALIGN_CST;
    715   uint8_t* mem;
    716 
    717   if (needed != (size_t)needed) return 0;  // check for overflow
    718   if (needed > dec->mem_size_) {
    719     WebPSafeFree(dec->mem_);
    720     dec->mem_size_ = 0;
    721     dec->mem_ = WebPSafeMalloc(needed, sizeof(uint8_t));
    722     if (dec->mem_ == NULL) {
    723       return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
    724                          "no memory during frame initialization.");
    725     }
    726     // down-cast is ok, thanks to WebPSafeMalloc() above.
    727     dec->mem_size_ = (size_t)needed;
    728   }
    729 
    730   mem = (uint8_t*)dec->mem_;
    731   dec->intra_t_ = mem;
    732   mem += intra_pred_mode_size;
    733 
    734   dec->yuv_t_ = (VP8TopSamples*)mem;
    735   mem += top_size;
    736 
    737   dec->mb_info_ = ((VP8MB*)mem) + 1;
    738   mem += mb_info_size;
    739 
    740   dec->f_info_ = f_info_size ? (VP8FInfo*)mem : NULL;
    741   mem += f_info_size;
    742   dec->thread_ctx_.id_ = 0;
    743   dec->thread_ctx_.f_info_ = dec->f_info_;
    744   if (dec->mt_method_ > 0) {
    745     // secondary cache line. The deblocking process need to make use of the
    746     // filtering strength from previous macroblock row, while the new ones
    747     // are being decoded in parallel. We'll just swap the pointers.
    748     dec->thread_ctx_.f_info_ += mb_w;
    749   }
    750 
    751   mem = (uint8_t*)WEBP_ALIGN(mem);
    752   assert((yuv_size & WEBP_ALIGN_CST) == 0);
    753   dec->yuv_b_ = mem;
    754   mem += yuv_size;
    755 
    756   dec->mb_data_ = (VP8MBData*)mem;
    757   dec->thread_ctx_.mb_data_ = (VP8MBData*)mem;
    758   if (dec->mt_method_ == 2) {
    759     dec->thread_ctx_.mb_data_ += mb_w;
    760   }
    761   mem += mb_data_size;
    762 
    763   dec->cache_y_stride_ = 16 * mb_w;
    764   dec->cache_uv_stride_ = 8 * mb_w;
    765   {
    766     const int extra_rows = kFilterExtraRows[dec->filter_type_];
    767     const int extra_y = extra_rows * dec->cache_y_stride_;
    768     const int extra_uv = (extra_rows / 2) * dec->cache_uv_stride_;
    769     dec->cache_y_ = mem + extra_y;
    770     dec->cache_u_ = dec->cache_y_
    771                   + 16 * num_caches * dec->cache_y_stride_ + extra_uv;
    772     dec->cache_v_ = dec->cache_u_
    773                   + 8 * num_caches * dec->cache_uv_stride_ + extra_uv;
    774     dec->cache_id_ = 0;
    775   }
    776   mem += cache_size;
    777 
    778   // alpha plane
    779   dec->alpha_plane_ = alpha_size ? mem : NULL;
    780   mem += alpha_size;
    781   assert(mem <= (uint8_t*)dec->mem_ + dec->mem_size_);
    782 
    783   // note: left/top-info is initialized once for all.
    784   memset(dec->mb_info_ - 1, 0, mb_info_size);
    785   VP8InitScanline(dec);   // initialize left too.
    786 
    787   // initialize top
    788   memset(dec->intra_t_, B_DC_PRED, intra_pred_mode_size);
    789 
    790   return 1;
    791 }
    792 
    793 static void InitIo(VP8Decoder* const dec, VP8Io* io) {
    794   // prepare 'io'
    795   io->mb_y = 0;
    796   io->y = dec->cache_y_;
    797   io->u = dec->cache_u_;
    798   io->v = dec->cache_v_;
    799   io->y_stride = dec->cache_y_stride_;
    800   io->uv_stride = dec->cache_uv_stride_;
    801   io->a = NULL;
    802 }
    803 
    804 int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io) {
    805   if (!InitThreadContext(dec)) return 0;  // call first. Sets dec->num_caches_.
    806   if (!AllocateMemory(dec)) return 0;
    807   InitIo(dec, io);
    808   VP8DspInit();  // Init critical function pointers and look-up tables.
    809   return 1;
    810 }
    811 
    812 //------------------------------------------------------------------------------
    813