tttocea608: use mul_div_round

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/314>
This commit is contained in:
Mathieu Duponchelle 2020-04-23 20:06:13 +02:00
parent 5c82e6fe6c
commit 510c1cf2df
3 changed files with 33 additions and 50 deletions

View file

@ -9,7 +9,6 @@ repository = "https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs"
[dependencies] [dependencies]
glib = { git = "https://github.com/gtk-rs/glib" } glib = { git = "https://github.com/gtk-rs/glib" }
gstreamer-sys = { git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs-sys" }
combine = "4.0" combine = "4.0"
either = "1" either = "1"
uuid = { version = "0.8", features = ["v4"] } uuid = { version = "0.8", features = ["v4"] }

View file

@ -30,8 +30,6 @@ extern crate gst;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate gstreamer_sys as gst_sys;
#[cfg(test)] #[cfg(test)]
#[macro_use] #[macro_use]
extern crate pretty_assertions; extern crate pretty_assertions;

View file

@ -26,26 +26,24 @@ use gst::subclass::prelude::*;
use super::cea608tott_ffi as ffi; use super::cea608tott_ffi as ffi;
use std::sync::Mutex; use std::sync::Mutex;
fn scale_round(val: u64, num: u64, denom: u64) -> u64 { fn decrement_pts(
unsafe { gst_sys::gst_util_uint64_scale_round(val, num, denom) } min_frame_no: u64,
} frame_no: &mut u64,
fps_n: u64,
fn decrement_pts(min_frame_no: u64, frame_no: &mut u64, fps_n: u64, fps_d: u64) -> (u64, u64) { fps_d: u64,
let old_pts = scale_round( ) -> (gst::ClockTime, gst::ClockTime) {
(*frame_no * gst::SECOND).nseconds().unwrap() as u64, let old_pts = (*frame_no * gst::SECOND)
fps_d, .mul_div_round(fps_d, fps_n)
fps_n, .unwrap();
);
if *frame_no > min_frame_no { if *frame_no > min_frame_no {
*frame_no -= 1; *frame_no -= 1;
} }
let new_pts = scale_round( let new_pts = (*frame_no * gst::SECOND)
(*frame_no * gst::SECOND).nseconds().unwrap() as u64, .mul_div_round(fps_d, fps_n)
fps_d, .unwrap();
fps_n,
);
let duration = old_pts - new_pts; let duration = old_pts - new_pts;
(new_pts, duration) (new_pts, duration)
@ -203,18 +201,12 @@ impl TtToCea608 {
*state.framerate.numer() as u64, *state.framerate.numer() as u64,
*state.framerate.denom() as u64, *state.framerate.denom() as u64,
); );
let start: gst::ClockTime = scale_round( let start = (last_frame_no * gst::SECOND)
(last_frame_no * gst::SECOND).nseconds().unwrap() as u64, .mul_div_round(fps_d, fps_n)
fps_d, .unwrap();
fps_n, let end = (new_frame_no * gst::SECOND)
) .mul_div_round(fps_d, fps_n)
.into(); .unwrap();
let end: gst::ClockTime = scale_round(
(new_frame_no * gst::SECOND).nseconds().unwrap() as u64,
fps_d,
fps_n,
)
.into();
let event = gst::Event::new_gap(start, end - start).build(); let event = gst::Event::new_gap(start, end - start).build();
@ -252,10 +244,10 @@ impl TtToCea608 {
let (pts, duration) = let (pts, duration) =
decrement_pts(min_frame_no, &mut erase_display_frame_no, fps_n, fps_d); decrement_pts(min_frame_no, &mut erase_display_frame_no, fps_n, fps_d);
erase_display_memory(bufferlist.get_mut().unwrap(), pts.into(), duration.into()); erase_display_memory(bufferlist.get_mut().unwrap(), pts, duration);
let (pts, duration) = let (pts, duration) =
decrement_pts(min_frame_no, &mut erase_display_frame_no, fps_n, fps_d); decrement_pts(min_frame_no, &mut erase_display_frame_no, fps_n, fps_d);
erase_display_memory(bufferlist.get_mut().unwrap(), pts.into(), duration.into()); erase_display_memory(bufferlist.get_mut().unwrap(), pts, duration);
drop(state); drop(state);
@ -404,8 +396,7 @@ impl TtToCea608 {
/* Calculate the frame for which we want the first of our /* Calculate the frame for which we want the first of our
* (doubled) end_of_caption control codes to be output * (doubled) end_of_caption control codes to be output
*/ */
let mut frame_no = let mut frame_no = (pts.mul_div_round(fps_n, fps_d).unwrap() / gst::SECOND).unwrap();
scale_round(pts.nseconds().unwrap(), fps_n, fps_d) / gst::SECOND.nseconds().unwrap();
let mut erase_display_frame_no = { let mut erase_display_frame_no = {
if state.erase_display_frame_no < Some(frame_no) { if state.erase_display_frame_no < Some(frame_no) {
@ -428,18 +419,16 @@ impl TtToCea608 {
state.last_frame_no = frame_no; state.last_frame_no = frame_no;
state.erase_display_frame_no = Some( state.erase_display_frame_no = Some(
scale_round((pts + duration).nseconds().unwrap(), fps_n, fps_d) ((pts + duration).mul_div_round(fps_n, fps_d).unwrap() / gst::SECOND).unwrap() + 2,
/ gst::SECOND.nseconds().unwrap()
+ 2,
); );
for mut buffer in buffers.drain(..).rev() { for mut buffer in buffers.drain(..).rev() {
/* Insert display erasure at the correct moment */ /* Insert display erasure at the correct moment */
if erase_display_frame_no == Some(frame_no) { if erase_display_frame_no == Some(frame_no) {
let (pts, duration) = decrement_pts(min_frame_no, &mut frame_no, fps_n, fps_d); let (pts, duration) = decrement_pts(min_frame_no, &mut frame_no, fps_n, fps_d);
erase_display_memory(bufferlist.get_mut().unwrap(), pts.into(), duration.into()); erase_display_memory(bufferlist.get_mut().unwrap(), pts, duration);
let (pts, duration) = decrement_pts(min_frame_no, &mut frame_no, fps_n, fps_d); let (pts, duration) = decrement_pts(min_frame_no, &mut frame_no, fps_n, fps_d);
erase_display_memory(bufferlist.get_mut().unwrap(), pts.into(), duration.into()); erase_display_memory(bufferlist.get_mut().unwrap(), pts, duration);
erase_display_frame_no = None; erase_display_frame_no = None;
} }
@ -447,8 +436,8 @@ impl TtToCea608 {
let (pts, duration) = decrement_pts(min_frame_no, &mut frame_no, fps_n, fps_d); let (pts, duration) = decrement_pts(min_frame_no, &mut frame_no, fps_n, fps_d);
let buf_mut = buffer.get_mut().unwrap(); let buf_mut = buffer.get_mut().unwrap();
buf_mut.set_pts(pts.into()); buf_mut.set_pts(pts);
buf_mut.set_duration(duration.into()); buf_mut.set_duration(duration);
bufferlist.get_mut().unwrap().insert(0, buffer); bufferlist.get_mut().unwrap().insert(0, buffer);
} }
@ -485,12 +474,9 @@ impl TtToCea608 {
*state.framerate.denom() as u64, *state.framerate.denom() as u64,
); );
let our_latency: gst::ClockTime = scale_round( let our_latency: gst::ClockTime = (LATENCY_BUFFERS * gst::SECOND)
(LATENCY_BUFFERS * gst::SECOND).nseconds().unwrap(), .mul_div_round(fps_d, fps_n)
fps_d, .unwrap();
fps_n,
)
.into();
min += our_latency; min += our_latency;
max += our_latency; max += our_latency;
@ -548,9 +534,9 @@ impl TtToCea608 {
); );
let (timestamp, duration) = e.get(); let (timestamp, duration) = e.get();
let mut frame_no = let mut frame_no = ((timestamp + duration).mul_div_round(fps_n, fps_d).unwrap()
scale_round((timestamp + duration).nseconds().unwrap(), fps_n, fps_d) / gst::SECOND)
/ gst::SECOND.nseconds().unwrap(); .unwrap();
if frame_no < LATENCY_BUFFERS { if frame_no < LATENCY_BUFFERS {
return true; return true;