forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB978e685Tahinli3 months ago627 commits

decoder.rs149 lines, 3.4 KiBHistoryRawDownload
1// Copyright 2022 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! This file contains a dummy backend whose only purpose is to let the decoder
6//! run so we can test it in isolation.
7
8use std::cell::RefCell;
9use std::rc::Rc;
10
11use crate::decoder::stateless::PoolLayer;
12use crate::decoder::stateless::StatelessCodec;
13use crate::decoder::stateless::StatelessDecoderBackend;
14use crate::decoder::stateless::StatelessDecoderBackendPicture;
15use crate::decoder::stateless::TryFormat;
16use crate::decoder::DecodedHandle;
17use crate::decoder::DynHandle;
18use crate::decoder::FramePool;
19use crate::decoder::MappableHandle;
20use crate::decoder::StreamInfo;
21use crate::DecodedFormat;
22use crate::Resolution;
23
24#[derive(Default)]
25pub struct BackendHandle(());
26
27impl MappableHandle for BackendHandle {
28 fn read(&mut self, _: &mut [u8]) -> anyhow::Result<()> {
29 Ok(())
30 }
31
32 fn image_size(&mut self) -> usize {
33 1
34 }
35}
36
37impl<'a> DynHandle for std::cell::RefMut<'a, BackendHandle> {
38 fn dyn_mappable_handle<'b>(&'b mut self) -> anyhow::Result<Box<dyn MappableHandle + 'b>> {
39 Ok(Box::<BackendHandle>::default())
40 }
41}
42
43pub struct Handle {
44 pub handle: Rc<RefCell<BackendHandle>>,
45}
46
47impl Clone for Handle {
48 fn clone(&self) -> Self {
49 Self { handle: Rc::clone(&self.handle) }
50 }
51}
52
53impl DecodedHandle for Handle {
54 type Descriptor = ();
55
56 fn coded_resolution(&self) -> Resolution {
57 Default::default()
58 }
59
60 fn display_resolution(&self) -> Resolution {
61 Default::default()
62 }
63
64 fn timestamp(&self) -> u64 {
65 0
66 }
67
68 fn dyn_picture<'a>(&'a self) -> Box<dyn DynHandle + 'a> {
69 Box::new(self.handle.borrow())
70 }
71
72 fn sync(&self) -> anyhow::Result<()> {
73 Ok(())
74 }
75
76 fn is_ready(&self) -> bool {
77 true
78 }
79
80 fn resource(&self) -> std::cell::Ref<()> {
81 std::cell::Ref::map(self.handle.borrow(), |h| &h.0)
82 }
83}
84
85/// Dummy backend that can be used for any codec.
86pub struct Backend {
87 stream_info: StreamInfo,
88}
89
90impl Backend {
91 pub(crate) fn new() -> Self {
92 Self {
93 stream_info: StreamInfo {
94 format: DecodedFormat::I420,
95 min_num_frames: 4,
96 coded_resolution: Resolution::from((320, 200)),
97 display_resolution: Resolution::from((320, 200)),
98 },
99 }
100 }
101}
102
103impl FramePool for Backend {
104 type Descriptor = ();
105
106 fn coded_resolution(&self) -> Resolution {
107 Resolution::from((320, 200))
108 }
109
110 fn set_coded_resolution(&mut self, _resolution: Resolution) {}
111
112 fn add_frames(&mut self, _descriptors: Vec<Self::Descriptor>) -> Result<(), anyhow::Error> {
113 Ok(())
114 }
115
116 fn num_free_frames(&self) -> usize {
117 4
118 }
119
120 fn num_managed_frames(&self) -> usize {
121 4
122 }
123
124 fn clear(&mut self) {}
125}
126
127impl<Codec: StatelessCodec> StatelessDecoderBackendPicture<Codec> for Backend {
128 type Picture = ();
129}
130
131impl<Codec: StatelessCodec> TryFormat<Codec> for Backend {
132 fn try_format(&mut self, _: &Codec::FormatInfo, _: DecodedFormat) -> anyhow::Result<()> {
133 Ok(())
134 }
135}
136
137impl StatelessDecoderBackend for Backend {
138 type Handle = Handle;
139
140 type FramePool = Self;
141
142 fn stream_info(&self) -> Option<&StreamInfo> {
143 Some(&self.stream_info)
144 }
145
146 fn frame_pool(&mut self, _: PoolLayer) -> Vec<&mut Self::FramePool> {
147 vec![self]
148 }
149}