• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <algorithm>
16 #include <array>
17 #include <cassert>
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstdlib>
21 #include <cstring>
22 #include <memory>
23 
24 #include "src/buffer_pool.h"
25 #include "src/dsp/constants.h"
26 #include "src/dsp/dsp.h"
27 #include "src/motion_vector.h"
28 #include "src/obu_parser.h"
29 #include "src/prediction_mask.h"
30 #include "src/tile.h"
31 #include "src/utils/array_2d.h"
32 #include "src/utils/bit_mask_set.h"
33 #include "src/utils/block_parameters_holder.h"
34 #include "src/utils/common.h"
35 #include "src/utils/constants.h"
36 #include "src/utils/logging.h"
37 #include "src/utils/memory.h"
38 #include "src/utils/types.h"
39 #include "src/warp_prediction.h"
40 #include "src/yuv_buffer.h"
41 
42 namespace libgav1 {
43 namespace {
44 
45 // Import all the constants in the anonymous namespace.
46 #include "src/inter_intra_masks.inc"
47 
48 // Precision bits when scaling reference frames.
49 constexpr int kReferenceScaleShift = 14;
50 constexpr int kAngleStep = 3;
51 constexpr int kPredictionModeToAngle[kIntraPredictionModesUV] = {
52     0, 90, 180, 45, 135, 113, 157, 203, 67, 0, 0, 0, 0};
53 
54 // The following modes need both the left_column and top_row for intra
55 // prediction. For directional modes left/top requirement is inferred based on
56 // the prediction angle. For Dc modes, left/top requirement is inferred based on
57 // whether or not left/top is available.
58 constexpr BitMaskSet kNeedsLeftAndTop(kPredictionModeSmooth,
59                                       kPredictionModeSmoothHorizontal,
60                                       kPredictionModeSmoothVertical,
61                                       kPredictionModePaeth);
62 
GetDirectionalIntraPredictorDerivative(const int angle)63 int16_t GetDirectionalIntraPredictorDerivative(const int angle) {
64   assert(angle >= 3);
65   assert(angle <= 87);
66   return kDirectionalIntraPredictorDerivative[DivideBy2(angle) - 1];
67 }
68 
69 // Maps the block_size to an index as follows:
70 //  kBlock8x8 => 0.
71 //  kBlock8x16 => 1.
72 //  kBlock8x32 => 2.
73 //  kBlock16x8 => 3.
74 //  kBlock16x16 => 4.
75 //  kBlock16x32 => 5.
76 //  kBlock32x8 => 6.
77 //  kBlock32x16 => 7.
78 //  kBlock32x32 => 8.
GetWedgeBlockSizeIndex(BlockSize block_size)79 int GetWedgeBlockSizeIndex(BlockSize block_size) {
80   assert(block_size >= kBlock8x8);
81   return block_size - kBlock8x8 - static_cast<int>(block_size >= kBlock16x8) -
82          static_cast<int>(block_size >= kBlock32x8);
83 }
84 
85 // Maps a dimension of 4, 8, 16 and 32 to indices 0, 1, 2 and 3 respectively.
GetInterIntraMaskLookupIndex(int dimension)86 int GetInterIntraMaskLookupIndex(int dimension) {
87   assert(dimension == 4 || dimension == 8 || dimension == 16 ||
88          dimension == 32);
89   return FloorLog2(dimension) - 2;
90 }
91 
92 // 7.11.2.9.
GetIntraEdgeFilterStrength(int width,int height,int filter_type,int delta)93 int GetIntraEdgeFilterStrength(int width, int height, int filter_type,
94                                int delta) {
95   const int sum = width + height;
96   delta = std::abs(delta);
97   if (filter_type == 0) {
98     if (sum <= 8) {
99       if (delta >= 56) return 1;
100     } else if (sum <= 16) {
101       if (delta >= 40) return 1;
102     } else if (sum <= 24) {
103       if (delta >= 32) return 3;
104       if (delta >= 16) return 2;
105       if (delta >= 8) return 1;
106     } else if (sum <= 32) {
107       if (delta >= 32) return 3;
108       if (delta >= 4) return 2;
109       return 1;
110     } else {
111       return 3;
112     }
113   } else {
114     if (sum <= 8) {
115       if (delta >= 64) return 2;
116       if (delta >= 40) return 1;
117     } else if (sum <= 16) {
118       if (delta >= 48) return 2;
119       if (delta >= 20) return 1;
120     } else if (sum <= 24) {
121       if (delta >= 4) return 3;
122     } else {
123       return 3;
124     }
125   }
126   return 0;
127 }
128 
129 // 7.11.2.10.
DoIntraEdgeUpsampling(int width,int height,int filter_type,int delta)130 bool DoIntraEdgeUpsampling(int width, int height, int filter_type, int delta) {
131   const int sum = width + height;
132   delta = std::abs(delta);
133   // This function should not be called when the prediction angle is 90 or 180.
134   assert(delta != 0);
135   if (delta >= 40) return false;
136   return (filter_type == 1) ? sum <= 8 : sum <= 16;
137 }
138 
139 constexpr uint8_t kQuantizedDistanceWeight[4][2] = {
140     {2, 3}, {2, 5}, {2, 7}, {1, kMaxFrameDistance}};
141 
142 constexpr uint8_t kQuantizedDistanceLookup[4][2] = {
143     {9, 7}, {11, 5}, {12, 4}, {13, 3}};
144 
GetDistanceWeights(const int distance[2],int weight[2])145 void GetDistanceWeights(const int distance[2], int weight[2]) {
146   // Note: distance[0] and distance[1] correspond to relative distance
147   // between current frame and reference frame [1] and [0], respectively.
148   const int order = static_cast<int>(distance[0] <= distance[1]);
149   if (distance[0] == 0 || distance[1] == 0) {
150     weight[0] = kQuantizedDistanceLookup[3][order];
151     weight[1] = kQuantizedDistanceLookup[3][1 - order];
152   } else {
153     int i;
154     for (i = 0; i < 3; ++i) {
155       const int weight_0 = kQuantizedDistanceWeight[i][order];
156       const int weight_1 = kQuantizedDistanceWeight[i][1 - order];
157       if (order == 0) {
158         if (distance[0] * weight_0 < distance[1] * weight_1) break;
159       } else {
160         if (distance[0] * weight_0 > distance[1] * weight_1) break;
161       }
162     }
163     weight[0] = kQuantizedDistanceLookup[i][order];
164     weight[1] = kQuantizedDistanceLookup[i][1 - order];
165   }
166 }
167 
GetIntraPredictor(PredictionMode mode,bool has_left,bool has_top)168 dsp::IntraPredictor GetIntraPredictor(PredictionMode mode, bool has_left,
169                                       bool has_top) {
170   if (mode == kPredictionModeDc) {
171     if (has_left && has_top) {
172       return dsp::kIntraPredictorDc;
173     }
174     if (has_left) {
175       return dsp::kIntraPredictorDcLeft;
176     }
177     if (has_top) {
178       return dsp::kIntraPredictorDcTop;
179     }
180     return dsp::kIntraPredictorDcFill;
181   }
182   switch (mode) {
183     case kPredictionModePaeth:
184       return dsp::kIntraPredictorPaeth;
185     case kPredictionModeSmooth:
186       return dsp::kIntraPredictorSmooth;
187     case kPredictionModeSmoothVertical:
188       return dsp::kIntraPredictorSmoothVertical;
189     case kPredictionModeSmoothHorizontal:
190       return dsp::kIntraPredictorSmoothHorizontal;
191     default:
192       return dsp::kNumIntraPredictors;
193   }
194 }
195 
GetStartPoint(Array2DView<uint8_t> * const buffer,const int plane,const int x,const int y,const int bitdepth)196 uint8_t* GetStartPoint(Array2DView<uint8_t>* const buffer, const int plane,
197                        const int x, const int y, const int bitdepth) {
198 #if LIBGAV1_MAX_BITDEPTH >= 10
199   if (bitdepth > 8) {
200     Array2DView<uint16_t> buffer16(
201         buffer[plane].rows(), buffer[plane].columns() / sizeof(uint16_t),
202         reinterpret_cast<uint16_t*>(&buffer[plane][0][0]));
203     return reinterpret_cast<uint8_t*>(&buffer16[y][x]);
204   }
205 #endif  // LIBGAV1_MAX_BITDEPTH >= 10
206   static_cast<void>(bitdepth);
207   return &buffer[plane][y][x];
208 }
209 
GetPixelPositionFromHighScale(int start,int step,int offset)210 int GetPixelPositionFromHighScale(int start, int step, int offset) {
211   return (start + step * offset) >> kScaleSubPixelBits;
212 }
213 
GetMaskBlendFunc(const dsp::Dsp & dsp,bool is_inter_intra,bool is_wedge_inter_intra,int subsampling_x,int subsampling_y)214 dsp::MaskBlendFunc GetMaskBlendFunc(const dsp::Dsp& dsp, bool is_inter_intra,
215                                     bool is_wedge_inter_intra,
216                                     int subsampling_x, int subsampling_y) {
217   return (is_inter_intra && !is_wedge_inter_intra)
218              ? dsp.mask_blend[0][/*is_inter_intra=*/true]
219              : dsp.mask_blend[subsampling_x + subsampling_y][is_inter_intra];
220 }
221 
222 }  // namespace
223 
224 template <typename Pixel>
IntraPrediction(const Block & block,Plane plane,int x,int y,bool has_left,bool has_top,bool has_top_right,bool has_bottom_left,PredictionMode mode,TransformSize tx_size)225 void Tile::IntraPrediction(const Block& block, Plane plane, int x, int y,
226                            bool has_left, bool has_top, bool has_top_right,
227                            bool has_bottom_left, PredictionMode mode,
228                            TransformSize tx_size) {
229   const int width = kTransformWidth[tx_size];
230   const int height = kTransformHeight[tx_size];
231   const int x_shift = subsampling_x_[plane];
232   const int y_shift = subsampling_y_[plane];
233   const int max_x = (MultiplyBy4(frame_header_.columns4x4) >> x_shift) - 1;
234   const int max_y = (MultiplyBy4(frame_header_.rows4x4) >> y_shift) - 1;
235   // For performance reasons, do not initialize the following two buffers.
236   alignas(kMaxAlignment) Pixel top_row_data[160];
237   alignas(kMaxAlignment) Pixel left_column_data[160];
238 #if LIBGAV1_MSAN
239   if (IsDirectionalMode(mode)) {
240     memset(top_row_data, 0, sizeof(top_row_data));
241     memset(left_column_data, 0, sizeof(left_column_data));
242   }
243 #endif
244   // Some predictors use |top_row_data| and |left_column_data| with a negative
245   // offset to access pixels to the top-left of the current block. So have some
246   // space before the arrays to allow populating those without having to move
247   // the rest of the array.
248   Pixel* const top_row = top_row_data + 16;
249   Pixel* const left_column = left_column_data + 16;
250   const int bitdepth = sequence_header_.color_config.bitdepth;
251   const int top_and_left_size = width + height;
252   const bool is_directional_mode = IsDirectionalMode(mode);
253   const PredictionParameters& prediction_parameters =
254       *block.bp->prediction_parameters;
255   const bool use_filter_intra =
256       (plane == kPlaneY && prediction_parameters.use_filter_intra);
257   const int prediction_angle =
258       is_directional_mode
259           ? kPredictionModeToAngle[mode] +
260                 prediction_parameters.angle_delta[GetPlaneType(plane)] *
261                     kAngleStep
262           : 0;
263   // Directional prediction requires buffers larger than the width or height.
264   const int top_size = is_directional_mode ? top_and_left_size : width;
265   const int left_size = is_directional_mode ? top_and_left_size : height;
266   const int top_right_size =
267       is_directional_mode ? (has_top_right ? 2 : 1) * width : width;
268   const int bottom_left_size =
269       is_directional_mode ? (has_bottom_left ? 2 : 1) * height : height;
270 
271   Array2DView<Pixel> buffer(buffer_[plane].rows(),
272                             buffer_[plane].columns() / sizeof(Pixel),
273                             reinterpret_cast<Pixel*>(&buffer_[plane][0][0]));
274   const bool needs_top = use_filter_intra || kNeedsLeftAndTop.Contains(mode) ||
275                          (is_directional_mode && prediction_angle < 180) ||
276                          (mode == kPredictionModeDc && has_top);
277   const bool needs_left = use_filter_intra || kNeedsLeftAndTop.Contains(mode) ||
278                           (is_directional_mode && prediction_angle > 90) ||
279                           (mode == kPredictionModeDc && has_left);
280 
281   const Pixel* top_row_src = buffer[y - 1];
282 
283   // Determine if we need to retrieve the top row from
284   // |intra_prediction_buffer_|.
285   if ((needs_top || needs_left) && use_intra_prediction_buffer_) {
286     // Superblock index of block.row4x4. block.row4x4 is always in luma
287     // dimension (no subsampling).
288     const int current_superblock_index =
289         block.row4x4 >> (sequence_header_.use_128x128_superblock ? 5 : 4);
290     // Superblock index of y - 1. y is in the plane dimension (chroma planes
291     // could be subsampled).
292     const int plane_shift = (sequence_header_.use_128x128_superblock ? 7 : 6) -
293                             subsampling_y_[plane];
294     const int top_row_superblock_index = (y - 1) >> plane_shift;
295     // If the superblock index of y - 1 is not that of the current superblock,
296     // then we will have to retrieve the top row from the
297     // |intra_prediction_buffer_|.
298     if (current_superblock_index != top_row_superblock_index) {
299       top_row_src = reinterpret_cast<const Pixel*>(
300           (*intra_prediction_buffer_)[plane].get());
301     }
302   }
303 
304   if (needs_top) {
305     // Compute top_row.
306     if (has_top || has_left) {
307       const int left_index = has_left ? x - 1 : x;
308       top_row[-1] = has_top ? top_row_src[left_index] : buffer[y][left_index];
309     } else {
310       top_row[-1] = 1 << (bitdepth - 1);
311     }
312     if (!has_top && has_left) {
313       Memset(top_row, buffer[y][x - 1], top_size);
314     } else if (!has_top && !has_left) {
315       Memset(top_row, (1 << (bitdepth - 1)) - 1, top_size);
316     } else {
317       const int top_limit = std::min(max_x - x + 1, top_right_size);
318       memcpy(top_row, &top_row_src[x], top_limit * sizeof(Pixel));
319       // Even though it is safe to call Memset with a size of 0, accessing
320       // top_row_src[top_limit - x + 1] is not allowed when this condition is
321       // false.
322       if (top_size - top_limit > 0) {
323         Memset(top_row + top_limit, top_row_src[top_limit + x - 1],
324                top_size - top_limit);
325       }
326     }
327   }
328   if (needs_left) {
329     // Compute left_column.
330     if (has_top || has_left) {
331       const int left_index = has_left ? x - 1 : x;
332       left_column[-1] =
333           has_top ? top_row_src[left_index] : buffer[y][left_index];
334     } else {
335       left_column[-1] = 1 << (bitdepth - 1);
336     }
337     if (!has_left && has_top) {
338       Memset(left_column, top_row_src[x], left_size);
339     } else if (!has_left && !has_top) {
340       Memset(left_column, (1 << (bitdepth - 1)) + 1, left_size);
341     } else {
342       const int left_limit = std::min(max_y - y + 1, bottom_left_size);
343       for (int i = 0; i < left_limit; ++i) {
344         left_column[i] = buffer[y + i][x - 1];
345       }
346       // Even though it is safe to call Memset with a size of 0, accessing
347       // buffer[left_limit - y + 1][x - 1] is not allowed when this condition is
348       // false.
349       if (left_size - left_limit > 0) {
350         Memset(left_column + left_limit, buffer[left_limit + y - 1][x - 1],
351                left_size - left_limit);
352       }
353     }
354   }
355   Pixel* const dest = &buffer[y][x];
356   const ptrdiff_t dest_stride = buffer_[plane].columns();
357   if (use_filter_intra) {
358     dsp_.filter_intra_predictor(dest, dest_stride, top_row, left_column,
359                                 prediction_parameters.filter_intra_mode, width,
360                                 height);
361   } else if (is_directional_mode) {
362     DirectionalPrediction(block, plane, x, y, has_left, has_top, needs_left,
363                           needs_top, prediction_angle, width, height, max_x,
364                           max_y, tx_size, top_row, left_column);
365   } else {
366     const dsp::IntraPredictor predictor =
367         GetIntraPredictor(mode, has_left, has_top);
368     assert(predictor != dsp::kNumIntraPredictors);
369     dsp_.intra_predictors[tx_size][predictor](dest, dest_stride, top_row,
370                                               left_column);
371   }
372 }
373 
374 template void Tile::IntraPrediction<uint8_t>(const Block& block, Plane plane,
375                                              int x, int y, bool has_left,
376                                              bool has_top, bool has_top_right,
377                                              bool has_bottom_left,
378                                              PredictionMode mode,
379                                              TransformSize tx_size);
380 #if LIBGAV1_MAX_BITDEPTH >= 10
381 template void Tile::IntraPrediction<uint16_t>(const Block& block, Plane plane,
382                                               int x, int y, bool has_left,
383                                               bool has_top, bool has_top_right,
384                                               bool has_bottom_left,
385                                               PredictionMode mode,
386                                               TransformSize tx_size);
387 #endif
388 
GetIntraEdgeFilterType(const Block & block,Plane plane) const389 int Tile::GetIntraEdgeFilterType(const Block& block, Plane plane) const {
390   bool top;
391   bool left;
392   if (plane == kPlaneY) {
393     top = block.top_available[kPlaneY] &&
394           kPredictionModeSmoothMask.Contains(block.bp_top->y_mode);
395     left = block.left_available[kPlaneY] &&
396            kPredictionModeSmoothMask.Contains(block.bp_left->y_mode);
397   } else {
398     top = block.top_available[plane] &&
399           block.bp->prediction_parameters->chroma_top_uses_smooth_prediction;
400     left = block.left_available[plane] &&
401            block.bp->prediction_parameters->chroma_left_uses_smooth_prediction;
402   }
403   return static_cast<int>(top || left);
404 }
405 
406 template <typename Pixel>
DirectionalPrediction(const Block & block,Plane plane,int x,int y,bool has_left,bool has_top,bool needs_left,bool needs_top,int prediction_angle,int width,int height,int max_x,int max_y,TransformSize tx_size,Pixel * const top_row,Pixel * const left_column)407 void Tile::DirectionalPrediction(const Block& block, Plane plane, int x, int y,
408                                  bool has_left, bool has_top, bool needs_left,
409                                  bool needs_top, int prediction_angle,
410                                  int width, int height, int max_x, int max_y,
411                                  TransformSize tx_size, Pixel* const top_row,
412                                  Pixel* const left_column) {
413   Array2DView<Pixel> buffer(buffer_[plane].rows(),
414                             buffer_[plane].columns() / sizeof(Pixel),
415                             reinterpret_cast<Pixel*>(&buffer_[plane][0][0]));
416   Pixel* const dest = &buffer[y][x];
417   const ptrdiff_t stride = buffer_[plane].columns();
418   if (prediction_angle == 90) {
419     dsp_.intra_predictors[tx_size][dsp::kIntraPredictorVertical](
420         dest, stride, top_row, left_column);
421     return;
422   }
423   if (prediction_angle == 180) {
424     dsp_.intra_predictors[tx_size][dsp::kIntraPredictorHorizontal](
425         dest, stride, top_row, left_column);
426     return;
427   }
428 
429   bool upsampled_top = false;
430   bool upsampled_left = false;
431   if (sequence_header_.enable_intra_edge_filter) {
432     const int filter_type = GetIntraEdgeFilterType(block, plane);
433     if (prediction_angle > 90 && prediction_angle < 180 &&
434         (width + height) >= 24) {
435       // 7.11.2.7.
436       left_column[-1] = top_row[-1] = RightShiftWithRounding(
437           left_column[0] * 5 + top_row[-1] * 6 + top_row[0] * 5, 4);
438     }
439     if (has_top && needs_top) {
440       const int strength = GetIntraEdgeFilterStrength(
441           width, height, filter_type, prediction_angle - 90);
442       if (strength > 0) {
443         const int num_pixels = std::min(width, max_x - x + 1) +
444                                ((prediction_angle < 90) ? height : 0) + 1;
445         dsp_.intra_edge_filter(top_row - 1, num_pixels, strength);
446       }
447     }
448     if (has_left && needs_left) {
449       const int strength = GetIntraEdgeFilterStrength(
450           width, height, filter_type, prediction_angle - 180);
451       if (strength > 0) {
452         const int num_pixels = std::min(height, max_y - y + 1) +
453                                ((prediction_angle > 180) ? width : 0) + 1;
454         dsp_.intra_edge_filter(left_column - 1, num_pixels, strength);
455       }
456     }
457     upsampled_top = DoIntraEdgeUpsampling(width, height, filter_type,
458                                           prediction_angle - 90);
459     if (upsampled_top && needs_top) {
460       const int num_pixels = width + ((prediction_angle < 90) ? height : 0);
461       dsp_.intra_edge_upsampler(top_row, num_pixels);
462     }
463     upsampled_left = DoIntraEdgeUpsampling(width, height, filter_type,
464                                            prediction_angle - 180);
465     if (upsampled_left && needs_left) {
466       const int num_pixels = height + ((prediction_angle > 180) ? width : 0);
467       dsp_.intra_edge_upsampler(left_column, num_pixels);
468     }
469   }
470 
471   if (prediction_angle < 90) {
472     const int dx = GetDirectionalIntraPredictorDerivative(prediction_angle);
473     dsp_.directional_intra_predictor_zone1(dest, stride, top_row, width, height,
474                                            dx, upsampled_top);
475   } else if (prediction_angle < 180) {
476     const int dx =
477         GetDirectionalIntraPredictorDerivative(180 - prediction_angle);
478     const int dy =
479         GetDirectionalIntraPredictorDerivative(prediction_angle - 90);
480     dsp_.directional_intra_predictor_zone2(dest, stride, top_row, left_column,
481                                            width, height, dx, dy, upsampled_top,
482                                            upsampled_left);
483   } else {
484     assert(prediction_angle < 270);
485     const int dy =
486         GetDirectionalIntraPredictorDerivative(270 - prediction_angle);
487     dsp_.directional_intra_predictor_zone3(dest, stride, left_column, width,
488                                            height, dy, upsampled_left);
489   }
490 }
491 
492 template <typename Pixel>
PalettePrediction(const Block & block,const Plane plane,const int start_x,const int start_y,const int x,const int y,const TransformSize tx_size)493 void Tile::PalettePrediction(const Block& block, const Plane plane,
494                              const int start_x, const int start_y, const int x,
495                              const int y, const TransformSize tx_size) {
496   const int tx_width = kTransformWidth[tx_size];
497   const int tx_height = kTransformHeight[tx_size];
498   const uint16_t* const palette =
499       block.bp->prediction_parameters->palette_mode_info.color[plane];
500   const PlaneType plane_type = GetPlaneType(plane);
501   const int x4 = MultiplyBy4(x);
502   const int y4 = MultiplyBy4(y);
503   Array2DView<Pixel> buffer(buffer_[plane].rows(),
504                             buffer_[plane].columns() / sizeof(Pixel),
505                             reinterpret_cast<Pixel*>(&buffer_[plane][0][0]));
506   for (int row = 0; row < tx_height; ++row) {
507     assert(block.bp->prediction_parameters
508                ->color_index_map[plane_type][y4 + row] != nullptr);
509     for (int column = 0; column < tx_width; ++column) {
510       buffer[start_y + row][start_x + column] =
511           palette[block.bp->prediction_parameters
512                       ->color_index_map[plane_type][y4 + row][x4 + column]];
513     }
514   }
515 }
516 
517 template void Tile::PalettePrediction<uint8_t>(
518     const Block& block, const Plane plane, const int start_x, const int start_y,
519     const int x, const int y, const TransformSize tx_size);
520 #if LIBGAV1_MAX_BITDEPTH >= 10
521 template void Tile::PalettePrediction<uint16_t>(
522     const Block& block, const Plane plane, const int start_x, const int start_y,
523     const int x, const int y, const TransformSize tx_size);
524 #endif
525 
526 template <typename Pixel>
ChromaFromLumaPrediction(const Block & block,const Plane plane,const int start_x,const int start_y,const TransformSize tx_size)527 void Tile::ChromaFromLumaPrediction(const Block& block, const Plane plane,
528                                     const int start_x, const int start_y,
529                                     const TransformSize tx_size) {
530   const int subsampling_x = subsampling_x_[plane];
531   const int subsampling_y = subsampling_y_[plane];
532   const PredictionParameters& prediction_parameters =
533       *block.bp->prediction_parameters;
534   Array2DView<Pixel> y_buffer(
535       buffer_[kPlaneY].rows(), buffer_[kPlaneY].columns() / sizeof(Pixel),
536       reinterpret_cast<Pixel*>(&buffer_[kPlaneY][0][0]));
537   if (!block.scratch_buffer->cfl_luma_buffer_valid) {
538     const int luma_x = start_x << subsampling_x;
539     const int luma_y = start_y << subsampling_y;
540     dsp_.cfl_subsamplers[tx_size][subsampling_x + subsampling_y](
541         block.scratch_buffer->cfl_luma_buffer,
542         prediction_parameters.max_luma_width - luma_x,
543         prediction_parameters.max_luma_height - luma_y,
544         reinterpret_cast<uint8_t*>(&y_buffer[luma_y][luma_x]),
545         buffer_[kPlaneY].columns());
546     block.scratch_buffer->cfl_luma_buffer_valid = true;
547   }
548   Array2DView<Pixel> buffer(buffer_[plane].rows(),
549                             buffer_[plane].columns() / sizeof(Pixel),
550                             reinterpret_cast<Pixel*>(&buffer_[plane][0][0]));
551   dsp_.cfl_intra_predictors[tx_size](
552       reinterpret_cast<uint8_t*>(&buffer[start_y][start_x]),
553       buffer_[plane].columns(), block.scratch_buffer->cfl_luma_buffer,
554       (plane == kPlaneU) ? prediction_parameters.cfl_alpha_u
555                          : prediction_parameters.cfl_alpha_v);
556 }
557 
558 template void Tile::ChromaFromLumaPrediction<uint8_t>(
559     const Block& block, const Plane plane, const int start_x, const int start_y,
560     const TransformSize tx_size);
561 #if LIBGAV1_MAX_BITDEPTH >= 10
562 template void Tile::ChromaFromLumaPrediction<uint16_t>(
563     const Block& block, const Plane plane, const int start_x, const int start_y,
564     const TransformSize tx_size);
565 #endif
566 
InterIntraPrediction(uint16_t * const prediction_0,const uint8_t * const prediction_mask,const ptrdiff_t prediction_mask_stride,const PredictionParameters & prediction_parameters,const int prediction_width,const int prediction_height,const int subsampling_x,const int subsampling_y,uint8_t * const dest,const ptrdiff_t dest_stride)567 void Tile::InterIntraPrediction(
568     uint16_t* const prediction_0, const uint8_t* const prediction_mask,
569     const ptrdiff_t prediction_mask_stride,
570     const PredictionParameters& prediction_parameters,
571     const int prediction_width, const int prediction_height,
572     const int subsampling_x, const int subsampling_y, uint8_t* const dest,
573     const ptrdiff_t dest_stride) {
574   assert(prediction_mask != nullptr);
575   assert(prediction_parameters.compound_prediction_type ==
576              kCompoundPredictionTypeIntra ||
577          prediction_parameters.compound_prediction_type ==
578              kCompoundPredictionTypeWedge);
579   // The first buffer of InterIntra is from inter prediction.
580   // The second buffer is from intra prediction.
581 #if LIBGAV1_MAX_BITDEPTH >= 10
582   if (sequence_header_.color_config.bitdepth > 8) {
583     GetMaskBlendFunc(dsp_, /*is_inter_intra=*/true,
584                      prediction_parameters.is_wedge_inter_intra, subsampling_x,
585                      subsampling_y)(
586         prediction_0, reinterpret_cast<uint16_t*>(dest),
587         dest_stride / sizeof(uint16_t), prediction_mask, prediction_mask_stride,
588         prediction_width, prediction_height, dest, dest_stride);
589     return;
590   }
591 #endif
592   const int function_index = prediction_parameters.is_wedge_inter_intra
593                                  ? subsampling_x + subsampling_y
594                                  : 0;
595   // |is_inter_intra| prediction values are stored in a Pixel buffer but it is
596   // currently declared as a uint16_t buffer.
597   // TODO(johannkoenig): convert the prediction buffer to a uint8_t buffer and
598   // remove the reinterpret_cast.
599   dsp_.inter_intra_mask_blend_8bpp[function_index](
600       reinterpret_cast<uint8_t*>(prediction_0), dest, dest_stride,
601       prediction_mask, prediction_mask_stride, prediction_width,
602       prediction_height);
603 }
604 
CompoundInterPrediction(const Block & block,const uint8_t * const prediction_mask,const ptrdiff_t prediction_mask_stride,const int prediction_width,const int prediction_height,const int subsampling_x,const int subsampling_y,const int candidate_row,const int candidate_column,uint8_t * dest,const ptrdiff_t dest_stride)605 void Tile::CompoundInterPrediction(
606     const Block& block, const uint8_t* const prediction_mask,
607     const ptrdiff_t prediction_mask_stride, const int prediction_width,
608     const int prediction_height, const int subsampling_x,
609     const int subsampling_y, const int candidate_row,
610     const int candidate_column, uint8_t* dest, const ptrdiff_t dest_stride) {
611   const PredictionParameters& prediction_parameters =
612       *block.bp->prediction_parameters;
613 
614   void* prediction[2];
615 #if LIBGAV1_MAX_BITDEPTH >= 10
616   const int bitdepth = sequence_header_.color_config.bitdepth;
617   if (bitdepth > 8) {
618     prediction[0] = block.scratch_buffer->prediction_buffer[0];
619     prediction[1] = block.scratch_buffer->prediction_buffer[1];
620   } else {
621 #endif
622     prediction[0] = block.scratch_buffer->compound_prediction_buffer_8bpp[0];
623     prediction[1] = block.scratch_buffer->compound_prediction_buffer_8bpp[1];
624 #if LIBGAV1_MAX_BITDEPTH >= 10
625   }
626 #endif
627 
628   switch (prediction_parameters.compound_prediction_type) {
629     case kCompoundPredictionTypeWedge:
630     case kCompoundPredictionTypeDiffWeighted:
631       GetMaskBlendFunc(dsp_, /*is_inter_intra=*/false,
632                        prediction_parameters.is_wedge_inter_intra,
633                        subsampling_x, subsampling_y)(
634           prediction[0], prediction[1],
635           /*prediction_stride=*/prediction_width, prediction_mask,
636           prediction_mask_stride, prediction_width, prediction_height, dest,
637           dest_stride);
638       break;
639     case kCompoundPredictionTypeDistance:
640       DistanceWeightedPrediction(prediction[0], prediction[1], prediction_width,
641                                  prediction_height, candidate_row,
642                                  candidate_column, dest, dest_stride);
643       break;
644     default:
645       assert(prediction_parameters.compound_prediction_type ==
646              kCompoundPredictionTypeAverage);
647       dsp_.average_blend(prediction[0], prediction[1], prediction_width,
648                          prediction_height, dest, dest_stride);
649       break;
650   }
651 }
652 
GetWarpParams(const Block & block,const Plane plane,const int prediction_width,const int prediction_height,const PredictionParameters & prediction_parameters,const ReferenceFrameType reference_type,bool * const is_local_valid,GlobalMotion * const global_motion_params,GlobalMotion * const local_warp_params) const653 GlobalMotion* Tile::GetWarpParams(
654     const Block& block, const Plane plane, const int prediction_width,
655     const int prediction_height,
656     const PredictionParameters& prediction_parameters,
657     const ReferenceFrameType reference_type, bool* const is_local_valid,
658     GlobalMotion* const global_motion_params,
659     GlobalMotion* const local_warp_params) const {
660   if (prediction_width < 8 || prediction_height < 8 ||
661       frame_header_.force_integer_mv == 1) {
662     return nullptr;
663   }
664   if (plane == kPlaneY) {
665     *is_local_valid =
666         prediction_parameters.motion_mode == kMotionModeLocalWarp &&
667         WarpEstimation(
668             prediction_parameters.num_warp_samples, DivideBy4(prediction_width),
669             DivideBy4(prediction_height), block.row4x4, block.column4x4,
670             block.bp->mv.mv[0], prediction_parameters.warp_estimate_candidates,
671             local_warp_params) &&
672         SetupShear(local_warp_params);
673   }
674   if (prediction_parameters.motion_mode == kMotionModeLocalWarp &&
675       *is_local_valid) {
676     return local_warp_params;
677   }
678   if (!IsScaled(reference_type)) {
679     GlobalMotionTransformationType global_motion_type =
680         (reference_type != kReferenceFrameIntra)
681             ? global_motion_params->type
682             : kNumGlobalMotionTransformationTypes;
683     const bool is_global_valid =
684         IsGlobalMvBlock(*block.bp, global_motion_type) &&
685         SetupShear(global_motion_params);
686     // Valid global motion type implies reference type can't be intra.
687     assert(!is_global_valid || reference_type != kReferenceFrameIntra);
688     if (is_global_valid) return global_motion_params;
689   }
690   return nullptr;
691 }
692 
InterPrediction(const Block & block,const Plane plane,const int x,const int y,const int prediction_width,const int prediction_height,int candidate_row,int candidate_column,bool * const is_local_valid,GlobalMotion * const local_warp_params)693 bool Tile::InterPrediction(const Block& block, const Plane plane, const int x,
694                            const int y, const int prediction_width,
695                            const int prediction_height, int candidate_row,
696                            int candidate_column, bool* const is_local_valid,
697                            GlobalMotion* const local_warp_params) {
698   const int bitdepth = sequence_header_.color_config.bitdepth;
699   const BlockParameters& bp = *block.bp;
700   const BlockParameters& bp_reference =
701       *block_parameters_holder_.Find(candidate_row, candidate_column);
702   const bool is_compound =
703       bp_reference.reference_frame[1] > kReferenceFrameIntra;
704   assert(bp.is_inter);
705   const bool is_inter_intra = bp.reference_frame[1] == kReferenceFrameIntra;
706 
707   const PredictionParameters& prediction_parameters =
708       *block.bp->prediction_parameters;
709   uint8_t* const dest = GetStartPoint(buffer_, plane, x, y, bitdepth);
710   const ptrdiff_t dest_stride = buffer_[plane].columns();  // In bytes.
711   for (int index = 0; index < 1 + static_cast<int>(is_compound); ++index) {
712     const ReferenceFrameType reference_type =
713         bp_reference.reference_frame[index];
714     GlobalMotion global_motion_params =
715         frame_header_.global_motion[reference_type];
716     GlobalMotion* warp_params =
717         GetWarpParams(block, plane, prediction_width, prediction_height,
718                       prediction_parameters, reference_type, is_local_valid,
719                       &global_motion_params, local_warp_params);
720     if (warp_params != nullptr) {
721       if (!BlockWarpProcess(block, plane, index, x, y, prediction_width,
722                             prediction_height, warp_params, is_compound,
723                             is_inter_intra, dest, dest_stride)) {
724         return false;
725       }
726     } else {
727       const int reference_index =
728           prediction_parameters.use_intra_block_copy
729               ? -1
730               : frame_header_.reference_frame_index[reference_type -
731                                                     kReferenceFrameLast];
732       if (!BlockInterPrediction(
733               block, plane, reference_index, bp_reference.mv.mv[index], x, y,
734               prediction_width, prediction_height, candidate_row,
735               candidate_column, block.scratch_buffer->prediction_buffer[index],
736               is_compound, is_inter_intra, dest, dest_stride)) {
737         return false;
738       }
739     }
740   }
741 
742   const int subsampling_x = subsampling_x_[plane];
743   const int subsampling_y = subsampling_y_[plane];
744   ptrdiff_t prediction_mask_stride = 0;
745   const uint8_t* prediction_mask = nullptr;
746   if (prediction_parameters.compound_prediction_type ==
747       kCompoundPredictionTypeWedge) {
748     const Array2D<uint8_t>& wedge_mask =
749         wedge_masks_[GetWedgeBlockSizeIndex(block.size)]
750                     [prediction_parameters.wedge_sign]
751                     [prediction_parameters.wedge_index];
752     prediction_mask = wedge_mask[0];
753     prediction_mask_stride = wedge_mask.columns();
754   } else if (prediction_parameters.compound_prediction_type ==
755              kCompoundPredictionTypeIntra) {
756     // 7.11.3.13. The inter intra masks are precomputed and stored as a set of
757     // look up tables.
758     assert(prediction_parameters.inter_intra_mode < kNumInterIntraModes);
759     prediction_mask =
760         kInterIntraMasks[prediction_parameters.inter_intra_mode]
761                         [GetInterIntraMaskLookupIndex(prediction_width)]
762                         [GetInterIntraMaskLookupIndex(prediction_height)];
763     prediction_mask_stride = prediction_width;
764   } else if (prediction_parameters.compound_prediction_type ==
765              kCompoundPredictionTypeDiffWeighted) {
766     if (plane == kPlaneY) {
767       assert(prediction_width >= 8);
768       assert(prediction_height >= 8);
769       dsp_.weight_mask[FloorLog2(prediction_width) - 3]
770                       [FloorLog2(prediction_height) - 3]
771                       [static_cast<int>(prediction_parameters.mask_is_inverse)](
772                           block.scratch_buffer->prediction_buffer[0],
773                           block.scratch_buffer->prediction_buffer[1],
774                           block.scratch_buffer->weight_mask, block.width);
775     }
776     prediction_mask = block.scratch_buffer->weight_mask;
777     prediction_mask_stride = block.width;
778   }
779 
780   if (is_compound) {
781     CompoundInterPrediction(block, prediction_mask, prediction_mask_stride,
782                             prediction_width, prediction_height, subsampling_x,
783                             subsampling_y, candidate_row, candidate_column,
784                             dest, dest_stride);
785   } else if (prediction_parameters.motion_mode == kMotionModeObmc) {
786     // Obmc mode is allowed only for single reference (!is_compound).
787     return ObmcPrediction(block, plane, prediction_width, prediction_height);
788   } else if (is_inter_intra) {
789     // InterIntra and obmc must be mutually exclusive.
790     InterIntraPrediction(
791         block.scratch_buffer->prediction_buffer[0], prediction_mask,
792         prediction_mask_stride, prediction_parameters, prediction_width,
793         prediction_height, subsampling_x, subsampling_y, dest, dest_stride);
794   }
795   return true;
796 }
797 
ObmcBlockPrediction(const Block & block,const MotionVector & mv,const Plane plane,const int reference_frame_index,const int width,const int height,const int x,const int y,const int candidate_row,const int candidate_column,const ObmcDirection blending_direction)798 bool Tile::ObmcBlockPrediction(const Block& block, const MotionVector& mv,
799                                const Plane plane,
800                                const int reference_frame_index, const int width,
801                                const int height, const int x, const int y,
802                                const int candidate_row,
803                                const int candidate_column,
804                                const ObmcDirection blending_direction) {
805   const int bitdepth = sequence_header_.color_config.bitdepth;
806   // Obmc's prediction needs to be clipped before blending with above/left
807   // prediction blocks.
808   // Obmc prediction is used only when is_compound is false. So it is safe to
809   // use prediction_buffer[1] as a temporary buffer for the Obmc prediction.
810   static_assert(sizeof(block.scratch_buffer->prediction_buffer[1]) >=
811                     64 * 64 * sizeof(uint16_t),
812                 "");
813   auto* const obmc_buffer =
814       reinterpret_cast<uint8_t*>(block.scratch_buffer->prediction_buffer[1]);
815   const ptrdiff_t obmc_buffer_stride =
816       (bitdepth == 8) ? width : width * sizeof(uint16_t);
817   if (!BlockInterPrediction(block, plane, reference_frame_index, mv, x, y,
818                             width, height, candidate_row, candidate_column,
819                             nullptr, false, false, obmc_buffer,
820                             obmc_buffer_stride)) {
821     return false;
822   }
823 
824   uint8_t* const prediction = GetStartPoint(buffer_, plane, x, y, bitdepth);
825   const ptrdiff_t prediction_stride = buffer_[plane].columns();
826   dsp_.obmc_blend[blending_direction](prediction, prediction_stride, width,
827                                       height, obmc_buffer, obmc_buffer_stride);
828   return true;
829 }
830 
ObmcPrediction(const Block & block,const Plane plane,const int width,const int height)831 bool Tile::ObmcPrediction(const Block& block, const Plane plane,
832                           const int width, const int height) {
833   const int subsampling_x = subsampling_x_[plane];
834   const int subsampling_y = subsampling_y_[plane];
835   if (block.top_available[kPlaneY] &&
836       !IsBlockSmallerThan8x8(block.residual_size[plane])) {
837     const int num_limit = std::min(uint8_t{4}, k4x4WidthLog2[block.size]);
838     const int column4x4_max =
839         std::min(block.column4x4 + block.width4x4, frame_header_.columns4x4);
840     const int candidate_row = block.row4x4 - 1;
841     const int block_start_y = MultiplyBy4(block.row4x4) >> subsampling_y;
842     int column4x4 = block.column4x4;
843     const int prediction_height = std::min(height >> 1, 32 >> subsampling_y);
844     for (int i = 0, step; i < num_limit && column4x4 < column4x4_max;
845          column4x4 += step) {
846       const int candidate_column = column4x4 | 1;
847       const BlockParameters& bp_top =
848           *block_parameters_holder_.Find(candidate_row, candidate_column);
849       const int candidate_block_size = bp_top.size;
850       step = Clip3(kNum4x4BlocksWide[candidate_block_size], 2, 16);
851       if (bp_top.reference_frame[0] > kReferenceFrameIntra) {
852         i++;
853         const int candidate_reference_frame_index =
854             frame_header_.reference_frame_index[bp_top.reference_frame[0] -
855                                                 kReferenceFrameLast];
856         const int prediction_width =
857             std::min(width, MultiplyBy4(step) >> subsampling_x);
858         if (!ObmcBlockPrediction(
859                 block, bp_top.mv.mv[0], plane, candidate_reference_frame_index,
860                 prediction_width, prediction_height,
861                 MultiplyBy4(column4x4) >> subsampling_x, block_start_y,
862                 candidate_row, candidate_column, kObmcDirectionVertical)) {
863           return false;
864         }
865       }
866     }
867   }
868 
869   if (block.left_available[kPlaneY]) {
870     const int num_limit = std::min(uint8_t{4}, k4x4HeightLog2[block.size]);
871     const int row4x4_max =
872         std::min(block.row4x4 + block.height4x4, frame_header_.rows4x4);
873     const int candidate_column = block.column4x4 - 1;
874     int row4x4 = block.row4x4;
875     const int block_start_x = MultiplyBy4(block.column4x4) >> subsampling_x;
876     const int prediction_width = std::min(width >> 1, 32 >> subsampling_x);
877     for (int i = 0, step; i < num_limit && row4x4 < row4x4_max;
878          row4x4 += step) {
879       const int candidate_row = row4x4 | 1;
880       const BlockParameters& bp_left =
881           *block_parameters_holder_.Find(candidate_row, candidate_column);
882       const int candidate_block_size = bp_left.size;
883       step = Clip3(kNum4x4BlocksHigh[candidate_block_size], 2, 16);
884       if (bp_left.reference_frame[0] > kReferenceFrameIntra) {
885         i++;
886         const int candidate_reference_frame_index =
887             frame_header_.reference_frame_index[bp_left.reference_frame[0] -
888                                                 kReferenceFrameLast];
889         const int prediction_height =
890             std::min(height, MultiplyBy4(step) >> subsampling_y);
891         if (!ObmcBlockPrediction(
892                 block, bp_left.mv.mv[0], plane, candidate_reference_frame_index,
893                 prediction_width, prediction_height, block_start_x,
894                 MultiplyBy4(row4x4) >> subsampling_y, candidate_row,
895                 candidate_column, kObmcDirectionHorizontal)) {
896           return false;
897         }
898       }
899     }
900   }
901   return true;
902 }
903 
DistanceWeightedPrediction(void * prediction_0,void * prediction_1,const int width,const int height,const int candidate_row,const int candidate_column,uint8_t * dest,ptrdiff_t dest_stride)904 void Tile::DistanceWeightedPrediction(void* prediction_0, void* prediction_1,
905                                       const int width, const int height,
906                                       const int candidate_row,
907                                       const int candidate_column, uint8_t* dest,
908                                       ptrdiff_t dest_stride) {
909   int distance[2];
910   int weight[2];
911   for (int reference = 0; reference < 2; ++reference) {
912     const BlockParameters& bp =
913         *block_parameters_holder_.Find(candidate_row, candidate_column);
914     // Note: distance[0] and distance[1] correspond to relative distance
915     // between current frame and reference frame [1] and [0], respectively.
916     distance[1 - reference] = std::min(
917         std::abs(static_cast<int>(
918             current_frame_.reference_info()
919                 ->relative_distance_from[bp.reference_frame[reference]])),
920         static_cast<int>(kMaxFrameDistance));
921   }
922   GetDistanceWeights(distance, weight);
923 
924   dsp_.distance_weighted_blend(prediction_0, prediction_1, weight[0], weight[1],
925                                width, height, dest, dest_stride);
926 }
927 
ScaleMotionVector(const MotionVector & mv,const Plane plane,const int reference_frame_index,const int x,const int y,int * const start_x,int * const start_y,int * const step_x,int * const step_y)928 void Tile::ScaleMotionVector(const MotionVector& mv, const Plane plane,
929                              const int reference_frame_index, const int x,
930                              const int y, int* const start_x,
931                              int* const start_y, int* const step_x,
932                              int* const step_y) {
933   const int reference_upscaled_width =
934       (reference_frame_index == -1)
935           ? frame_header_.upscaled_width
936           : reference_frames_[reference_frame_index]->upscaled_width();
937   const int reference_height =
938       (reference_frame_index == -1)
939           ? frame_header_.height
940           : reference_frames_[reference_frame_index]->frame_height();
941   assert(2 * frame_header_.width >= reference_upscaled_width &&
942          2 * frame_header_.height >= reference_height &&
943          frame_header_.width <= 16 * reference_upscaled_width &&
944          frame_header_.height <= 16 * reference_height);
945   const bool is_scaled_x = reference_upscaled_width != frame_header_.width;
946   const bool is_scaled_y = reference_height != frame_header_.height;
947   const int half_sample = 1 << (kSubPixelBits - 1);
948   int orig_x = (x << kSubPixelBits) + ((2 * mv.mv[1]) >> subsampling_x_[plane]);
949   int orig_y = (y << kSubPixelBits) + ((2 * mv.mv[0]) >> subsampling_y_[plane]);
950   const int rounding_offset =
951       DivideBy2(1 << (kScaleSubPixelBits - kSubPixelBits));
952   if (is_scaled_x) {
953     const int scale_x = ((reference_upscaled_width << kReferenceScaleShift) +
954                          DivideBy2(frame_header_.width)) /
955                         frame_header_.width;
956     *step_x = RightShiftWithRoundingSigned(
957         scale_x, kReferenceScaleShift - kScaleSubPixelBits);
958     orig_x += half_sample;
959     // When frame size is 4k and above, orig_x can be above 16 bits, scale_x can
960     // be up to 15 bits. So we use int64_t to hold base_x.
961     const int64_t base_x = static_cast<int64_t>(orig_x) * scale_x -
962                            (half_sample << kReferenceScaleShift);
963     *start_x =
964         RightShiftWithRoundingSigned(
965             base_x, kReferenceScaleShift + kSubPixelBits - kScaleSubPixelBits) +
966         rounding_offset;
967   } else {
968     *step_x = 1 << kScaleSubPixelBits;
969     *start_x = LeftShift(orig_x, 6) + rounding_offset;
970   }
971   if (is_scaled_y) {
972     const int scale_y = ((reference_height << kReferenceScaleShift) +
973                          DivideBy2(frame_header_.height)) /
974                         frame_header_.height;
975     *step_y = RightShiftWithRoundingSigned(
976         scale_y, kReferenceScaleShift - kScaleSubPixelBits);
977     orig_y += half_sample;
978     const int64_t base_y = static_cast<int64_t>(orig_y) * scale_y -
979                            (half_sample << kReferenceScaleShift);
980     *start_y =
981         RightShiftWithRoundingSigned(
982             base_y, kReferenceScaleShift + kSubPixelBits - kScaleSubPixelBits) +
983         rounding_offset;
984   } else {
985     *step_y = 1 << kScaleSubPixelBits;
986     *start_y = LeftShift(orig_y, 6) + rounding_offset;
987   }
988 }
989 
990 // static.
GetReferenceBlockPosition(const int reference_frame_index,const bool is_scaled,const int width,const int height,const int ref_start_x,const int ref_last_x,const int ref_start_y,const int ref_last_y,const int start_x,const int start_y,const int step_x,const int step_y,const int left_border,const int right_border,const int top_border,const int bottom_border,int * ref_block_start_x,int * ref_block_start_y,int * ref_block_end_x,int * ref_block_end_y)991 bool Tile::GetReferenceBlockPosition(
992     const int reference_frame_index, const bool is_scaled, const int width,
993     const int height, const int ref_start_x, const int ref_last_x,
994     const int ref_start_y, const int ref_last_y, const int start_x,
995     const int start_y, const int step_x, const int step_y,
996     const int left_border, const int right_border, const int top_border,
997     const int bottom_border, int* ref_block_start_x, int* ref_block_start_y,
998     int* ref_block_end_x, int* ref_block_end_y) {
999   *ref_block_start_x = GetPixelPositionFromHighScale(start_x, 0, 0);
1000   *ref_block_start_y = GetPixelPositionFromHighScale(start_y, 0, 0);
1001   if (reference_frame_index == -1) {
1002     return false;
1003   }
1004   *ref_block_start_x -= kConvolveBorderLeftTop;
1005   *ref_block_start_y -= kConvolveBorderLeftTop;
1006   *ref_block_end_x = GetPixelPositionFromHighScale(start_x, step_x, width - 1) +
1007                      kConvolveBorderRight;
1008   *ref_block_end_y =
1009       GetPixelPositionFromHighScale(start_y, step_y, height - 1) +
1010       kConvolveBorderBottom;
1011   if (is_scaled) {
1012     const int block_height =
1013         (((height - 1) * step_y + (1 << kScaleSubPixelBits) - 1) >>
1014          kScaleSubPixelBits) +
1015         kSubPixelTaps;
1016     *ref_block_end_x += kConvolveScaleBorderRight - kConvolveBorderRight;
1017     *ref_block_end_y = *ref_block_start_y + block_height - 1;
1018   }
1019   // Determines if we need to extend beyond the left/right/top/bottom border.
1020   return *ref_block_start_x < (ref_start_x - left_border) ||
1021          *ref_block_end_x > (ref_last_x + right_border) ||
1022          *ref_block_start_y < (ref_start_y - top_border) ||
1023          *ref_block_end_y > (ref_last_y + bottom_border);
1024 }
1025 
1026 // Builds a block as the input for convolve, by copying the content of
1027 // reference frame (either a decoded reference frame, or current frame).
1028 // |block_extended_width| is the combined width of the block and its borders.
1029 template <typename Pixel>
BuildConvolveBlock(const Plane plane,const int reference_frame_index,const bool is_scaled,const int height,const int ref_start_x,const int ref_last_x,const int ref_start_y,const int ref_last_y,const int step_y,const int ref_block_start_x,const int ref_block_end_x,const int ref_block_start_y,uint8_t * block_buffer,ptrdiff_t convolve_buffer_stride,ptrdiff_t block_extended_width)1030 void Tile::BuildConvolveBlock(
1031     const Plane plane, const int reference_frame_index, const bool is_scaled,
1032     const int height, const int ref_start_x, const int ref_last_x,
1033     const int ref_start_y, const int ref_last_y, const int step_y,
1034     const int ref_block_start_x, const int ref_block_end_x,
1035     const int ref_block_start_y, uint8_t* block_buffer,
1036     ptrdiff_t convolve_buffer_stride, ptrdiff_t block_extended_width) {
1037   const YuvBuffer* const reference_buffer =
1038       (reference_frame_index == -1)
1039           ? current_frame_.buffer()
1040           : reference_frames_[reference_frame_index]->buffer();
1041   Array2DView<const Pixel> reference_block(
1042       reference_buffer->height(plane),
1043       reference_buffer->stride(plane) / sizeof(Pixel),
1044       reinterpret_cast<const Pixel*>(reference_buffer->data(plane)));
1045   auto* const block_head = reinterpret_cast<Pixel*>(block_buffer);
1046   convolve_buffer_stride /= sizeof(Pixel);
1047   int block_height = height + kConvolveBorderLeftTop + kConvolveBorderBottom;
1048   if (is_scaled) {
1049     block_height = (((height - 1) * step_y + (1 << kScaleSubPixelBits) - 1) >>
1050                     kScaleSubPixelBits) +
1051                    kSubPixelTaps;
1052   }
1053   const int copy_start_x = Clip3(ref_block_start_x, ref_start_x, ref_last_x);
1054   const int copy_start_y = Clip3(ref_block_start_y, ref_start_y, ref_last_y);
1055   const int copy_end_x = Clip3(ref_block_end_x, copy_start_x, ref_last_x);
1056   const int block_width = copy_end_x - copy_start_x + 1;
1057   const bool extend_left = ref_block_start_x < ref_start_x;
1058   const bool extend_right = ref_block_end_x > ref_last_x;
1059   const bool out_of_left = copy_start_x > ref_block_end_x;
1060   const bool out_of_right = copy_end_x < ref_block_start_x;
1061   if (out_of_left || out_of_right) {
1062     const int ref_x = out_of_left ? copy_start_x : copy_end_x;
1063     Pixel* buf_ptr = block_head;
1064     for (int y = 0, ref_y = copy_start_y; y < block_height; ++y) {
1065       Memset(buf_ptr, reference_block[ref_y][ref_x], block_extended_width);
1066       if (ref_block_start_y + y >= ref_start_y &&
1067           ref_block_start_y + y < ref_last_y) {
1068         ++ref_y;
1069       }
1070       buf_ptr += convolve_buffer_stride;
1071     }
1072   } else {
1073     Pixel* buf_ptr = block_head;
1074     const int left_width = copy_start_x - ref_block_start_x;
1075     for (int y = 0, ref_y = copy_start_y; y < block_height; ++y) {
1076       if (extend_left) {
1077         Memset(buf_ptr, reference_block[ref_y][copy_start_x], left_width);
1078       }
1079       memcpy(buf_ptr + left_width, &reference_block[ref_y][copy_start_x],
1080              block_width * sizeof(Pixel));
1081       if (extend_right) {
1082         Memset(buf_ptr + left_width + block_width,
1083                reference_block[ref_y][copy_end_x],
1084                block_extended_width - left_width - block_width);
1085       }
1086       if (ref_block_start_y + y >= ref_start_y &&
1087           ref_block_start_y + y < ref_last_y) {
1088         ++ref_y;
1089       }
1090       buf_ptr += convolve_buffer_stride;
1091     }
1092   }
1093 }
1094 
BlockInterPrediction(const Block & block,const Plane plane,const int reference_frame_index,const MotionVector & mv,const int x,const int y,const int width,const int height,const int candidate_row,const int candidate_column,uint16_t * const prediction,const bool is_compound,const bool is_inter_intra,uint8_t * const dest,const ptrdiff_t dest_stride)1095 bool Tile::BlockInterPrediction(
1096     const Block& block, const Plane plane, const int reference_frame_index,
1097     const MotionVector& mv, const int x, const int y, const int width,
1098     const int height, const int candidate_row, const int candidate_column,
1099     uint16_t* const prediction, const bool is_compound,
1100     const bool is_inter_intra, uint8_t* const dest,
1101     const ptrdiff_t dest_stride) {
1102   const BlockParameters& bp =
1103       *block_parameters_holder_.Find(candidate_row, candidate_column);
1104   int start_x;
1105   int start_y;
1106   int step_x;
1107   int step_y;
1108   ScaleMotionVector(mv, plane, reference_frame_index, x, y, &start_x, &start_y,
1109                     &step_x, &step_y);
1110   const int horizontal_filter_index = bp.interpolation_filter[1];
1111   const int vertical_filter_index = bp.interpolation_filter[0];
1112   const int subsampling_x = subsampling_x_[plane];
1113   const int subsampling_y = subsampling_y_[plane];
1114   // reference_frame_index equal to -1 indicates using current frame as
1115   // reference.
1116   const YuvBuffer* const reference_buffer =
1117       (reference_frame_index == -1)
1118           ? current_frame_.buffer()
1119           : reference_frames_[reference_frame_index]->buffer();
1120   const int reference_upscaled_width =
1121       (reference_frame_index == -1)
1122           ? MultiplyBy4(frame_header_.columns4x4)
1123           : reference_frames_[reference_frame_index]->upscaled_width();
1124   const int reference_height =
1125       (reference_frame_index == -1)
1126           ? MultiplyBy4(frame_header_.rows4x4)
1127           : reference_frames_[reference_frame_index]->frame_height();
1128   const int ref_start_x = 0;
1129   const int ref_last_x =
1130       SubsampledValue(reference_upscaled_width, subsampling_x) - 1;
1131   const int ref_start_y = 0;
1132   const int ref_last_y = SubsampledValue(reference_height, subsampling_y) - 1;
1133 
1134   const bool is_scaled = (reference_frame_index != -1) &&
1135                          (frame_header_.width != reference_upscaled_width ||
1136                           frame_header_.height != reference_height);
1137   const int bitdepth = sequence_header_.color_config.bitdepth;
1138   const int pixel_size = (bitdepth == 8) ? sizeof(uint8_t) : sizeof(uint16_t);
1139   int ref_block_start_x;
1140   int ref_block_start_y;
1141   int ref_block_end_x;
1142   int ref_block_end_y;
1143   const bool extend_block = GetReferenceBlockPosition(
1144       reference_frame_index, is_scaled, width, height, ref_start_x, ref_last_x,
1145       ref_start_y, ref_last_y, start_x, start_y, step_x, step_y,
1146       reference_buffer->left_border(plane),
1147       reference_buffer->right_border(plane),
1148       reference_buffer->top_border(plane),
1149       reference_buffer->bottom_border(plane), &ref_block_start_x,
1150       &ref_block_start_y, &ref_block_end_x, &ref_block_end_y);
1151 
1152   // In frame parallel mode, ensure that the reference block has been decoded
1153   // and available for referencing.
1154   if (reference_frame_index != -1 && frame_parallel_) {
1155     // For U and V planes with subsampling, we need to multiply the value of
1156     // ref_block_end_y by 2 since we only track the progress of the Y planes.
1157     const int reference_y_max = LeftShift(
1158         std::min(ref_block_end_y + kSubPixelTaps, ref_last_y), subsampling_y);
1159     if (reference_frame_progress_cache_[reference_frame_index] <
1160             reference_y_max &&
1161         !reference_frames_[reference_frame_index]->WaitUntil(
1162             reference_y_max,
1163             &reference_frame_progress_cache_[reference_frame_index])) {
1164       return false;
1165     }
1166   }
1167 
1168   const uint8_t* block_start = nullptr;
1169   ptrdiff_t convolve_buffer_stride;
1170   if (!extend_block) {
1171     const YuvBuffer* const reference_buffer =
1172         (reference_frame_index == -1)
1173             ? current_frame_.buffer()
1174             : reference_frames_[reference_frame_index]->buffer();
1175     convolve_buffer_stride = reference_buffer->stride(plane);
1176     if (reference_frame_index == -1 || is_scaled) {
1177       block_start = reference_buffer->data(plane) +
1178                     ref_block_start_y * reference_buffer->stride(plane) +
1179                     ref_block_start_x * pixel_size;
1180     } else {
1181       block_start = reference_buffer->data(plane) +
1182                     (ref_block_start_y + kConvolveBorderLeftTop) *
1183                         reference_buffer->stride(plane) +
1184                     (ref_block_start_x + kConvolveBorderLeftTop) * pixel_size;
1185     }
1186   } else {
1187     const int border_right =
1188         is_scaled ? kConvolveScaleBorderRight : kConvolveBorderRight;
1189     // The block width can be at most 2 times as much as current
1190     // block's width because of scaling.
1191     auto block_extended_width = Align<ptrdiff_t>(
1192         (2 * width + kConvolveBorderLeftTop + border_right) * pixel_size,
1193         kMaxAlignment);
1194     convolve_buffer_stride = block.scratch_buffer->convolve_block_buffer_stride;
1195 #if LIBGAV1_MAX_BITDEPTH >= 10
1196     if (bitdepth > 8) {
1197       BuildConvolveBlock<uint16_t>(
1198           plane, reference_frame_index, is_scaled, height, ref_start_x,
1199           ref_last_x, ref_start_y, ref_last_y, step_y, ref_block_start_x,
1200           ref_block_end_x, ref_block_start_y,
1201           block.scratch_buffer->convolve_block_buffer.get(),
1202           convolve_buffer_stride, block_extended_width);
1203     } else {
1204 #endif
1205       BuildConvolveBlock<uint8_t>(
1206           plane, reference_frame_index, is_scaled, height, ref_start_x,
1207           ref_last_x, ref_start_y, ref_last_y, step_y, ref_block_start_x,
1208           ref_block_end_x, ref_block_start_y,
1209           block.scratch_buffer->convolve_block_buffer.get(),
1210           convolve_buffer_stride, block_extended_width);
1211 #if LIBGAV1_MAX_BITDEPTH >= 10
1212     }
1213 #endif
1214     block_start = block.scratch_buffer->convolve_block_buffer.get() +
1215                   (is_scaled ? 0
1216                              : kConvolveBorderLeftTop * convolve_buffer_stride +
1217                                    kConvolveBorderLeftTop * pixel_size);
1218   }
1219 
1220   void* const output =
1221       (is_compound || is_inter_intra) ? prediction : static_cast<void*>(dest);
1222   ptrdiff_t output_stride = (is_compound || is_inter_intra)
1223                                 ? /*prediction_stride=*/width
1224                                 : dest_stride;
1225 #if LIBGAV1_MAX_BITDEPTH >= 10
1226   // |is_inter_intra| calculations are written to the |prediction| buffer.
1227   // Unlike the |is_compound| calculations the output is Pixel and not uint16_t.
1228   // convolve_func() expects |output_stride| to be in bytes and not Pixels.
1229   // |prediction_stride| is in units of uint16_t. Adjust |output_stride| to
1230   // account for this.
1231   if (is_inter_intra && sequence_header_.color_config.bitdepth > 8) {
1232     output_stride *= 2;
1233   }
1234 #endif
1235   assert(output != nullptr);
1236   if (is_scaled) {
1237     dsp::ConvolveScaleFunc convolve_func = dsp_.convolve_scale[is_compound];
1238     assert(convolve_func != nullptr);
1239 
1240     convolve_func(block_start, convolve_buffer_stride, horizontal_filter_index,
1241                   vertical_filter_index, start_x, start_y, step_x, step_y,
1242                   width, height, output, output_stride);
1243   } else {
1244     const int horizontal_filter_id = (start_x >> 6) & kSubPixelMask;
1245     const int vertical_filter_id = (start_y >> 6) & kSubPixelMask;
1246 
1247     dsp::ConvolveFunc convolve_func =
1248         dsp_.convolve[reference_frame_index == -1][is_compound]
1249                      [vertical_filter_id != 0][horizontal_filter_id != 0];
1250     assert(convolve_func != nullptr);
1251 
1252     convolve_func(block_start, convolve_buffer_stride, horizontal_filter_index,
1253                   vertical_filter_index, horizontal_filter_id,
1254                   vertical_filter_id, width, height, output, output_stride);
1255   }
1256   return true;
1257 }
1258 
BlockWarpProcess(const Block & block,const Plane plane,const int index,const int block_start_x,const int block_start_y,const int width,const int height,GlobalMotion * const warp_params,const bool is_compound,const bool is_inter_intra,uint8_t * const dest,const ptrdiff_t dest_stride)1259 bool Tile::BlockWarpProcess(const Block& block, const Plane plane,
1260                             const int index, const int block_start_x,
1261                             const int block_start_y, const int width,
1262                             const int height, GlobalMotion* const warp_params,
1263                             const bool is_compound, const bool is_inter_intra,
1264                             uint8_t* const dest, const ptrdiff_t dest_stride) {
1265   assert(width >= 8 && height >= 8);
1266   const BlockParameters& bp = *block.bp;
1267   const int reference_frame_index =
1268       frame_header_.reference_frame_index[bp.reference_frame[index] -
1269                                           kReferenceFrameLast];
1270   const uint8_t* const source =
1271       reference_frames_[reference_frame_index]->buffer()->data(plane);
1272   ptrdiff_t source_stride =
1273       reference_frames_[reference_frame_index]->buffer()->stride(plane);
1274   const int source_width =
1275       reference_frames_[reference_frame_index]->buffer()->width(plane);
1276   const int source_height =
1277       reference_frames_[reference_frame_index]->buffer()->height(plane);
1278   uint16_t* const prediction = block.scratch_buffer->prediction_buffer[index];
1279 
1280   // In frame parallel mode, ensure that the reference block has been decoded
1281   // and available for referencing.
1282   if (frame_parallel_) {
1283     int reference_y_max = -1;
1284     // Find out the maximum y-coordinate for warping.
1285     for (int start_y = block_start_y; start_y < block_start_y + height;
1286          start_y += 8) {
1287       for (int start_x = block_start_x; start_x < block_start_x + width;
1288            start_x += 8) {
1289         const int src_x = (start_x + 4) << subsampling_x_[plane];
1290         const int src_y = (start_y + 4) << subsampling_y_[plane];
1291         const int64_t dst_y =
1292             src_x * warp_params->params[4] +
1293             static_cast<int64_t>(src_y) * warp_params->params[5] +
1294             warp_params->params[1];
1295         const int64_t y4 = dst_y >> subsampling_y_[plane];
1296         const int iy4 = static_cast<int>(y4 >> kWarpedModelPrecisionBits);
1297         reference_y_max = std::max(iy4 + 8, reference_y_max);
1298       }
1299     }
1300     // For U and V planes with subsampling, we need to multiply reference_y_max
1301     // by 2 since we only track the progress of Y planes.
1302     reference_y_max = LeftShift(reference_y_max, subsampling_y_[plane]);
1303     if (reference_frame_progress_cache_[reference_frame_index] <
1304             reference_y_max &&
1305         !reference_frames_[reference_frame_index]->WaitUntil(
1306             reference_y_max,
1307             &reference_frame_progress_cache_[reference_frame_index])) {
1308       return false;
1309     }
1310   }
1311   if (is_compound) {
1312     dsp_.warp_compound(source, source_stride, source_width, source_height,
1313                        warp_params->params, subsampling_x_[plane],
1314                        subsampling_y_[plane], block_start_x, block_start_y,
1315                        width, height, warp_params->alpha, warp_params->beta,
1316                        warp_params->gamma, warp_params->delta, prediction,
1317                        /*prediction_stride=*/width);
1318   } else {
1319     void* const output = is_inter_intra ? static_cast<void*>(prediction) : dest;
1320     ptrdiff_t output_stride =
1321         is_inter_intra ? /*prediction_stride=*/width : dest_stride;
1322 #if LIBGAV1_MAX_BITDEPTH >= 10
1323     // |is_inter_intra| calculations are written to the |prediction| buffer.
1324     // Unlike the |is_compound| calculations the output is Pixel and not
1325     // uint16_t. warp_clip() expects |output_stride| to be in bytes and not
1326     // Pixels. |prediction_stride| is in units of uint16_t. Adjust
1327     // |output_stride| to account for this.
1328     if (is_inter_intra && sequence_header_.color_config.bitdepth > 8) {
1329       output_stride *= 2;
1330     }
1331 #endif
1332     dsp_.warp(source, source_stride, source_width, source_height,
1333               warp_params->params, subsampling_x_[plane], subsampling_y_[plane],
1334               block_start_x, block_start_y, width, height, warp_params->alpha,
1335               warp_params->beta, warp_params->gamma, warp_params->delta, output,
1336               output_stride);
1337   }
1338   return true;
1339 }
1340 
1341 }  // namespace libgav1
1342