1// Copyright (c) 2018-2022, The rav1e contributors. All rights reserved
2//
3// This source code is subject to the terms of the BSD 2 Clause License and
4// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
5// was not distributed with this source code in the LICENSE file, you can
6// obtain it at www.aomedia.org/license/software. If the Alliance for Open
7// Media Patent License 1.0 was not distributed with this source code in the
8// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
9
10use num_derive::FromPrimitive;
11
12use crate::api::{Opaque, T35};
13use crate::context::SB_SIZE;
14use crate::mc::SUBPEL_FILTER_SIZE;
15use crate::util::*;
16
17use crate::tiling::*;
18
19mod plane;
20pub use plane::*;
21
22const FRAME_MARGIN: usize = 16 + SUBPEL_FILTER_SIZE;
23const LUMA_PADDING: usize = SB_SIZE + FRAME_MARGIN;
24
25/// Override the frame type decision
26///
27/// Only certain frame types can be selected.
28#[derive(Debug, PartialEq, Eq, Clone, Copy, FromPrimitive, Default)]
29#[repr(C)]
30pub enum FrameTypeOverride {
31 /// Do not force any decision.
32 #[default]
33 No,
34 /// Force the frame to be a Keyframe.
35 Key,
36}
37
38/// Optional per-frame encoder parameters
39#[derive(Debug, Default)]
40pub struct FrameParameters {
41 /// Force emitted frame to be of the type selected
42 pub frame_type_override: FrameTypeOverride,
43 /// Output the provided data in the matching encoded Packet
44 pub opaque: Option<Opaque>,
45 /// List of t35 metadata associated with this frame
46 pub t35_metadata: Box<[T35]>,
47}
48
49pub use v_frame::frame::Frame;
50
51/// Public Trait Interface for Frame Allocation
52pub(crate) trait FrameAlloc {
53 /// Initialise new frame default type
54 fn new(width: usize, height: usize, chroma_sampling: ChromaSampling)
55 -> Self;
56}
57
58impl<T: Pixel> FrameAlloc for Frame<T> {
59 /// Creates a new frame with the given parameters.
60 /// new function calls `new_with_padding` function which takes `luma_padding`
61 /// as parameter
62 fn new(
63 width: usize, height: usize, chroma_sampling: ChromaSampling,
64 ) -> Self {
65 v_frame::frame::Frame::new_with_padding(
66 width,
67 height,
68 chroma_sampling,
69 LUMA_PADDING,
70 )
71 }
72}
73
74/// Public Trait for calculating Padding
75pub(crate) trait FramePad {
76 fn pad(&mut self, w: usize, h: usize, planes: usize);
77}
78
79impl<T: Pixel> FramePad for Frame<T> {
80 fn pad(&mut self, w: usize, h: usize, planes: usize) {
81 for pli: usize in 0..planes {
82 self.planes[pli].pad(w, h);
83 }
84 }
85}
86
87/// Public Trait for new Tile of a frame
88pub(crate) trait AsTile<T: Pixel> {
89 fn as_tile(&self) -> Tile<'_, T>;
90 fn as_tile_mut(&mut self) -> TileMut<'_, T>;
91}
92
93impl<T: Pixel> AsTile<T> for Frame<T> {
94 #[inline(always)]
95 fn as_tile(&self) -> Tile<'_, T> {
96 let PlaneConfig { width: usize, height: usize, .. } = self.planes[0].cfg;
97 Tile::new(self, luma_rect:TileRect { x: 0, y: 0, width, height })
98 }
99 #[inline(always)]
100 fn as_tile_mut(&mut self) -> TileMut<'_, T> {
101 let PlaneConfig { width: usize, height: usize, .. } = self.planes[0].cfg;
102 TileMut::new(self, luma_rect:TileRect { x: 0, y: 0, width, height })
103 }
104}
105