closedcaption: implement cea708overlay element

Can overlay any single CEA-708 service or any single CEA-608 channel.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1519>
This commit is contained in:
Matthew Waters 2024-03-25 13:25:55 +11:00 committed by GStreamer Marge Bot
parent f0c38621c1
commit fbce73f6fc
9 changed files with 1838 additions and 18 deletions

4
Cargo.lock generated
View file

@ -1020,9 +1020,9 @@ dependencies = [
[[package]] [[package]]
name = "cea708-types" name = "cea708-types"
version = "0.3.1" version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd7f33493cb6f19aa19c6e688708f66bf792bc2c75137da2a03c7ebbdf7a44f9" checksum = "82b825228dce83e7156c7cd189bcfe5ef8014320deca7dd619787fe594946426"
dependencies = [ dependencies = [
"env_logger 0.10.2", "env_logger 0.10.2",
"log", "log",

View file

@ -5311,6 +5311,75 @@
}, },
"rank": "none" "rank": "none"
}, },
"cea708overlay": {
"author": "Matthew Waters <matthew@centricular.com>",
"description": "Renders CEA 708 closed caption meta over raw video frames",
"hierarchy": [
"GstCea708Overlay",
"GstElement",
"GstObject",
"GInitiallyUnowned",
"GObject"
],
"klass": "Video/Overlay/Subtitle",
"pad-templates": {
"sink": {
"caps": "video/x-raw:\n format: { A444_16LE, A444_16BE, AYUV64, RGBA64_LE, ARGB64, ARGB64_LE, BGRA64_LE, ABGR64_LE, RGBA64_BE, ARGB64_BE, BGRA64_BE, ABGR64_BE, A422_16LE, A422_16BE, A420_16LE, A420_16BE, A444_12LE, GBRA_12LE, A444_12BE, GBRA_12BE, Y412_LE, Y412_BE, A422_12LE, A422_12BE, A420_12LE, A420_12BE, A444_10LE, GBRA_10LE, A444_10BE, GBRA_10BE, A422_10LE, A422_10BE, A420_10LE, A420_10BE, BGR10A2_LE, RGB10A2_LE, Y410, A444, GBRA, AYUV, VUYA, RGBA, RBGA, ARGB, BGRA, ABGR, A422, A420, AV12, Y444_16LE, GBR_16LE, Y444_16BE, GBR_16BE, v216, P016_LE, P016_BE, Y444_12LE, GBR_12LE, Y444_12BE, GBR_12BE, I422_12LE, I422_12BE, Y212_LE, Y212_BE, I420_12LE, I420_12BE, P012_LE, P012_BE, Y444_10LE, GBR_10LE, Y444_10BE, GBR_10BE, r210, I422_10LE, I422_10BE, NV16_10LE32, Y210, UYVP, v210, I420_10LE, I420_10BE, P010_10LE, NV12_10LE40, NV12_10LE32, P010_10BE, MT2110R, MT2110T, NV12_10BE_8L128, NV12_10LE40_4L4, Y444, BGRP, GBR, RGBP, NV24, v308, IYU2, RGBx, xRGB, BGRx, xBGR, RGB, BGR, Y42B, NV16, NV61, YUY2, YVYU, UYVY, VYUY, I420, YV12, NV12, NV21, NV12_16L32S, NV12_32L32, NV12_4L4, NV12_64Z32, NV12_8L128, Y41B, IYU1, YUV9, YVU9, BGR16, RGB16, BGR15, RGB15, RGB8P, GRAY16_LE, GRAY16_BE, GRAY10_LE32, GRAY8 }\n width: [ 1, 2147483647 ]\n height: [ 1, 2147483647 ]\n framerate: [ 0/1, 2147483647/1 ]\n",
"direction": "sink",
"presence": "always"
},
"src": {
"caps": "video/x-raw:\n format: { A444_16LE, A444_16BE, AYUV64, RGBA64_LE, ARGB64, ARGB64_LE, BGRA64_LE, ABGR64_LE, RGBA64_BE, ARGB64_BE, BGRA64_BE, ABGR64_BE, A422_16LE, A422_16BE, A420_16LE, A420_16BE, A444_12LE, GBRA_12LE, A444_12BE, GBRA_12BE, Y412_LE, Y412_BE, A422_12LE, A422_12BE, A420_12LE, A420_12BE, A444_10LE, GBRA_10LE, A444_10BE, GBRA_10BE, A422_10LE, A422_10BE, A420_10LE, A420_10BE, BGR10A2_LE, RGB10A2_LE, Y410, A444, GBRA, AYUV, VUYA, RGBA, RBGA, ARGB, BGRA, ABGR, A422, A420, AV12, Y444_16LE, GBR_16LE, Y444_16BE, GBR_16BE, v216, P016_LE, P016_BE, Y444_12LE, GBR_12LE, Y444_12BE, GBR_12BE, I422_12LE, I422_12BE, Y212_LE, Y212_BE, I420_12LE, I420_12BE, P012_LE, P012_BE, Y444_10LE, GBR_10LE, Y444_10BE, GBR_10BE, r210, I422_10LE, I422_10BE, NV16_10LE32, Y210, UYVP, v210, I420_10LE, I420_10BE, P010_10LE, NV12_10LE40, NV12_10LE32, P010_10BE, MT2110R, MT2110T, NV12_10BE_8L128, NV12_10LE40_4L4, Y444, BGRP, GBR, RGBP, NV24, v308, IYU2, RGBx, xRGB, BGRx, xBGR, RGB, BGR, Y42B, NV16, NV61, YUY2, YVYU, UYVY, VYUY, I420, YV12, NV12, NV21, NV12_16L32S, NV12_32L32, NV12_4L4, NV12_64Z32, NV12_8L128, Y41B, IYU1, YUV9, YVU9, BGR16, RGB16, BGR15, RGB15, RGB8P, GRAY16_LE, GRAY16_BE, GRAY10_LE32, GRAY8 }\n width: [ 1, 2147483647 ]\n height: [ 1, 2147483647 ]\n framerate: [ 0/1, 2147483647/1 ]\n",
"direction": "src",
"presence": "always"
}
},
"properties": {
"cea608-channel": {
"blurb": "The cea608 channel (CC1-4) to render the caption for when available, (-1=automatic, 0=disabled)",
"conditionally-available": false,
"construct": false,
"construct-only": false,
"controllable": false,
"default": "-1",
"max": "4",
"min": "-1",
"mutable": "playing",
"readable": true,
"type": "gint",
"writable": true
},
"service": {
"blurb": "The service to render the caption for when available, (-1=automatic, 0=disabled)",
"conditionally-available": false,
"construct": false,
"construct-only": false,
"controllable": false,
"default": "1",
"max": "31",
"min": "-1",
"mutable": "playing",
"readable": true,
"type": "gint",
"writable": true
},
"timeout": {
"blurb": "Duration after which to erase overlay when no cc data has arrived for the selected service/channel",
"conditionally-available": false,
"construct": false,
"construct-only": false,
"controllable": false,
"default": "18446744073709551615",
"max": "18446744073709551615",
"min": "16000000000",
"mutable": "playing",
"readable": true,
"type": "guint64",
"writable": true
}
},
"rank": "primary"
},
"jsontovtt": { "jsontovtt": {
"author": "Jan Schmidt <jan@centricular.com>", "author": "Jan Schmidt <jan@centricular.com>",
"description": "Converts JSON to WebVTT", "description": "Converts JSON to WebVTT",

View file

@ -20,7 +20,7 @@ pangocairo.workspace = true
byteorder = "1" byteorder = "1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] } serde_json = { version = "1.0", features = ["raw_value"] }
cea708-types = "0.3.1" cea708-types = "0.3.2"
cea608-types = "0.1.1" cea608-types = "0.1.1"
once_cell.workspace = true once_cell.workspace = true
gst = { workspace = true, features = ["v1_16"]} gst = { workspace = true, features = ["v1_16"]}

View file

@ -155,13 +155,12 @@ pub(crate) fn recalculate_pango_layout(
layout: &pango::Layout, layout: &pango::Layout,
video_width: u32, video_width: u32,
video_height: u32, video_height: u32,
) -> i32 { ) -> (i32, i32) {
let mut font_desc = pango::FontDescription::from_string("monospace"); let mut font_desc = pango::FontDescription::from_string("monospace");
let video_width = video_width * 80 / 100; let video_width = video_width * 80 / 100;
let video_height = video_height * 80 / 100; let video_height = video_height * 80 / 100;
let mut font_size = 1; let mut font_size = 1;
let mut left_alignment = 0;
loop { loop {
font_desc.set_size(font_size * pango::SCALE); font_desc.set_size(font_size * pango::SCALE);
layout.set_font_description(Some(&font_desc)); layout.set_font_description(Some(&font_desc));
@ -175,10 +174,12 @@ pub(crate) fn recalculate_pango_layout(
layout.set_font_description(Some(&font_desc)); layout.set_font_description(Some(&font_desc));
break; break;
} }
left_alignment = (video_width as i32 - logical_rect.width() / pango::SCALE) / 2
+ video_width as i32 / 10;
font_size += 1; font_size += 1;
} }
left_alignment let (_ink_rect, logical_rect) = layout.extents();
(
logical_rect.width() / pango::SCALE,
logical_rect.height() / pango::SCALE,
)
} }

View file

@ -501,7 +501,7 @@ impl Cea608Renderer {
context.set_base_dir(pango::Direction::Ltr); context.set_base_dir(pango::Direction::Ltr);
let layout = pango::Layout::new(&context); let layout = pango::Layout::new(&context);
layout.set_alignment(pango::Alignment::Left); layout.set_alignment(pango::Alignment::Left);
let left_alignment = recalculate_pango_layout(&layout, video_width, video_height); recalculate_pango_layout(&layout, video_width, video_height);
Self { Self {
frame: Cea608Frame::new(), frame: Cea608Frame::new(),
state: Cea608State::default(), state: Cea608State::default(),
@ -510,7 +510,7 @@ impl Cea608Renderer {
rectangle: None, rectangle: None,
video_width, video_width,
video_height, video_height,
left_alignment, left_alignment: 0,
black_background: false, black_background: false,
} }
} }
@ -533,13 +533,19 @@ impl Cea608Renderer {
} }
} }
pub fn channel(&self) -> Option<cea608_types::tables::Channel> {
self.frame.selected_channel
}
pub fn set_video_size(&mut self, width: u32, height: u32) { pub fn set_video_size(&mut self, width: u32, height: u32) {
if width != self.video_width || height != self.video_height { if width != self.video_width || height != self.video_height {
self.video_width = width; self.video_width = width;
self.video_height = height; self.video_height = height;
self.layout = pango::Layout::new(&self.context); self.layout = pango::Layout::new(&self.context);
self.layout.set_alignment(pango::Alignment::Left); self.layout.set_alignment(pango::Alignment::Left);
self.left_alignment = recalculate_pango_layout(&self.layout, width, height); let (max_layout_width, _max_layout_height) =
recalculate_pango_layout(&self.layout, width, height);
self.left_alignment = (width as i32 - max_layout_width) / 2 + width as i32 / 10;
self.rectangle.take(); self.rectangle.take();
} }
} }

View file

@ -0,0 +1,633 @@
// Copyright (C) 2024 Matthew Waters <matthew@centricular.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at
// <https://mozilla.org/MPL/2.0/>.
//
// SPDX-License-Identifier: MPL-2.0
use gst::glib;
use gst::prelude::*;
use gst::subclass::prelude::*;
use gst_video::prelude::*;
use once_cell::sync::Lazy;
use std::sync::Mutex;
use crate::ccutils::extract_cdp;
use crate::cea708utils::{Cea708Renderer, ServiceOrChannel};
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
gst::DebugCategory::new(
"cea708overlay",
gst::DebugColorFlags::empty(),
Some("CEA 708 overlay element"),
)
});
const DEFAULT_CEA608_CHANNEL: i32 = -1;
const DEFAULT_SERVICE: i32 = 1;
#[derive(Debug, Clone)]
struct Settings {
changed: bool,
cea608_channel: i32,
service: i32,
timeout: Option<gst::ClockTime>,
}
impl Default for Settings {
fn default() -> Self {
Settings {
changed: true,
cea608_channel: DEFAULT_CEA608_CHANNEL,
service: DEFAULT_SERVICE,
timeout: gst::ClockTime::NONE,
}
}
}
struct State {
selected: Option<ServiceOrChannel>,
enabled_608: bool,
enabled_708: bool,
upstream_caps: Option<gst::Caps>,
video_info: Option<gst_video::VideoInfo>,
cc_data_parser: cea708_types::CCDataParser,
cea708_renderer: Cea708Renderer,
attach: bool,
last_cc_pts: Option<gst::ClockTime>,
}
impl Default for State {
fn default() -> Self {
let mut cc_data_parser = cea708_types::CCDataParser::default();
cc_data_parser.handle_cea608();
Self {
selected: None,
enabled_608: true,
enabled_708: true,
upstream_caps: None,
video_info: None,
cc_data_parser,
cea708_renderer: Cea708Renderer::new(),
attach: false,
last_cc_pts: gst::ClockTime::NONE,
}
}
}
pub struct Cea708Overlay {
srcpad: gst::Pad,
sinkpad: gst::Pad,
state: Mutex<State>,
settings: Mutex<Settings>,
}
impl Cea708Overlay {
fn render(&self, state: &mut State) -> Option<gst_video::VideoOverlayComposition> {
state.cea708_renderer.generate_composition()
}
fn check_service_channel(&self, state: &mut State) {
let mut settings = self.settings.lock().unwrap();
if !settings.changed {
return;
}
state.selected = match settings.service {
-1 => state.selected,
0 => {
if matches!(state.selected, Some(ServiceOrChannel::Service(_))) {
None
} else {
state.selected
}
}
val => Some(ServiceOrChannel::Service(val as u8)),
};
if state.selected.is_none() || settings.cea608_channel == 0 {
state.selected = match settings.cea608_channel {
-1 => state.selected,
0 => {
if matches!(state.selected, Some(ServiceOrChannel::Cea608Channel(_))) {
None
} else {
state.selected
}
}
val => Some(ServiceOrChannel::Cea608Channel(
cea608_types::Id::from_value(val as i8),
)),
};
}
state.enabled_608 = settings.cea608_channel != 0;
state.enabled_708 = settings.service != 0;
gst::info!(
CAT,
"set service channel {:?}, from settings: {settings:?}",
state.selected
);
state.cea708_renderer.set_service_channel(state.selected);
settings.changed = false;
}
fn negotiate(&self) {
let mut state = self.state.lock().unwrap();
let Some(caps) = state.upstream_caps.as_ref() else {
gst::element_imp_error!(
self,
gst::CoreError::Negotiation,
["Element hasn't received valid video caps at negotiation time"]
);
self.srcpad.mark_reconfigure();
return;
};
let Some(video_info) = state.video_info.clone() else {
gst::element_imp_error!(
self,
gst::CoreError::Negotiation,
["Element hasn't received valid video caps at negotiation time"]
);
self.srcpad.mark_reconfigure();
return;
};
let mut downstream_accepts_meta = false;
let mut caps = caps.clone();
let upstream_has_meta = state
.upstream_caps
.as_ref()
.and_then(|caps| {
caps.features(0)
.map(|f| f.contains(gst_video::CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION))
})
.unwrap_or(false);
if !upstream_has_meta {
let mut caps_clone = caps.clone();
let overlay_caps = caps_clone.make_mut();
if let Some(features) = overlay_caps.features_mut(0) {
features.add(gst_video::CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
drop(state);
let peercaps = self.srcpad.peer_query_caps(Some(&caps_clone));
downstream_accepts_meta = !peercaps.is_empty();
if downstream_accepts_meta {
caps = caps_clone;
}
state = self.state.lock().unwrap();
}
}
state.attach = upstream_has_meta || downstream_accepts_meta;
state
.cea708_renderer
.set_video_size(video_info.width(), video_info.height());
if !self.srcpad.push_event(gst::event::Caps::new(&caps)) {
self.srcpad.mark_reconfigure();
}
}
fn have_cea608(
&self,
state: &mut State,
field: cea608_types::tables::Field,
cea608: [u8; 2],
pts: gst::ClockTime,
) {
gst::trace!(CAT, imp: self, "Handling CEA-608 for field {field:?} (selected: {:?}) data: {cea608:x?}", state.selected);
match state.cea708_renderer.push_cea608(field, cea608) {
Err(e) => gst::warning!(CAT, imp: self, "Failed to parse CEA-608 data: {e:?}"),
Ok(true) => self.reset_timeout(state, pts),
_ => (),
}
}
fn handle_cc_data(&self, state: &mut State, pts: gst::ClockTime) {
let cea608 = state.cc_data_parser.cea608().map(|c| c.to_vec());
while let Some(packet) = state.cc_data_parser.pop_packet() {
if !state.enabled_708
|| !matches!(state.selected, None | Some(ServiceOrChannel::Service(_)))
{
continue;
}
for service in packet.services() {
if state.selected.is_none() {
gst::info!(CAT, imp: self, "Automatic selection chose CEA-708 service {}", service.number());
state.selected = Some(ServiceOrChannel::Service(service.number()));
}
if Some(ServiceOrChannel::Service(service.number())) != state.selected {
continue;
}
state.cea708_renderer.push_service(service);
}
}
let Some(cea608) = cea608 else {
gst::log!(CAT, imp: self, "No CEA-608");
return;
};
if !state.enabled_608
|| !matches!(
state.selected,
None | Some(ServiceOrChannel::Cea608Channel(_))
)
{
gst::log!(CAT, imp: self, "CEA-608 not to be used (enabled {}, selected {:?})", state.enabled_608, state.selected);
return;
}
for pair in cea608 {
let (field, pair) = match pair {
cea708_types::Cea608::Field1(byte0, byte1) => {
(cea608_types::tables::Field::ONE, [byte0, byte1])
}
cea708_types::Cea608::Field2(byte0, byte1) => {
(cea608_types::tables::Field::TWO, [byte0, byte1])
}
};
self.have_cea608(state, field, pair, pts);
}
}
fn decode_s334_1a(&self, state: &mut State, data: &[u8], pts: gst::ClockTime) {
if data.len() % 3 != 0 {
gst::warning!(CAT, "cc_data length is not a multiple of 3, truncating");
}
for triple in data.chunks_exact(3) {
let field = if triple[0] & 0x01 == 0x0 {
cea608_types::tables::Field::ONE
} else {
cea608_types::tables::Field::TWO
};
self.have_cea608(state, field, [triple[1], triple[2]], pts);
}
}
fn reset_timeout(&self, state: &mut State, pts: gst::ClockTime) {
state.last_cc_pts = Some(pts);
}
fn sink_chain(
&self,
pad: &gst::Pad,
mut buffer: gst::Buffer,
) -> Result<gst::FlowSuccess, gst::FlowError> {
gst::log!(CAT, obj: pad, "Handling buffer {:?}", buffer);
let pts = buffer.pts().ok_or_else(|| {
gst::error!(CAT, obj: pad, "Require timestamped buffers");
gst::FlowError::Error
})?;
let settings = self.settings.lock().unwrap();
let caption_timeout = settings.timeout;
drop(settings);
if self.srcpad.check_reconfigure() {
self.negotiate();
}
let mut state = self.state.lock().unwrap();
self.check_service_channel(&mut state);
for meta in buffer.iter_meta::<gst_video::VideoCaptionMeta>() {
gst::log!(CAT, imp: self, "Have caption meta of type {:?}", meta.caption_type());
if meta.caption_type() == gst_video::VideoCaptionType::Cea708Cdp {
match extract_cdp(meta.data()) {
Ok(data) => {
let mut cc_data = vec![0x80 | 0x40 | ((data.len() / 3) & 0x1f) as u8, 0xFF];
cc_data.extend(data);
match state.cc_data_parser.push(&cc_data) {
Ok(_) => self.handle_cc_data(&mut state, pts),
Err(e) => {
gst::warning!(CAT, "Failed to parse incoming data: {e}");
gst::element_imp_warning!(
self,
gst::StreamError::Decode,
["Failed to parse incoming data {e}"]
);
state.cc_data_parser.flush();
}
}
}
Err(e) => {
gst::warning!(CAT, "{e}");
gst::element_imp_warning!(self, gst::StreamError::Decode, ["{e}"]);
}
}
} else if meta.caption_type() == gst_video::VideoCaptionType::Cea708Raw {
let mut cc_data = vec![0; 2];
// reserved | process_cc_data | length
cc_data[0] = 0x80 | 0x40 | ((meta.data().len() / 3) & 0x1f) as u8;
cc_data[1] = 0xFF;
cc_data.extend(meta.data());
match state.cc_data_parser.push(&cc_data) {
Ok(_) => self.handle_cc_data(&mut state, pts),
Err(e) => {
gst::warning!(CAT, "Failed to parse incoming data: {e}");
gst::element_imp_warning!(
self,
gst::StreamError::Decode,
["Failed to parse incoming data: {e}"]
);
state.cc_data_parser.flush();
}
}
} else if meta.caption_type() == gst_video::VideoCaptionType::Cea608S3341a {
self.decode_s334_1a(&mut state, meta.data(), pts);
} else if meta.caption_type() == gst_video::VideoCaptionType::Cea608Raw {
let data = meta.data();
assert!(data.len() % 2 == 0);
for pair in data.chunks_exact(2) {
self.have_cea608(
&mut state,
cea608_types::tables::Field::ONE,
[pair[0], pair[1]],
pts,
);
}
}
}
let composition = self.render(&mut state);
if let Some(timeout) = caption_timeout {
if let Some(interval) = pts.opt_saturating_sub(state.last_cc_pts) {
if interval > timeout {
gst::info!(CAT, imp: self, "Reached timeout, clearing overlay");
state.cea708_renderer.clear_composition();
state.last_cc_pts.take();
}
}
}
if let Some(composition) = &composition {
let buffer = buffer.make_mut();
if state.attach {
gst_video::VideoOverlayCompositionMeta::add(buffer, composition);
} else {
let mut frame = gst_video::VideoFrameRef::from_buffer_ref_writable(
buffer,
state.video_info.as_ref().unwrap(),
)
.unwrap();
if composition.blend(&mut frame).is_err() {
gst::error!(CAT, obj: pad, "Failed to blend composition");
}
}
}
drop(state);
self.srcpad.push(buffer)
}
fn sink_event(&self, pad: &gst::Pad, event: gst::Event) -> bool {
use gst::EventView;
gst::log!(CAT, obj: pad, "Handling event {:?}", event);
match event.view() {
EventView::Caps(c) => {
let mut state = self.state.lock().unwrap();
state.upstream_caps = Some(c.caps_owned());
state.video_info = gst_video::VideoInfo::from_caps(c.caps()).ok();
drop(state);
self.srcpad.check_reconfigure();
self.negotiate();
true
}
EventView::FlushStop(..) => {
let mut state = self.state.lock().unwrap();
state.cea708_renderer = Cea708Renderer::new();
//state.cea608_renderer.set_black_background(settings.black_background);
drop(state);
gst::Pad::event_default(pad, Some(&*self.obj()), event)
}
_ => gst::Pad::event_default(pad, Some(&*self.obj()), event),
}
}
}
#[glib::object_subclass]
impl ObjectSubclass for Cea708Overlay {
const NAME: &'static str = "GstCea708Overlay";
type Type = super::Cea708Overlay;
type ParentType = gst::Element;
fn with_class(klass: &Self::Class) -> Self {
let templ = klass.pad_template("sink").unwrap();
let sinkpad = gst::Pad::builder_from_template(&templ)
.chain_function(|pad, parent, buffer| {
Cea708Overlay::catch_panic_pad_function(
parent,
|| Err(gst::FlowError::Error),
|overlay| overlay.sink_chain(pad, buffer),
)
})
.event_function(|pad, parent, event| {
Cea708Overlay::catch_panic_pad_function(
parent,
|| false,
|overlay| overlay.sink_event(pad, event),
)
})
.flags(gst::PadFlags::PROXY_CAPS)
.flags(gst::PadFlags::PROXY_ALLOCATION)
.build();
let templ = klass.pad_template("src").unwrap();
let srcpad = gst::Pad::builder_from_template(&templ)
.flags(gst::PadFlags::PROXY_CAPS)
.flags(gst::PadFlags::PROXY_ALLOCATION)
.build();
Self {
srcpad,
sinkpad,
state: Mutex::new(State::default()),
settings: Mutex::new(Settings::default()),
}
}
}
impl ObjectImpl for Cea708Overlay {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
vec![
glib::ParamSpecInt::builder("cea608-channel")
.nick("CEA-608 Channel")
.blurb("The cea608 channel (CC1-4) to render the caption for when available, (-1=automatic, 0=disabled)")
.minimum(-1)
.maximum(4)
.default_value(DEFAULT_CEA608_CHANNEL)
.mutable_playing()
.build(),
glib::ParamSpecInt::builder("service")
.nick("Service")
.blurb("The service to render the caption for when available, (-1=automatic, 0=disabled)")
.minimum(-1)
.maximum(31)
.default_value(DEFAULT_SERVICE)
.mutable_playing()
.build(),
glib::ParamSpecUInt64::builder("timeout")
.nick("Timeout")
.blurb("Duration after which to erase overlay when no cc data has arrived for the selected service/channel")
.minimum(16.seconds().nseconds())
.default_value(u64::MAX)
.mutable_playing()
.build(),
]
});
PROPERTIES.as_ref()
}
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
match pspec.name() {
"cea608-channel" => {
let mut settings = self.settings.lock().unwrap();
let new = value.get().expect("type checked upstream");
if new != settings.cea608_channel {
settings.cea608_channel = new;
settings.changed = true;
}
}
"service" => {
let mut settings = self.settings.lock().unwrap();
let new = value.get().expect("type checked upstream");
if new != settings.service {
settings.service = new;
settings.changed = true;
}
}
"timeout" => {
let mut settings = self.settings.lock().unwrap();
let timeout = value.get().expect("type checked upstream");
settings.timeout = match timeout {
u64::MAX => gst::ClockTime::NONE,
_ => Some(timeout.nseconds()),
};
}
_ => unimplemented!(),
}
}
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
match pspec.name() {
"cea608-channel" => {
let settings = self.settings.lock().unwrap();
settings.cea608_channel.to_value()
}
"service" => {
let settings = self.settings.lock().unwrap();
settings.service.to_value()
}
"timeout" => {
let settings = self.settings.lock().unwrap();
if let Some(timeout) = settings.timeout {
timeout.nseconds().to_value()
} else {
u64::MAX.to_value()
}
}
_ => unimplemented!(),
}
}
fn constructed(&self) {
self.parent_constructed();
let obj = self.obj();
obj.add_pad(&self.sinkpad).unwrap();
obj.add_pad(&self.srcpad).unwrap();
}
}
impl GstObjectImpl for Cea708Overlay {}
impl ElementImpl for Cea708Overlay {
fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
gst::subclass::ElementMetadata::new(
"CEA 708 overlay",
"Video/Overlay/Subtitle",
"Renders CEA 708 closed caption meta over raw video frames",
"Matthew Waters <matthew@centricular.com>",
)
});
Some(&*ELEMENT_METADATA)
}
fn pad_templates() -> &'static [gst::PadTemplate] {
static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
let caps = gst_video::VideoFormat::iter_raw()
.into_video_caps()
.unwrap()
.build();
let sink_pad_template = gst::PadTemplate::new(
"sink",
gst::PadDirection::Sink,
gst::PadPresence::Always,
&caps,
)
.unwrap();
let src_pad_template = gst::PadTemplate::new(
"src",
gst::PadDirection::Src,
gst::PadPresence::Always,
&caps,
)
.unwrap();
vec![src_pad_template, sink_pad_template]
});
PAD_TEMPLATES.as_ref()
}
fn change_state(
&self,
transition: gst::StateChange,
) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
gst::trace!(CAT, imp: self, "Changing state {:?}", transition);
match transition {
gst::StateChange::ReadyToPaused | gst::StateChange::PausedToReady => {
// Reset the whole state
let mut state = self.state.lock().unwrap();
*state = State::default();
drop(state);
let mut settings = self.settings.lock().unwrap();
settings.changed = true;
}
_ => (),
}
self.parent_change_state(transition)
}
}

View file

@ -0,0 +1,31 @@
// Copyright (C) 2024 Matthew Waters <matthew@centricular.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at
// <https://mozilla.org/MPL/2.0/>.
//
// SPDX-License-Identifier: MPL-2.0
// Example command-line:
//
// gst-launch-1.0 cccombiner name=ccc ! cea708overlay ! autovideosink \
// videotestsrc ! video/x-raw, width=1280, height=720 ! queue ! ccc.sink \
// filesrc location=input.srt ! subparse ! tttocea708 ! queue ! ccc.caption
use gst::glib;
use gst::prelude::*;
mod imp;
glib::wrapper! {
pub struct Cea708Overlay(ObjectSubclass<imp::Cea708Overlay>) @extends gst::Element, gst::Object;
}
pub fn register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {
gst::Element::register(
Some(plugin),
"cea708overlay",
gst::Rank::PRIMARY,
Cea708Overlay::static_type(),
)
}

File diff suppressed because it is too large Load diff

View file

@ -25,6 +25,7 @@ mod cea608tojson;
mod cea608tott; mod cea608tott;
mod cea608utils; mod cea608utils;
mod cea708mux; mod cea708mux;
mod cea708overlay;
mod cea708utils; mod cea708utils;
mod jsontovtt; mod jsontovtt;
mod line_reader; mod line_reader;
@ -60,6 +61,7 @@ fn plugin_init(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {
cea608tocea708::register(plugin)?; cea608tocea708::register(plugin)?;
cea708mux::register(plugin)?; cea708mux::register(plugin)?;
tttocea708::register(plugin)?; tttocea708::register(plugin)?;
cea708overlay::register(plugin)?;
Ok(()) Ok(())
} }