basis_universal/transcoding/transcoder.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
use super::*;
use crate::UserData;
use basis_universal_sys as sys;
/// A transcoder that can convert compressed basis-universal data to compressed GPU formats or raw
/// color data
pub struct Transcoder(*mut sys::Transcoder);
/// Lightweight description of a mip level on a single image within basis data
#[derive(Default, Debug, Copy, Clone)]
pub struct ImageLevelDescription {
pub original_width: u32,
pub original_height: u32,
pub block_count: u32,
}
/// Info for an image within basis data
pub type ImageInfo = sys::basist_basisu_image_info;
/// Info for a mip level of a single image within basis data
pub type ImageLevelInfo = sys::basist_basisu_image_level_info;
/// Info for the complete basis file
pub type FileInfo = sys::FileInfo;
/// Extra parameters for transcoding an image
#[derive(Default, Debug, Clone)]
pub struct TranscodeParameters {
/// The image to transcode
pub image_index: u32,
/// The mip level of the image to transcode
pub level_index: u32,
/// Optional flags can affect transcoding in various ways
pub decode_flags: Option<DecodeFlags>,
/// Optional override for row pitch
pub output_row_pitch_in_blocks_or_pixels: Option<u32>,
/// Optional override for number of rows to transcode
pub output_rows_in_pixels: Option<u32>,
}
/// Error result from trying to transcode an image
#[derive(Debug, Copy, Clone)]
pub enum TranscodeError {
TranscodeFormatNotSupported,
ImageLevelNotFound,
TranscodeFailed,
}
impl Default for Transcoder {
fn default() -> Self {
Self::new()
}
}
impl Transcoder {
/// Create a transcoder
pub fn new() -> Transcoder {
unsafe { Transcoder(sys::transcoder_new()) }
}
/// Validates the .basis file. This computes a crc16 over the entire file, so it's slow.
pub fn validate_file_checksums(
&self,
data: &[u8],
full_validation: bool,
) -> bool {
unsafe {
sys::transcoder_validate_file_checksums(
self.0,
data.as_ptr() as _,
data.len() as u32,
full_validation,
)
}
}
/// Quick header validation - no crc16 checks.
pub fn validate_header(
&self,
data: &[u8],
) -> bool {
unsafe { sys::transcoder_validate_header(self.0, data.as_ptr() as _, data.len() as u32) }
}
/// The type of texture represented by the basis data
pub fn basis_texture_type(
&self,
data: &[u8],
) -> BasisTextureType {
unsafe {
sys::transcoder_get_texture_type(self.0, data.as_ptr() as _, data.len() as u32).into()
}
}
/// The basis texture format of the basis data
pub fn basis_texture_format(
&self,
data: &[u8],
) -> BasisTextureFormat {
unsafe {
sys::transcoder_get_tex_format(self.0, data.as_ptr() as _, data.len() as u32).into()
}
}
pub fn user_data(
&self,
data: &[u8],
) -> Result<UserData, ()> {
let mut userdata = UserData::default();
let result = unsafe {
sys::transcoder_get_userdata(
self.0,
data.as_ptr() as _,
data.len() as u32,
&mut userdata.userdata0,
&mut userdata.userdata1,
)
};
if result {
Ok(userdata)
} else {
Err(())
}
}
/// Number of images in the basis data
pub fn image_count(
&self,
data: &[u8],
) -> u32 {
unsafe { sys::transcoder_get_total_images(self.0, data.as_ptr() as _, data.len() as u32) }
}
/// Number of mipmap levels for the specified image in the basis data
pub fn image_level_count(
&self,
data: &[u8],
image_index: u32,
) -> u32 {
unsafe {
sys::transcoder_get_total_image_levels(
self.0,
data.as_ptr() as _,
data.len() as u32,
image_index,
)
}
}
/// Returns basic information about an image. Note that orig_width/orig_height may not be a multiple of 4.
pub fn image_level_description(
&self,
data: &[u8],
image_index: u32,
level_index: u32,
) -> Option<ImageLevelDescription> {
let mut description = ImageLevelDescription::default();
unsafe {
if sys::transcoder_get_image_level_desc(
self.0,
data.as_ptr() as _,
data.len() as u32,
image_index,
level_index,
&mut description.original_width,
&mut description.original_height,
&mut description.block_count,
) {
Some(description)
} else {
None
}
}
}
/// Returns information about the specified image.
pub fn image_info(
&self,
data: &[u8],
image_index: u32,
) -> Option<ImageInfo> {
let mut image_info = unsafe { std::mem::zeroed::<ImageInfo>() };
unsafe {
if sys::transcoder_get_image_info(
self.0,
data.as_ptr() as _,
data.len() as u32,
&mut image_info,
image_index,
) {
Some(image_info)
} else {
None
}
}
}
/// Returns information about the specified image's mipmap level.
pub fn image_level_info(
&self,
data: &[u8],
image_index: u32,
level_index: u32,
) -> Option<ImageLevelInfo> {
let mut image_level_info = unsafe { std::mem::zeroed::<ImageLevelInfo>() };
unsafe {
if sys::transcoder_get_image_level_info(
self.0,
data.as_ptr() as _,
data.len() as u32,
&mut image_level_info,
image_index,
level_index,
) {
Some(image_level_info)
} else {
None
}
}
}
/// Get a description of the basis file and low-level information about each slice.
pub fn file_info(
&self,
data: &[u8],
) -> Option<FileInfo> {
let mut file_info = unsafe { std::mem::zeroed::<FileInfo>() };
unsafe {
if sys::transcoder_get_file_info(
self.0,
data.as_ptr() as _,
data.len() as u32,
&mut file_info,
) {
Some(file_info)
} else {
None
}
}
}
/// prepare_transcoding() must be called before calling transcode_slice() or transcode_image_level().
/// This is `start_transcoding` in the original library
/// For ETC1S files, this call decompresses the selector/endpoint codebooks, so ideally you would only call this once per .basis file (not each image/mipmap level).
pub fn prepare_transcoding(
&mut self,
data: &[u8],
) -> Result<(), ()> {
transcoder_init();
unsafe {
if sys::transcoder_start_transcoding(self.0, data.as_ptr() as _, data.len() as u32) {
Ok(())
} else {
Err(())
}
}
}
/// Parallel with `prepare_transcoding()`, named `stop_transcoding` in the original library
pub fn end_transcoding(&mut self) {
unsafe {
let result = sys::transcoder_stop_transcoding(self.0);
// I think this function is actually infallible, so don't return a result
debug_assert!(result);
}
}
/// Returns true if prepare_transcoding() has been called.
pub fn is_prepared_to_transcode(&self) -> bool {
unsafe { sys::transcoder_get_ready_to_transcode(self.0) }
}
/// transcode_image_level() decodes a single mipmap level from the .basis file to any of the supported output texture formats.
/// It'll first find the slice(s) to transcode, then call transcode_slice() one or two times to decode both the color and alpha texture data (or RG texture data from two slices for BC5).
/// If the .basis file doesn't have alpha slices, the output alpha blocks will be set to fully opaque (all 255's).
/// Currently, to decode to PVRTC1 the basis texture's dimensions in pixels must be a power of 2, due to PVRTC1 format requirements.
/// output_blocks_buf_size_in_blocks_or_pixels should be at least the image level's total_blocks (num_blocks_x * num_blocks_y), or the total number of output pixels if fmt==cTFRGBA32.
/// output_row_pitch_in_blocks_or_pixels: Number of blocks or pixels per row. If 0, the transcoder uses the slice's num_blocks_x or orig_width (NOT num_blocks_x * 4). Ignored for PVRTC1 (due to texture swizzling).
/// output_rows_in_pixels: Ignored unless fmt is cRGBA32. The total number of output rows in the output buffer. If 0, the transcoder assumes the slice's orig_height (NOT num_blocks_y * 4).
/// Notes:
/// - basisu_transcoder_init() must have been called first to initialize the transcoder lookup tables before calling this function.
/// - This method assumes the output texture buffer is readable. In some cases to handle alpha, the transcoder will write temporary data to the output texture in
/// a first pass, which will be read in a second pass.
pub fn transcode_image_level(
&self,
data: &[u8],
transcode_format: TranscoderTextureFormat,
transcode_parameters: TranscodeParameters,
) -> Result<Vec<u8>, TranscodeError> {
let image_index = transcode_parameters.image_index;
let level_index = transcode_parameters.level_index;
//
// Check that the transcode format is supported for the stored texture's basis format
//
let basis_format = self.basis_texture_format(data);
if !basis_format.can_transcode_to_format(transcode_format) {
return Err(TranscodeError::TranscodeFormatNotSupported);
}
//
// Determine required size for the buffer
//
let description = self
.image_level_description(data, image_index, level_index)
.ok_or(TranscodeError::ImageLevelNotFound)?;
let required_buffer_bytes = transcode_format.calculate_minimum_output_buffer_bytes(
description.original_width,
description.original_height,
description.block_count,
transcode_parameters.output_row_pitch_in_blocks_or_pixels,
transcode_parameters.output_rows_in_pixels,
) as usize;
//
// unwrap_or() the optional parameters
//
let decode_flags = transcode_parameters
.decode_flags
.unwrap_or_else(DecodeFlags::empty);
let output_row_pitch_in_blocks_or_pixels = transcode_parameters
.output_row_pitch_in_blocks_or_pixels
.unwrap_or(0);
let output_rows_in_pixels = transcode_parameters.output_rows_in_pixels.unwrap_or(0);
let transcoder_state = std::ptr::null_mut();
//
// Transcode
//
let mut output = vec![0_u8; required_buffer_bytes];
let success = unsafe {
sys::transcoder_transcode_image_level(
self.0,
data.as_ptr() as _,
data.len() as u32,
image_index,
level_index,
output.as_mut_ptr() as _,
output.len() as u32,
transcode_format.into(),
decode_flags.bits(),
output_row_pitch_in_blocks_or_pixels,
transcoder_state,
output_rows_in_pixels,
)
};
if success {
Ok(output)
} else {
Err(TranscodeError::TranscodeFailed)
}
}
// Not implemented
//
// // Finds the basis slice corresponding to the specified image/level/alpha params, or -1 if the slice can't be found.
// int find_slice(const void *pData, uint32_t data_size, uint32_t image_index, uint32_t level_index, bool alpha_data) const;
//
// // transcode_slice() decodes a single slice from the .basis file. It's a low-level API - most likely you want to use transcode_image_level().
// // This is a low-level API, and will be needed to be called multiple times to decode some texture formats (like BC3, BC5, or ETC2).
// // output_blocks_buf_size_in_blocks_or_pixels is just used for verification to make sure the output buffer is large enough.
// // output_blocks_buf_size_in_blocks_or_pixels should be at least the image level's total_blocks (num_blocks_x * num_blocks_y), or the total number of output pixels if fmt==cTFRGBA32.
// // output_block_stride_in_bytes: Number of bytes between each output block.
// // output_row_pitch_in_blocks_or_pixels: Number of blocks or pixels per row. If 0, the transcoder uses the slice's num_blocks_x or orig_width (NOT num_blocks_x * 4). Ignored for PVRTC1 (due to texture swizzling).
// // output_rows_in_pixels: Ignored unless fmt is cRGBA32. The total number of output rows in the output buffer. If 0, the transcoder assumes the slice's orig_height (NOT num_blocks_y * 4).
// // Notes:
// // - basisu_transcoder_init() must have been called first to initialize the transcoder lookup tables before calling this function.
// bool transcode_slice(const void *pData, uint32_t data_size, uint32_t slice_index,
// void *pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels,
// block_format fmt, uint32_t output_block_stride_in_bytes, uint32_t decode_flags = 0, uint32_t output_row_pitch_in_blocks_or_pixels = 0, basisu_transcoder_state * pState = nullptr, void* pAlpha_blocks = nullptr,
// uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1) const;
//
// static void write_opaque_alpha_blocks(
// uint32_t num_blocks_x, uint32_t num_blocks_y,
// void* pOutput_blocks, block_format fmt,
// uint32_t block_stride_in_bytes, uint32_t output_row_pitch_in_blocks_or_pixels);
}
impl Drop for Transcoder {
fn drop(&mut self) {
unsafe {
sys::transcoder_delete(self.0);
}
}
}
pub struct LowLevelUastcTranscoder(*mut sys::LowLevelUastcTranscoder);
impl Default for LowLevelUastcTranscoder {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug)]
pub struct SliceParametersUastc {
pub num_blocks_x: u32,
pub num_blocks_y: u32,
pub has_alpha: bool,
pub original_width: u32,
pub original_height: u32,
}
impl LowLevelUastcTranscoder {
/// Create a LowLevelUastcTranscoder
pub fn new() -> LowLevelUastcTranscoder {
transcoder_init();
unsafe { LowLevelUastcTranscoder(sys::low_level_uastc_transcoder_new()) }
}
pub fn transcode_slice(
&self,
data: &[u8],
slice_parameters: SliceParametersUastc,
decode_flags: DecodeFlags,
transcode_block_format: TranscoderBlockFormat,
) -> Result<Vec<u8>, TranscodeError> {
let bc1_allow_threecolor_blocks = false;
let transcoder_state = std::ptr::null_mut();
let channel0 = 0;
let channel1 = 3;
let output_row_pitch_in_blocks_or_pixels =
(slice_parameters.original_width + transcode_block_format.block_width() - 1)
/ transcode_block_format.block_width();
let output_rows_in_pixels = slice_parameters.original_height;
let total_slice_blocks = slice_parameters.num_blocks_x * slice_parameters.num_blocks_y;
let required_buffer_bytes = transcode_block_format.calculate_minimum_output_buffer_bytes(
slice_parameters.original_width,
slice_parameters.original_height,
total_slice_blocks,
Some(output_row_pitch_in_blocks_or_pixels),
Some(output_rows_in_pixels),
) as usize;
let output_block_or_pixel_stride_in_bytes =
transcode_block_format.bytes_per_block_or_pixel();
let mut output = vec![0_u8; required_buffer_bytes];
let success = unsafe {
sys::low_level_uastc_transcoder_transcode_slice(
self.0,
output.as_mut_ptr() as _,
slice_parameters.num_blocks_x,
slice_parameters.num_blocks_y,
data.as_ptr() as _,
data.len() as u32,
transcode_block_format.into(),
output_block_or_pixel_stride_in_bytes,
bc1_allow_threecolor_blocks,
slice_parameters.has_alpha,
slice_parameters.original_width,
slice_parameters.original_height,
output_row_pitch_in_blocks_or_pixels,
transcoder_state,
output_rows_in_pixels,
channel0,
channel1,
decode_flags.bits(),
)
};
if success {
Ok(output)
} else {
Err(TranscodeError::TranscodeFailed)
}
}
}
impl Drop for LowLevelUastcTranscoder {
fn drop(&mut self) {
unsafe {
sys::low_level_uastc_transcoder_delete(self.0);
}
}
}