closedcaption: Add h264ccinserter element
Adding new element for inserting closed caption SEI to H.264 stream Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/8496>
This commit is contained in:
parent
55b2dcc121
commit
6a711ede04
@ -35,6 +35,7 @@
|
||||
#include "ccutils.h"
|
||||
#include "gsth264ccextractor.h"
|
||||
#include "gsth265ccextractor.h"
|
||||
#include "gsth264ccinserter.h"
|
||||
|
||||
static gboolean
|
||||
closedcaption_init (GstPlugin * plugin)
|
||||
@ -53,6 +54,7 @@ closedcaption_init (GstPlugin * plugin)
|
||||
ret |= GST_ELEMENT_REGISTER (line21encoder, plugin);
|
||||
ret |= GST_ELEMENT_REGISTER (h264ccextractor, plugin);
|
||||
ret |= GST_ELEMENT_REGISTER (h265ccextractor, plugin);
|
||||
ret |= GST_ELEMENT_REGISTER (h264ccinserter, plugin);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -0,0 +1,746 @@
|
||||
/* GStreamer
|
||||
* Copyright (C) 2025 Seungha Yang <seungha@centricular.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "gstcodecccinserter.h"
|
||||
#include <string.h>
|
||||
|
||||
GST_DEBUG_CATEGORY_STATIC (gst_codec_cc_inserter_debug);
|
||||
#define GST_CAT_DEFAULT gst_codec_cc_inserter_debug
|
||||
|
||||
/**
|
||||
* GstCodecCCInsertMetaOrder:
|
||||
*
|
||||
* Since: 1.26
|
||||
*/
|
||||
#define GST_TYPE_CODEC_CC_INSERT_META_ORDER (gst_codec_cc_insert_meta_order_mode_get_type())
|
||||
static GType
|
||||
gst_codec_cc_insert_meta_order_mode_get_type (void)
|
||||
{
|
||||
static GType type = 0;
|
||||
static const GEnumValue order_types[] = {
|
||||
{GST_CODEC_CC_INSERT_META_ORDER_DECODE, "Decode", "decode"},
|
||||
{GST_CODEC_CC_INSERT_META_ORDER_DISPLAY, "Display", "display"},
|
||||
{0, NULL, NULL},
|
||||
};
|
||||
|
||||
if (g_once_init_enter ((gsize *) & type)) {
|
||||
GType tmp = g_enum_register_static ("GstCodecCCInsertMetaOrder",
|
||||
order_types);
|
||||
g_once_init_leave ((gsize *) & type, tmp);
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
enum
|
||||
{
|
||||
PROP_0,
|
||||
PROP_CAPTION_META_ORDER,
|
||||
PROP_REMOVE_CAPTION_META,
|
||||
};
|
||||
|
||||
#define DEFAULT_CAPTION_META_ORDER GST_CODEC_CC_INSERT_META_ORDER_DECODE
|
||||
#define DEFAULT_REMOVE_CAPTION_META FALSE
|
||||
|
||||
struct _GstCodecCCInserterPrivate
|
||||
{
|
||||
GMutex lock;
|
||||
|
||||
GList *current_frame_events;
|
||||
GPtrArray *caption_metas;
|
||||
GstClockTime latency;
|
||||
|
||||
GstCodecCCInsertMetaOrder meta_order;
|
||||
gboolean remove_meta;
|
||||
};
|
||||
|
||||
static void gst_codec_cc_inserter_class_init (GstCodecCCInserterClass * klass);
|
||||
static void gst_codec_cc_inserter_init (GstCodecCCInserter * self,
|
||||
GstCodecCCInserterClass * klass);
|
||||
static void gst_codec_cc_inserter_finalize (GObject * object);
|
||||
static void gst_codec_cc_inserter_set_property (GObject * object, guint prop_id,
|
||||
const GValue * value, GParamSpec * pspec);
|
||||
static void gst_codec_cc_inserter_get_property (GObject * object, guint prop_id,
|
||||
GValue * value, GParamSpec * pspec);
|
||||
|
||||
static GstFlowReturn gst_codec_cc_inserter_chain (GstPad * pad,
|
||||
GstObject * parent, GstBuffer * buffer);
|
||||
static gboolean gst_codec_cc_inserter_sink_event (GstPad * pad,
|
||||
GstObject * parent, GstEvent * event);
|
||||
static gboolean gst_codec_cc_inserter_sink_query (GstPad * pad,
|
||||
GstObject * parent, GstQuery * query);
|
||||
static gboolean gst_codec_cc_inserter_src_query (GstPad * pad,
|
||||
GstObject * parent, GstQuery * query);
|
||||
static GstCaps *gst_codec_cc_inserter_get_caps (GstCodecCCInserter * self,
|
||||
GstCaps * filter);
|
||||
static GstStateChangeReturn gst_codec_cc_inserter_change_state (GstElement *
|
||||
element, GstStateChange transition);
|
||||
static void gst_codec_cc_inserter_reset (GstCodecCCInserter * self);
|
||||
static void gst_codec_cc_inserter_drain (GstCodecCCInserter * self);
|
||||
|
||||
static GTypeClass *parent_class = NULL;
|
||||
static gint private_offset = 0;
|
||||
|
||||
/**
|
||||
* GstCodecCCInserter:
|
||||
*
|
||||
* Since: 1.26
|
||||
*/
|
||||
|
||||
/* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
|
||||
* method to get to the padtemplates */
|
||||
GType
|
||||
gst_codec_cc_inserter_get_type (void)
|
||||
{
|
||||
static gsize type = 0;
|
||||
|
||||
if (g_once_init_enter (&type)) {
|
||||
GType _type;
|
||||
static const GTypeInfo info = {
|
||||
sizeof (GstCodecCCInserterClass),
|
||||
NULL,
|
||||
NULL,
|
||||
(GClassInitFunc) gst_codec_cc_inserter_class_init,
|
||||
NULL,
|
||||
NULL,
|
||||
sizeof (GstCodecCCInserter),
|
||||
0,
|
||||
(GInstanceInitFunc) gst_codec_cc_inserter_init,
|
||||
};
|
||||
|
||||
_type = g_type_register_static (GST_TYPE_ELEMENT,
|
||||
"GstCodecCCInserter", &info, G_TYPE_FLAG_ABSTRACT);
|
||||
|
||||
private_offset = g_type_add_instance_private (_type,
|
||||
sizeof (GstCodecCCInserterPrivate));
|
||||
|
||||
g_once_init_leave (&type, _type);
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
static inline GstCodecCCInserterPrivate *
|
||||
gst_codec_cc_inserter_get_instance_private (GstCodecCCInserter * self)
|
||||
{
|
||||
return (G_STRUCT_MEMBER_P (self, private_offset));
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_inserter_class_init (GstCodecCCInserterClass * klass)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
||||
|
||||
parent_class = g_type_class_peek_parent (klass);
|
||||
if (private_offset)
|
||||
g_type_class_adjust_private_offset (klass, &private_offset);
|
||||
|
||||
object_class->set_property = gst_codec_cc_inserter_set_property;
|
||||
object_class->get_property = gst_codec_cc_inserter_get_property;
|
||||
object_class->finalize = gst_codec_cc_inserter_finalize;
|
||||
|
||||
g_object_class_install_property (object_class, PROP_CAPTION_META_ORDER,
|
||||
g_param_spec_enum ("caption-meta-order", "Caption Meta Order",
|
||||
"Order of caption metas attached on buffers. In case of \"display\" order, "
|
||||
"inserter will reorder captions to decoding order",
|
||||
GST_TYPE_CODEC_CC_INSERT_META_ORDER, DEFAULT_CAPTION_META_ORDER,
|
||||
GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (object_class, PROP_REMOVE_CAPTION_META,
|
||||
g_param_spec_boolean ("remove-caption-meta", "Remove Caption Meta",
|
||||
"Remove caption meta from outgoing video buffers", FALSE,
|
||||
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
||||
|
||||
element_class->change_state =
|
||||
GST_DEBUG_FUNCPTR (gst_codec_cc_inserter_change_state);
|
||||
|
||||
GST_DEBUG_CATEGORY_INIT (gst_codec_cc_inserter_debug, "codecccinserter", 0,
|
||||
"codecccinserter");
|
||||
|
||||
gst_type_mark_as_plugin_api (GST_TYPE_CODEC_CC_INSERTER, 0);
|
||||
gst_type_mark_as_plugin_api (GST_TYPE_CODEC_CC_INSERT_META_ORDER, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_inserter_init (GstCodecCCInserter * self,
|
||||
GstCodecCCInserterClass * klass)
|
||||
{
|
||||
GstCodecCCInserterPrivate *priv;
|
||||
GstPadTemplate *template;
|
||||
|
||||
self->priv = priv = gst_codec_cc_inserter_get_instance_private (self);
|
||||
|
||||
template = gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass),
|
||||
"sink");
|
||||
self->sinkpad = gst_pad_new_from_template (template, "sink");
|
||||
gst_pad_set_chain_function (self->sinkpad,
|
||||
GST_DEBUG_FUNCPTR (gst_codec_cc_inserter_chain));
|
||||
gst_pad_set_event_function (self->sinkpad,
|
||||
GST_DEBUG_FUNCPTR (gst_codec_cc_inserter_sink_event));
|
||||
gst_pad_set_query_function (self->sinkpad,
|
||||
GST_DEBUG_FUNCPTR (gst_codec_cc_inserter_sink_query));
|
||||
|
||||
GST_PAD_SET_PROXY_SCHEDULING (self->sinkpad);
|
||||
GST_PAD_SET_ACCEPT_INTERSECT (self->sinkpad);
|
||||
GST_PAD_SET_ACCEPT_TEMPLATE (self->sinkpad);
|
||||
GST_PAD_SET_PROXY_CAPS (self->sinkpad);
|
||||
|
||||
gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
|
||||
|
||||
template = gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass),
|
||||
"src");
|
||||
self->srcpad = gst_pad_new_from_template (template, "src");
|
||||
gst_pad_set_query_function (self->srcpad,
|
||||
GST_DEBUG_FUNCPTR (gst_codec_cc_inserter_src_query));
|
||||
GST_PAD_SET_PROXY_SCHEDULING (self->srcpad);
|
||||
|
||||
gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
|
||||
|
||||
g_mutex_init (&priv->lock);
|
||||
priv->meta_order = DEFAULT_CAPTION_META_ORDER;
|
||||
priv->remove_meta = DEFAULT_REMOVE_CAPTION_META;
|
||||
priv->caption_metas = g_ptr_array_new ();
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_inserter_set_property (GObject * object, guint prop_id,
|
||||
const GValue * value, GParamSpec * pspec)
|
||||
{
|
||||
GstCodecCCInserter *self = GST_CODEC_CC_INSERTER (object);
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
|
||||
g_mutex_lock (&priv->lock);
|
||||
|
||||
switch (prop_id) {
|
||||
case PROP_CAPTION_META_ORDER:
|
||||
priv->meta_order = g_value_get_enum (value);
|
||||
break;
|
||||
case PROP_REMOVE_CAPTION_META:
|
||||
priv->remove_meta = g_value_get_boolean (value);
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
|
||||
g_mutex_unlock (&priv->lock);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_inserter_get_property (GObject * object, guint prop_id,
|
||||
GValue * value, GParamSpec * pspec)
|
||||
{
|
||||
GstCodecCCInserter *self = GST_CODEC_CC_INSERTER (object);
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
|
||||
g_mutex_lock (&priv->lock);
|
||||
|
||||
switch (prop_id) {
|
||||
case PROP_CAPTION_META_ORDER:
|
||||
g_value_set_enum (value, priv->meta_order);
|
||||
break;
|
||||
case PROP_REMOVE_CAPTION_META:
|
||||
g_value_set_boolean (value, priv->remove_meta);
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
|
||||
g_mutex_unlock (&priv->lock);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_inserter_finalize (GObject * object)
|
||||
{
|
||||
GstCodecCCInserter *self = GST_CODEC_CC_INSERTER (object);
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
|
||||
g_mutex_clear (&priv->lock);
|
||||
g_ptr_array_unref (priv->caption_metas);
|
||||
|
||||
G_OBJECT_CLASS (parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_inserter_flush_events (GstCodecCCInserter * self, GList ** events)
|
||||
{
|
||||
GList *iter;
|
||||
|
||||
for (iter = *events; iter; iter = g_list_next (iter)) {
|
||||
GstEvent *ev = GST_EVENT (iter->data);
|
||||
|
||||
if (GST_EVENT_IS_STICKY (ev) && GST_EVENT_TYPE (ev) != GST_EVENT_EOS &&
|
||||
GST_EVENT_TYPE (ev) != GST_EVENT_SEGMENT) {
|
||||
gst_pad_store_sticky_event (self->srcpad, ev);
|
||||
}
|
||||
|
||||
gst_event_unref (ev);
|
||||
}
|
||||
|
||||
g_clear_pointer (events, g_list_free);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_inserter_flush (GstCodecCCInserter * self)
|
||||
{
|
||||
GstCodecCCInserterClass *klass = GST_CODEC_CC_INSERTER_GET_CLASS (self);
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
GstVideoCodecFrame *frame;
|
||||
|
||||
klass->drain (self);
|
||||
|
||||
while ((frame = klass->pop (self)) != NULL) {
|
||||
gst_codec_cc_inserter_flush_events (self, &frame->events);
|
||||
gst_video_codec_frame_unref (frame);
|
||||
}
|
||||
|
||||
gst_codec_cc_inserter_flush_events (self, &priv->current_frame_events);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_insert_update_latency (GstCodecCCInserter * self,
|
||||
GstClockTime latency)
|
||||
{
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
gboolean post_msg = FALSE;
|
||||
|
||||
if (!GST_CLOCK_TIME_IS_VALID (latency))
|
||||
return;
|
||||
|
||||
g_mutex_lock (&priv->lock);
|
||||
if (GST_CLOCK_TIME_IS_VALID (latency) && priv->latency < latency) {
|
||||
priv->latency = latency;
|
||||
post_msg = TRUE;
|
||||
}
|
||||
g_mutex_unlock (&priv->lock);
|
||||
|
||||
if (post_msg) {
|
||||
gst_element_post_message (GST_ELEMENT_CAST (self),
|
||||
gst_message_new_latency (GST_OBJECT_CAST (self)));
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_codec_cc_inserter_sink_event (GstPad * pad, GstObject * parent,
|
||||
GstEvent * event)
|
||||
{
|
||||
GstCodecCCInserter *self = GST_CODEC_CC_INSERTER (parent);
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
GstCodecCCInserterClass *klass = GST_CODEC_CC_INSERTER_GET_CLASS (self);
|
||||
gboolean forward = FALSE;
|
||||
|
||||
switch (GST_EVENT_TYPE (event)) {
|
||||
case GST_EVENT_CAPS:
|
||||
{
|
||||
GstCaps *caps;
|
||||
GstClockTime latency = 0;
|
||||
|
||||
gst_event_parse_caps (event, &caps);
|
||||
if (!klass->set_caps (self, caps, &latency)) {
|
||||
GST_ERROR_OBJECT (self, "Couldn't set caps");
|
||||
gst_event_unref (event);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
gst_codec_cc_insert_update_latency (self, latency);
|
||||
|
||||
if (klass->get_num_buffered (self) == 0) {
|
||||
GST_DEBUG_OBJECT (self, "No buffered frame, forward caps immediately");
|
||||
forward = TRUE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GST_EVENT_SEGMENT:
|
||||
{
|
||||
GstSegment segment;
|
||||
gst_event_copy_segment (event, &segment);
|
||||
if (segment.rate < 0) {
|
||||
GST_ERROR_OBJECT (self, "Negative rate is not supported");
|
||||
gst_event_unref (event);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (klass->get_num_buffered (self) == 0) {
|
||||
GST_DEBUG_OBJECT (self,
|
||||
"No buffered frame, forward segment immediately");
|
||||
forward = TRUE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GST_EVENT_STREAM_START:
|
||||
case GST_EVENT_EOS:
|
||||
gst_codec_cc_inserter_drain (self);
|
||||
if (priv->current_frame_events) {
|
||||
GList *iter;
|
||||
|
||||
for (iter = priv->current_frame_events; iter; iter = g_list_next (iter))
|
||||
gst_pad_push_event (self->srcpad, GST_EVENT (iter->data));
|
||||
|
||||
g_clear_pointer (&priv->current_frame_events, g_list_free);
|
||||
}
|
||||
forward = TRUE;
|
||||
break;
|
||||
case GST_EVENT_FLUSH_STOP:
|
||||
gst_codec_cc_inserter_flush (self);
|
||||
forward = TRUE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!GST_EVENT_IS_SERIALIZED (event) || forward) {
|
||||
return gst_pad_event_default (pad, parent, event);
|
||||
}
|
||||
|
||||
/* Store event to serialize queued frames */
|
||||
priv->current_frame_events = g_list_append (priv->current_frame_events,
|
||||
event);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
remove_caption_meta (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
|
||||
{
|
||||
if ((*meta)->info->api == GST_VIDEO_CAPTION_META_API_TYPE)
|
||||
*meta = NULL;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
copy_caption_meta (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
|
||||
{
|
||||
GstVideoCaptionMeta *cc_meta;
|
||||
GstBuffer *outbuf = GST_BUFFER (user_data);
|
||||
|
||||
if ((*meta)->info->api != GST_VIDEO_CAPTION_META_API_TYPE)
|
||||
return TRUE;
|
||||
|
||||
cc_meta = (GstVideoCaptionMeta *) (*meta);
|
||||
gst_buffer_add_video_caption_meta (outbuf, cc_meta->caption_type,
|
||||
cc_meta->data, cc_meta->size);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
extract_caption_meta (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
|
||||
{
|
||||
GstVideoCaptionMeta *cc_meta;
|
||||
GPtrArray *array = user_data;
|
||||
|
||||
if ((*meta)->info->api != GST_VIDEO_CAPTION_META_API_TYPE)
|
||||
return TRUE;
|
||||
|
||||
cc_meta = (GstVideoCaptionMeta *) (*meta);
|
||||
/* TODO: Support conversion to cea708 cc_data? */
|
||||
if (cc_meta->caption_type == GST_VIDEO_CAPTION_TYPE_CEA708_RAW)
|
||||
g_ptr_array_add (array, cc_meta);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static GstFlowReturn
|
||||
gst_codec_cc_inserter_output_frame (GstCodecCCInserter * self,
|
||||
GstVideoCodecFrame * frame)
|
||||
{
|
||||
GstCodecCCInserterClass *klass = GST_CODEC_CC_INSERTER_GET_CLASS (self);
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
GList *iter;
|
||||
GstFlowReturn ret;
|
||||
GstBuffer *output;
|
||||
GstBuffer *caption_source;
|
||||
gboolean reordered = FALSE;
|
||||
|
||||
for (iter = frame->events; iter; iter = g_list_next (iter)) {
|
||||
GstEvent *event = GST_EVENT (iter->data);
|
||||
gst_pad_push_event (self->srcpad, event);
|
||||
}
|
||||
|
||||
g_clear_pointer (&frame->events, g_list_free);
|
||||
|
||||
output = gst_buffer_copy (frame->input_buffer);
|
||||
g_mutex_lock (&priv->lock);
|
||||
caption_source = frame->input_buffer;
|
||||
if (priv->meta_order == GST_CODEC_CC_INSERT_META_ORDER_DISPLAY &&
|
||||
frame->output_buffer) {
|
||||
caption_source = frame->output_buffer;
|
||||
if (frame->output_buffer != frame->input_buffer)
|
||||
reordered = TRUE;
|
||||
}
|
||||
|
||||
/* Remove caption meta form outgoing buffer if requested or
|
||||
* caption is reordered */
|
||||
if (priv->remove_meta || reordered)
|
||||
gst_buffer_foreach_meta (output, remove_caption_meta, NULL);
|
||||
|
||||
/* Attach meta again if meta was removed because of reordering */
|
||||
if (!priv->remove_meta && reordered)
|
||||
gst_buffer_foreach_meta (caption_source, copy_caption_meta, output);
|
||||
|
||||
g_ptr_array_set_size (priv->caption_metas, 0);
|
||||
/* Collects metas */
|
||||
gst_buffer_foreach_meta (caption_source,
|
||||
extract_caption_meta, priv->caption_metas);
|
||||
|
||||
output = klass->insert_cc (self, output, priv->caption_metas);
|
||||
g_mutex_unlock (&priv->lock);
|
||||
|
||||
gst_video_codec_frame_unref (frame);
|
||||
|
||||
GST_LOG_OBJECT (self, "Output %" GST_PTR_FORMAT, output);
|
||||
|
||||
ret = gst_pad_push (self->srcpad, output);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_inserter_drain (GstCodecCCInserter * self)
|
||||
{
|
||||
GstCodecCCInserterClass *klass = GST_CODEC_CC_INSERTER_GET_CLASS (self);
|
||||
GstVideoCodecFrame *frame;
|
||||
|
||||
klass->drain (self);
|
||||
|
||||
while ((frame = klass->pop (self)) != NULL)
|
||||
gst_codec_cc_inserter_output_frame (self, frame);
|
||||
}
|
||||
|
||||
static GstFlowReturn
|
||||
gst_codec_cc_inserter_chain (GstPad * pad, GstObject * parent,
|
||||
GstBuffer * buffer)
|
||||
{
|
||||
GstCodecCCInserter *self = GST_CODEC_CC_INSERTER (parent);
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
GstCodecCCInserterClass *klass = GST_CODEC_CC_INSERTER_GET_CLASS (self);
|
||||
GstVideoCodecFrame *frame;
|
||||
GstClockTime latency = 0;
|
||||
|
||||
GST_LOG_OBJECT (self, "Handle %" GST_PTR_FORMAT, buffer);
|
||||
|
||||
frame = g_new0 (GstVideoCodecFrame, 1);
|
||||
frame->ref_count = 1;
|
||||
frame->input_buffer = buffer;
|
||||
frame->events = priv->current_frame_events;
|
||||
priv->current_frame_events = NULL;
|
||||
|
||||
gst_video_codec_frame_ref (frame);
|
||||
if (!klass->push (self, frame, &latency)) {
|
||||
GST_ERROR_OBJECT (self, "Couldn't process frame");
|
||||
priv->current_frame_events = frame->events;
|
||||
gst_video_codec_frame_unref (frame);
|
||||
/* TODO: return error in case of too many decoding error */
|
||||
return GST_FLOW_OK;
|
||||
}
|
||||
|
||||
gst_video_codec_frame_unref (frame);
|
||||
gst_codec_cc_insert_update_latency (self, latency);
|
||||
|
||||
while ((frame = klass->pop (self)) != NULL) {
|
||||
GstFlowReturn ret = gst_codec_cc_inserter_output_frame (self, frame);
|
||||
if (ret != GST_FLOW_OK)
|
||||
return ret;
|
||||
}
|
||||
|
||||
return GST_FLOW_OK;
|
||||
}
|
||||
|
||||
static GstCaps *
|
||||
gst_codec_cc_inserter_get_caps (GstCodecCCInserter * self, GstCaps * filter)
|
||||
{
|
||||
GstCaps *peercaps, *templ;
|
||||
GstCaps *res, *tmp, *pcopy;
|
||||
|
||||
templ = gst_pad_get_pad_template_caps (self->sinkpad);
|
||||
if (filter) {
|
||||
GstCaps *fcopy = gst_caps_copy (filter);
|
||||
|
||||
peercaps = gst_pad_peer_query_caps (self->srcpad, fcopy);
|
||||
gst_caps_unref (fcopy);
|
||||
} else {
|
||||
peercaps = gst_pad_peer_query_caps (self->srcpad, NULL);
|
||||
}
|
||||
|
||||
pcopy = gst_caps_copy (peercaps);
|
||||
|
||||
res = gst_caps_intersect_full (pcopy, templ, GST_CAPS_INTERSECT_FIRST);
|
||||
gst_caps_unref (pcopy);
|
||||
gst_caps_unref (templ);
|
||||
|
||||
if (filter) {
|
||||
GstCaps *tmp = gst_caps_intersect_full (res, filter,
|
||||
GST_CAPS_INTERSECT_FIRST);
|
||||
gst_caps_unref (res);
|
||||
res = tmp;
|
||||
}
|
||||
|
||||
/* Try if we can put the downstream caps first */
|
||||
pcopy = gst_caps_copy (peercaps);
|
||||
tmp = gst_caps_intersect_full (pcopy, res, GST_CAPS_INTERSECT_FIRST);
|
||||
gst_caps_unref (pcopy);
|
||||
if (!gst_caps_is_empty (tmp))
|
||||
res = gst_caps_merge (tmp, res);
|
||||
else
|
||||
gst_caps_unref (tmp);
|
||||
|
||||
gst_caps_unref (peercaps);
|
||||
return res;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_codec_cc_inserter_sink_query (GstPad * pad, GstObject * parent,
|
||||
GstQuery * query)
|
||||
{
|
||||
GstCodecCCInserter *self = GST_CODEC_CC_INSERTER (parent);
|
||||
|
||||
switch (GST_QUERY_TYPE (query)) {
|
||||
case GST_QUERY_CAPS:{
|
||||
GstCaps *caps, *filter;
|
||||
|
||||
gst_query_parse_caps (query, &filter);
|
||||
caps = gst_codec_cc_inserter_get_caps (self, filter);
|
||||
GST_LOG_OBJECT (self, "sink getcaps returning caps %" GST_PTR_FORMAT,
|
||||
caps);
|
||||
gst_query_set_caps_result (query, caps);
|
||||
gst_caps_unref (caps);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return gst_pad_query_default (pad, parent, query);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_codec_cc_inserter_src_query (GstPad * pad, GstObject * parent,
|
||||
GstQuery * query)
|
||||
{
|
||||
GstCodecCCInserter *self = GST_CODEC_CC_INSERTER (parent);
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
|
||||
switch (GST_QUERY_TYPE (query)) {
|
||||
case GST_QUERY_LATENCY:
|
||||
{
|
||||
gboolean ret;
|
||||
|
||||
ret = gst_pad_peer_query (self->sinkpad, query);
|
||||
if (ret) {
|
||||
GstClockTime min, max;
|
||||
gboolean live;
|
||||
|
||||
gst_query_parse_latency (query, &live, &min, &max);
|
||||
|
||||
g_mutex_lock (&priv->lock);
|
||||
if (GST_CLOCK_TIME_IS_VALID (priv->latency)) {
|
||||
min += priv->latency;
|
||||
|
||||
if (GST_CLOCK_TIME_IS_VALID (max))
|
||||
max += priv->latency;
|
||||
}
|
||||
g_mutex_unlock (&priv->lock);
|
||||
|
||||
gst_query_set_latency (query, live, min, max);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return gst_pad_query_default (pad, parent, query);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_codec_cc_inserter_reset (GstCodecCCInserter * self)
|
||||
{
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
|
||||
if (priv->current_frame_events) {
|
||||
g_list_free_full (priv->current_frame_events,
|
||||
(GDestroyNotify) gst_event_unref);
|
||||
priv->current_frame_events = NULL;
|
||||
}
|
||||
|
||||
priv->latency = 0;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_codec_cc_inserter_start (GstCodecCCInserter * self)
|
||||
{
|
||||
GstCodecCCInserterClass *klass = GST_CODEC_CC_INSERTER_GET_CLASS (self);
|
||||
GstCodecCCInserterPrivate *priv = self->priv;
|
||||
|
||||
gst_codec_cc_inserter_reset (self);
|
||||
|
||||
if (klass->start)
|
||||
return klass->start (self, priv->meta_order);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_codec_cc_inserter_stop (GstCodecCCInserter * self)
|
||||
{
|
||||
GstCodecCCInserterClass *klass = GST_CODEC_CC_INSERTER_GET_CLASS (self);
|
||||
|
||||
gst_codec_cc_inserter_reset (self);
|
||||
|
||||
if (klass->stop)
|
||||
return klass->stop (self);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static GstStateChangeReturn
|
||||
gst_codec_cc_inserter_change_state (GstElement * element,
|
||||
GstStateChange transition)
|
||||
{
|
||||
GstCodecCCInserter *self = GST_CODEC_CC_INSERTER (element);
|
||||
GstStateChangeReturn ret;
|
||||
|
||||
switch (transition) {
|
||||
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
||||
gst_codec_cc_inserter_start (self);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
||||
|
||||
switch (transition) {
|
||||
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
||||
gst_codec_cc_inserter_stop (self);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
/* GStreamer
|
||||
* Copyright (C) 2025 Seungha Yang <seungha@centricular.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gst/gst.h>
|
||||
#include <gst/video/video.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define GST_TYPE_CODEC_CC_INSERTER (gst_codec_cc_inserter_get_type())
|
||||
#define GST_CODEC_CC_INSERTER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_CODEC_CC_INSERTER,GstCodecCCInserter))
|
||||
#define GST_CODEC_CC_INSERTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_CODEC_CC_INSERTER,GstCodecCCInserterClass))
|
||||
#define GST_CODEC_CC_INSERTER_GET_CLASS(obj) (GST_CODEC_CC_INSERTER_CLASS(G_OBJECT_GET_CLASS(obj)))
|
||||
#define GST_CODEC_CC_INSERTER_CAST(obj) ((GstCodecCCInserter*)(obj))
|
||||
|
||||
typedef struct _GstCodecCCInserter GstCodecCCInserter;
|
||||
typedef struct _GstCodecCCInserterClass GstCodecCCInserterClass;
|
||||
typedef struct _GstCodecCCInserterPrivate GstCodecCCInserterPrivate;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GST_CODEC_CC_INSERT_META_ORDER_DECODE,
|
||||
GST_CODEC_CC_INSERT_META_ORDER_DISPLAY,
|
||||
} GstCodecCCInsertMetaOrder;
|
||||
|
||||
struct _GstCodecCCInserter
|
||||
{
|
||||
GstElement parent;
|
||||
|
||||
GstPad *sinkpad;
|
||||
GstPad *srcpad;
|
||||
|
||||
GstCodecCCInserterPrivate *priv;
|
||||
};
|
||||
|
||||
struct _GstCodecCCInserterClass
|
||||
{
|
||||
GstElementClass parent_class;
|
||||
|
||||
gboolean (*start) (GstCodecCCInserter * inserter,
|
||||
GstCodecCCInsertMetaOrder meta_order);
|
||||
|
||||
gboolean (*stop) (GstCodecCCInserter * inserter);
|
||||
|
||||
gboolean (*set_caps) (GstCodecCCInserter * inserter,
|
||||
GstCaps * caps,
|
||||
GstClockTime * latency);
|
||||
|
||||
guint (*get_num_buffered) (GstCodecCCInserter * inserter);
|
||||
|
||||
gboolean (*push) (GstCodecCCInserter * inserter,
|
||||
GstVideoCodecFrame * frame,
|
||||
GstClockTime * latency);
|
||||
|
||||
GstVideoCodecFrame * (*pop) (GstCodecCCInserter * inserter);
|
||||
|
||||
void (*drain) (GstCodecCCInserter * inserter);
|
||||
|
||||
GstBuffer * (*insert_cc) (GstCodecCCInserter * inserter,
|
||||
GstBuffer * buffer,
|
||||
GPtrArray * metas);
|
||||
};
|
||||
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GstCodecCCInserter, gst_object_unref);
|
||||
|
||||
GType gst_codec_cc_inserter_get_type (void);
|
||||
|
||||
G_END_DECLS
|
@ -0,0 +1,277 @@
|
||||
/* GStreamer
|
||||
* Copyright (C) 2025 Seungha Yang <seungha@centricular.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
/**
|
||||
* SECTION:element-h264ccinserter
|
||||
* @title: h264ccinserter
|
||||
*
|
||||
* Extracts closed caption meta from buffer and inserts closed caption SEI message
|
||||
*
|
||||
* ## Example launch line
|
||||
* ```
|
||||
* gst-launch-1.0.exe filesrc location=video.mp4 ! parsebin name=p ! h264parse ! \
|
||||
* queue ! cccombiner name=c ! \
|
||||
* h264ccinserter remove-caption-meta=true caption-meta-order=display ! \
|
||||
* h264parse ! avdec_h264 ! videoconvert ! cea608overlay ! queue ! autovideosink \
|
||||
* filesrc location=caption.mcc ! mccparse ! ccconverter ! \
|
||||
* closedcaption/x-cea-708,format=(string)cc_data ! queue ! c.caption
|
||||
* ```
|
||||
*
|
||||
* Above pipeline inserts closed caption data to already encoded H.264 stream
|
||||
* and renders. Because mccparse outputs caption data in display order,
|
||||
* "caption-meta-order=display" property is required in this example.
|
||||
*
|
||||
* Since: 1.26
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "gsth264ccinserter.h"
|
||||
#include "gsth264reorder.h"
|
||||
#include <gst/codecparsers/gsth264parser.h>
|
||||
#include <string.h>
|
||||
|
||||
GST_DEBUG_CATEGORY_STATIC (gst_h264_cc_inserter_debug);
|
||||
#define GST_CAT_DEFAULT gst_h264_cc_inserter_debug
|
||||
|
||||
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
||||
GST_PAD_SINK,
|
||||
GST_PAD_ALWAYS,
|
||||
GST_STATIC_CAPS ("video/x-h264, alignment=(string) au"));
|
||||
|
||||
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
||||
GST_PAD_SRC,
|
||||
GST_PAD_ALWAYS,
|
||||
GST_STATIC_CAPS ("video/x-h264, alignment=(string) au"));
|
||||
|
||||
struct _GstH264CCInserter
|
||||
{
|
||||
GstCodecCCInserter parent;
|
||||
|
||||
GstH264Reorder *reorder;
|
||||
GArray *sei_array;
|
||||
};
|
||||
|
||||
static void gst_h264_cc_inserter_finalize (GObject * object);
|
||||
|
||||
static gboolean gst_h264_cc_inserter_start (GstCodecCCInserter * inserter,
|
||||
GstCodecCCInsertMetaOrder meta_order);
|
||||
static gboolean gst_h264_cc_inserter_stop (GstCodecCCInserter * inserter);
|
||||
static gboolean gst_h264_cc_inserter_set_caps (GstCodecCCInserter * inserter,
|
||||
GstCaps * caps, GstClockTime * latency);
|
||||
static guint
|
||||
gst_h264_cc_inserter_get_num_buffered (GstCodecCCInserter * inserter);
|
||||
static gboolean gst_h264_cc_inserter_push (GstCodecCCInserter * inserter,
|
||||
GstVideoCodecFrame * frame, GstClockTime * latency);
|
||||
static GstVideoCodecFrame *gst_h264_cc_inserter_pop (GstCodecCCInserter *
|
||||
inserter);
|
||||
static void gst_h264_cc_inserter_drain (GstCodecCCInserter * inserter);
|
||||
static GstBuffer *gst_h264_cc_inserter_insert_cc (GstCodecCCInserter * inserter,
|
||||
GstBuffer * buffer, GPtrArray * metas);
|
||||
|
||||
#define gst_h264_cc_inserter_parent_class parent_class
|
||||
G_DEFINE_TYPE (GstH264CCInserter,
|
||||
gst_h264_cc_inserter, GST_TYPE_CODEC_CC_INSERTER);
|
||||
GST_ELEMENT_REGISTER_DEFINE (h264ccinserter, "h264ccinserter",
|
||||
GST_RANK_NONE, GST_TYPE_H264_CC_INSERTER);
|
||||
|
||||
static void
|
||||
gst_h264_cc_inserter_class_init (GstH264CCInserterClass * klass)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
||||
GstCodecCCInserterClass *inserter_class = GST_CODEC_CC_INSERTER_CLASS (klass);
|
||||
|
||||
object_class->finalize = gst_h264_cc_inserter_finalize;
|
||||
|
||||
gst_element_class_add_static_pad_template (element_class, &sinktemplate);
|
||||
gst_element_class_add_static_pad_template (element_class, &srctemplate);
|
||||
|
||||
gst_element_class_set_static_metadata (element_class,
|
||||
"H.264 Closed Caption Inserter",
|
||||
"Codec/Video/Filter", "Insert closed caption data to H.264 streams",
|
||||
"Seungha Yang <seungha@centricular.com>");
|
||||
|
||||
inserter_class->start = GST_DEBUG_FUNCPTR (gst_h264_cc_inserter_start);
|
||||
inserter_class->stop = GST_DEBUG_FUNCPTR (gst_h264_cc_inserter_stop);
|
||||
inserter_class->set_caps = GST_DEBUG_FUNCPTR (gst_h264_cc_inserter_set_caps);
|
||||
inserter_class->get_num_buffered =
|
||||
GST_DEBUG_FUNCPTR (gst_h264_cc_inserter_get_num_buffered);
|
||||
inserter_class->push = GST_DEBUG_FUNCPTR (gst_h264_cc_inserter_push);
|
||||
inserter_class->pop = GST_DEBUG_FUNCPTR (gst_h264_cc_inserter_pop);
|
||||
inserter_class->drain = GST_DEBUG_FUNCPTR (gst_h264_cc_inserter_drain);
|
||||
inserter_class->insert_cc =
|
||||
GST_DEBUG_FUNCPTR (gst_h264_cc_inserter_insert_cc);
|
||||
|
||||
GST_DEBUG_CATEGORY_INIT (gst_h264_cc_inserter_debug, "h264ccinserter", 0,
|
||||
"h264ccinserter");
|
||||
}
|
||||
|
||||
static void
|
||||
gst_h264_cc_inserter_init (GstH264CCInserter * self)
|
||||
{
|
||||
self->sei_array = g_array_new (FALSE, FALSE, sizeof (GstH264SEIMessage));
|
||||
g_array_set_clear_func (self->sei_array, (GDestroyNotify) gst_h264_sei_clear);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_h264_cc_inserter_finalize (GObject * object)
|
||||
{
|
||||
GstH264CCInserter *self = GST_H264_CC_INSERTER (object);
|
||||
|
||||
g_array_unref (self->sei_array);
|
||||
|
||||
G_OBJECT_CLASS (parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_h264_cc_inserter_start (GstCodecCCInserter * inserter,
|
||||
GstCodecCCInsertMetaOrder meta_order)
|
||||
{
|
||||
GstH264CCInserter *self = GST_H264_CC_INSERTER (inserter);
|
||||
gboolean need_reorder = FALSE;
|
||||
if (meta_order == GST_CODEC_CC_INSERT_META_ORDER_DISPLAY)
|
||||
need_reorder = TRUE;
|
||||
|
||||
self->reorder = gst_h264_reorder_new (need_reorder);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_h264_cc_inserter_stop (GstCodecCCInserter * inserter)
|
||||
{
|
||||
GstH264CCInserter *self = GST_H264_CC_INSERTER (inserter);
|
||||
|
||||
gst_clear_object (&self->reorder);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_h264_cc_inserter_set_caps (GstCodecCCInserter * inserter, GstCaps * caps,
|
||||
GstClockTime * latency)
|
||||
{
|
||||
GstH264CCInserter *self = GST_H264_CC_INSERTER (inserter);
|
||||
|
||||
return gst_h264_reorder_set_caps (self->reorder, caps, latency);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_h264_cc_inserter_push (GstCodecCCInserter * inserter,
|
||||
GstVideoCodecFrame * frame, GstClockTime * latency)
|
||||
{
|
||||
GstH264CCInserter *self = GST_H264_CC_INSERTER (inserter);
|
||||
|
||||
return gst_h264_reorder_push (self->reorder, frame, latency);
|
||||
}
|
||||
|
||||
static GstVideoCodecFrame *
|
||||
gst_h264_cc_inserter_pop (GstCodecCCInserter * inserter)
|
||||
{
|
||||
GstH264CCInserter *self = GST_H264_CC_INSERTER (inserter);
|
||||
|
||||
return gst_h264_reorder_pop (self->reorder);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_h264_cc_inserter_drain (GstCodecCCInserter * inserter)
|
||||
{
|
||||
GstH264CCInserter *self = GST_H264_CC_INSERTER (inserter);
|
||||
|
||||
gst_h264_reorder_drain (self->reorder);
|
||||
}
|
||||
|
||||
static guint
|
||||
gst_h264_cc_inserter_get_num_buffered (GstCodecCCInserter * inserter)
|
||||
{
|
||||
GstH264CCInserter *self = GST_H264_CC_INSERTER (inserter);
|
||||
|
||||
return gst_h264_reorder_get_num_buffered (self->reorder);
|
||||
}
|
||||
|
||||
static GstBuffer *
|
||||
gst_h264_cc_inserter_insert_cc (GstCodecCCInserter * inserter,
|
||||
GstBuffer * buffer, GPtrArray * metas)
|
||||
{
|
||||
GstH264CCInserter *self = GST_H264_CC_INSERTER (inserter);
|
||||
guint i;
|
||||
GstBuffer *new_buf;
|
||||
|
||||
g_array_set_size (self->sei_array, 0);
|
||||
|
||||
for (i = 0; i < metas->len; i++) {
|
||||
GstVideoCaptionMeta *meta = g_ptr_array_index (metas, i);
|
||||
GstH264SEIMessage sei;
|
||||
GstH264RegisteredUserData *rud;
|
||||
guint8 *data;
|
||||
|
||||
if (meta->caption_type != GST_VIDEO_CAPTION_TYPE_CEA708_RAW)
|
||||
continue;
|
||||
|
||||
memset (&sei, 0, sizeof (GstH264SEIMessage));
|
||||
sei.payloadType = GST_H264_SEI_REGISTERED_USER_DATA;
|
||||
rud = &sei.payload.registered_user_data;
|
||||
|
||||
rud->country_code = 181;
|
||||
rud->size = meta->size + 10;
|
||||
|
||||
data = g_malloc (rud->size);
|
||||
memcpy (data + 9, meta->data, meta->size);
|
||||
|
||||
data[0] = 0; /* 16-bits itu_t_t35_provider_code */
|
||||
data[1] = 49;
|
||||
data[2] = 'G'; /* 32-bits ATSC_user_identifier */
|
||||
data[3] = 'A';
|
||||
data[4] = '9';
|
||||
data[5] = '4';
|
||||
data[6] = 3; /* 8-bits ATSC1_data_user_data_type_code */
|
||||
/* 8-bits:
|
||||
* 1 bit process_em_data_flag (0)
|
||||
* 1 bit process_cc_data_flag (1)
|
||||
* 1 bit additional_data_flag (0)
|
||||
* 5-bits cc_count
|
||||
*/
|
||||
data[7] = ((meta->size / 3) & 0x1f) | 0x40;
|
||||
data[8] = 255; /* 8 bits em_data, unused */
|
||||
data[meta->size + 9] = 255; /* 8 marker bits */
|
||||
|
||||
rud->data = data;
|
||||
|
||||
g_array_append_val (self->sei_array, sei);
|
||||
}
|
||||
|
||||
if (self->sei_array->len == 0)
|
||||
return buffer;
|
||||
|
||||
new_buf = gst_h264_reorder_insert_sei (self->reorder,
|
||||
buffer, self->sei_array);
|
||||
|
||||
g_array_set_size (self->sei_array, 0);
|
||||
|
||||
if (!new_buf) {
|
||||
GST_WARNING_OBJECT (self, "Couldn't insert SEI");
|
||||
return buffer;
|
||||
}
|
||||
|
||||
gst_buffer_unref (buffer);
|
||||
return new_buf;
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/* GStreamer
|
||||
* Copyright (C) 2025 Seungha Yang <seungha@centricular.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gstcodecccinserter.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define GST_TYPE_H264_CC_INSERTER (gst_h264_cc_inserter_get_type())
|
||||
G_DECLARE_FINAL_TYPE (GstH264CCInserter, gst_h264_cc_inserter,
|
||||
GST, H264_CC_INSERTER, GstCodecCCInserter);
|
||||
|
||||
GST_ELEMENT_REGISTER_DECLARE (h264ccinserter);
|
||||
|
||||
G_END_DECLS
|
1956
subprojects/gst-plugins-bad/ext/closedcaption/gsth264reorder.c
Normal file
1956
subprojects/gst-plugins-bad/ext/closedcaption/gsth264reorder.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,55 @@
|
||||
/* GStreamer
|
||||
* Copyright (C) 2025 Seungha Yang <seungha@centricular.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gst/gst.h>
|
||||
#include <gst/video/video.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define GST_TYPE_H264_REORDER (gst_h264_reorder_get_type())
|
||||
G_DECLARE_FINAL_TYPE (GstH264Reorder, gst_h264_reorder,
|
||||
GST, H264_REORDER, GstObject);
|
||||
|
||||
GstH264Reorder * gst_h264_reorder_new (gboolean need_reorder);
|
||||
|
||||
gboolean gst_h264_reorder_set_caps (GstH264Reorder * reorder,
|
||||
GstCaps * caps,
|
||||
GstClockTime * latency);
|
||||
|
||||
gboolean gst_h264_reorder_push (GstH264Reorder * reorder,
|
||||
GstVideoCodecFrame * frame,
|
||||
GstClockTime * latency);
|
||||
|
||||
GstVideoCodecFrame * gst_h264_reorder_pop (GstH264Reorder * reorder);
|
||||
|
||||
void gst_h264_reorder_drain (GstH264Reorder * reorder);
|
||||
|
||||
guint gst_h264_reorder_get_num_buffered (GstH264Reorder * reorder);
|
||||
|
||||
GstBuffer * gst_h264_reorder_insert_sei (GstH264Reorder * reorder,
|
||||
GstBuffer * au,
|
||||
GArray * sei);
|
||||
|
||||
gboolean gst_h264_reorder_is_cea708_sei (guint8 country_code,
|
||||
const guint8 * data,
|
||||
gsize size);
|
||||
|
||||
G_END_DECLS
|
@ -14,6 +14,9 @@ closedcaption_sources = [
|
||||
'ccutils.c',
|
||||
'gsth264ccextractor.c',
|
||||
'gsth265ccextractor.c',
|
||||
'gsth264reorder.c',
|
||||
'gstcodecccinserter.c',
|
||||
'gsth264ccinserter.c',
|
||||
]
|
||||
|
||||
closedcaption_headers = [
|
||||
|
Loading…
x
Reference in New Issue
Block a user