encoder.rs 81.2 KB
Newer Older
rzumer's avatar
rzumer committed
1 2 3 4 5 6 7 8 9
// Copyright (c) 2018, The rav1e contributors. All rights reserved
//
// This source code is subject to the terms of the BSD 2 Clause License and
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
// was not distributed with this source code in the LICENSE file, you can
// obtain it at www.aomedia.org/license/software. If the Alliance for Open
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.

Raphaël Zumer's avatar
Raphaël Zumer committed
10 11 12 13
use crate::api::*;
use crate::cdef::*;
use crate::context::*;
use crate::deblock::*;
Luca Barbato's avatar
Luca Barbato committed
14
use crate::frame::*;
Raphaël Zumer's avatar
Raphaël Zumer committed
15 16 17 18 19 20
use crate::ec::*;
use crate::lrf::*;
use crate::me::*;
use crate::partition::*;
use crate::plane::*;
use crate::quantize::*;
21 22 23
use crate::rate::QuantizerParameters;
use crate::rate::FRAME_SUBTYPE_I;
use crate::rate::FRAME_SUBTYPE_P;
Raphaël Zumer's avatar
Raphaël Zumer committed
24 25
use crate::rdo::*;
use crate::segmentation::*;
Romain Vimont's avatar
Romain Vimont committed
26
use crate::tiling::*;
Raphaël Zumer's avatar
Raphaël Zumer committed
27 28 29
use crate::transform::*;
use crate::util::*;
use crate::partition::PartitionType::*;
Thomas Daede's avatar
Thomas Daede committed
30
use crate::partition::RefType::*;
31
use crate::header::*;
32

33
use arg_enum_proc_macro::ArgEnum;
34
use bitstream_io::{BitWriter, BigEndian};
35
use bincode::{serialize, deserialize};
Romain Vimont's avatar
Romain Vimont committed
36
use rayon::iter::*;
37
use std;
38
use std::{fmt, io, mem};
Josh Holmer's avatar
Josh Holmer committed
39
use std::io::Write;
40
use std::io::Read;
Raphaël Zumer's avatar
Raphaël Zumer committed
41
use std::sync::Arc;
42
use std::fs::File;
43
use arrayvec::*;
44

45 46
pub static TEMPORAL_DELIMITER: [u8; 2] = [0x12, 0x00];

Luca Barbato's avatar
Luca Barbato committed
47 48 49
const MAX_NUM_TEMPORAL_LAYERS: usize = 8;
const MAX_NUM_SPATIAL_LAYERS: usize = 4;
const MAX_NUM_OPERATING_POINTS: usize = MAX_NUM_TEMPORAL_LAYERS * MAX_NUM_SPATIAL_LAYERS;
50

51
#[derive(Debug, Clone)]
52
pub struct ReferenceFrame<T: Pixel> {
fbossen's avatar
fbossen committed
53
  pub order_hint: u32,
54 55 56
  pub frame: Frame<T>,
  pub input_hres: Plane<T>,
  pub input_qres: Plane<T>,
57
  pub cdfs: CDFContext,
Romain Vimont's avatar
Romain Vimont committed
58
  pub frame_mvs: Vec<FrameMotionVectors>,
59 60
}

61
#[derive(Debug, Clone, Default)]
62 63
pub struct ReferenceFramesSet<T: Pixel> {
  pub frames: [Option<Arc<ReferenceFrame<T>>>; (REF_FRAMES as usize)],
64
  pub deblock: [DeblockState; (REF_FRAMES as usize)]
65 66
}

67 68 69
impl<T: Pixel> ReferenceFramesSet<T> {
  pub fn new() -> Self {
    Self {
70 71
      frames: Default::default(),
      deblock: Default::default()
72
    }
73
  }
74 75
}

Luca Barbato's avatar
Luca Barbato committed
76

77

78 79 80 81 82
#[derive(ArgEnum, Copy, Clone, Debug, PartialEq)]
#[repr(C)]
pub enum Tune {
  Psnr,
  Psychovisual
83 84 85
}

impl Default for Tune {
86
  fn default() -> Self {
87
    Tune::Psychovisual
88
  }
89 90
}

91
#[derive(Copy, Clone, Debug)]
92
pub struct Sequence {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  // OBU Sequence header of AV1
  pub profile: u8,
  pub num_bits_width: u32,
  pub num_bits_height: u32,
  pub bit_depth: usize,
  pub chroma_sampling: ChromaSampling,
  pub chroma_sample_position: ChromaSamplePosition,
  pub pixel_range: PixelRange,
  pub color_description: Option<ColorDescription>,
  pub mastering_display: Option<MasteringDisplay>,
  pub content_light: Option<ContentLight>,
  pub max_frame_width: u32,
  pub max_frame_height: u32,
  pub frame_id_numbers_present_flag: bool,
  pub frame_id_length: u32,
  pub delta_frame_id_length: u32,
  pub use_128x128_superblock: bool,
  pub order_hint_bits_minus_1: u32,
  pub force_screen_content_tools: u32,  // 0 - force off
  // 1 - force on
  // 2 - adaptive
  pub force_integer_mv: u32,      // 0 - Not to force. MV can be in 1/4 or 1/8
  // 1 - force to integer
  // 2 - adaptive
  pub still_picture: bool,               // Video is a single frame still picture
  pub reduced_still_picture_hdr: bool,   // Use reduced header for still picture
119
  pub enable_filter_intra: bool,         // enables/disables filter_intra
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  pub enable_intra_edge_filter: bool,    // enables/disables corner/edge/upsampling
  pub enable_interintra_compound: bool,  // enables/disables interintra_compound
  pub enable_masked_compound: bool,      // enables/disables masked compound
  pub enable_dual_filter: bool,         // 0 - disable dual interpolation filter
  // 1 - enable vert/horiz filter selection
  pub enable_order_hint: bool,     // 0 - disable order hint, and related tools
  // jnt_comp, ref_frame_mvs, frame_sign_bias
  // if 0, enable_jnt_comp and
  // enable_ref_frame_mvs must be set zs 0.
  pub enable_jnt_comp: bool,        // 0 - disable joint compound modes
  // 1 - enable it
  pub enable_ref_frame_mvs: bool,  // 0 - disable ref frame mvs
  // 1 - enable it
  pub enable_warped_motion: bool,   // 0 - disable warped motion for sequence
  // 1 - enable it for the sequence
  pub enable_superres: bool,// 0 - Disable superres for the sequence, and disable
  //     transmitting per-frame superres enabled flag.
  // 1 - Enable superres for the sequence, and also
  //     enable per-frame flag to denote if superres is
  //     enabled for that frame.
  pub enable_cdef: bool,         // To turn on/off CDEF
  pub enable_restoration: bool,  // To turn on/off loop restoration
  pub operating_points_cnt_minus_1: usize,
  pub operating_point_idc: [u16; MAX_NUM_OPERATING_POINTS],
  pub display_model_info_present_flag: bool,
  pub decoder_model_info_present_flag: bool,
  pub level: [[usize; 2]; MAX_NUM_OPERATING_POINTS],	// minor, major
  pub tier: [usize; MAX_NUM_OPERATING_POINTS],  // seq_tier in the spec. One bit: 0
  // or 1.
  pub film_grain_params_present: bool,
  pub separate_uv_delta_q: bool,
151 152 153
}

impl Sequence {
154 155 156
  pub fn new(config: &EncoderConfig) -> Sequence {
    let width_bits = 32 - (config.width as u32).leading_zeros();
    let height_bits = 32 - (config.height as u32).leading_zeros();
157 158
    assert!(width_bits <= 16);
    assert!(height_bits <= 16);
159

160 161
    let profile = if config.bit_depth == 12 ||
      config.chroma_sampling == ChromaSampling::Cs422 {
162
      2
163
    } else if config.chroma_sampling == ChromaSampling::Cs444 {
164 165 166 167
      1
    } else {
      0
    };
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    let mut operating_point_idc = [0 as u16; MAX_NUM_OPERATING_POINTS];
    let mut level = [[1, 2 as usize]; MAX_NUM_OPERATING_POINTS];
    let mut tier = [0 as usize; MAX_NUM_OPERATING_POINTS];

    for i in 0..MAX_NUM_OPERATING_POINTS {
      operating_point_idc[i] = 0;
      level[i][0] = 1;    // minor
      level[i][1] = 2;    // major
      tier[i] = 0;
    }

    Sequence {
      profile,
      num_bits_width: width_bits,
      num_bits_height: height_bits,
184 185 186 187 188 189 190 191 192
      bit_depth: config.bit_depth,
      chroma_sampling: config.chroma_sampling,
      chroma_sample_position: config.chroma_sample_position,
      pixel_range: config.pixel_range,
      color_description: config.color_description,
      mastering_display: config.mastering_display,
      content_light: config.content_light,
      max_frame_width: config.width as u32,
      max_frame_height: config.height as u32,
193 194 195 196 197 198 199 200 201
      frame_id_numbers_present_flag: false,
      frame_id_length: 0,
      delta_frame_id_length: 0,
      use_128x128_superblock: false,
      order_hint_bits_minus_1: 5,
      force_screen_content_tools: 0,
      force_integer_mv: 2,
      still_picture: false,
      reduced_still_picture_hdr: false,
202
      enable_filter_intra: false,
203 204 205 206 207 208 209 210 211
      enable_intra_edge_filter: false,
      enable_interintra_compound: false,
      enable_masked_compound: false,
      enable_dual_filter: false,
      enable_order_hint: true,
      enable_jnt_comp: false,
      enable_ref_frame_mvs: false,
      enable_warped_motion: false,
      enable_superres: false,
212
      enable_cdef: config.speed_settings.cdef,
213 214
      enable_restoration: config.chroma_sampling != ChromaSampling::Cs422 &&
        config.chroma_sampling != ChromaSampling::Cs444, // FIXME: not working yet
215 216 217 218 219 220 221
      operating_points_cnt_minus_1: 0,
      operating_point_idc,
      display_model_info_present_flag: false,
      decoder_model_info_present_flag: false,
      level,
      tier,
      film_grain_params_present: false,
222
      separate_uv_delta_q: true,
223
    }
224
  }
fbossen's avatar
fbossen committed
225

226 227 228 229 230
  pub fn get_relative_dist(&self, a: u32, b: u32) -> i32 {
    let diff = a as i32 - b as i32;
    let m = 1 << self.order_hint_bits_minus_1;
    (diff & (m - 1)) - (diff & m)
  }
fbossen's avatar
fbossen committed
231

232
  pub fn get_skip_mode_allowed<T: Pixel>(&self, fi: &FrameInvariants<T>, reference_select: bool) -> bool {
233
    if fi.intra_only || !reference_select || !self.enable_order_hint {
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      return false;
    }

    let mut forward_idx: isize = -1;
    let mut backward_idx: isize = -1;
    let mut forward_hint = 0;
    let mut backward_hint = 0;

    for i in 0..INTER_REFS_PER_FRAME {
      if let Some(ref rec) = fi.rec_buffer.frames[fi.ref_frames[i] as usize] {
        let ref_hint = rec.order_hint;

        if self.get_relative_dist(ref_hint, fi.order_hint) < 0 {
          if forward_idx < 0 || self.get_relative_dist(ref_hint, forward_hint) > 0 {
            forward_idx = i as isize;
            forward_hint = ref_hint;
          }
        } else if self.get_relative_dist(ref_hint, fi.order_hint) > 0 &&
          (backward_idx < 0 || self.get_relative_dist(ref_hint, backward_hint) > 0) {
          backward_idx = i as isize;
          backward_hint = ref_hint;
        }
      }
    }

    if forward_idx < 0 {
260
      false
261 262 263
    } else if backward_idx >= 0 {
      // set skip_mode_frame
      true
264
    } else {
265 266 267
      let mut second_forward_idx: isize = -1;
      let mut second_forward_hint = 0;

268 269 270
      for i in 0..INTER_REFS_PER_FRAME {
        if let Some(ref rec) = fi.rec_buffer.frames[fi.ref_frames[i] as usize] {
          let ref_hint = rec.order_hint;
271 272 273 274 275

          if self.get_relative_dist(ref_hint, forward_hint) < 0 &&
            (second_forward_idx < 0 || self.get_relative_dist(ref_hint, second_forward_hint) > 0) {
              second_forward_idx = i as isize;
              second_forward_hint = ref_hint;
276 277 278
          }
        }
      }
279

280 281
      // TODO: Set skip_mode_frame, when second_forward_idx is not less than 0.
      second_forward_idx >= 0
fbossen's avatar
fbossen committed
282
    }
283
  }
284 285 286 287 288 289 290 291 292 293

  #[inline(always)]
  pub fn sb_size_log2(&self) -> usize {
    if self.use_128x128_superblock { 7 } else { 6 }
  }

  #[inline(always)]
  pub fn sb_size(&self) -> usize {
    1 << self.sb_size_log2()
  }
294 295 296
}

#[derive(Debug)]
297
pub struct FrameState<T: Pixel> {
298
  pub sb_size_log2: usize,
299 300 301 302
  pub input: Arc<Frame<T>>,
  pub input_hres: Plane<T>, // half-resolution version of input luma
  pub input_qres: Plane<T>, // quarter-resolution version of input luma
  pub rec: Frame<T>,
303
  pub cdfs: CDFContext,
304
  pub context_update_tile_id: usize, // tile id used for the CDFontext
305
  pub max_tile_size_bytes: u32,
306 307 308
  pub deblock: DeblockState,
  pub segmentation: SegmentationState,
  pub restoration: RestorationState,
Romain Vimont's avatar
Romain Vimont committed
309
  pub frame_mvs: Vec<FrameMotionVectors>,
310
  pub t: RDOTracker,
311 312
}

313 314
impl<T: Pixel> FrameState<T> {
  pub fn new(fi: &FrameInvariants<T>) -> Self {
315
    // TODO(negge): Use fi.cfg.chroma_sampling when we store VideoDetails in FrameInvariants
316
    FrameState::new_with_frame(fi, Arc::new(Frame::new(
317
      fi.width, fi.height, fi.sequence.chroma_sampling)))
318 319
  }

320
  pub fn new_with_frame(fi: &FrameInvariants<T>, frame: Arc<Frame<T>>) -> Self {
321
    let rs = RestorationState::new(fi, &frame);
Raphaël Zumer's avatar
Raphaël Zumer committed
322 323 324 325 326
    let luma_width = frame.planes[0].cfg.width;
    let luma_height = frame.planes[0].cfg.height;
    let luma_padding_x = frame.planes[0].cfg.xpad;
    let luma_padding_y = frame.planes[0].cfg.ypad;

327
    Self {
328
      sb_size_log2: fi.sb_size_log2(),
329
      input: frame,
Raphaël Zumer's avatar
Raphaël Zumer committed
330 331
      input_hres: Plane::new(luma_width / 2, luma_height / 2, 1, 1, luma_padding_x / 2, luma_padding_y / 2),
      input_qres: Plane::new(luma_width / 4, luma_height / 4, 2, 2, luma_padding_x / 4, luma_padding_y / 4),
332
      rec: Frame::new(luma_width, luma_height, fi.sequence.chroma_sampling),
333
      cdfs: CDFContext::new(0),
334
      context_update_tile_id: 0,
335
      max_tile_size_bytes: 0,
336 337 338
      deblock: Default::default(),
      segmentation: Default::default(),
      restoration: rs,
Romain Vimont's avatar
Romain Vimont committed
339 340 341 342 343 344 345
      frame_mvs: {
        let mut vec = Vec::with_capacity(REF_FRAMES);
        for _ in 0..REF_FRAMES {
          vec.push(FrameMotionVectors::new(fi.w_in_b, fi.h_in_b));
        }
        vec
      },
346
      t: RDOTracker::new()
347
    }
348
  }
349 350 351 352 353 354 355

  #[inline(always)]
  pub fn as_tile_state_mut(&mut self) -> TileStateMut<'_, T> {
    let PlaneConfig { width, height, .. } = self.rec.planes[0].cfg;
    let sbo_0 = SuperBlockOffset { x: 0, y: 0 };
    TileStateMut::new(self, sbo_0, self.sb_size_log2, width, height)
  }
356 357
}

358 359
#[derive(Copy, Clone, Debug)]
pub struct DeblockState {
360 361 362 363 364 365 366 367 368
  pub levels: [u8; PLANES+1],  // Y vertical edges, Y horizontal, U, V
  pub sharpness: u8,
  pub deltas_enabled: bool,
  pub delta_updates_enabled: bool,
  pub ref_deltas: [i8; REF_FRAMES],
  pub mode_deltas: [i8; 2],
  pub block_deltas_enabled: bool,
  pub block_delta_shift: u8,
  pub block_delta_multi: bool,
369 370 371
}

impl Default for DeblockState {
372 373 374 375 376 377 378 379 380 381 382
  fn default() -> Self {
    DeblockState {
      levels: [8,8,4,4],
      sharpness: 0,
      deltas_enabled: false, // requires delta_q_enabled
      delta_updates_enabled: false,
      ref_deltas: [1, 0, 0, 0, 0, -1, -1, -1],
      mode_deltas: [0, 0],
      block_deltas_enabled: false,
      block_delta_shift: 0,
      block_delta_multi: false
383
    }
384
  }
385 386
}

387 388
#[derive(Copy, Clone, Debug)]
pub struct SegmentationState {
389 390 391 392 393 394 395
  pub enabled: bool,
  pub update_data: bool,
  pub update_map: bool,
  pub preskip: bool,
  pub last_active_segid: u8,
  pub features: [[bool; SegLvl::SEG_LVL_MAX as usize]; 8],
  pub data: [[i16; SegLvl::SEG_LVL_MAX as usize]; 8],
396 397 398
}

impl Default for SegmentationState {
399 400 401 402 403 404 405 406 407
  fn default() -> Self {
    SegmentationState {
      enabled: false,
      update_data: false,
      update_map: false,
      preskip: true,
      last_active_segid: 0,
      features: [[false; SegLvl::SEG_LVL_MAX as usize]; 8],
      data: [[0; SegLvl::SEG_LVL_MAX as usize]; 8],
408
    }
409
  }
410 411
}

412 413
// Frame Invariants are invariant inside a frame
#[allow(dead_code)]
Josh Holmer's avatar
Josh Holmer committed
414
#[derive(Debug, Clone)]
415
pub struct FrameInvariants<T: Pixel> {
416 417 418 419 420 421 422
  pub sequence: Sequence,
  pub width: usize,
  pub height: usize,
  pub sb_width: usize,
  pub sb_height: usize,
  pub w_in_b: usize,
  pub h_in_b: usize,
423
  pub tiling: TilingInfo,
424 425 426 427 428 429 430 431 432 433 434 435 436 437
  pub number: u64,
  pub order_hint: u32,
  pub show_frame: bool,
  pub showable_frame: bool,
  pub error_resilient: bool,
  pub intra_only: bool,
  pub allow_high_precision_mv: bool,
  pub frame_type: FrameType,
  pub show_existing_frame: bool,
  pub frame_to_show_map_idx: u32,
  pub use_reduced_tx_set: bool,
  pub reference_mode: ReferenceMode,
  pub use_prev_frame_mvs: bool,
  pub min_partition_size: BlockSize,
Thomas Daede's avatar
Thomas Daede committed
438
  pub globalmv_transformation_type: [GlobalMVMode; INTER_REFS_PER_FRAME],
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  pub num_tg: usize,
  pub large_scale_tile: bool,
  pub disable_cdf_update: bool,
  pub allow_screen_content_tools: u32,
  pub force_integer_mv: u32,
  pub primary_ref_frame: u32,
  pub refresh_frame_flags: u32,  // a bitmask that specifies which
  // reference frame slots will be updated with the current frame
  // after it is decoded.
  pub allow_intrabc: bool,
  pub use_ref_frame_mvs: bool,
  pub is_filter_switchable: bool,
  pub is_motion_mode_switchable: bool,
  pub disable_frame_end_update_cdf: bool,
  pub allow_warped_motion: bool,
  pub cdef_damping: u8,
  pub cdef_bits: u8,
  pub cdef_y_strengths: [u8; 8],
  pub cdef_uv_strengths: [u8; 8],
  pub delta_q_present: bool,
  pub config: EncoderConfig,
  pub ref_frames: [u8; INTER_REFS_PER_FRAME],
  pub ref_frame_sign_bias: [bool; INTER_REFS_PER_FRAME],
462
  pub rec_buffer: ReferenceFramesSet<T>,
463 464 465
  pub base_q_idx: u8,
  pub dc_delta_q: [i8; 3],
  pub ac_delta_q: [i8; 3],
466 467
  pub lambda: f64,
  pub me_lambda: f64,
468 469
  pub me_range_scale: u8,
  pub use_tx_domain_distortion: bool,
470
  pub use_tx_domain_rate: bool,
471 472
  pub inter_cfg: Option<InterPropsConfig>,
  pub enable_early_exit: bool,
473
  pub tx_mode_select: bool,
474 475
}

476
pub(crate) fn pos_to_lvl(pos: u64, pyramid_depth: u64) -> u64 {
477 478 479 480 481 482 483 484 485 486
  // Derive level within pyramid for a frame with a given coding order position
  // For example, with a pyramid of depth 2, the 2 least significant bits of the
  // position determine the level:
  // 00 -> 0
  // 01 -> 2
  // 10 -> 1
  // 11 -> 2
  pyramid_depth - (pos | (1 << pyramid_depth)).trailing_zeros() as u64
}

487
impl<T: Pixel> FrameInvariants<T> {
488
  #[allow(clippy::erasing_op, clippy::identity_op)]
489 490
  pub fn new(config: EncoderConfig, sequence: Sequence) -> Self {
    assert!(sequence.bit_depth <= mem::size_of::<T>() * 8, "bit depth cannot fit into u8");
491 492 493
    // Speed level decides the minimum partition size, i.e. higher speed --> larger min partition size,
    // with exception that SBs on right or bottom frame borders split down to BLOCK_4X4.
    // At speed = 0, RDO search is exhaustive.
494
    let min_partition_size = config.speed_settings.min_block_size;
495
    assert!(min_partition_size.is_sqr());
496 497
    let use_reduced_tx_set = config.speed_settings.reduced_tx_set;
    let use_tx_domain_distortion = config.tune == Tune::Psnr && config.speed_settings.tx_domain_distortion;
498
    let use_tx_domain_rate = config.speed_settings.tx_domain_rate;
499

500 501 502
    let w_in_b = 2 * config.width.align_power_of_two_and_shift(3); // MiCols, ((width+7)/8)<<3 >> MI_SIZE_LOG2
    let h_in_b = 2 * config.height.align_power_of_two_and_shift(3); // MiRows, ((height+7)/8)<<3 >> MI_SIZE_LOG2

503 504 505 506 507 508 509 510
    let tiling = TilingInfo::new(
      sequence.sb_size_log2(),
      config.width,
      config.height,
      config.tile_cols_log2,
      config.tile_rows_log2
    );

511
    Self {
512
      sequence,
513 514 515 516
      width: config.width,
      height: config.height,
      sb_width: config.width.align_power_of_two_and_shift(6),
      sb_height: config.height.align_power_of_two_and_shift(6),
517 518
      w_in_b,
      h_in_b,
519
      tiling,
520 521 522 523 524 525 526 527 528 529 530 531 532 533
      number: 0,
      order_hint: 0,
      show_frame: true,
      showable_frame: true,
      error_resilient: false,
      intra_only: false,
      allow_high_precision_mv: false,
      frame_type: FrameType::KEY,
      show_existing_frame: false,
      frame_to_show_map_idx: 0,
      use_reduced_tx_set,
      reference_mode: ReferenceMode::SINGLE,
      use_prev_frame_mvs: false,
      min_partition_size,
Thomas Daede's avatar
Thomas Daede committed
534
      globalmv_transformation_type: [GlobalMVMode::IDENTITY; INTER_REFS_PER_FRAME],
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
      num_tg: 1,
      large_scale_tile: false,
      disable_cdf_update: false,
      allow_screen_content_tools: 0,
      force_integer_mv: 0,
      primary_ref_frame: PRIMARY_REF_NONE,
      refresh_frame_flags: 0,
      allow_intrabc: false,
      use_ref_frame_mvs: false,
      is_filter_switchable: false,
      is_motion_mode_switchable: false, // 0: only the SIMPLE motion mode will be used.
      disable_frame_end_update_cdf: false,
      allow_warped_motion: false,
      cdef_damping: 3,
      cdef_bits: 3,
      cdef_y_strengths: [0*4+0, 1*4+0, 2*4+1, 3*4+1, 5*4+2, 7*4+3, 10*4+3, 13*4+3],
      cdef_uv_strengths: [0*4+0, 1*4+0, 2*4+1, 3*4+1, 5*4+2, 7*4+3, 10*4+3, 13*4+3],
      delta_q_present: false,
      ref_frames: [0; INTER_REFS_PER_FRAME],
      ref_frame_sign_bias: [false; INTER_REFS_PER_FRAME],
      rec_buffer: ReferenceFramesSet::new(),
      base_q_idx: config.quantizer as u8,
      dc_delta_q: [0; 3],
      ac_delta_q: [0; 3],
559 560
      lambda: 0.0,
      me_lambda: 0.0,
561 562
      me_range_scale: 1,
      use_tx_domain_distortion,
563
      use_tx_domain_rate,
564 565
      inter_cfg: None,
      enable_early_exit: true,
566
      config,
567
      tx_mode_select : false,
Josh Holmer's avatar
Josh Holmer committed
568
    }
569
  }
Josh Holmer's avatar
Josh Holmer committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

  pub fn new_key_frame(previous_fi: &Self, segment_start_frame: u64) -> Self {
    let mut fi = previous_fi.clone();
    fi.frame_type = FrameType::KEY;
    fi.intra_only = true;
    fi.inter_cfg = None;
    fi.order_hint = 0;
    fi.refresh_frame_flags = ALL_REF_FRAMES_MASK;
    fi.show_frame = true;
    fi.show_existing_frame = false;
    fi.frame_to_show_map_idx = 0;
    fi.primary_ref_frame = PRIMARY_REF_NONE;
    fi.number = segment_start_frame;
    for i in 0..INTER_REFS_PER_FRAME {
      fi.ref_frames[i] = 0;
    }
586

587
    fi.tx_mode_select = fi.config.speed_settings.rdo_tx_decision;
588

Josh Holmer's avatar
Josh Holmer committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
    fi
  }

  fn apply_inter_props_cfg(&mut self, idx_in_segment: u64) {
    let reorder = !self.config.low_latency;
    let multiref = reorder || self.config.speed_settings.multiref;

    let pyramid_depth = if reorder { 2 } else { 0 };
    let group_src_len = 1 << pyramid_depth;
    let group_len = group_src_len + if reorder { pyramid_depth } else { 0 };

    let idx_in_group = (idx_in_segment - 1) % group_len;
    let group_idx = (idx_in_segment - 1) / group_len;

    self.inter_cfg = Some(InterPropsConfig {
      reorder,
      multiref,
      pyramid_depth,
      group_src_len,
      group_len,
      idx_in_group,
      group_idx,
    })
  }

  /// Returns the created FrameInvariants along with a bool indicating success.
  /// This interface provides simpler usage, because we always need the produced
  /// FrameInvariants regardless of success or failure.
617 618 619 620
  pub fn new_inter_frame(
    previous_fi: &Self, segment_start_frame: u64, idx_in_segment: u64,
    next_keyframe: u64
  ) -> (Self, bool) {
Josh Holmer's avatar
Josh Holmer committed
621 622 623 624
    let mut fi = previous_fi.clone();
    fi.frame_type = FrameType::INTER;
    fi.intra_only = false;
    fi.apply_inter_props_cfg(idx_in_segment);
625
    fi.tx_mode_select = false;
Josh Holmer's avatar
Josh Holmer committed
626 627
    let inter_cfg = fi.inter_cfg.unwrap();

628 629 630 631 632 633 634
    fi.order_hint =
      (inter_cfg.group_src_len * inter_cfg.group_idx +
       if inter_cfg.reorder && inter_cfg.idx_in_group < inter_cfg.pyramid_depth {
         inter_cfg.group_src_len >> inter_cfg.idx_in_group
       } else {
         inter_cfg.idx_in_group - inter_cfg.pyramid_depth + 1
       }) as u32;
Josh Holmer's avatar
Josh Holmer committed
635 636 637 638 639 640 641
    let number = segment_start_frame + fi.order_hint as u64;
    if number >= next_keyframe {
      fi.show_existing_frame = false;
      fi.show_frame = false;
      return (fi, false);
    }

642 643 644 645 646 647 648 649
    // A group always starts with zero or more no-show frames, followed by
    // the group_src_len of shown frames. For example, for a pryamid depth of 2,
    // the group is as follows:
    // |TU         |TU |TU |TU
    // 0   1   2   3   4   5
    // ^^^^^   ^^^^^^^^^^^^^
    // no-show show

Josh Holmer's avatar
Josh Holmer committed
650 651 652
    let lvl = if !inter_cfg.reorder {
      0
    } else if inter_cfg.idx_in_group < inter_cfg.pyramid_depth {
653
      // no-show frames are output first (to be shown in future)
Josh Holmer's avatar
Josh Holmer committed
654 655
      inter_cfg.idx_in_group
    } else {
656
      // show frames
Josh Holmer's avatar
Josh Holmer committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
      pos_to_lvl(inter_cfg.idx_in_group - inter_cfg.pyramid_depth + 1, inter_cfg.pyramid_depth)
    };

    // Frames with lvl == 0 are stored in slots 0..4 and frames with higher values
    // of lvl in slots 4..8
    let slot_idx = if lvl == 0 {
      (fi.order_hint >> inter_cfg.pyramid_depth) % 4 as u32
    } else {
      3 + lvl as u32
    };
    fi.show_frame = !inter_cfg.reorder || inter_cfg.idx_in_group >= inter_cfg.pyramid_depth;
    fi.show_existing_frame = fi.show_frame && inter_cfg.reorder &&
      (inter_cfg.idx_in_group - inter_cfg.pyramid_depth + 1).count_ones() == 1 &&
      inter_cfg.idx_in_group != inter_cfg.pyramid_depth;
    fi.frame_to_show_map_idx = slot_idx;
    fi.refresh_frame_flags = if fi.show_existing_frame {
      0
    } else {
      1 << slot_idx
    };

    let second_ref_frame = if !inter_cfg.multiref {
Thomas Daede's avatar
Thomas Daede committed
679
      LAST_FRAME // make second_ref_frame match first
Josh Holmer's avatar
Josh Holmer committed
680 681 682 683 684 685 686 687
    } else if !inter_cfg.reorder || inter_cfg.idx_in_group == 0 {
      LAST2_FRAME
    } else {
      ALTREF_FRAME
    };
    let ref_in_previous_group = LAST3_FRAME;

    // reuse probability estimates from previous frames only in top level frames
688 689 690
    fi.primary_ref_frame = if lvl > 0 {
      PRIMARY_REF_NONE
    } else {
Thomas Daede's avatar
Thomas Daede committed
691
      (ref_in_previous_group.to_index()) as u32
692
    };
Josh Holmer's avatar
Josh Holmer committed
693 694 695

    for i in 0..INTER_REFS_PER_FRAME {
      fi.ref_frames[i] = if lvl == 0 {
Thomas Daede's avatar
Thomas Daede committed
696
        if i == second_ref_frame.to_index() {
Josh Holmer's avatar
Josh Holmer committed
697 698 699
          (slot_idx + 4 - 2) as u8 % 4
        } else {
          (slot_idx + 4 - 1) as u8 % 4
700
        }
701 702 703 704 705 706 707 708 709 710 711 712 713 714
      } else if i == second_ref_frame.to_index() {
        let oh = fi.order_hint + (inter_cfg.group_src_len as u32 >> lvl);
        let lvl2 = pos_to_lvl(oh as u64, inter_cfg.pyramid_depth);
        if lvl2 == 0 {
          ((oh >> inter_cfg.pyramid_depth) % 4) as u8
        } else {
          3 + lvl2 as u8
        }
      } else if i == ref_in_previous_group.to_index() {
        if lvl == 0 {
          (slot_idx + 4 - 1) as u8 % 4
        } else {
          slot_idx as u8
        }
Josh Holmer's avatar
Josh Holmer committed
715
      } else {
716 717 718 719
        let oh = fi.order_hint - (inter_cfg.group_src_len as u32 >> lvl);
        let lvl1 = pos_to_lvl(oh as u64, inter_cfg.pyramid_depth);
        if lvl1 == 0 {
          ((oh >> inter_cfg.pyramid_depth) % 4) as u8
Josh Holmer's avatar
Josh Holmer committed
720
        } else {
721
          3 + lvl1 as u8
Josh Holmer's avatar
Josh Holmer committed
722 723
        }
      }
724
    }
Josh Holmer's avatar
Josh Holmer committed
725 726 727 728 729 730 731 732 733 734

    fi.reference_mode = if inter_cfg.multiref && inter_cfg.reorder && inter_cfg.idx_in_group != 0 {
      ReferenceMode::SELECT
    } else {
      ReferenceMode::SINGLE
    };
    fi.number = number;
    fi.me_range_scale = (inter_cfg.group_src_len >> lvl) as u8;
    (fi, true)
  }
735

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
  pub fn get_frame_subtype(&self) -> usize {
    if self.frame_type == FrameType::KEY {
      FRAME_SUBTYPE_I
    } else {
      let inter_cfg = self.inter_cfg.unwrap();
      let lvl = if !inter_cfg.reorder {
        0
      } else if inter_cfg.idx_in_group < inter_cfg.pyramid_depth {
        inter_cfg.idx_in_group
      } else {
        pos_to_lvl(
          inter_cfg.idx_in_group - inter_cfg.pyramid_depth + 1,
          inter_cfg.pyramid_depth
        )
      };
      FRAME_SUBTYPE_P + (lvl as usize)
    }
  }

  pub fn set_quantizers(&mut self, qps: &QuantizerParameters) {
756
    self.base_q_idx = qps.ac_qi[0];
757 758 759 760 761
    if self.frame_type != FrameType::KEY {
      self.cdef_bits = 3 - ((self.base_q_idx.max(128) - 128) >> 5);
    } else {
      self.cdef_bits = 3;
    }
762
    let base_q_idx = self.base_q_idx as i32;
763
    for pi in 0..3 {
764 765 766 767 768 769
      debug_assert!(qps.dc_qi[pi] as i32 - base_q_idx >= -128);
      debug_assert!((qps.dc_qi[pi] as i32 - base_q_idx) < 128);
      debug_assert!(qps.ac_qi[pi] as i32 - base_q_idx >= -128);
      debug_assert!((qps.ac_qi[pi] as i32 - base_q_idx) < 128);
      self.dc_delta_q[pi] = (qps.dc_qi[pi] as i32 - base_q_idx) as i8;
      self.ac_delta_q[pi] = (qps.ac_qi[pi] as i32 - base_q_idx) as i8;
770 771
    }
    self.lambda =
772
      qps.lambda * ((1 << (2 * (self.sequence.bit_depth - 8))) as f64);
773 774
    self.me_lambda = self.lambda.sqrt();
  }
775 776 777 778 779 780 781 782 783 784

  #[inline(always)]
  pub fn sb_size_log2(&self) -> usize {
    self.sequence.sb_size_log2()
  }

  #[inline(always)]
  pub fn sb_size(&self) -> usize {
    self.sequence.sb_size()
  }
785 786
}

787
impl<T: Pixel> fmt::Display for FrameInvariants<T> {
788 789 790
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "Frame {} - {}", self.number, self.frame_type)
  }
791 792
}

Josh Holmer's avatar
Josh Holmer committed
793 794 795 796 797
#[derive(Debug, Clone, Copy)]
pub struct InterPropsConfig {
  pub reorder: bool,
  pub multiref: bool,
  pub pyramid_depth: u64,
798
  /// number of source frames in group
Josh Holmer's avatar
Josh Holmer committed
799
  pub group_src_len: u64,
800
  /// number of output frames on group (group_src_len + pyramid_depth)
Josh Holmer's avatar
Josh Holmer committed
801 802
  pub group_len: u64,
  pub idx_in_group: u64,
803
  /// segment-relative group
Josh Holmer's avatar
Josh Holmer committed
804 805 806
  pub group_idx: u64,
}

807 808 809
pub fn write_temporal_delimiter(
  packet: &mut dyn io::Write
) -> io::Result<()> {
810
  packet.write_all(&TEMPORAL_DELIMITER)?;
811 812 813
  Ok(())
}

814 815
fn write_obus<T: Pixel>(
  packet: &mut dyn io::Write, fi: &mut FrameInvariants<T>, fs: &FrameState<T>
816 817
) -> io::Result<()> {
  let obu_extension = 0 as u32;
818

819
  let mut buf1 = Vec::new();
820

821 822
  // write sequence header obu if KEY_FRAME, preceded by 4-byte size
  if fi.frame_type == FrameType::KEY {
823 824
    let mut buf2 = Vec::new();
    {
825 826
      let mut bw2 = BitWriter::endian(&mut buf2, BigEndian);
      bw2.write_sequence_header_obu(fi)?;
Raphaël Zumer's avatar
Raphaël Zumer committed
827
      bw2.write_bit(true)?; // trailing bit
828
      bw2.byte_align()?;
829 830 831
    }

    {
832
      let mut bw1 = BitWriter::endian(&mut buf1, BigEndian);
833
      bw1.write_obu_header(ObuType::OBU_SEQUENCE_HEADER, obu_extension)?;
834
    }
835
    packet.write_all(&buf1).unwrap();
836 837 838
    buf1.clear();

    {
839
      let mut bw1 = BitWriter::endian(&mut buf1, BigEndian);
840
      bw1.write_uleb128(buf2.len() as u64)?;
841
    }
842
    packet.write_all(&buf1).unwrap();
843 844
    buf1.clear();

845
    packet.write_all(&buf2).unwrap();
846
    buf2.clear();
847 848

    if fi.sequence.content_light.is_some() {
849 850
      let mut bw1 = BitWriter::endian(&mut buf1, BigEndian);
      bw1.write_metadata_obu(ObuMetaType::OBU_META_HDR_CLL, fi.sequence)?;
851 852 853 854 855
      packet.write_all(&buf1).unwrap();
      buf1.clear();
    }

    if fi.sequence.mastering_display.is_some() {
856 857
      let mut bw1 = BitWriter::endian(&mut buf1, BigEndian);
      bw1.write_metadata_obu(ObuMetaType::OBU_META_HDR_MDCV, fi.sequence)?;
858 859 860
      packet.write_all(&buf1).unwrap();
      buf1.clear();
    }
861
  }
862

863 864 865 866 867 868 869 870
  let mut buf2 = Vec::new();
  {
    let mut bw2 = BitWriter::endian(&mut buf2, BigEndian);
    bw2.write_frame_header_obu(fi, fs)?;
  }

  {
    let mut bw1 = BitWriter::endian(&mut buf1, BigEndian);
871
    bw1.write_obu_header(ObuType::OBU_FRAME_HEADER, obu_extension)?;
872 873 874 875 876 877
  }
  packet.write_all(&buf1).unwrap();
  buf1.clear();

  {
    let mut bw1 = BitWriter::endian(&mut buf1, BigEndian);
878
    bw1.write_uleb128(buf2.len() as u64)?;
879
  }
Raphaël Zumer's avatar
Raphaël Zumer committed
880

881 882 883 884 885 886 887
  packet.write_all(&buf1).unwrap();
  buf1.clear();

  packet.write_all(&buf2).unwrap();
  buf2.clear();

  Ok(())
888 889 890
}

/// Write into `dst` the difference between the blocks at `src1` and `src2`
Romain Vimont's avatar
Romain Vimont committed
891 892 893 894 895 896 897
fn diff<T: Pixel>(
  dst: &mut [i16],
  src1: &PlaneRegion<'_, T>,
  src2: &PlaneRegion<'_, T>,
  width: usize,
  height: usize,
) {
fbossen's avatar
fbossen committed
898
  for ((l, s1), s2) in dst.chunks_mut(width).take(height)
899 900
    .zip(src1.rows_iter())
    .zip(src2.rows_iter()) {
901
      for ((r, v1), v2) in l.iter_mut().zip(s1).zip(s2) {
902
        *r = i16::cast_from(*v1) - i16::cast_from(*v2);
903
      }
904 905 906
    }
}

907
fn get_qidx<T: Pixel>(fi: &FrameInvariants<T>, ts: &TileStateMut<'_, T>, cw: &ContextWriter, tile_bo: BlockOffset) -> u8 {
908
  let mut qidx = fi.base_q_idx;
909
  let sidx = cw.bc.blocks[tile_bo].segmentation_idx as usize;
Romain Vimont's avatar
Romain Vimont committed
910 911
  if ts.segmentation.features[sidx][SegLvl::SEG_LVL_ALT_Q as usize] {
    let delta = ts.segmentation.data[sidx][SegLvl::SEG_LVL_ALT_Q as usize];
912 913 914
    qidx = clamp((qidx as i16) + delta, 0, 255) as u8;
  }
  qidx
915 916
}

917 918 919
// For a transform block,
// predict, transform, quantize, write coefficients to a bitstream,
// dequantize, inverse-transform.
920
pub fn encode_tx_block<T: Pixel>(
Romain Vimont's avatar
Romain Vimont committed
921
  fi: &FrameInvariants<T>, ts: &mut TileStateMut<'_, T>, cw: &mut ContextWriter,
922
  w: &mut dyn Writer, p: usize, tile_bo: BlockOffset, mode: PredictionMode,
Romain Vimont's avatar
Romain Vimont committed
923
  tx_size: TxSize, tx_type: TxType, plane_bsize: BlockSize, po: PlaneOffset,
924
  skip: bool, ac: &[i16], alpha: i16, rdo_type: RDOType, for_rdo_use: bool
925
) -> (bool, i64) {
926
  let qidx = get_qidx(fi, ts, cw, tile_bo);
Romain Vimont's avatar
Romain Vimont committed
927
  let PlaneConfig { xdec, ydec, .. } = ts.input.planes[p].cfg;
928 929
  let tile_rect = ts.tile_rect().decimated(xdec, ydec);
  let rec = &mut ts.rec.planes[p];
930
  let area = Area::BlockStartingAt { bo: tile_bo };
931

932
  assert!(tx_size.sqr() <= TxSize::TX_32X32 || tx_type == TxType::DCT_DCT);
933 934 935
  debug_assert!(p != 0 || !mode.is_intra() || tx_size.block_size() == plane_bsize || !for_rdo_use,
    "mode.is_intra()={:#?}, plane={:#?}, tx_size.block_size()={:#?}, plane_bsize={:#?}, for_rdo_use={:#?}",
    mode.is_intra(), p, tx_size.block_size(), plane_bsize, for_rdo_use);
936

937 938
  if mode.is_intra() {
    let bit_depth = fi.sequence.bit_depth;
Romain Vimont's avatar
Romain Vimont committed
939
    let edge_buf = get_intra_edges(&rec.as_const(), po, tx_size, bit_depth, Some(mode));
940
    mode.predict_intra(tile_rect, &mut rec.subregion_mut(area), tx_size, bit_depth, &ac, alpha, &edge_buf);
941
  }
942

943
  if skip { return (false, -1); }
944

945 946 947 948 949 950 951 952
  let mut residual_storage: AlignedArray<[i16; 64 * 64]> = UninitializedAlignedArray();
  let mut coeffs_storage: AlignedArray<[i32; 64 * 64]> = UninitializedAlignedArray();
  let mut qcoeffs_storage: AlignedArray<[i32; 64 * 64]> = UninitializedAlignedArray();
  let mut rcoeffs_storage: AlignedArray<[i32; 64 * 64]> = UninitializedAlignedArray();
  let residual = &mut residual_storage.array[..tx_size.area()];
  let coeffs = &mut coeffs_storage.array[..tx_size.area()];
  let qcoeffs = &mut qcoeffs_storage.array[..tx_size.area()];
  let rcoeffs = &mut rcoeffs_storage.array[..tx_size.area()];
953

Romain Vimont's avatar
Romain Vimont committed
954 955 956 957 958 959
  diff(
    residual,
    &ts.input_tile.planes[p].subregion(area),
    &rec.subregion(area),
    tx_size.width(),
    tx_size.height());
960

961
  forward_transform(residual, coeffs, tx_size.width(), tx_size, tx_type, fi.sequence.bit_depth);
962

963
  let coded_tx_size = av1_get_coded_tx_size(tx_size).area();
Romain Vimont's avatar
Romain Vimont committed
964
  ts.qc.quantize(coeffs, qcoeffs, coded_tx_size);
965

966 967
  let tell_coeffs = w.tell_frac();
  let has_coeff = if !for_rdo_use || rdo_type.needs_coeff_rate() {
968
    cw.write_coeffs_lv_map(w, p, tile_bo, &qcoeffs, mode, tx_size, tx_type, plane_bsize, xdec, ydec,
969 970 971 972 973
                           fi.use_reduced_tx_set)
  } else {
    true
  };
  let cost_coeffs = w.tell_frac() - tell_coeffs;
974 975
  // Reconstruct
  dequantize(qidx, qcoeffs, rcoeffs, tx_size, fi.sequence.bit_depth, fi.dc_delta_q[p], fi.ac_delta_q[p]);
976

977
  let mut tx_dist: i64 = -1;
978

979
  if !fi.use_tx_domain_distortion || !for_rdo_use {
Romain Vimont's avatar
Romain Vimont committed
980
    inverse_transform_add(rcoeffs, &mut rec.subregion_mut(area), tx_size, tx_type, fi.sequence.bit_depth);
981 982
  }
  if rdo_type.needs_tx_dist() {
983 984 985 986 987 988 989 990 991 992 993 994 995
    // Store tx-domain distortion of this block
    tx_dist = coeffs
      .iter()
      .zip(rcoeffs)
      .map(|(a, b)| {
        let c = *a as i32 - *b as i32;
        (c * c) as u64
      }).sum::<u64>() as i64;

    let tx_dist_scale_bits = 2*(3 - get_log_tx_scale(tx_size));
    let tx_dist_scale_rounding_offset = 1 << (tx_dist_scale_bits - 1);
    tx_dist = (tx_dist + tx_dist_scale_rounding_offset) >> tx_dist_scale_bits;
  }
996
  if fi.config.train_rdo {
Romain Vimont's avatar
Romain Vimont committed
997
    ts.rdo.add_rate(fi.base_q_idx, tx_size, tx_dist as u64, cost_coeffs as u64);
998 999 1000 1001 1002 1003 1004
  }

  if rdo_type == RDOType::TxDistEstRate {
    // look up rate and distortion in table
    let estimated_rate = estimate_rate(fi.base_q_idx, tx_size, tx_dist as u64);
    w.add_bits_frac(estimated_rate as u32);
  }
1005
  (has_coeff, tx_dist)
1006 1007
}

1008
pub fn motion_compensate<T: Pixel>(
Romain Vimont's avatar
Romain Vimont committed
1009
  fi: &FrameInvariants<T>, ts: &mut TileStateMut<'_, T>, cw: &mut ContextWriter,
Thomas Daede's avatar
Thomas Daede committed
1010
  luma_mode: PredictionMode, ref_frames: [RefType; 2], mvs: [MotionVector; 2],
1011
  bsize: BlockSize, tile_bo: BlockOffset, luma_only: bool
1012
) {
1013
  debug_assert!(!luma_mode.is_intra());
1014

1015
  let PlaneConfig { xdec: u_xdec, ydec: u_ydec, .. } = ts.input.planes[1].cfg;
1016 1017 1018

  // Inter mode prediction can take place once for a whole partition,
  // instead of each tx-block.
1019
  let num_planes = 1 + if !luma_only && has_chroma(tile_bo, bsize, u_xdec, u_ydec) { 2 } else { 0 };
1020

1021
  let luma_tile_rect = ts.tile_rect();
1022 1023
  for p in 0..num_planes {
    let plane_bsize = if p == 0 { bsize }
1024
    else { get_plane_block_size(bsize, u_xdec, u_ydec) };
1025

Romain Vimont's avatar
Romain Vimont committed
1026
    let rec = &mut ts.rec.planes[p];
1027
    let po = tile_bo.plane_offset(&rec.plane_cfg);
1028 1029
    let &PlaneConfig { xdec, ydec, .. } = rec.plane_cfg;
    let tile_rect = luma_tile_rect.decimated(xdec, ydec);
1030

1031
    let area = Area::BlockStartingAt { bo: tile_bo };
1032 1033 1034
    if p > 0 && bsize < BlockSize::BLOCK_8X8 {
      let mut some_use_intra = false;
      if bsize == BlockSize::BLOCK_4X4 || bsize == BlockSize::BLOCK_4X8 {
1035
        some_use_intra |= cw.bc.blocks[tile_bo.with_offset(-1,0)].mode.is_intra(); };
1036
      if !some_use_intra && bsize == BlockSize::BLOCK_4X4 || bsize == BlockSize::BLOCK_8X4 {
1037
        some_use_intra |= cw.bc.blocks[tile_bo.with_offset(0,-1)].mode.is_intra(); };
1038
      if !some_use_intra && bsize == BlockSize::BLOCK_4X4 {
1039
        some_use_intra |= cw.bc.blocks[tile_bo.with_offset(-1,-1)].mode.is_intra(); };
1040 1041

      if some_use_intra {
1042
        luma_mode.predict_inter(fi, tile_rect, p, po, &mut rec.subregion_mut(area), plane_bsize.width(),
1043
                                plane_bsize.height(), ref_frames, mvs);
1044
      } else {
1045
        assert!(u_xdec == 1 && u_ydec == 1);
1046 1047
        // TODO: these are absolutely only valid for 4:2:0
        if bsize == BlockSize::BLOCK_4X4 {
1048 1049 1050 1051
          let mv0 = cw.bc.blocks[tile_bo.with_offset(-1,-1)].mv;
          let rf0 = cw.bc.blocks[tile_bo.with_offset(-1,-1)].ref_frames;
          let mv1 = cw.bc.blocks[tile_bo.with_offset(0,-1)].mv;
          let rf1 = cw.bc.blocks[tile_bo.with_offset(0,-1)].ref_frames;
1052
          let po1 = PlaneOffset { x: po.x+2, y: po.y };
Romain Vimont's avatar
Romain Vimont committed
1053
          let area1 = Area::StartingAt { x: po1.x, y: po1.y };
1054 1055
          let mv2 = cw.bc.blocks[tile_bo.with_offset(-1,0)].mv;
          let rf2 = cw.bc.blocks[tile_bo.with_offset(-1,0)].ref_frames;
1056
          let po2 = PlaneOffset { x: po.x, y: po.y+2 };
Romain Vimont's avatar
Romain Vimont committed
1057
          let area2 = Area::StartingAt { x: po2.x, y: po2.y };
1058
          let po3 = PlaneOffset { x: po.x+2, y: po.y+2 };
Romain Vimont's avatar
Romain Vimont committed
1059
          let area3 = Area::StartingAt { x: po3.x, y: po3.y };
1060 1061 1062 1063
          luma_mode.predict_inter(fi, tile_rect, p, po, &mut rec.subregion_mut(area), 2, 2, rf0, mv0);
          luma_mode.predict_inter(fi, tile_rect, p, po1, &mut rec.subregion_mut(area1), 2, 2, rf1, mv1);
          luma_mode.predict_inter(fi, tile_rect, p, po2, &mut rec.subregion_mut(area2), 2, 2, rf2, mv2);
          luma_mode.predict_inter(fi, tile_rect, p, po3, &mut rec.subregion_mut(area3), 2, 2, ref_frames, mvs);
1064 1065
        }
        if bsize == BlockSize::BLOCK_8X4 {
1066 1067
          let mv1 = cw.bc.blocks[tile_bo.with_offset(0,-1)].mv;
          let rf1 = cw.bc.blocks[tile_bo.with_offset(0,-1)].ref_frames;
1068
          luma_mode.predict_inter(fi, tile_rect, p, po, &mut rec.subregion_mut(area), 4, 2, rf1, mv1);
1069
          let po3 = PlaneOffset { x: po.x, y: po.y+2 };
Romain Vimont's avatar
Romain Vimont committed
1070
          let area3 = Area::StartingAt { x: po3.x, y: po3.y };
1071
          luma_mode.predict_inter(fi, tile_rect, p, po3, &mut rec.subregion_mut(area3), 4, 2, ref_frames, mvs);
1072 1073
        }
        if bsize == BlockSize::BLOCK_4X8 {
1074 1075
          let mv2 = cw.bc.blocks[tile_bo.with_offset(-1,0)].mv;
          let rf2 = cw.bc.blocks[tile_bo.with_offset(-1,0)].ref_frames;
1076
          luma_mode.predict_inter(fi, tile_rect, p, po, &mut rec.subregion_mut(area), 2, 4, rf2, mv2);
1077
          let po3 = PlaneOffset { x: po.x+2, y: po.y };
Romain Vimont's avatar
Romain Vimont committed
1078
          let area3 = Area::StartingAt { x: po3.x, y: po3.y };
1079
          luma_mode.predict_inter(fi, tile_rect, p, po3, &mut rec.subregion_mut(area3), 2, 4, ref_frames, mvs);
1080
        }
1081 1082
      }
    } else {
1083
      luma_mode.predict_inter(fi, tile_rect, p, po, &mut rec.subregion_mut(area), plane_bsize.width(),
1084
                              plane_bsize.height(), ref_frames, mvs);
1085 1086 1087 1088
    }
  }
}

1089
pub fn save_block_motion<T: Pixel>(
1090
   ts: &mut TileStateMut<'_, T>,
1091
   bsize: BlockSize, tile_bo: BlockOffset,
1092 1093
   ref_frame: usize, mv: MotionVector,
) {
1094
  let tile_mvs = &mut ts.mvs[ref_frame];
Romain Vimont's avatar
Romain Vimont committed
1095 1096
  let tile_bo_x_end = (tile_bo.x + bsize.width_mi()).min(ts.mi_width);
  let tile_bo_y_end = (tile_bo.y + bsize.height_mi()).min(ts.mi_height);
1097 1098
  for mi_y in tile_bo.y..tile_bo_y_end {
    for mi_x in tile_bo.x..tile_bo_x_end {
1099
      tile_mvs[mi_y][mi_x] = mv;
1100 1101 1102 1103
    }
  }
}

1104
pub fn encode_block_a<T: Pixel>(
Romain Vimont's avatar
Romain Vimont committed
1105
  seq: &Sequence, ts: &TileStateMut<'_, T>,
1106
  cw: &mut ContextWriter, w: &mut dyn Writer,
1107
  bsize: BlockSize, tile_bo: BlockOffset, skip: bool