use dcmnorm_dictionary::tags;
use dcmnorm_object::DefaultDicomObject;
pub(crate) fn transfer_syntax_uid(object: &DefaultDicomObject) -> String {
object
.meta()
.transfer_syntax
.trim_end_matches(['\0', ' '])
.to_owned()
}
pub(crate) fn frames_per_second(object: &DefaultDicomObject) -> f64 {
if let Some(rate) = object
.get(tags::CINE_RATE)
.and_then(|e| e.to_str().ok())
.and_then(|s| s.trim().parse::<f64>().ok())
{
if rate > 0.0 {
return rate;
}
}
if let Some(frame_time_ms) = object
.get(tags::FRAME_TIME)
.and_then(|e| e.to_str().ok())
.and_then(|s| s.trim().parse::<f64>().ok())
{
if frame_time_ms > 0.0 {
return 1000.0 / frame_time_ms;
}
}
30.0
}
#[cfg(feature = "ffmpeg-codec-linked")]
pub mod mpeg_impl {
use dcmnorm_dictionary::tags;
use dcmnorm_object::DefaultDicomObject;
use super::{frames_per_second, transfer_syntax_uid};
use ffmpeg::codec::{self, packet::Packet};
use ffmpeg::format::Pixel;
use ffmpeg::frame;
use ffmpeg::software::scaling;
use ffmpeg_next as ffmpeg;
fn split_into_access_units(
codec_id: codec::Id,
decoder_context: *mut ffmpeg::ffi::AVCodecContext,
stream: &[u8],
) -> Result<Vec<Vec<u8>>, String> {
let raw_id: ffmpeg::ffi::AVCodecID = codec_id.into();
let parser = unsafe { ffmpeg::ffi::av_parser_init(raw_id as i32) };
if parser.is_null() {
return Err(format!("no bitstream parser available for {codec_id:?}"));
}
struct ParserGuard(*mut ffmpeg::ffi::AVCodecParserContext);
impl Drop for ParserGuard {
fn drop(&mut self) {
unsafe { ffmpeg::ffi::av_parser_close(self.0) };
}
}
let _guard = ParserGuard(parser);
let parse_one = |parser: *mut ffmpeg::ffi::AVCodecParserContext,
buf: &[u8]|
-> Result<(i32, Vec<u8>), String> {
let mut out_ptr: *mut u8 = std::ptr::null_mut();
let mut out_size: std::os::raw::c_int = 0;
let consumed = unsafe {
ffmpeg::ffi::av_parser_parse2(
parser,
decoder_context,
&mut out_ptr,
&mut out_size,
buf.as_ptr(),
buf.len() as std::os::raw::c_int,
ffmpeg::ffi::AV_NOPTS_VALUE,
ffmpeg::ffi::AV_NOPTS_VALUE,
0,
)
};
if consumed < 0 {
return Err("bitstream parser error while splitting video access units".to_owned());
}
let packet = if out_size > 0 && !out_ptr.is_null() {
unsafe { std::slice::from_raw_parts(out_ptr, out_size as usize) }.to_vec()
} else {
Vec::new()
};
Ok((consumed, packet))
};
let mut packets = Vec::new();
let mut remaining = stream;
while !remaining.is_empty() {
let (consumed, packet) = parse_one(parser, remaining)?;
if !packet.is_empty() {
packets.push(packet);
}
if consumed == 0 {
break;
}
remaining = &remaining[consumed as usize..];
}
let (_, packet) = parse_one(parser, &[])?;
if !packet.is_empty() {
packets.push(packet);
}
Ok(packets)
}
fn ensure_ffmpeg_init() -> Result<(), String> {
static INIT: std::sync::Once = std::sync::Once::new();
static INIT_RESULT: std::sync::OnceLock<Result<(), String>> = std::sync::OnceLock::new();
INIT.call_once(|| {
let _ =
INIT_RESULT.set(ffmpeg::init().map_err(|e| format!("ffmpeg::init failed: {e}")));
});
INIT_RESULT.get().cloned().unwrap_or(Ok(()))
}
fn codec_id_for_transfer_syntax(uid: &str) -> Option<codec::Id> {
match uid {
"1.2.840.10008.1.2.4.100" | "1.2.840.10008.1.2.4.101" => Some(codec::Id::MPEG2VIDEO),
"1.2.840.10008.1.2.4.102"
| "1.2.840.10008.1.2.4.103"
| "1.2.840.10008.1.2.4.104"
| "1.2.840.10008.1.2.4.105"
| "1.2.840.10008.1.2.4.106" => Some(codec::Id::H264),
"1.2.840.10008.1.2.4.107" | "1.2.840.10008.1.2.4.108" => Some(codec::Id::HEVC),
_ => None,
}
}
pub fn can_encode_video(uid: &str) -> bool {
ensure_ffmpeg_init().is_ok()
&& codec_id_for_transfer_syntax(uid)
.and_then(ffmpeg::encoder::find)
.is_some()
}
pub fn decode_mpeg_pixel_data(object: &DefaultDicomObject) -> Result<Vec<u8>, String> {
ensure_ffmpeg_init()?;
let ts_uid = transfer_syntax_uid(object);
let codec_id = codec_id_for_transfer_syntax(&ts_uid)
.ok_or_else(|| format!("unsupported video transfer syntax: {ts_uid}"))?;
let fragments = object
.element(tags::PIXEL_DATA)
.map_err(|e| format!("missing PixelData: {e}"))?
.fragments()
.ok_or_else(|| "expected encapsulated video pixel data".to_owned())?;
if fragments.is_empty() {
return Err("no video data to decode".to_owned());
}
let mut bitstream = Vec::new();
for fragment in fragments {
bitstream.extend_from_slice(fragment);
}
let rows = object
.get(tags::ROWS)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing Rows attribute".to_owned())?;
let cols = object
.get(tags::COLUMNS)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing Columns attribute".to_owned())?;
let samples_per_pixel = object
.get(tags::SAMPLES_PER_PIXEL)
.and_then(|e| e.uint16().ok())
.unwrap_or(1);
let dst_format = if samples_per_pixel == 1 {
Pixel::GRAY8
} else {
Pixel::RGB24
};
let decoder_codec = ffmpeg::decoder::find(codec_id)
.ok_or_else(|| format!("no {codec_id:?} decoder available in this FFmpeg build"))?;
let mut decoder = ffmpeg::decoder::new()
.open_as(decoder_codec)
.map_err(|e| format!("failed to open {codec_id:?} decoder: {e}"))?
.video()
.map_err(|e| format!("{codec_id:?} decoder is not a video decoder: {e}"))?;
let mut scaler: Option<scaling::Context> = None;
let mut out = Vec::new();
let mut raw = frame::Video::empty();
let mut drain_frames = |decoder: &mut ffmpeg::decoder::Video| -> Result<(), String> {
loop {
match decoder.receive_frame(&mut raw) {
Ok(()) => {
if scaler.is_none() {
scaler = Some(
scaling::Context::get(
decoder.format(),
decoder.width(),
decoder.height(),
dst_format,
u32::from(cols),
u32::from(rows),
scaling::flag::Flags::BILINEAR,
)
.map_err(|e| {
format!("failed to create pixel format converter: {e}")
})?,
);
}
let mut converted = frame::Video::empty();
scaler
.as_mut()
.unwrap()
.run(&raw, &mut converted)
.map_err(|e| format!("pixel format conversion failed: {e}"))?;
let bytes_per_row = usize::from(cols) * usize::from(samples_per_pixel);
let stride = converted.stride(0);
let data = converted.data(0);
for y in 0..usize::from(rows) {
let start = y * stride;
out.extend_from_slice(&data[start..start + bytes_per_row]);
}
}
Err(ffmpeg::Error::Other { errno }) if errno == ffmpeg::error::EAGAIN => break,
Err(ffmpeg::Error::Eof) => break,
Err(e) => return Err(format!("video decode failed: {e}")),
}
}
Ok(())
};
let decoder_context_ptr = unsafe { decoder.as_mut_ptr() };
let access_units = split_into_access_units(codec_id, decoder_context_ptr, &bitstream)?;
if access_units.is_empty() {
return Err("bitstream parser found no access units to decode".to_owned());
}
for access_unit in &access_units {
decoder
.send_packet(&Packet::copy(access_unit))
.map_err(|e| format!("failed to send video data to decoder: {e}"))?;
drain_frames(&mut decoder)?;
}
decoder
.send_eof()
.map_err(|e| format!("failed to flush decoder: {e}"))?;
drain_frames(&mut decoder)?;
if out.is_empty() {
return Err("no frames decoded".to_owned());
}
Ok(out)
}
pub fn encode_mpeg_pixel_data(
object: &DefaultDicomObject,
target_uid: &str,
) -> Result<Vec<Vec<u8>>, String> {
ensure_ffmpeg_init()?;
let codec_id = codec_id_for_transfer_syntax(target_uid)
.ok_or_else(|| format!("unsupported video transfer syntax: {target_uid}"))?;
let rows = object
.get(tags::ROWS)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing Rows attribute".to_owned())?;
let cols = object
.get(tags::COLUMNS)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing Columns attribute".to_owned())?;
let samples_per_pixel = object
.get(tags::SAMPLES_PER_PIXEL)
.and_then(|e| e.uint16().ok())
.unwrap_or(1);
let bits_allocated = object
.get(tags::BITS_ALLOCATED)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing BitsAllocated attribute".to_owned())?;
if bits_allocated != 8 {
return Err(format!(
"video encoding only supports 8-bit samples, got BitsAllocated={bits_allocated}"
));
}
let number_of_frames = object
.get(tags::NUMBER_OF_FRAMES)
.and_then(|e| e.to_str().ok())
.and_then(|s| s.trim().parse::<usize>().ok())
.unwrap_or(1)
.max(1);
let pixel_data = object
.element(tags::PIXEL_DATA)
.map_err(|e| format!("missing PixelData: {e}"))?
.to_bytes()
.map_err(|e| format!("failed to access pixel data: {e}"))?
.to_vec();
let src_format = if samples_per_pixel == 1 {
Pixel::GRAY8
} else {
Pixel::RGB24
};
let frame_len = usize::from(rows) * usize::from(cols) * usize::from(samples_per_pixel);
let expected_len = frame_len * number_of_frames;
if pixel_data.len() < expected_len {
return Err(format!(
"PixelData too short for {number_of_frames} frame(s) of {rows}x{cols}x{samples_per_pixel}: \
expected at least {expected_len} bytes, got {}",
pixel_data.len()
));
}
let encoder_codec = ffmpeg::encoder::find(codec_id)
.ok_or_else(|| format!("no {codec_id:?} encoder available in this FFmpeg build"))?;
let fps = frames_per_second(object);
let time_base = ffmpeg::Rational::new(1, (fps.round() as i32).max(1));
let mut encoder = codec::context::Context::new_with_codec(encoder_codec)
.encoder()
.video()
.map_err(|e| format!("failed to create {codec_id:?} encoder context: {e}"))?;
encoder.set_width(u32::from(cols));
encoder.set_height(u32::from(rows));
encoder.set_format(Pixel::YUV420P);
encoder.set_time_base(time_base);
encoder.set_frame_rate(Some(time_base.invert()));
let mut encoder = encoder
.open()
.map_err(|e| format!("failed to open {codec_id:?} encoder: {e}"))?;
let mut scaler = scaling::Context::get(
src_format,
u32::from(cols),
u32::from(rows),
Pixel::YUV420P,
u32::from(cols),
u32::from(rows),
scaling::flag::Flags::BILINEAR,
)
.map_err(|e| format!("failed to create pixel format converter: {e}"))?;
let mut bitstream = Vec::new();
let mut encoded = Packet::empty();
let mut drain_packets = |encoder: &mut ffmpeg::encoder::Video,
bitstream: &mut Vec<u8>|
-> Result<(), String> {
loop {
match encoder.receive_packet(&mut encoded) {
Ok(()) => {
if let Some(data) = encoded.data() {
bitstream.extend_from_slice(data);
}
}
Err(ffmpeg::Error::Other { errno }) if errno == ffmpeg::error::EAGAIN => break,
Err(ffmpeg::Error::Eof) => break,
Err(e) => return Err(format!("video encode failed: {e}")),
}
}
Ok(())
};
for frame_index in 0..number_of_frames {
let start = frame_index * frame_len;
let frame_bytes = &pixel_data[start..start + frame_len];
let mut src_frame = frame::Video::new(src_format, u32::from(cols), u32::from(rows));
let src_bytes_per_row = usize::from(cols) * usize::from(samples_per_pixel);
let src_stride = src_frame.stride(0);
{
let dst = src_frame.data_mut(0);
for y in 0..usize::from(rows) {
let dst_start = y * src_stride;
let src_start = y * src_bytes_per_row;
dst[dst_start..dst_start + src_bytes_per_row]
.copy_from_slice(&frame_bytes[src_start..src_start + src_bytes_per_row]);
}
}
let mut yuv_frame = frame::Video::empty();
scaler
.run(&src_frame, &mut yuv_frame)
.map_err(|e| format!("pixel format conversion failed: {e}"))?;
yuv_frame.set_pts(Some(frame_index as i64));
encoder
.send_frame(&yuv_frame)
.map_err(|e| format!("failed to send frame {frame_index} to encoder: {e}"))?;
drain_packets(&mut encoder, &mut bitstream)?;
}
encoder
.send_eof()
.map_err(|e| format!("failed to flush encoder: {e}"))?;
drain_packets(&mut encoder, &mut bitstream)?;
if bitstream.is_empty() {
return Err("encoder produced no data".to_owned());
}
Ok(vec![bitstream])
}
}
#[cfg(not(feature = "ffmpeg-codec-linked"))]
pub mod mpeg_impl {
use std::io::{ErrorKind, Read, Write};
use std::process::{Command, Stdio};
use dcmnorm_dictionary::tags;
use dcmnorm_object::DefaultDicomObject;
use super::{frames_per_second, transfer_syntax_uid};
pub const FFMPEG_NOT_FOUND: &str = "MPEG-2, MPEG-4 AVC/H.264 and HEVC transfer syntaxes need the `ffmpeg` command on PATH, \
and it was not found. Install FFmpeg (for example `apt install ffmpeg`, `brew install ffmpeg` \
or `winget install ffmpeg`) and try again.";
fn raw_format(uid: &str, demux: bool) -> Option<&'static str> {
match uid {
"1.2.840.10008.1.2.4.100" | "1.2.840.10008.1.2.4.101" => {
Some(if demux { "mpegvideo" } else { "mpeg2video" })
}
"1.2.840.10008.1.2.4.102"
| "1.2.840.10008.1.2.4.103"
| "1.2.840.10008.1.2.4.104"
| "1.2.840.10008.1.2.4.105"
| "1.2.840.10008.1.2.4.106" => Some("h264"),
"1.2.840.10008.1.2.4.107" | "1.2.840.10008.1.2.4.108" => Some("hevc"),
_ => None,
}
}
pub fn can_encode_video(uid: &str) -> bool {
raw_format(uid, false).is_some()
}
fn run_ffmpeg(args: &[String], input: Vec<u8>) -> Result<Vec<u8>, String> {
run_program("ffmpeg", args, input)
}
pub(super) fn run_program(
program: &str,
args: &[String],
input: Vec<u8>,
) -> Result<Vec<u8>, String> {
let mut child = Command::new(program)
.args(["-hide_banner", "-loglevel", "error"])
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| {
if e.kind() == ErrorKind::NotFound {
FFMPEG_NOT_FOUND.to_owned()
} else {
format!("could not start ffmpeg: {e}")
}
})?;
let mut stdin = child.stdin.take().expect("stdin is piped");
let writer = std::thread::spawn(move || {
let _ = stdin.write_all(&input);
});
let mut stderr = child.stderr.take().expect("stderr is piped");
let stderr_reader = std::thread::spawn(move || {
let mut text = String::new();
let _ = stderr.read_to_string(&mut text);
text
});
let mut output = Vec::new();
child
.stdout
.take()
.expect("stdout is piped")
.read_to_end(&mut output)
.map_err(|e| format!("failed to read ffmpeg output: {e}"))?;
let status = child
.wait()
.map_err(|e| format!("failed to wait for ffmpeg: {e}"))?;
let _ = writer.join();
let stderr_text = stderr_reader.join().unwrap_or_default();
if !status.success() {
let detail = stderr_text.trim();
return Err(if detail.is_empty() {
format!("ffmpeg failed ({status})")
} else {
format!("ffmpeg failed ({status}): {detail}")
});
}
Ok(output)
}
fn raw_pixel_format(samples_per_pixel: u16) -> &'static str {
if samples_per_pixel == 1 {
"gray"
} else {
"rgb24"
}
}
pub fn decode_mpeg_pixel_data(object: &DefaultDicomObject) -> Result<Vec<u8>, String> {
let ts_uid = transfer_syntax_uid(object);
let format = raw_format(&ts_uid, true)
.ok_or_else(|| format!("unsupported video transfer syntax: {ts_uid}"))?;
let fragments = object
.element(tags::PIXEL_DATA)
.map_err(|e| format!("missing PixelData: {e}"))?
.fragments()
.ok_or_else(|| "expected encapsulated video pixel data".to_owned())?;
if fragments.is_empty() {
return Err("no video data to decode".to_owned());
}
let mut bitstream = Vec::new();
for fragment in fragments {
bitstream.extend_from_slice(fragment);
}
let rows = object
.get(tags::ROWS)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing Rows attribute".to_owned())?;
let cols = object
.get(tags::COLUMNS)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing Columns attribute".to_owned())?;
let samples_per_pixel = object
.get(tags::SAMPLES_PER_PIXEL)
.and_then(|e| e.uint16().ok())
.unwrap_or(1);
let args: Vec<String> = [
"-f",
format,
"-i",
"pipe:0",
"-f",
"rawvideo",
"-pix_fmt",
raw_pixel_format(samples_per_pixel),
"-s",
&format!("{cols}x{rows}"),
"-sws_flags",
"bilinear",
"pipe:1",
]
.iter()
.map(|s| s.to_string())
.collect();
let out = run_ffmpeg(&args, bitstream)?;
let frame_len = usize::from(rows) * usize::from(cols) * usize::from(samples_per_pixel);
if out.is_empty() {
return Err("no frames decoded".to_owned());
}
if frame_len == 0 || out.len() % frame_len != 0 {
return Err(format!(
"ffmpeg returned {} bytes, not a whole number of {cols}x{rows}x{samples_per_pixel} frames",
out.len()
));
}
Ok(out)
}
pub fn encode_mpeg_pixel_data(
object: &DefaultDicomObject,
target_uid: &str,
) -> Result<Vec<Vec<u8>>, String> {
let format = raw_format(target_uid, false)
.ok_or_else(|| format!("unsupported video transfer syntax: {target_uid}"))?;
let rows = object
.get(tags::ROWS)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing Rows attribute".to_owned())?;
let cols = object
.get(tags::COLUMNS)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing Columns attribute".to_owned())?;
let samples_per_pixel = object
.get(tags::SAMPLES_PER_PIXEL)
.and_then(|e| e.uint16().ok())
.unwrap_or(1);
let bits_allocated = object
.get(tags::BITS_ALLOCATED)
.and_then(|e| e.uint16().ok())
.ok_or_else(|| "missing BitsAllocated attribute".to_owned())?;
if bits_allocated != 8 {
return Err(format!(
"video encoding only supports 8-bit samples, got BitsAllocated={bits_allocated}"
));
}
let number_of_frames = object
.get(tags::NUMBER_OF_FRAMES)
.and_then(|e| e.to_str().ok())
.and_then(|s| s.trim().parse::<usize>().ok())
.unwrap_or(1)
.max(1);
let mut pixel_data = object
.element(tags::PIXEL_DATA)
.map_err(|e| format!("missing PixelData: {e}"))?
.to_bytes()
.map_err(|e| format!("failed to access pixel data: {e}"))?
.to_vec();
let frame_len = usize::from(rows) * usize::from(cols) * usize::from(samples_per_pixel);
let expected_len = frame_len * number_of_frames;
if pixel_data.len() < expected_len {
return Err(format!(
"PixelData too short for {number_of_frames} frame(s) of {rows}x{cols}x{samples_per_pixel}: \
expected at least {expected_len} bytes, got {}",
pixel_data.len()
));
}
pixel_data.truncate(expected_len);
let fps = (frames_per_second(object).round() as i64).max(1);
let args: Vec<String> = [
"-f",
"rawvideo",
"-pix_fmt",
raw_pixel_format(samples_per_pixel),
"-s",
&format!("{cols}x{rows}"),
"-framerate",
&fps.to_string(),
"-i",
"pipe:0",
"-pix_fmt",
"yuv420p",
"-sws_flags",
"bilinear",
"-f",
format,
"pipe:1",
]
.iter()
.map(|s| s.to_string())
.collect();
let bitstream = run_ffmpeg(&args, pixel_data)?;
if bitstream.is_empty() {
return Err("encoder produced no data".to_owned());
}
Ok(vec![bitstream])
}
}
pub use mpeg_impl::{can_encode_video, decode_mpeg_pixel_data, encode_mpeg_pixel_data};
#[cfg(test)]
mod tests {
use super::{decode_mpeg_pixel_data, encode_mpeg_pixel_data};
use dcmnorm_core::value::PixelFragmentSequence;
use dcmnorm_core::{DataElement, PrimitiveValue, VR};
use dcmnorm_dictionary::tags;
use dcmnorm_object::{DefaultDicomObject, FileMetaTableBuilder};
const ROWS: u16 = 120;
const COLS: u16 = 256;
const FRAME_LEN: usize = ROWS as usize * COLS as usize * 3;
fn encoder_available(ts_uid: &str) -> bool {
let available = super::can_encode_video(ts_uid);
if !available {
eprintln!("skipping {ts_uid}: this build has no encoder for it");
}
available
}
fn video_codec_available() -> bool {
if cfg!(feature = "ffmpeg-codec-linked") {
return true;
}
let found = std::process::Command::new("ffmpeg")
.arg("-version")
.output()
.map(|o| o.status.success())
.unwrap_or(false);
if !found {
eprintln!("skipping: no ffmpeg on PATH");
}
found
}
fn solid_band_frame(band_rows_from_top: usize, color: [u8; 3]) -> Vec<u8> {
let mut frame = vec![0u8; FRAME_LEN];
for y in 0..band_rows_from_top {
for x in 0..usize::from(COLS) {
let i = (y * usize::from(COLS) + x) * 3;
frame[i..i + 3].copy_from_slice(&color);
}
}
frame
}
fn top_band_match_ratio(frame: &[u8], color: [u8; 3]) -> f64 {
let mut matches = 0usize;
let checked = 20 * usize::from(COLS);
for i in 0..checked {
let px = &frame[i * 3..i * 3 + 3];
if (0..3).all(|c| (i32::from(px[c]) - i32::from(color[c])).abs() < 40) {
matches += 1;
}
}
matches as f64 / checked as f64
}
fn empty_object_with_transfer_syntax(ts_uid: &str) -> DefaultDicomObject {
let meta = FileMetaTableBuilder::new()
.transfer_syntax(ts_uid)
.build()
.expect("minimal meta table should build");
DefaultDicomObject::new_empty_with_meta(meta)
}
fn native_rgb_object(number_of_frames: usize, pixel_data: Vec<u8>) -> DefaultDicomObject {
let mut object = empty_object_with_transfer_syntax("1.2.840.10008.1.2.1");
for element in [
DataElement::new(tags::ROWS, VR::US, PrimitiveValue::from(ROWS)),
DataElement::new(tags::COLUMNS, VR::US, PrimitiveValue::from(COLS)),
DataElement::new(tags::SAMPLES_PER_PIXEL, VR::US, PrimitiveValue::from(3u16)),
DataElement::new(tags::BITS_ALLOCATED, VR::US, PrimitiveValue::from(8u16)),
DataElement::new(
tags::PIXEL_REPRESENTATION,
VR::US,
PrimitiveValue::from(0u16),
),
DataElement::new(
tags::PLANAR_CONFIGURATION,
VR::US,
PrimitiveValue::from(0u16),
),
DataElement::new(
tags::PHOTOMETRIC_INTERPRETATION,
VR::CS,
PrimitiveValue::from("RGB".to_owned()),
),
DataElement::new(
tags::NUMBER_OF_FRAMES,
VR::IS,
PrimitiveValue::from(number_of_frames.to_string()),
),
DataElement::new(tags::PIXEL_DATA, VR::OW, PrimitiveValue::from(pixel_data)),
] {
object.put(element);
}
object
}
fn encapsulated_video_object(
source: &DefaultDicomObject,
ts_uid: &str,
bitstream: Vec<u8>,
) -> DefaultDicomObject {
let mut object = empty_object_with_transfer_syntax(ts_uid);
for element in source.iter() {
object.put(element.clone());
}
object.put(DataElement::new(
tags::PIXEL_DATA,
VR::OB,
PixelFragmentSequence::new(vec![0], vec![bitstream]),
));
object
}
#[test]
fn h264_multi_frame_round_trip_preserves_frame_content_and_order() {
if !video_codec_available() || !encoder_available("1.2.840.10008.1.2.4.102") {
return;
}
let frame0 = solid_band_frame(0, [0, 0, 0]);
let frame1 = solid_band_frame(20, [255, 0, 0]);
let frame2 = solid_band_frame(20, [0, 255, 0]);
let mut pixel_data = Vec::with_capacity(FRAME_LEN * 3);
pixel_data.extend_from_slice(&frame0);
pixel_data.extend_from_slice(&frame1);
pixel_data.extend_from_slice(&frame2);
let source = native_rgb_object(3, pixel_data);
let fragments = encode_mpeg_pixel_data(&source, "1.2.840.10008.1.2.4.102")
.expect("H.264 encode should succeed");
assert_eq!(
fragments.len(),
1,
"PS3.5 requires the whole bitstream in one fragment"
);
let encoded =
encapsulated_video_object(&source, "1.2.840.10008.1.2.4.102", fragments[0].clone());
let decoded = decode_mpeg_pixel_data(&encoded).expect("H.264 decode should succeed");
assert_eq!(decoded.len(), FRAME_LEN * 3, "expected all 3 frames back");
let decoded_frames: Vec<&[u8]> = decoded.chunks_exact(FRAME_LEN).collect();
assert!(
top_band_match_ratio(decoded_frames[0], [0, 0, 0]) > 0.9,
"frame 0 should stay black"
);
assert!(
top_band_match_ratio(decoded_frames[1], [255, 0, 0]) > 0.9,
"frame 1 should be red"
);
assert!(
top_band_match_ratio(decoded_frames[2], [0, 255, 0]) > 0.9,
"frame 2 should be green"
);
}
#[test]
fn mpeg2_and_hevc_multi_frame_round_trips_preserve_frame_order() {
if !video_codec_available() {
return;
}
let frame0 = solid_band_frame(0, [0, 0, 0]);
let frame1 = solid_band_frame(20, [255, 0, 0]);
let frame2 = solid_band_frame(20, [0, 255, 0]);
let mut pixel_data = Vec::with_capacity(FRAME_LEN * 3);
pixel_data.extend_from_slice(&frame0);
pixel_data.extend_from_slice(&frame1);
pixel_data.extend_from_slice(&frame2);
let source = native_rgb_object(3, pixel_data);
for ts_uid in ["1.2.840.10008.1.2.4.100", "1.2.840.10008.1.2.4.107"] {
if !encoder_available(ts_uid) {
continue;
}
let fragments = encode_mpeg_pixel_data(&source, ts_uid)
.unwrap_or_else(|e| panic!("encode for {ts_uid} should succeed: {e}"));
let encoded = encapsulated_video_object(&source, ts_uid, fragments[0].clone());
let decoded = decode_mpeg_pixel_data(&encoded)
.unwrap_or_else(|e| panic!("decode for {ts_uid} should succeed: {e}"));
assert_eq!(
decoded.len(),
FRAME_LEN * 3,
"{ts_uid}: expected all 3 frames back"
);
let decoded_frames: Vec<&[u8]> = decoded.chunks_exact(FRAME_LEN).collect();
assert!(
top_band_match_ratio(decoded_frames[0], [0, 0, 0]) > 0.85,
"{ts_uid}: frame 0 should stay black"
);
assert!(
top_band_match_ratio(decoded_frames[1], [255, 0, 0]) > 0.85,
"{ts_uid}: frame 1 should be red"
);
assert!(
top_band_match_ratio(decoded_frames[2], [0, 255, 0]) > 0.85,
"{ts_uid}: frame 2 should be green"
);
}
}
#[test]
fn encode_rejects_unsupported_transfer_syntax() {
let source = native_rgb_object(1, vec![0u8; FRAME_LEN]);
let result = encode_mpeg_pixel_data(&source, "1.2.840.10008.1.2.4.90");
assert!(result.is_err(), "expected an error, got {result:?}");
}
#[cfg(not(feature = "ffmpeg-codec-linked"))]
#[test]
fn missing_ffmpeg_command_says_how_to_install_it() {
let result = super::mpeg_impl::run_program("dcmnorm-test-no-such-ffmpeg", &[], Vec::new());
assert_eq!(result.unwrap_err(), super::mpeg_impl::FFMPEG_NOT_FOUND);
}
#[test]
fn mpeg2_is_always_encodable() {
assert!(super::can_encode_video("1.2.840.10008.1.2.4.100"));
assert!(!super::can_encode_video("1.2.840.10008.1.2.4.90"));
}
}