Skip to content

Commit

Permalink
Move out ordqueue impl
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 24, 2024
1 parent c6a6975 commit 688741d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 98 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ loop9 = "0.1.5"
# noisy-float 0.2 bug
num-traits = { version = "0.2.17", features = ["i128", "std"] }
crossbeam-utils = "0.8.19"
ordered-channel = { version = "1.0.0", features = ["crossbeam-channel"] }

[dependencies.ffmpeg]
package = "ffmpeg-next"
Expand Down
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ impl From<gif::EncodingError> for Error {
}
}

impl<T> From<crossbeam_channel::SendError<T>> for Error {
impl<T> From<ordered_channel::SendError<T>> for Error {
#[cold]
fn from(_: crossbeam_channel::SendError<T>) -> Self {
fn from(_: ordered_channel::SendError<T>) -> Self {
Self::ThreadSend
}
}

impl From<crossbeam_channel::RecvError> for Error {
impl From<ordered_channel::RecvError> for Error {
#[cold]
fn from(_: crossbeam_channel::RecvError) -> Self {
fn from(_: ordered_channel::RecvError) -> Self {
Self::Aborted
}
}
21 changes: 11 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ use rgb::*;

mod error;
pub use crate::error::*;
mod ordqueue;
use crate::ordqueue::{OrdQueue, OrdQueueIter};
use ordered_channel::Sender as OrdQueue;
use ordered_channel::Receiver as OrdQueueIter;
use ordered_channel::bounded as ordqueue_new;
pub mod progress;
use crate::progress::*;
pub mod c_api;
Expand Down Expand Up @@ -480,15 +481,15 @@ impl Writer {

#[inline(never)]
fn write_frames(&self, write_queue: Receiver<FrameMessage>, writer: &mut dyn Write, reporter: &mut dyn ProgressReporter) -> CatResult<()> {
let (send, recv) = ordqueue::new(2);
let (lzw_queue, lzw_recv) = ordqueue_new(2);
minipool::new_scope((if self.settings.s.fast || self.settings.gifsicle_loss() > 0 { 3 } else { 1 }).try_into().unwrap(), "lzw", move || {
let mut pts_in_delay_units = 0_u64;

let written = Rc::new(Cell::new(0));
let mut enc = RustEncoder::new(writer, written.clone());

let mut n_done = 0;
for tmp in recv {
for tmp in lzw_recv {
let (end_pts, ordinal_frame_number, frame, screen_width, screen_height): (f64, _, _, _, _) = tmp;
// delay=1 doesn't work, and it's too late to drop frames now
let delay = ((end_pts * 100_f64).round() as u64)
Expand Down Expand Up @@ -520,7 +521,7 @@ impl Writer {
}

let frame = RustEncoder::<&mut dyn std::io::Write>::compress_frame(frame, &self.settings)?;
send.push(frame_index, (end_pts, ordinal_frame_number, frame, screen_width, screen_height))?;
lzw_queue.send(frame_index, (end_pts, ordinal_frame_number, frame, screen_width, screen_height))?;
}
Ok(())
})
Expand All @@ -540,15 +541,15 @@ impl Writer {
#[inline(never)]
fn write_inner(&self, decode_queue_recv: Receiver<InputFrame>, writer: &mut dyn Write, reporter: &mut dyn ProgressReporter) -> CatResult<()> {
thread::scope(|s| {
let (diff_queue, diff_queue_recv) = ordqueue::new(0);
let (diff_queue, diff_queue_recv) = ordqueue_new(0);
let resize_thread = thread::Builder::new().name("resize".into()).spawn_scoped(s, move || {
self.make_resize(decode_queue_recv, diff_queue)
})?;
let (quant_queue, quant_queue_recv) = crossbeam_channel::bounded(0);
let diff_thread = thread::Builder::new().name("diff".into()).spawn_scoped(s, move || {
self.make_diffs(diff_queue_recv, quant_queue)
})?;
let (remap_queue, remap_queue_recv) = ordqueue::new(0);
let (remap_queue, remap_queue_recv) = ordqueue_new(0);
let quant_thread = thread::Builder::new().name("quant".into()).spawn_scoped(s, move || {
self.quantize_frames(quant_queue_recv, remap_queue)
})?;
Expand Down Expand Up @@ -591,7 +592,7 @@ impl Writer {
};
let resized = resized_binary_alpha(image, self.settings.s.width, self.settings.s.height, self.settings.matte)?;
let frame_blurred = if self.settings.extra_effort { smart_blur(resized.as_ref()) } else { less_smart_blur(resized.as_ref()) };
diff_queue.push(frame.frame_index, InputFrameResized {
diff_queue.send(frame.frame_index, InputFrameResized {
frame: resized,
frame_blurred,
presentation_timestamp: frame.presentation_timestamp,
Expand Down Expand Up @@ -745,15 +746,15 @@ impl Writer {
let needs_transparency = frame_index > 0 || (frame_index == 0 && first_frame_has_transparency);
let (liq, remap, liq_image, out_buf) = self.quantize(image, &importance_map, frame_index == 0, needs_transparency, prev_frame_keeps)?;

remap_queue.push(frame_index as usize, RemapMessage {
Ok(remap_queue.send(frame_index as usize, RemapMessage {
ordinal_frame_number,
end_pts,
dispose,
liq, remap,
liq_image,
out_buf,
has_next_frame,
})
})?)
})
}

Expand Down
84 changes: 0 additions & 84 deletions src/ordqueue.rs

This file was deleted.

0 comments on commit 688741d

Please sign in to comment.