| 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::Pixel; |
| 13 |
use aom_sys::*; |
| 14 |
use log::debug; |
| 15 |
use std::collections::VecDeque; |
| 16 |
use std::ffi::CStr; |
| 17 |
use std::marker::PhantomData; |
| 18 |
use std::{mem::MaybeUninit, ptr, slice}; |
| 19 |
|
| 20 |
pub(crate) struct AomDecoder<T: Pixel> { |
| 21 |
dec: aom_codec_ctx, |
| 22 |
iter: aom_codec_iter_t, |
| 23 |
pixel: PhantomData<T>, |
| 24 |
} |
| 25 |
|
| 26 |
impl<T: Pixel> TestDecoder<T> for AomDecoder<T> { |
| 27 |
fn setup_decoder(w: usize, h: usize) -> Self { |
| 28 |
unsafe { |
| 29 |
let interface = aom_codec_av1_dx(); |
| 30 |
let cfg = aom_codec_dec_cfg_t { |
| 31 |
threads: 1, |
| 32 |
w: w as u32, |
| 33 |
h: h as u32, |
| 34 |
allow_lowbitdepth: 1, |
| 35 |
}; |
| 36 |
|
| 37 |
let mut dec = MaybeUninit::uninit(); |
| 38 |
let ret = aom_codec_dec_init_ver( |
| 39 |
dec.as_mut_ptr(), |
| 40 |
interface, |
| 41 |
&cfg, |
| 42 |
0, |
| 43 |
AOM_DECODER_ABI_VERSION as i32, |
| 44 |
); |
| 45 |
if ret != 0 { |
| 46 |
panic!("Cannot instantiate the decoder {}", ret); |
| 47 |
} |
| 48 |
|
| 49 |
// Was initialized by aom_codec_dec_init_ver(). |
| 50 |
let dec = dec.assume_init(); |
| 51 |
AomDecoder { dec, iter: ptr::null_mut(), pixel: PhantomData } |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
fn decode_packet( |
| 56 |
&mut self, packet: &[u8], rec_fifo: &mut VecDeque<Frame<T>>, w: usize, |
| 57 |
h: usize, chroma_sampling: ChromaSampling, bit_depth: usize, verify: bool, |
| 58 |
) -> DecodeResult { |
| 59 |
let mut corrupted_count = 0; |
| 60 |
unsafe { |
| 61 |
let ret = aom_codec_decode( |
| 62 |
&mut self.dec, |
| 63 |
packet.as_ptr(), |
| 64 |
packet.len(), |
| 65 |
ptr::null_mut(), |
| 66 |
); |
| 67 |
debug!("Decoded. -> {}", ret); |
| 68 |
if ret != 0 { |
| 69 |
let error_msg = aom_codec_error(&mut self.dec); |
| 70 |
debug!( |
| 71 |
" Decode codec_decode failed: {}", |
| 72 |
CStr::from_ptr(error_msg).to_string_lossy() |
| 73 |
); |
| 74 |
let detail = aom_codec_error_detail(&mut self.dec); |
| 75 |
if !detail.is_null() { |
| 76 |
debug!( |
| 77 |
" Decode codec_decode failed {}", |
| 78 |
CStr::from_ptr(detail).to_string_lossy() |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
corrupted_count += 1; |
| 83 |
} |
| 84 |
|
| 85 |
if ret == 0 { |
| 86 |
loop { |
| 87 |
debug!("Retrieving frame"); |
| 88 |
let img = aom_codec_get_frame(&mut self.dec, &mut self.iter); |
| 89 |
debug!("Retrieved."); |
| 90 |
if img.is_null() { |
| 91 |
return DecodeResult::Done; |
| 92 |
} |
| 93 |
let mut corrupted = 0; |
| 94 |
let ret = aom_codec_control( |
| 95 |
&mut self.dec, |
| 96 |
aom_dec_control_id::AOMD_GET_FRAME_CORRUPTED as i32, |
| 97 |
&mut corrupted, |
| 98 |
); |
| 99 |
if ret != 0 { |
| 100 |
let detail = aom_codec_error_detail(&mut self.dec); |
| 101 |
panic!( |
| 102 |
"Decode codec_control failed {}", |
| 103 |
CStr::from_ptr(detail).to_string_lossy() |
| 104 |
); |
| 105 |
} |
| 106 |
corrupted_count += corrupted; |
| 107 |
|
| 108 |
if verify { |
| 109 |
let rec = rec_fifo.pop_front().unwrap(); |
| 110 |
compare_img(img, &rec, bit_depth, w, h, chroma_sampling); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
if corrupted_count > 0 { |
| 116 |
DecodeResult::Corrupted(corrupted_count) |
| 117 |
} else { |
| 118 |
DecodeResult::NotDone |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl<T: Pixel> Drop for AomDecoder<T> { |
| 124 |
fn drop(&mut self) { |
| 125 |
unsafe { aom_codec_destroy(&mut self.dec) }; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
fn compare_img<T: Pixel>( |
| 130 |
img: *const aom_image_t, frame: &Frame<T>, bit_depth: usize, width: usize, |
| 131 |
height: usize, chroma_sampling: ChromaSampling, |
| 132 |
) { |
| 133 |
let img = unsafe { *img }; |
| 134 |
let img_iter = img.planes.iter().zip(img.stride.iter()); |
| 135 |
let planes = if chroma_sampling == ChromaSampling::Cs400 { 1 } else { 3 }; |
| 136 |
|
| 137 |
for (pli, (img_plane, frame_plane)) in |
| 138 |
img_iter.zip(frame.planes.iter()).enumerate().take(planes) |
| 139 |
{ |
| 140 |
let w = width >> frame_plane.cfg.xdec; |
| 141 |
let h = height >> frame_plane.cfg.ydec; |
| 142 |
let rec_stride = frame_plane.cfg.stride; |
| 143 |
|
| 144 |
if bit_depth > 8 { |
| 145 |
let dec_stride = *img_plane.1 as usize / 2; |
| 146 |
|
| 147 |
let dec = unsafe { |
| 148 |
let data = *img_plane.0 as *const u16; |
| 149 |
let size = dec_stride * h; |
| 150 |
|
| 151 |
slice::from_raw_parts(data, size) |
| 152 |
}; |
| 153 |
|
| 154 |
let rec: Vec<u16> = |
| 155 |
frame_plane.data_origin().iter().map(|&v| u16::cast_from(v)).collect(); |
| 156 |
|
| 157 |
compare_plane::<u16>(&rec[..], rec_stride, dec, dec_stride, w, h, pli); |
| 158 |
} else { |
| 159 |
let dec_stride = *img_plane.1 as usize; |
| 160 |
|
| 161 |
let dec = unsafe { |
| 162 |
let data = *img_plane.0 as *const u8; |
| 163 |
let size = dec_stride * h; |
| 164 |
|
| 165 |
slice::from_raw_parts(data, size) |
| 166 |
}; |
| 167 |
|
| 168 |
let rec: Vec<u8> = |
| 169 |
frame_plane.data_origin().iter().map(|&v| u8::cast_from(v)).collect(); |
| 170 |
|
| 171 |
compare_plane::<u8>(&rec[..], rec_stride, dec, dec_stride, w, h, pli); |
| 172 |
} |
| 173 |
} |
| 174 |
} |