| 1 | // Copyright 2025 LiveKit, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #![allow(clippy::too_many_arguments)] |
| 16 | |
| 17 | use webrtc_sys::yuv_helper as yuv_sys; |
| 18 | |
| 19 | fn argb_assert_safety(src: &[u8], src_stride: u32, _width: i32, height: i32) { |
| 20 | let height_abs = height.unsigned_abs(); |
| 21 | let min = (src_stride * height_abs) as usize; |
| 22 | assert!(src.len() >= min, "src isn't large enough"); |
| 23 | } |
| 24 | |
| 25 | fn i420_assert_safety( |
| 26 | src_y: &[u8], |
| 27 | src_stride_y: u32, |
| 28 | src_u: &[u8], |
| 29 | src_stride_u: u32, |
| 30 | src_v: &[u8], |
| 31 | src_stride_v: u32, |
| 32 | _width: i32, |
| 33 | height: i32, |
| 34 | ) { |
| 35 | let height_abs = height.unsigned_abs(); |
| 36 | let chroma_height = (height_abs + 1) / 2; |
| 37 | let min_y = (src_stride_y * height_abs) as usize; |
| 38 | let min_u = (src_stride_u * chroma_height) as usize; |
| 39 | let min_v = (src_stride_v * chroma_height) as usize; |
| 40 | |
| 41 | assert!(src_y.len() >= min_y, "src_y isn't large enough"); |
| 42 | assert!(src_u.len() >= min_u, "src_u isn't large enough"); |
| 43 | assert!(src_v.len() >= min_v, "src_v isn't large enough"); |
| 44 | } |
| 45 | |
| 46 | fn nv12_assert_safety( |
| 47 | src_y: &[u8], |
| 48 | src_stride_y: u32, |
| 49 | src_uv: &[u8], |
| 50 | src_stride_uv: u32, |
| 51 | _width: i32, |
| 52 | height: i32, |
| 53 | ) { |
| 54 | let height_abs = height.unsigned_abs(); |
| 55 | let chroma_height = (height_abs + 1) / 2; |
| 56 | |
| 57 | let min_y = (src_stride_y * height_abs) as usize; |
| 58 | let min_uv = (src_stride_uv * chroma_height) as usize; |
| 59 | |
| 60 | assert!(src_y.len() >= min_y, "src_y isn't large enough"); |
| 61 | assert!(src_uv.len() >= min_uv, "src_uv isn't large enough"); |
| 62 | } |
| 63 | |
| 64 | fn i444_assert_safety( |
| 65 | src_y: &[u8], |
| 66 | src_stride_y: u32, |
| 67 | src_u: &[u8], |
| 68 | src_stride_u: u32, |
| 69 | src_v: &[u8], |
| 70 | src_stride_v: u32, |
| 71 | _width: i32, |
| 72 | height: i32, |
| 73 | ) { |
| 74 | let height_abs = height.unsigned_abs(); |
| 75 | let min_y = (src_stride_y * height_abs) as usize; |
| 76 | let min_u = (src_stride_u * height_abs) as usize; |
| 77 | let min_v = (src_stride_v * height_abs) as usize; |
| 78 | |
| 79 | assert!(src_y.len() >= min_y, "src_y isn't large enough"); |
| 80 | assert!(src_u.len() >= min_u, "src_u isn't large enough"); |
| 81 | assert!(src_v.len() >= min_v, "src_v isn't large enough"); |
| 82 | } |
| 83 | |
| 84 | fn i422_assert_safety( |
| 85 | src_y: &[u8], |
| 86 | src_stride_y: u32, |
| 87 | src_u: &[u8], |
| 88 | src_stride_u: u32, |
| 89 | src_v: &[u8], |
| 90 | src_stride_v: u32, |
| 91 | _width: i32, |
| 92 | height: i32, |
| 93 | ) { |
| 94 | let height_abs = height.unsigned_abs(); |
| 95 | let min_y = (src_stride_y * height_abs) as usize; |
| 96 | let min_u = (src_stride_u * height_abs) as usize; |
| 97 | let min_v = (src_stride_v * height_abs) as usize; |
| 98 | |
| 99 | assert!(src_y.len() >= min_y, "src_y isn't large enough"); |
| 100 | assert!(src_u.len() >= min_u, "src_u isn't large enough"); |
| 101 | assert!(src_v.len() >= min_v, "src_v isn't large enough"); |
| 102 | } |
| 103 | |
| 104 | fn i010_assert_safety( |
| 105 | src_y: &[u16], |
| 106 | src_stride_y: u32, |
| 107 | src_u: &[u16], |
| 108 | src_stride_u: u32, |
| 109 | src_v: &[u16], |
| 110 | src_stride_v: u32, |
| 111 | _width: i32, |
| 112 | height: i32, |
| 113 | ) { |
| 114 | let height_abs: u32 = height.unsigned_abs(); |
| 115 | let chroma_height = height_abs / 2; |
| 116 | let min_y = (src_stride_y * height_abs) as usize / 2; |
| 117 | let min_u = (src_stride_u * chroma_height) as usize / 2; |
| 118 | let min_v = (src_stride_v * chroma_height) as usize / 2; |
| 119 | |
| 120 | assert!(src_y.len() >= min_y, "src_y isn't large enough"); |
| 121 | assert!(src_u.len() >= min_u, "src_u isn't large enough"); |
| 122 | assert!(src_v.len() >= min_v, "src_v isn't large enough"); |
| 123 | } |
| 124 | |
| 125 | macro_rules! i420_to_rgba { |
| 126 | ($x:ident) => { |
| 127 | pub fn $x( |
| 128 | src_y: &[u8], |
| 129 | src_stride_y: u32, |
| 130 | src_u: &[u8], |
| 131 | src_stride_u: u32, |
| 132 | src_v: &[u8], |
| 133 | src_stride_v: u32, |
| 134 | dst: &mut [u8], |
| 135 | dst_stride: u32, |
| 136 | width: i32, |
| 137 | height: i32, |
| 138 | ) { |
| 139 | i420_assert_safety( |
| 140 | src_y, |
| 141 | src_stride_y, |
| 142 | src_u, |
| 143 | src_stride_u, |
| 144 | src_v, |
| 145 | src_stride_v, |
| 146 | width, |
| 147 | height, |
| 148 | ); |
| 149 | argb_assert_safety(dst, dst_stride, width, height); |
| 150 | |
| 151 | unsafe { |
| 152 | yuv_sys::ffi::$x( |
| 153 | src_y.as_ptr(), |
| 154 | src_stride_y as i32, |
| 155 | src_u.as_ptr(), |
| 156 | src_stride_u as i32, |
| 157 | src_v.as_ptr(), |
| 158 | src_stride_v as i32, |
| 159 | dst.as_mut_ptr(), |
| 160 | dst_stride as i32, |
| 161 | width, |
| 162 | height, |
| 163 | ) |
| 164 | .unwrap(); |
| 165 | } |
| 166 | } |
| 167 | }; |
| 168 | } |
| 169 | |
| 170 | macro_rules! rgba_to_i420 { |
| 171 | ($x:ident) => { |
| 172 | pub fn $x( |
| 173 | src_argb: &[u8], |
| 174 | src_stride_argb: u32, |
| 175 | dst_y: &mut [u8], |
| 176 | dst_stride_y: u32, |
| 177 | dst_u: &mut [u8], |
| 178 | dst_stride_u: u32, |
| 179 | dst_v: &mut [u8], |
| 180 | dst_stride_v: u32, |
| 181 | width: i32, |
| 182 | height: i32, |
| 183 | ) { |
| 184 | i420_assert_safety( |
| 185 | dst_y, |
| 186 | dst_stride_y, |
| 187 | dst_u, |
| 188 | dst_stride_u, |
| 189 | dst_v, |
| 190 | dst_stride_v, |
| 191 | width, |
| 192 | height, |
| 193 | ); |
| 194 | argb_assert_safety(src_argb, src_stride_argb, width, height); |
| 195 | |
| 196 | unsafe { |
| 197 | yuv_sys::ffi::$x( |
| 198 | src_argb.as_ptr(), |
| 199 | src_stride_argb as i32, |
| 200 | dst_y.as_mut_ptr(), |
| 201 | dst_stride_y as i32, |
| 202 | dst_u.as_mut_ptr(), |
| 203 | dst_stride_u as i32, |
| 204 | dst_v.as_mut_ptr(), |
| 205 | dst_stride_v as i32, |
| 206 | width, |
| 207 | height, |
| 208 | ) |
| 209 | .unwrap(); |
| 210 | } |
| 211 | } |
| 212 | }; |
| 213 | } |
| 214 | |
| 215 | pub fn argb_to_rgb24( |
| 216 | src_argb: &[u8], |
| 217 | src_stride_argb: u32, |
| 218 | dst_rgb24: &mut [u8], |
| 219 | dst_stride_rgb24: u32, |
| 220 | width: i32, |
| 221 | height: i32, |
| 222 | ) { |
| 223 | argb_assert_safety(src_argb, src_stride_argb, width, height); |
| 224 | argb_assert_safety(dst_rgb24, dst_stride_rgb24, width, height); |
| 225 | |
| 226 | unsafe { |
| 227 | yuv_sys::ffi::argb_to_rgb24( |
| 228 | src_argb.as_ptr(), |
| 229 | src_stride_argb as i32, |
| 230 | dst_rgb24.as_mut_ptr(), |
| 231 | dst_stride_rgb24 as i32, |
| 232 | width, |
| 233 | height, |
| 234 | ) |
| 235 | .unwrap(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // I420 <> RGB conversion |
| 240 | rgba_to_i420!(argb_to_i420); |
| 241 | rgba_to_i420!(abgr_to_i420); |
| 242 | |
| 243 | i420_to_rgba!(i420_to_argb); |
| 244 | i420_to_rgba!(i420_to_bgra); |
| 245 | i420_to_rgba!(i420_to_abgr); |
| 246 | i420_to_rgba!(i420_to_rgba); |
| 247 | |
| 248 | pub fn i420_to_nv12( |
| 249 | src_y: &[u8], |
| 250 | src_stride_y: u32, |
| 251 | src_u: &[u8], |
| 252 | src_stride_u: u32, |
| 253 | src_v: &[u8], |
| 254 | src_stride_v: u32, |
| 255 | dst_y: &mut [u8], |
| 256 | dst_stride_y: u32, |
| 257 | dst_uv: &mut [u8], |
| 258 | dst_stride_uv: u32, |
| 259 | width: i32, |
| 260 | height: i32, |
| 261 | ) { |
| 262 | i420_assert_safety( |
| 263 | src_y, |
| 264 | src_stride_y, |
| 265 | src_u, |
| 266 | src_stride_u, |
| 267 | src_v, |
| 268 | src_stride_v, |
| 269 | width, |
| 270 | height, |
| 271 | ); |
| 272 | nv12_assert_safety(dst_y, dst_stride_y, dst_uv, dst_stride_uv, width, height); |
| 273 | |
| 274 | unsafe { |
| 275 | yuv_sys::ffi::i420_to_nv12( |
| 276 | src_y.as_ptr(), |
| 277 | src_stride_y as i32, |
| 278 | src_u.as_ptr(), |
| 279 | src_stride_u as i32, |
| 280 | src_v.as_ptr(), |
| 281 | src_stride_v as i32, |
| 282 | dst_y.as_mut_ptr(), |
| 283 | dst_stride_y as i32, |
| 284 | dst_uv.as_mut_ptr(), |
| 285 | dst_stride_uv as i32, |
| 286 | width, |
| 287 | height, |
| 288 | ) |
| 289 | .unwrap(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | pub fn nv12_to_i420( |
| 294 | src_y: &[u8], |
| 295 | src_stride_y: u32, |
| 296 | src_uv: &[u8], |
| 297 | src_stride_uv: u32, |
| 298 | dst_y: &mut [u8], |
| 299 | dst_stride_y: u32, |
| 300 | dst_u: &mut [u8], |
| 301 | dst_stride_u: u32, |
| 302 | dst_v: &mut [u8], |
| 303 | dst_stride_v: u32, |
| 304 | width: i32, |
| 305 | height: i32, |
| 306 | ) { |
| 307 | nv12_assert_safety(src_y, src_stride_y, src_uv, src_stride_uv, width, height); |
| 308 | i420_assert_safety( |
| 309 | dst_y, |
| 310 | dst_stride_y, |
| 311 | dst_u, |
| 312 | dst_stride_u, |
| 313 | dst_v, |
| 314 | dst_stride_v, |
| 315 | width, |
| 316 | height, |
| 317 | ); |
| 318 | |
| 319 | unsafe { |
| 320 | yuv_sys::ffi::nv12_to_i420( |
| 321 | src_y.as_ptr(), |
| 322 | src_stride_y as i32, |
| 323 | src_uv.as_ptr(), |
| 324 | src_stride_uv as i32, |
| 325 | dst_y.as_mut_ptr(), |
| 326 | dst_stride_y as i32, |
| 327 | dst_u.as_mut_ptr(), |
| 328 | dst_stride_u as i32, |
| 329 | dst_v.as_mut_ptr(), |
| 330 | dst_stride_v as i32, |
| 331 | width, |
| 332 | height, |
| 333 | ) |
| 334 | .unwrap(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | pub fn i444_to_i420( |
| 339 | src_y: &[u8], |
| 340 | src_stride_y: u32, |
| 341 | src_u: &[u8], |
| 342 | src_stride_u: u32, |
| 343 | src_v: &[u8], |
| 344 | src_stride_v: u32, |
| 345 | dst_y: &mut [u8], |
| 346 | dst_stride_y: u32, |
| 347 | dst_u: &mut [u8], |
| 348 | dst_stride_u: u32, |
| 349 | dst_v: &mut [u8], |
| 350 | dst_stride_v: u32, |
| 351 | width: i32, |
| 352 | height: i32, |
| 353 | ) { |
| 354 | i444_assert_safety( |
| 355 | src_y, |
| 356 | src_stride_y, |
| 357 | src_u, |
| 358 | src_stride_u, |
| 359 | src_v, |
| 360 | src_stride_v, |
| 361 | width, |
| 362 | height, |
| 363 | ); |
| 364 | i420_assert_safety( |
| 365 | dst_y, |
| 366 | dst_stride_y, |
| 367 | dst_u, |
| 368 | dst_stride_u, |
| 369 | dst_v, |
| 370 | dst_stride_v, |
| 371 | width, |
| 372 | height, |
| 373 | ); |
| 374 | |
| 375 | unsafe { |
| 376 | yuv_sys::ffi::i444_to_i420( |
| 377 | src_y.as_ptr(), |
| 378 | src_stride_y as i32, |
| 379 | src_u.as_ptr(), |
| 380 | src_stride_u as i32, |
| 381 | src_v.as_ptr(), |
| 382 | src_stride_v as i32, |
| 383 | dst_y.as_mut_ptr(), |
| 384 | dst_stride_y as i32, |
| 385 | dst_u.as_mut_ptr(), |
| 386 | dst_stride_u as i32, |
| 387 | dst_v.as_mut_ptr(), |
| 388 | dst_stride_v as i32, |
| 389 | width, |
| 390 | height, |
| 391 | ) |
| 392 | .unwrap(); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | pub fn i422_to_i420( |
| 397 | src_y: &[u8], |
| 398 | src_stride_y: u32, |
| 399 | src_u: &[u8], |
| 400 | src_stride_u: u32, |
| 401 | src_v: &[u8], |
| 402 | src_stride_v: u32, |
| 403 | dst_y: &mut [u8], |
| 404 | dst_stride_y: u32, |
| 405 | dst_u: &mut [u8], |
| 406 | dst_stride_u: u32, |
| 407 | dst_v: &mut [u8], |
| 408 | dst_stride_v: u32, |
| 409 | width: i32, |
| 410 | height: i32, |
| 411 | ) { |
| 412 | i422_assert_safety( |
| 413 | src_y, |
| 414 | src_stride_y, |
| 415 | src_u, |
| 416 | src_stride_u, |
| 417 | src_v, |
| 418 | src_stride_v, |
| 419 | width, |
| 420 | height, |
| 421 | ); |
| 422 | i420_assert_safety( |
| 423 | dst_y, |
| 424 | dst_stride_y, |
| 425 | dst_u, |
| 426 | dst_stride_u, |
| 427 | dst_v, |
| 428 | dst_stride_v, |
| 429 | width, |
| 430 | height, |
| 431 | ); |
| 432 | |
| 433 | unsafe { |
| 434 | yuv_sys::ffi::i422_to_i420( |
| 435 | src_y.as_ptr(), |
| 436 | src_stride_y as i32, |
| 437 | src_u.as_ptr(), |
| 438 | src_stride_u as i32, |
| 439 | src_v.as_ptr(), |
| 440 | src_stride_v as i32, |
| 441 | dst_y.as_mut_ptr(), |
| 442 | dst_stride_y as i32, |
| 443 | dst_u.as_mut_ptr(), |
| 444 | dst_stride_u as i32, |
| 445 | dst_v.as_mut_ptr(), |
| 446 | dst_stride_v as i32, |
| 447 | width, |
| 448 | height, |
| 449 | ) |
| 450 | .unwrap() |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | pub fn i010_to_i420( |
| 455 | src_y: &[u16], |
| 456 | src_stride_y: u32, |
| 457 | src_u: &[u16], |
| 458 | src_stride_u: u32, |
| 459 | src_v: &[u16], |
| 460 | src_stride_v: u32, |
| 461 | dst_y: &mut [u8], |
| 462 | dst_stride_y: u32, |
| 463 | dst_u: &mut [u8], |
| 464 | dst_stride_u: u32, |
| 465 | dst_v: &mut [u8], |
| 466 | dst_stride_v: u32, |
| 467 | width: i32, |
| 468 | height: i32, |
| 469 | ) { |
| 470 | i010_assert_safety( |
| 471 | src_y, |
| 472 | src_stride_y, |
| 473 | src_u, |
| 474 | src_stride_u, |
| 475 | src_v, |
| 476 | src_stride_v, |
| 477 | width, |
| 478 | height, |
| 479 | ); |
| 480 | i420_assert_safety( |
| 481 | dst_y, |
| 482 | dst_stride_y, |
| 483 | dst_u, |
| 484 | dst_stride_u, |
| 485 | dst_v, |
| 486 | dst_stride_v, |
| 487 | width, |
| 488 | height, |
| 489 | ); |
| 490 | |
| 491 | unsafe { |
| 492 | yuv_sys::ffi::i010_to_i420( |
| 493 | src_y.as_ptr(), |
| 494 | src_stride_y as i32, |
| 495 | src_u.as_ptr(), |
| 496 | src_stride_u as i32, |
| 497 | src_v.as_ptr(), |
| 498 | src_stride_v as i32, |
| 499 | dst_y.as_mut_ptr(), |
| 500 | dst_stride_y as i32, |
| 501 | dst_u.as_mut_ptr(), |
| 502 | dst_stride_u as i32, |
| 503 | dst_v.as_mut_ptr(), |
| 504 | dst_stride_v as i32, |
| 505 | width, |
| 506 | height, |
| 507 | ) |
| 508 | .unwrap() |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | pub fn nv12_to_argb( |
| 513 | src_y: &[u8], |
| 514 | src_stride_y: u32, |
| 515 | src_uv: &[u8], |
| 516 | src_stride_uv: u32, |
| 517 | dst_argb: &mut [u8], |
| 518 | dst_stride_argb: u32, |
| 519 | width: i32, |
| 520 | height: i32, |
| 521 | ) { |
| 522 | nv12_assert_safety(src_y, src_stride_y, src_uv, src_stride_uv, width, height); |
| 523 | argb_assert_safety(dst_argb, dst_stride_argb, width, height); |
| 524 | |
| 525 | unsafe { |
| 526 | yuv_sys::ffi::nv12_to_argb( |
| 527 | src_y.as_ptr(), |
| 528 | src_stride_y as i32, |
| 529 | src_uv.as_ptr(), |
| 530 | src_stride_uv as i32, |
| 531 | dst_argb.as_mut_ptr(), |
| 532 | dst_stride_argb as i32, |
| 533 | width, |
| 534 | height, |
| 535 | ) |
| 536 | .unwrap(); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | pub fn nv12_to_abgr( |
| 541 | src_y: &[u8], |
| 542 | src_stride_y: u32, |
| 543 | src_uv: &[u8], |
| 544 | src_stride_uv: u32, |
| 545 | dst_abgr: &mut [u8], |
| 546 | dst_stride_abgr: u32, |
| 547 | width: i32, |
| 548 | height: i32, |
| 549 | ) { |
| 550 | nv12_assert_safety(src_y, src_stride_y, src_uv, src_stride_uv, width, height); |
| 551 | argb_assert_safety(dst_abgr, dst_stride_abgr, width, height); |
| 552 | |
| 553 | unsafe { |
| 554 | yuv_sys::ffi::nv12_to_abgr( |
| 555 | src_y.as_ptr(), |
| 556 | src_stride_y as i32, |
| 557 | src_uv.as_ptr(), |
| 558 | src_stride_uv as i32, |
| 559 | dst_abgr.as_mut_ptr(), |
| 560 | dst_stride_abgr as i32, |
| 561 | width, |
| 562 | height, |
| 563 | ) |
| 564 | .unwrap(); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | pub fn i444_to_argb( |
| 569 | src_y: &[u8], |
| 570 | src_stride_y: u32, |
| 571 | src_u: &[u8], |
| 572 | src_stride_u: u32, |
| 573 | src_v: &[u8], |
| 574 | src_stride_v: u32, |
| 575 | dst_argb: &mut [u8], |
| 576 | dst_stride_argb: u32, |
| 577 | width: i32, |
| 578 | height: i32, |
| 579 | ) { |
| 580 | i444_assert_safety( |
| 581 | src_y, |
| 582 | src_stride_y, |
| 583 | src_u, |
| 584 | src_stride_u, |
| 585 | src_v, |
| 586 | src_stride_v, |
| 587 | width, |
| 588 | height, |
| 589 | ); |
| 590 | argb_assert_safety(dst_argb, dst_stride_argb, width, height); |
| 591 | |
| 592 | unsafe { |
| 593 | yuv_sys::ffi::i444_to_argb( |
| 594 | src_y.as_ptr(), |
| 595 | src_stride_y as i32, |
| 596 | src_u.as_ptr(), |
| 597 | src_stride_u as i32, |
| 598 | src_v.as_ptr(), |
| 599 | src_stride_v as i32, |
| 600 | dst_argb.as_mut_ptr(), |
| 601 | dst_stride_argb as i32, |
| 602 | width, |
| 603 | height, |
| 604 | ) |
| 605 | .unwrap(); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | pub fn i444_to_abgr( |
| 610 | src_y: &[u8], |
| 611 | src_stride_y: u32, |
| 612 | src_u: &[u8], |
| 613 | src_stride_u: u32, |
| 614 | src_v: &[u8], |
| 615 | src_stride_v: u32, |
| 616 | dst_abgr: &mut [u8], |
| 617 | dst_stride_abgr: u32, |
| 618 | width: i32, |
| 619 | height: i32, |
| 620 | ) { |
| 621 | i444_assert_safety( |
| 622 | src_y, |
| 623 | src_stride_y, |
| 624 | src_u, |
| 625 | src_stride_u, |
| 626 | src_v, |
| 627 | src_stride_v, |
| 628 | width, |
| 629 | height, |
| 630 | ); |
| 631 | argb_assert_safety(dst_abgr, dst_stride_abgr, width, height); |
| 632 | |
| 633 | unsafe { |
| 634 | yuv_sys::ffi::i444_to_abgr( |
| 635 | src_y.as_ptr(), |
| 636 | src_stride_y as i32, |
| 637 | src_u.as_ptr(), |
| 638 | src_stride_u as i32, |
| 639 | src_v.as_ptr(), |
| 640 | src_stride_v as i32, |
| 641 | dst_abgr.as_mut_ptr(), |
| 642 | dst_stride_abgr as i32, |
| 643 | width, |
| 644 | height, |
| 645 | ) |
| 646 | .unwrap() |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | pub fn i422_to_argb( |
| 651 | src_y: &[u8], |
| 652 | src_stride_y: u32, |
| 653 | src_u: &[u8], |
| 654 | src_stride_u: u32, |
| 655 | src_v: &[u8], |
| 656 | src_stride_v: u32, |
| 657 | dst_argb: &mut [u8], |
| 658 | dst_stride_argb: u32, |
| 659 | width: i32, |
| 660 | height: i32, |
| 661 | ) { |
| 662 | i422_assert_safety( |
| 663 | src_y, |
| 664 | src_stride_y, |
| 665 | src_u, |
| 666 | src_stride_u, |
| 667 | src_v, |
| 668 | src_stride_v, |
| 669 | width, |
| 670 | height, |
| 671 | ); |
| 672 | argb_assert_safety(dst_argb, dst_stride_argb, width, height); |
| 673 | |
| 674 | unsafe { |
| 675 | yuv_sys::ffi::i422_to_argb( |
| 676 | src_y.as_ptr(), |
| 677 | src_stride_y as i32, |
| 678 | src_u.as_ptr(), |
| 679 | src_stride_u as i32, |
| 680 | src_v.as_ptr(), |
| 681 | src_stride_v as i32, |
| 682 | dst_argb.as_mut_ptr(), |
| 683 | dst_stride_argb as i32, |
| 684 | width, |
| 685 | height, |
| 686 | ) |
| 687 | .unwrap(); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | pub fn i422_to_abgr( |
| 692 | src_y: &[u8], |
| 693 | src_stride_y: u32, |
| 694 | src_u: &[u8], |
| 695 | src_stride_u: u32, |
| 696 | src_v: &[u8], |
| 697 | src_stride_v: u32, |
| 698 | dst_abgr: &mut [u8], |
| 699 | dst_stride_abgr: u32, |
| 700 | width: i32, |
| 701 | height: i32, |
| 702 | ) { |
| 703 | i422_assert_safety( |
| 704 | src_y, |
| 705 | src_stride_y, |
| 706 | src_u, |
| 707 | src_stride_u, |
| 708 | src_v, |
| 709 | src_stride_v, |
| 710 | width, |
| 711 | height, |
| 712 | ); |
| 713 | argb_assert_safety(dst_abgr, dst_stride_abgr, width, height); |
| 714 | |
| 715 | unsafe { |
| 716 | yuv_sys::ffi::i422_to_abgr( |
| 717 | src_y.as_ptr(), |
| 718 | src_stride_y as i32, |
| 719 | src_u.as_ptr(), |
| 720 | src_stride_u as i32, |
| 721 | src_v.as_ptr(), |
| 722 | src_stride_v as i32, |
| 723 | dst_abgr.as_mut_ptr(), |
| 724 | dst_stride_abgr as i32, |
| 725 | width, |
| 726 | height, |
| 727 | ) |
| 728 | .unwrap() |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | pub fn i010_to_argb( |
| 733 | src_y: &[u16], |
| 734 | src_stride_y: u32, |
| 735 | src_u: &[u16], |
| 736 | src_stride_u: u32, |
| 737 | src_v: &[u16], |
| 738 | src_stride_v: u32, |
| 739 | dst_argb: &mut [u8], |
| 740 | dst_stride_argb: u32, |
| 741 | width: i32, |
| 742 | height: i32, |
| 743 | ) { |
| 744 | i010_assert_safety( |
| 745 | src_y, |
| 746 | src_stride_y, |
| 747 | src_u, |
| 748 | src_stride_u, |
| 749 | src_v, |
| 750 | src_stride_v, |
| 751 | width, |
| 752 | height, |
| 753 | ); |
| 754 | argb_assert_safety(dst_argb, dst_stride_argb, width, height); |
| 755 | |
| 756 | unsafe { |
| 757 | yuv_sys::ffi::i010_to_argb( |
| 758 | src_y.as_ptr(), |
| 759 | src_stride_y as i32, |
| 760 | src_u.as_ptr(), |
| 761 | src_stride_u as i32, |
| 762 | src_v.as_ptr(), |
| 763 | src_stride_v as i32, |
| 764 | dst_argb.as_mut_ptr(), |
| 765 | dst_stride_argb as i32, |
| 766 | width, |
| 767 | height, |
| 768 | ) |
| 769 | .unwrap() |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | pub fn i010_to_abgr( |
| 774 | src_y: &[u16], |
| 775 | src_stride_y: u32, |
| 776 | src_u: &[u16], |
| 777 | src_stride_u: u32, |
| 778 | src_v: &[u16], |
| 779 | src_stride_v: u32, |
| 780 | dst_abgr: &mut [u8], |
| 781 | dst_stride_abgr: u32, |
| 782 | width: i32, |
| 783 | height: i32, |
| 784 | ) { |
| 785 | i010_assert_safety( |
| 786 | src_y, |
| 787 | src_stride_y, |
| 788 | src_u, |
| 789 | src_stride_u, |
| 790 | src_v, |
| 791 | src_stride_v, |
| 792 | width, |
| 793 | height, |
| 794 | ); |
| 795 | argb_assert_safety(dst_abgr, dst_stride_abgr, width, height); |
| 796 | |
| 797 | unsafe { |
| 798 | yuv_sys::ffi::i010_to_abgr( |
| 799 | src_y.as_ptr(), |
| 800 | src_stride_y as i32, |
| 801 | src_u.as_ptr(), |
| 802 | src_stride_u as i32, |
| 803 | src_v.as_ptr(), |
| 804 | src_stride_v as i32, |
| 805 | dst_abgr.as_mut_ptr(), |
| 806 | dst_stride_abgr as i32, |
| 807 | width, |
| 808 | height, |
| 809 | ) |
| 810 | .unwrap() |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | pub fn abgr_to_nv12( |
| 815 | src_abgr: &[u8], |
| 816 | src_stride_abgr: u32, |
| 817 | dst_y: &mut [u8], |
| 818 | dst_stride_y: u32, |
| 819 | dst_uv: &mut [u8], |
| 820 | dst_stride_uv: u32, |
| 821 | width: i32, |
| 822 | height: i32, |
| 823 | ) { |
| 824 | argb_assert_safety(src_abgr, src_stride_abgr, width, height); |
| 825 | nv12_assert_safety(dst_y, dst_stride_y, dst_uv, dst_stride_uv, width, height); |
| 826 | |
| 827 | unsafe { |
| 828 | yuv_sys::ffi::abgr_to_nv12( |
| 829 | src_abgr.as_ptr(), |
| 830 | src_stride_abgr as i32, |
| 831 | dst_y.as_mut_ptr(), |
| 832 | dst_stride_y as i32, |
| 833 | dst_uv.as_mut_ptr(), |
| 834 | dst_stride_uv as i32, |
| 835 | width, |
| 836 | height, |
| 837 | ) |
| 838 | .unwrap() |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | pub fn argb_to_nv12( |
| 843 | src_argb: &[u8], |
| 844 | src_stride_argb: u32, |
| 845 | dst_y: &mut [u8], |
| 846 | dst_stride_y: u32, |
| 847 | dst_uv: &mut [u8], |
| 848 | dst_stride_uv: u32, |
| 849 | width: i32, |
| 850 | height: i32, |
| 851 | ) { |
| 852 | argb_assert_safety(src_argb, src_stride_argb, width, height); |
| 853 | nv12_assert_safety(dst_y, dst_stride_y, dst_uv, dst_stride_uv, width, height); |
| 854 | |
| 855 | unsafe { |
| 856 | yuv_sys::ffi::argb_to_nv12( |
| 857 | src_argb.as_ptr(), |
| 858 | src_stride_argb as i32, |
| 859 | dst_y.as_mut_ptr(), |
| 860 | dst_stride_y as i32, |
| 861 | dst_uv.as_mut_ptr(), |
| 862 | dst_stride_uv as i32, |
| 863 | width, |
| 864 | height, |
| 865 | ) |
| 866 | .unwrap() |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | pub fn yuy2_to_i420( |
| 871 | src_yuy2: &[u8], |
| 872 | src_stride_yuy2: u32, |
| 873 | dst_y: &mut [u8], |
| 874 | dst_stride_y: u32, |
| 875 | dst_u: &mut [u8], |
| 876 | dst_stride_u: u32, |
| 877 | dst_v: &mut [u8], |
| 878 | dst_stride_v: u32, |
| 879 | width: i32, |
| 880 | height: i32, |
| 881 | ) { |
| 882 | let row_bytes = width.unsigned_abs().saturating_mul(2); |
| 883 | assert!( |
| 884 | src_stride_yuy2 >= row_bytes, |
| 885 | "yuy2 stride is shorter than the row" |
| 886 | ); |
| 887 | argb_assert_safety(src_yuy2, src_stride_yuy2, width, height); |
| 888 | i420_assert_safety( |
| 889 | dst_y, |
| 890 | dst_stride_y, |
| 891 | dst_u, |
| 892 | dst_stride_u, |
| 893 | dst_v, |
| 894 | dst_stride_v, |
| 895 | width, |
| 896 | height, |
| 897 | ); |
| 898 | |
| 899 | unsafe { |
| 900 | yuv_sys::ffi::yuy2_to_i420( |
| 901 | src_yuy2.as_ptr(), |
| 902 | src_stride_yuy2 as i32, |
| 903 | dst_y.as_mut_ptr(), |
| 904 | dst_stride_y as i32, |
| 905 | dst_u.as_mut_ptr(), |
| 906 | dst_stride_u as i32, |
| 907 | dst_v.as_mut_ptr(), |
| 908 | dst_stride_v as i32, |
| 909 | width, |
| 910 | height, |
| 911 | ) |
| 912 | .unwrap() |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | pub fn argb_scale( |
| 917 | src: &[u8], |
| 918 | src_stride: u32, |
| 919 | src_width: i32, |
| 920 | src_height: i32, |
| 921 | dst: &mut [u8], |
| 922 | dst_stride: u32, |
| 923 | dst_width: i32, |
| 924 | dst_height: i32, |
| 925 | ) { |
| 926 | argb_assert_safety(src, src_stride, src_width, src_height); |
| 927 | argb_assert_safety(dst, dst_stride, dst_width, dst_height); |
| 928 | unsafe { |
| 929 | yuv_sys::ffi::argb_scale( |
| 930 | src.as_ptr(), |
| 931 | src_stride as i32, |
| 932 | src_width, |
| 933 | src_height, |
| 934 | dst.as_mut_ptr(), |
| 935 | dst_stride as i32, |
| 936 | dst_width, |
| 937 | dst_height, |
| 938 | ) |
| 939 | .unwrap() |
| 940 | } |
| 941 | } |