predict.rs 5.41 KB
Newer Older
Luca Barbato's avatar
Luca Barbato committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2017-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.

use criterion::*;
use rand::{ChaChaRng, Rng, SeedableRng};
use rav1e::partition::BlockSize;
use rav1e::predict::{Block4x4, Intra};

pub const BLOCK_SIZE: BlockSize = BlockSize::BLOCK_32X32;

pub fn generate_block(rng: &mut ChaChaRng) -> (Vec<u16>, Vec<u16>, Vec<u16>) {
  let block = vec![0u16; BLOCK_SIZE.width() * BLOCK_SIZE.height()];
Kyle Siefring's avatar
Kyle Siefring committed
19 20 21 22
  let above_context: Vec<u16> =
    (0..BLOCK_SIZE.height()).map(|_| rng.gen()).collect();
  let left_context: Vec<u16> =
    (0..BLOCK_SIZE.width()).map(|_| rng.gen()).collect();
Luca Barbato's avatar
Luca Barbato committed
23 24 25 26

  (block, above_context, left_context)
}

David Michael Barr's avatar
David Michael Barr committed
27 28 29 30 31 32 33 34 35 36
pub fn generate_block_u8(rng: &mut ChaChaRng) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let block = vec![0u8; BLOCK_SIZE.width() * BLOCK_SIZE.height()];
  let above_context: Vec<u8> =
    (0..BLOCK_SIZE.height()).map(|_| rng.gen()).collect();
  let left_context: Vec<u8> =
    (0..BLOCK_SIZE.width()).map(|_| rng.gen()).collect();

  (block, above_context, left_context)
}

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
pub fn bench_pred_fn<F>(c: &mut Criterion, id: &str, f: F)
where
  F: FnMut(&mut Bencher) + 'static
{
  let b = Benchmark::new(id, f);
  c.bench(
    id,
    if id.ends_with("_4x4_u8") {
      b.throughput(Throughput::Bytes(16))
    } else if id.ends_with("_4x4") {
      b.throughput(Throughput::Bytes(32))
    } else {
      b
    }
  );
}

Luca Barbato's avatar
Luca Barbato committed
54
pub fn pred_bench(c: &mut Criterion) {
55 56 57 58 59 60 61 62 63 64 65
  bench_pred_fn(c, "intra_dc_4x4", intra_dc_4x4);
  bench_pred_fn(c, "intra_dc_128_4x4_u8", intra_dc_128_4x4_u8);
  bench_pred_fn(c, "intra_dc_left_4x4", intra_dc_left_4x4);
  bench_pred_fn(c, "intra_dc_top_4x4", intra_dc_top_4x4);
  bench_pred_fn(c, "intra_h_4x4", intra_h_4x4);
  bench_pred_fn(c, "intra_v_4x4", intra_v_4x4);
  bench_pred_fn(c, "intra_paeth_4x4", intra_paeth_4x4);
  bench_pred_fn(c, "intra_smooth_4x4", intra_smooth_4x4);
  bench_pred_fn(c, "intra_smooth_h_4x4", intra_smooth_h_4x4);
  bench_pred_fn(c, "intra_smooth_v_4x4", intra_smooth_v_4x4);
  bench_pred_fn(c, "intra_cfl_4x4", intra_cfl_4x4);
Luca Barbato's avatar
Luca Barbato committed
66 67 68 69 70 71 72
}

pub fn intra_dc_4x4(b: &mut Bencher) {
  let mut ra = ChaChaRng::from_seed([0; 32]);
  let (mut block, above, left) = generate_block(&mut ra);

  b.iter(|| {
73
    Block4x4::pred_dc(&mut block, BLOCK_SIZE.width(), &above[..4], &left[..4]);
Luca Barbato's avatar
Luca Barbato committed
74 75 76
  })
}

David Michael Barr's avatar
David Michael Barr committed
77 78
pub fn intra_dc_128_4x4_u8(b: &mut Bencher) {
  let mut ra = ChaChaRng::from_seed([0; 32]);
79
  let (mut block, _above, _left) = generate_block_u8(&mut ra);
David Michael Barr's avatar
David Michael Barr committed
80 81

  b.iter(|| {
82
    Block4x4::pred_dc_128(&mut block, BLOCK_SIZE.width(), 8);
David Michael Barr's avatar
David Michael Barr committed
83 84 85
  })
}

86 87 88 89 90
pub fn intra_dc_left_4x4(b: &mut Bencher) {
  let mut ra = ChaChaRng::from_seed([0; 32]);
  let (mut block, above, left) = generate_block(&mut ra);

  b.iter(|| {
91 92 93 94 95 96
    Block4x4::pred_dc_left(
      &mut block,
      BLOCK_SIZE.width(),
      &above[..4],
      &left[..4]
    );
97 98 99
  })
}

100 101 102 103 104
pub fn intra_dc_top_4x4(b: &mut Bencher) {
  let mut ra = ChaChaRng::from_seed([0; 32]);
  let (mut block, above, left) = generate_block(&mut ra);

  b.iter(|| {
105 106 107 108 109 110
    Block4x4::pred_dc_top(
      &mut block,
      BLOCK_SIZE.width(),
      &above[..4],
      &left[..4]
    );
111 112 113
  })
}

Luca Barbato's avatar
Luca Barbato committed
114 115 116 117 118
pub fn intra_h_4x4(b: &mut Bencher) {
  let mut rng = ChaChaRng::from_seed([0; 32]);
  let (mut block, _above, left) = generate_block(&mut rng);

  b.iter(|| {
119
    Block4x4::pred_h(&mut block, BLOCK_SIZE.width(), &left[..4]);
Luca Barbato's avatar
Luca Barbato committed
120 121 122 123 124 125 126 127
  })
}

pub fn intra_v_4x4(b: &mut Bencher) {
  let mut rng = ChaChaRng::from_seed([0; 32]);
  let (mut block, above, _left) = generate_block(&mut rng);

  b.iter(|| {
128
    Block4x4::pred_v(&mut block, BLOCK_SIZE.width(), &above[..4]);
Luca Barbato's avatar
Luca Barbato committed
129 130 131 132 133 134 135 136 137
  })
}

pub fn intra_paeth_4x4(b: &mut Bencher) {
  let mut rng = ChaChaRng::from_seed([0; 32]);
  let (mut block, above, left) = generate_block(&mut rng);
  let above_left = unsafe { *above.as_ptr().offset(-1) };

  b.iter(|| {
138 139 140 141 142 143 144
    Block4x4::pred_paeth(
      &mut block,
      BLOCK_SIZE.width(),
      &above[..4],
      &left[..4],
      above_left
    );
Luca Barbato's avatar
Luca Barbato committed
145 146 147 148 149 150 151 152
  })
}

pub fn intra_smooth_4x4(b: &mut Bencher) {
  let mut rng = ChaChaRng::from_seed([0; 32]);
  let (mut block, above, left) = generate_block(&mut rng);

  b.iter(|| {
153 154 155 156 157 158
    Block4x4::pred_smooth(
      &mut block,
      BLOCK_SIZE.width(),
      &above[..4],
      &left[..4]
    );
Luca Barbato's avatar
Luca Barbato committed
159 160 161 162 163 164 165 166
  })
}

pub fn intra_smooth_h_4x4(b: &mut Bencher) {
  let mut rng = ChaChaRng::from_seed([0; 32]);
  let (mut block, above, left) = generate_block(&mut rng);

  b.iter(|| {
167 168 169 170 171 172
    Block4x4::pred_smooth_h(
      &mut block,
      BLOCK_SIZE.width(),
      &above[..4],
      &left[..4]
    );
Luca Barbato's avatar
Luca Barbato committed
173 174 175 176 177 178 179 180
  })
}

pub fn intra_smooth_v_4x4(b: &mut Bencher) {
  let mut rng = ChaChaRng::from_seed([0; 32]);
  let (mut block, above, left) = generate_block(&mut rng);

  b.iter(|| {
181 182 183 184 185 186
    Block4x4::pred_smooth_v(
      &mut block,
      BLOCK_SIZE.width(),
      &above[..4],
      &left[..4]
    );
Luca Barbato's avatar
Luca Barbato committed
187 188 189 190 191 192 193 194 195 196
  })
}

pub fn intra_cfl_4x4(b: &mut Bencher) {
  let mut rng = ChaChaRng::from_seed([0; 32]);
  let (mut block, _above, _left) = generate_block(&mut rng);
  let ac: Vec<i16> = (0..(32 * 32)).map(|_| rng.gen()).collect();
  let alpha = -1 as i16;

  b.iter(|| {
197
    Block4x4::pred_cfl(&mut block, BLOCK_SIZE.width(), &ac, alpha, 8);
Luca Barbato's avatar
Luca Barbato committed
198 199
  })
}