| 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 | |
| 10 | use num_derive::FromPrimitive; |
| 11 | |
| 12 | use crate::api::{Opaque, T35}; |
| 13 | use crate::context::SB_SIZE; |
| 14 | use crate::mc::SUBPEL_FILTER_SIZE; |
| 15 | use crate::util::*; |
| 16 | |
| 17 | use crate::tiling::*; |
| 18 | |
| 19 | mod plane; |
| 20 | pub use plane::*; |
| 21 | |
| 22 | const FRAME_MARGIN: usize = 16 + SUBPEL_FILTER_SIZE; |
| 23 | const 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)] |
| 30 | pub 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)] |
| 40 | pub 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 | |
| 49 | pub use v_frame::frame::Frame; |
| 50 | |
| 51 | /// Public Trait Interface for Frame Allocation |
| 52 | pub(crate) trait FrameAlloc { |
| 53 | /// Initialise new frame default type |
| 54 | fn new(width: usize, height: usize, chroma_sampling: ChromaSampling) |
| 55 | -> Self; |
| 56 | } |
| 57 | |
| 58 | impl<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 |
| 75 | pub(crate) trait FramePad { |
| 76 | fn pad(&mut self, w: usize, h: usize, planes: usize); |
| 77 | } |
| 78 | |
| 79 | impl<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 |
| 88 | pub(crate) trait AsTile<T: Pixel> { |
| 89 | fn as_tile(&self) -> Tile<'_, T>; |
| 90 | fn as_tile_mut(&mut self) -> TileMut<'_, T>; |
| 91 | } |
| 92 | |
| 93 | impl<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 | |