| 1 |
// Copyright (c) 2017-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 crate::decoder::VideoDetails; |
| 11 |
use rav1e::prelude::*; |
| 12 |
use std::io::Write; |
| 13 |
use std::slice; |
| 14 |
|
| 15 |
#[profiling::function] |
| 16 |
pub fn write_y4m_frame<T: Pixel>( |
| 17 |
y4m_enc: &mut y4m::Encoder<Box<dyn Write + Send>>, rec: &Frame<T>, |
| 18 |
y4m_details: VideoDetails, |
| 19 |
) { |
| 20 |
let planes = |
| 21 |
if y4m_details.chroma_sampling == ChromaSampling::Cs400 { 1 } else { 3 }; |
| 22 |
let bytes_per_sample = if y4m_details.bit_depth > 8 { 2 } else { 1 }; |
| 23 |
let (chroma_width, chroma_height) = y4m_details |
| 24 |
.chroma_sampling |
| 25 |
.get_chroma_dimensions(y4m_details.width, y4m_details.height); |
| 26 |
let pitch_y = y4m_details.width * bytes_per_sample; |
| 27 |
let pitch_uv = chroma_width * bytes_per_sample; |
| 28 |
|
| 29 |
let (mut rec_y, mut rec_u, mut rec_v) = ( |
| 30 |
vec![128u8; pitch_y * y4m_details.height], |
| 31 |
vec![128u8; pitch_uv * chroma_height], |
| 32 |
vec![128u8; pitch_uv * chroma_height], |
| 33 |
); |
| 34 |
|
| 35 |
let (stride_y, stride_u, stride_v) = ( |
| 36 |
rec.planes[0].cfg.stride, |
| 37 |
rec.planes[1].cfg.stride, |
| 38 |
rec.planes[2].cfg.stride, |
| 39 |
); |
| 40 |
|
| 41 |
for (line, line_out) in |
| 42 |
rec.planes[0].data_origin().chunks(stride_y).zip(rec_y.chunks_mut(pitch_y)) |
| 43 |
{ |
| 44 |
if y4m_details.bit_depth > 8 { |
| 45 |
// SAFETY: This is essentially doing a transmute to u16, but safer. |
| 46 |
unsafe { |
| 47 |
line_out.copy_from_slice(slice::from_raw_parts::<u8>( |
| 48 |
line.as_ptr() as *const u8, |
| 49 |
pitch_y, |
| 50 |
)); |
| 51 |
} |
| 52 |
} else { |
| 53 |
line_out.copy_from_slice( |
| 54 |
&line.iter().map(|&v| u8::cast_from(v)).collect::<Vec<u8>>() |
| 55 |
[..pitch_y], |
| 56 |
); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if planes > 1 { |
| 61 |
for (line, line_out) in rec.planes[1] |
| 62 |
.data_origin() |
| 63 |
.chunks(stride_u) |
| 64 |
.zip(rec_u.chunks_mut(pitch_uv)) |
| 65 |
{ |
| 66 |
if y4m_details.bit_depth > 8 { |
| 67 |
// SAFETY: This is essentially doing a transmute to u16, but safer. |
| 68 |
unsafe { |
| 69 |
line_out.copy_from_slice(slice::from_raw_parts::<u8>( |
| 70 |
line.as_ptr() as *const u8, |
| 71 |
pitch_uv, |
| 72 |
)); |
| 73 |
} |
| 74 |
} else { |
| 75 |
line_out.copy_from_slice( |
| 76 |
&line.iter().map(|&v| u8::cast_from(v)).collect::<Vec<u8>>() |
| 77 |
[..pitch_uv], |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
for (line, line_out) in rec.planes[2] |
| 82 |
.data_origin() |
| 83 |
.chunks(stride_v) |
| 84 |
.zip(rec_v.chunks_mut(pitch_uv)) |
| 85 |
{ |
| 86 |
if y4m_details.bit_depth > 8 { |
| 87 |
// SAFETY: This is essentially doing a transmute to u16, but safer. |
| 88 |
unsafe { |
| 89 |
line_out.copy_from_slice(slice::from_raw_parts::<u8>( |
| 90 |
line.as_ptr() as *const u8, |
| 91 |
pitch_uv, |
| 92 |
)); |
| 93 |
} |
| 94 |
} else { |
| 95 |
line_out.copy_from_slice( |
| 96 |
&line.iter().map(|&v| u8::cast_from(v)).collect::<Vec<u8>>() |
| 97 |
[..pitch_uv], |
| 98 |
); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
let rec_frame = y4m::Frame::new([&rec_y, &rec_u, &rec_v], None); |
| 104 |
y4m_enc.write_frame(&rec_frame).unwrap(); |
| 105 |
} |