Skip to content

Commit

Permalink
codec: av1: add an AV1 parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dwlsalmeida committed Sep 26, 2023
1 parent 000e9fa commit 37958cf
Show file tree
Hide file tree
Showing 7 changed files with 3,633 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! There shall be no dependencies from other modules of this crate to this module, so that it
//! can be turned into a crate of its own if needed in the future.
pub mod av1;
pub mod h264;
pub mod h265;
pub mod vp8;
Expand Down
7 changes: 7 additions & 0 deletions src/codec/av1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

mod helpers;
pub mod parser;
pub mod reader;
119 changes: 119 additions & 0 deletions src/codec/av1/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::codec::av1::parser::NUM_REF_FRAMES;

/// Implements FloorLog2(x), which is defined to be the floor of the base 2
/// logarithm of the input x.
///
/// The input x will always be an integer, and will always be greater than or equal to 1.
/// This function extracts the location of the most significant bit in x.
pub fn floor_log2(mut x: u8) -> u8 {
assert!(x > 0);
let mut s = 0;

while x != 0 {
x >>= 1;
s += 1;
}

s - 1
}

pub fn get_relative_dist(enable_order_hint: bool, order_hint_bits: i32, a: i32, b: i32) -> i32 {
if !enable_order_hint {
0
} else {
let diff = a - b;
let m = 1 << (order_hint_bits - 1);
(diff & (m - 1)) - (diff & m)
}
}

pub fn find_latest_backward(
shifted_order_hints: &[i32; NUM_REF_FRAMES],
used_frame: &[bool; NUM_REF_FRAMES],
cur_frame_hint: i32,
latest_order_hint: &mut i32,
) -> i32 {
let mut _ref = -1;

for i in 0..NUM_REF_FRAMES {
let hint = shifted_order_hints[i];
if !used_frame[i] && hint >= cur_frame_hint && (_ref < 0 || hint >= *latest_order_hint) {
_ref = i as i32;
*latest_order_hint = hint;
}
}

_ref
}

pub fn find_earliest_backward(
shifted_order_hints: &[i32; NUM_REF_FRAMES],
used_frame: &[bool; NUM_REF_FRAMES],
cur_frame_hint: i32,
earliest_order_hint: &mut i32,
) -> i32 {
let mut _ref = -1;

for i in 0..NUM_REF_FRAMES {
let hint = shifted_order_hints[i];
if !used_frame[i] && hint >= cur_frame_hint && (_ref < 0 || hint < *earliest_order_hint) {
_ref = i as i32;
*earliest_order_hint = hint;
}
}

_ref
}

pub fn find_latest_forward(
shifted_order_hints: &[i32; NUM_REF_FRAMES],
used_frame: &[bool; NUM_REF_FRAMES],
cur_frame_hint: i32,
latest_order_hint: &mut i32,
) -> i32 {
let mut _ref = -1;
for i in 0..NUM_REF_FRAMES {
let hint = shifted_order_hints[i];
if !used_frame[i] && hint < cur_frame_hint && (_ref < 0 || hint >= *latest_order_hint) {
_ref = i as i32;
*latest_order_hint = hint;
}
}

_ref
}

pub fn tile_log2(blk_size: u32, target: u32) -> u32 {
let mut k = 0;

while (blk_size << k) < target {
k += 1;
}

k
}

pub fn clip3(x: i32, y: i32, z: i32) -> i32 {
if z < x {
x
} else if z > y {
y
} else {
z
}
}

/// 5.9.29
pub fn inverse_recenter(r: i32, v: i32) -> i32 {
if v > 2 * r {
v
} else if v & 1 != 0 {
r - ((v + 1) >> 1)
} else {
r + (v >> 1)
}
}
Loading

0 comments on commit 37958cf

Please sign in to comment.