Debian Codegraph @

.cargo/vendor/rav1e/src/test_encode_decode/dav1d.rs

raw
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 super::*;
11 use crate::test_encode_decode::{compare_plane, DecodeResult, TestDecoder};
12 use crate::util::{CastFromPrimitive, Pixel};
13 use log::debug;
14 use std::collections::VecDeque;
15 use std::marker::PhantomData;
16 use std::os::raw::c_int;
17 use std::{
18 mem::{self, MaybeUninit},
19 ptr, slice,
20 };
21
22 use dav1d_sys::*;
23
24 pub(crate) struct Dav1dDecoder<T: Pixel> {
25 dec: *mut Dav1dContext,
26 pixel: PhantomData<T>,
27 }
28
29 impl<T: Pixel> TestDecoder<T> for Dav1dDecoder<T> {
30 fn setup_decoder(_w: usize, _h: usize) -> Self {
31 unsafe {
32 let mut settings = MaybeUninit::uninit();
33 dav1d_default_settings(settings.as_mut_ptr());
34
35 // Was initialized by dav1d_default_settings().
36 let settings = settings.assume_init();
37
38 let mut dec: Dav1dDecoder<T> =
39 Dav1dDecoder { dec: ptr::null_mut(), pixel: PhantomData };
40 let ret = dav1d_open(&mut dec.dec, &settings);
41
42 if ret != 0 {
43 panic!("Cannot instantiate the decoder {}", ret);
44 }
45
46 dec
47 }
48 }
49
50 fn decode_packet(
51 &mut self, packet: &[u8], rec_fifo: &mut VecDeque<Frame<T>>, w: usize,
52 h: usize, chroma_sampling: ChromaSampling, bit_depth: usize, verify: bool,
53 ) -> DecodeResult {
54 let mut corrupted_count = 0;
55 let mut data = SafeDav1dData::new(packet);
56 let ret = data.send(self.dec);
57 debug!("Decoded. -> {}", ret);
58 if ret != 0 {
59 corrupted_count += 1;
60 }
61
62 if ret == 0 {
63 loop {
64 let mut pic = SafeDav1dPicture::default();
65 debug!("Retrieving frame");
66 let ret = pic.get(self.dec);
67 debug!("Retrieved.");
68 if ret == DAV1D_ERR_AGAIN {
69 return DecodeResult::Done;
70 }
71 if ret != 0 {
72 panic!("Decode fail");
73 }
74
75 if verify {
76 let rec = rec_fifo.pop_front().unwrap();
77 compare_pic(&pic.0, &rec, bit_depth, w, h, chroma_sampling);
78 }
79 }
80 }
81 if corrupted_count > 0 {
82 DecodeResult::Corrupted(corrupted_count)
83 } else {
84 DecodeResult::NotDone
85 }
86 }
87 }
88
89 impl<T: Pixel> Drop for Dav1dDecoder<T> {
90 fn drop(&mut self) {
91 unsafe { dav1d_close(&mut self.dec) };
92 }
93 }
94
95 struct SafeDav1dData(Dav1dData);
96
97 impl SafeDav1dData {
98 fn new(packet: &[u8]) -> Self {
99 unsafe {
100 let mut data = Self { 0: mem::zeroed() };
101 let ptr = dav1d_data_create(&mut data.0, packet.len());
102 ptr::copy_nonoverlapping(packet.as_ptr(), ptr, packet.len());
103 data
104 }
105 }
106
107 fn send(&mut self, context: *mut Dav1dContext) -> c_int {
108 unsafe { dav1d_send_data(context, &mut self.0) }
109 }
110 }
111
112 impl Drop for SafeDav1dData {
113 fn drop(&mut self) {
114 unsafe { dav1d_data_unref(&mut self.0) };
115 }
116 }
117
118 struct SafeDav1dPicture(Dav1dPicture);
119
120 impl Default for SafeDav1dPicture {
121 fn default() -> Self {
122 Self { 0: unsafe { mem::zeroed() } }
123 }
124 }
125
126 impl SafeDav1dPicture {
127 fn get(&mut self, context: *mut Dav1dContext) -> c_int {
128 unsafe { dav1d_get_picture(context, &mut self.0) }
129 }
130 }
131
132 impl Drop for SafeDav1dPicture {
133 fn drop(&mut self) {
134 unsafe { dav1d_picture_unref(&mut self.0) }
135 }
136 }
137
138 fn compare_pic<T: Pixel>(
139 pic: &Dav1dPicture, frame: &Frame<T>, bit_depth: usize, width: usize,
140 height: usize, chroma_sampling: ChromaSampling,
141 ) {
142 use crate::frame::Plane;
143
144 let cmp_plane = |data, stride, frame_plane: &Plane<T>, pli| {
145 let w = width >> frame_plane.cfg.xdec;
146 let h = height >> frame_plane.cfg.ydec;
147 let rec_stride = frame_plane.cfg.stride;
148
149 if bit_depth > 8 {
150 let stride = stride / 2;
151 let dec = unsafe {
152 let data = data as *const u16;
153 let size = stride * h;
154
155 slice::from_raw_parts(data, size)
156 };
157
158 let rec: Vec<u16> =
159 frame_plane.data_origin().iter().map(|&v| u16::cast_from(v)).collect();
160
161 compare_plane::<u16>(&rec[..], rec_stride, dec, stride, w, h, pli);
162 } else {
163 let dec = unsafe {
164 let data = data as *const u8;
165 let size = stride * h;
166
167 slice::from_raw_parts(data, size)
168 };
169
170 let rec: Vec<u8> =
171 frame_plane.data_origin().iter().map(|&v| u8::cast_from(v)).collect();
172
173 compare_plane::<u8>(&rec[..], rec_stride, dec, stride, w, h, pli);
174 }
175 };
176
177 let lstride = pic.stride[0] as usize;
178 cmp_plane(pic.data[0], lstride, &frame.planes[0], 0);
179
180 if chroma_sampling != ChromaSampling::Cs400 {
181 let cstride = pic.stride[1] as usize;
182 cmp_plane(pic.data[1], cstride, &frame.planes[1], 1);
183 cmp_plane(pic.data[2], cstride, &frame.planes[2], 2);
184 }
185 }