2010-08-13 15:36:38 +00:00
|
|
|
/* GStreamer Smart Video Encoder element
|
|
|
|
* Copyright (C) <2010> Edward Hervey <bilboed@gmail.com>
|
2020-07-04 16:33:20 +00:00
|
|
|
* Copyright (C) <2020> Thibault Saunier <tsaunier@igalia.com>
|
2010-08-13 15:36:38 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-08-13 15:36:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
2021-10-05 13:46:26 +00:00
|
|
|
#include <gst/video/video.h>
|
2010-08-13 15:36:38 +00:00
|
|
|
#include "gstsmartencoder.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (smart_encoder_debug);
|
|
|
|
#define GST_CAT_DEFAULT smart_encoder_debug
|
|
|
|
|
|
|
|
/* FIXME : Update this with new caps */
|
|
|
|
/* WARNING : We can only allow formats with closed-GOP */
|
|
|
|
#define ALLOWED_CAPS "video/x-h263;video/x-intel-h263;"\
|
2020-07-30 22:49:52 +00:00
|
|
|
"video/x-vp8;"\
|
|
|
|
"video/x-vp9;"\
|
2020-07-04 16:33:20 +00:00
|
|
|
"video/x-h264;"\
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
"video/x-h265;"\
|
2010-08-13 15:36:38 +00:00
|
|
|
"video/mpeg,mpegversion=(int)1,systemstream=(boolean)false;"\
|
|
|
|
"video/mpeg,mpegversion=(int)2,systemstream=(boolean)false;"
|
|
|
|
|
|
|
|
static GstStaticPadTemplate src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS (ALLOWED_CAPS)
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS (ALLOWED_CAPS)
|
|
|
|
);
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
G_DEFINE_TYPE (GstSmartEncoder, gst_smart_encoder, GST_TYPE_BIN);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
static void
|
|
|
|
smart_encoder_reset (GstSmartEncoder * self)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_segment_init (&self->internal_segment, GST_FORMAT_UNDEFINED);
|
|
|
|
gst_segment_init (&self->input_segment, GST_FORMAT_UNDEFINED);
|
|
|
|
gst_segment_init (&self->output_segment, GST_FORMAT_UNDEFINED);
|
|
|
|
|
|
|
|
if (self->decoder) {
|
|
|
|
/* Clean up/remove internal encoding elements */
|
|
|
|
gst_element_set_state (self->encoder, GST_STATE_NULL);
|
|
|
|
gst_element_set_state (self->decoder, GST_STATE_NULL);
|
|
|
|
gst_clear_object (&self->internal_srcpad);
|
|
|
|
gst_element_remove_pad (GST_ELEMENT (self), self->internal_sinkpad);
|
|
|
|
gst_bin_remove (GST_BIN (self), gst_object_ref (self->encoder));
|
|
|
|
gst_bin_remove (GST_BIN (self), self->decoder);
|
|
|
|
|
|
|
|
self->decoder = NULL;
|
|
|
|
self->internal_sinkpad = NULL;
|
|
|
|
}
|
|
|
|
gst_clear_event (&self->segment_event);
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
static void
|
|
|
|
translate_timestamp_from_internal_to_src (GstSmartEncoder * self,
|
|
|
|
GstClockTime * ts)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2020-07-04 16:33:20 +00:00
|
|
|
GstClockTime running_time;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
if (gst_segment_to_running_time_full (&self->internal_segment,
|
|
|
|
GST_FORMAT_TIME, *ts, &running_time) > 0)
|
|
|
|
*ts = running_time + self->output_segment.start;
|
|
|
|
else /* Negative timestamp */
|
|
|
|
*ts = self->output_segment.start - running_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_smart_encoder_finish_buffer (GstSmartEncoder * self, GstBuffer * buf)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2022-01-21 00:02:52 +00:00
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (buf)))
|
|
|
|
GST_BUFFER_DTS (buf) = GST_BUFFER_PTS (buf);
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
translate_timestamp_from_internal_to_src (self, &GST_BUFFER_PTS (buf));
|
|
|
|
translate_timestamp_from_internal_to_src (self, &GST_BUFFER_DTS (buf));
|
2022-01-21 00:02:52 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
if (self->last_dts > GST_BUFFER_DTS (buf)) {
|
|
|
|
/* Hack to always produces dts increasing DTS-s that are close to what the
|
|
|
|
* encoder produced. */
|
|
|
|
GST_BUFFER_DTS (buf) = self->last_dts + 1;
|
|
|
|
}
|
|
|
|
self->last_dts = GST_BUFFER_DTS (buf);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
return gst_pad_push (self->srcpad, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************
|
|
|
|
* Internal encoder/decoder pipeline *
|
|
|
|
******************************************/
|
|
|
|
static gboolean
|
|
|
|
internal_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstSmartEncoder *self = GST_SMART_ENCODER (parent);
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_EOS:
|
|
|
|
g_mutex_lock (&self->internal_flow_lock);
|
|
|
|
if (self->internal_flow == GST_FLOW_CUSTOM_SUCCESS)
|
|
|
|
self->internal_flow = GST_FLOW_OK;
|
|
|
|
g_cond_signal (&self->internal_flow_cond);
|
|
|
|
g_mutex_unlock (&self->internal_flow_lock);
|
|
|
|
break;
|
|
|
|
case GST_EVENT_SEGMENT:
|
|
|
|
gst_event_copy_segment (event, &self->internal_segment);
|
|
|
|
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
if (self->output_segment.format == GST_FORMAT_UNDEFINED) {
|
|
|
|
gst_segment_init (&self->output_segment, GST_FORMAT_TIME);
|
2020-07-04 16:33:20 +00:00
|
|
|
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
/* Ensure that we can represent negative DTS in our 'single' segment */
|
|
|
|
self->output_segment.start = 60 * 60 * GST_SECOND * 1000;
|
|
|
|
if (!gst_pad_push_event (self->srcpad,
|
|
|
|
gst_event_new_segment (&self->output_segment))) {
|
|
|
|
GST_ERROR_OBJECT (self, "Could not push segment!");
|
2020-07-04 16:33:20 +00:00
|
|
|
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
GST_ELEMENT_FLOW_ERROR (self, GST_FLOW_ERROR);
|
2020-07-04 16:33:20 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
break;
|
|
|
|
case GST_EVENT_CAPS:
|
|
|
|
{
|
2020-07-04 16:33:20 +00:00
|
|
|
return gst_pad_push_event (self->srcpad, event);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
return gst_pad_event_default (pad, parent, event);
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
internal_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
return gst_smart_encoder_finish_buffer (GST_SMART_ENCODER (parent), buf);
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
static void
|
2020-07-04 16:33:20 +00:00
|
|
|
decodebin_src_pad_added_cb (GstElement * decodebin, GstPad * srcpad,
|
|
|
|
GstSmartEncoder * self)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2020-07-04 16:33:20 +00:00
|
|
|
GstPadLinkReturn ret = gst_pad_link (srcpad, self->encoder->sinkpads->data);
|
|
|
|
|
|
|
|
if (ret != GST_PAD_LINK_OK) {
|
|
|
|
GST_ERROR_OBJECT (self, "Could not link decoder with encoder! %s",
|
|
|
|
gst_pad_link_get_name (ret));
|
|
|
|
g_mutex_lock (&self->internal_flow_lock);
|
|
|
|
self->internal_flow = GST_FLOW_NOT_LINKED;
|
|
|
|
g_mutex_unlock (&self->internal_flow_lock);
|
|
|
|
}
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
static gboolean
|
|
|
|
setup_recoder_pipeline (GstSmartEncoder * self)
|
|
|
|
{
|
|
|
|
GstPad *tmppad;
|
|
|
|
GstElement *capsfilter;
|
|
|
|
GstPadLinkReturn lret;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
/* Fast path */
|
|
|
|
if (G_UNLIKELY (self->decoder))
|
|
|
|
return TRUE;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
g_assert (self->encoder);
|
|
|
|
GST_DEBUG ("Creating internal decoder and encoder");
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
/* Create decoder/encoder */
|
|
|
|
self->decoder = gst_element_factory_make ("decodebin", NULL);
|
|
|
|
if (G_UNLIKELY (self->decoder == NULL))
|
|
|
|
goto no_decoder;
|
|
|
|
g_signal_connect (self->decoder, "pad-added",
|
|
|
|
G_CALLBACK (decodebin_src_pad_added_cb), self);
|
|
|
|
gst_element_set_locked_state (self->decoder, TRUE);
|
|
|
|
gst_bin_add (GST_BIN (self), self->decoder);
|
|
|
|
gst_bin_add (GST_BIN (self), gst_object_ref (self->encoder));
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Creating internal pads");
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
/* Create internal pads */
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
/* Source pad which we'll use to feed data to decoders */
|
|
|
|
self->internal_srcpad = gst_pad_new ("internal_src", GST_PAD_SRC);
|
|
|
|
self->internal_sinkpad = gst_pad_new ("internal_sink", GST_PAD_SINK);
|
|
|
|
gst_pad_set_iterate_internal_links_function (self->internal_sinkpad, NULL);
|
|
|
|
if (!gst_element_add_pad (GST_ELEMENT (self), self->internal_sinkpad)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Could not add internal sinkpad %" GST_PTR_FORMAT,
|
|
|
|
self->internal_sinkpad);
|
|
|
|
return FALSE;
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_pad_set_chain_function (self->internal_sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (internal_chain));
|
|
|
|
gst_pad_set_event_function (self->internal_sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (internal_event_func));
|
|
|
|
gst_pad_set_active (self->internal_sinkpad, TRUE);
|
|
|
|
gst_pad_set_active (self->internal_srcpad, TRUE);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Linking pads to elements");
|
|
|
|
|
|
|
|
/* Link everything */
|
|
|
|
capsfilter = gst_element_factory_make ("capsfilter", NULL);
|
|
|
|
if (!gst_bin_add (GST_BIN (self), capsfilter)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Could not add capsfilter!");
|
|
|
|
return FALSE;
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_element_sync_state_with_parent (capsfilter);
|
|
|
|
if (!gst_element_link (self->encoder, capsfilter))
|
|
|
|
goto encoder_capsfilter_link_fail;
|
|
|
|
tmppad = gst_element_get_static_pad (capsfilter, "src");
|
|
|
|
if ((lret =
|
|
|
|
gst_pad_link_full (tmppad, self->internal_sinkpad,
|
|
|
|
GST_PAD_LINK_CHECK_NOTHING)) < GST_PAD_LINK_OK)
|
|
|
|
goto sinkpad_link_fail;
|
|
|
|
gst_object_unref (tmppad);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
tmppad = gst_element_get_static_pad (self->decoder, "sink");
|
|
|
|
if (GST_PAD_LINK_FAILED (gst_pad_link_full (self->internal_srcpad,
|
|
|
|
tmppad, GST_PAD_LINK_CHECK_NOTHING)))
|
|
|
|
goto srcpad_link_fail;
|
|
|
|
gst_object_unref (tmppad);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_DEBUG ("Done creating internal elements/pads");
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
return TRUE;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
no_decoder:
|
|
|
|
{
|
|
|
|
GST_WARNING ("Couldn't find a decodebin?!");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
srcpad_link_fail:
|
|
|
|
{
|
|
|
|
gst_object_unref (tmppad);
|
|
|
|
GST_WARNING ("Couldn't link internal srcpad to decoder");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sinkpad_link_fail:
|
|
|
|
{
|
|
|
|
gst_object_unref (tmppad);
|
|
|
|
GST_WARNING ("Couldn't link encoder to internal sinkpad: %s",
|
|
|
|
gst_pad_link_get_name (lret));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder_capsfilter_link_fail:
|
|
|
|
{
|
|
|
|
GST_WARNING ("Couldn't link encoder to capsfilter");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_smart_encoder_reencode_gop (GstSmartEncoder * self)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
2020-07-04 16:33:20 +00:00
|
|
|
GstCaps *caps = NULL;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Reencoding GOP!");
|
|
|
|
if (self->decoder == NULL) {
|
|
|
|
if (!setup_recoder_pipeline (self)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Could not setup reencoder pipeline");
|
2010-08-13 15:36:38 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2020-07-04 16:33:20 +00:00
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Activate elements */
|
|
|
|
/* Set elements to PAUSED */
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_element_set_state (self->encoder, GST_STATE_PLAYING);
|
|
|
|
gst_element_set_state (self->decoder, GST_STATE_PLAYING);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
GST_INFO ("Pushing Flush start/stop to clean decoder/encoder");
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_pad_push_event (self->internal_srcpad, gst_event_new_flush_start ());
|
|
|
|
gst_pad_push_event (self->internal_srcpad, gst_event_new_flush_stop (TRUE));
|
|
|
|
|
|
|
|
/* push segment_event */
|
|
|
|
GST_INFO ("Pushing segment_event %" GST_PTR_FORMAT, self->segment_event);
|
|
|
|
gst_pad_push_event (self->internal_srcpad,
|
|
|
|
gst_event_ref (self->stream_start_event));
|
|
|
|
caps = gst_pad_get_current_caps (self->sinkpad);
|
|
|
|
gst_pad_push_event (self->internal_srcpad, gst_event_new_caps (caps));
|
|
|
|
gst_caps_unref (caps);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_pad_push_event (self->internal_srcpad,
|
|
|
|
gst_event_ref (self->segment_event));
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
/* Push buffers through our pads */
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_DEBUG ("Pushing %d pending buffers", g_list_length (self->pending_gop));
|
|
|
|
|
|
|
|
g_mutex_lock (&self->internal_flow_lock);
|
|
|
|
self->internal_flow = GST_FLOW_CUSTOM_SUCCESS;
|
|
|
|
g_mutex_unlock (&self->internal_flow_lock);
|
|
|
|
while (self->pending_gop) {
|
|
|
|
GstBuffer *buf = (GstBuffer *) self->pending_gop->data;
|
|
|
|
|
|
|
|
self->pending_gop =
|
|
|
|
g_list_remove_link (self->pending_gop, self->pending_gop);
|
|
|
|
res = gst_pad_push (self->internal_srcpad, buf);
|
|
|
|
if (res == GST_FLOW_EOS) {
|
|
|
|
GST_INFO_OBJECT (self, "Got eos... waiting for the event"
|
|
|
|
" waiting for encoding to be done");
|
2010-08-13 15:36:38 +00:00
|
|
|
break;
|
2020-07-04 16:33:20 +00:00
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
if (res != GST_FLOW_OK) {
|
|
|
|
GST_WARNING ("Error pushing pending buffers : %s",
|
|
|
|
gst_flow_get_name (res));
|
|
|
|
goto done;
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "-> Drain encoder.");
|
|
|
|
gst_pad_push_event (self->internal_srcpad, gst_event_new_eos ());
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
g_mutex_lock (&self->internal_flow_lock);
|
|
|
|
while (self->internal_flow == GST_FLOW_CUSTOM_SUCCESS) {
|
|
|
|
g_cond_wait (&self->internal_flow_cond, &self->internal_flow_lock);
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&self->internal_flow_lock);
|
|
|
|
|
|
|
|
res = self->internal_flow;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Done reencoding GOP.");
|
|
|
|
gst_element_set_state (self->encoder, GST_STATE_NULL);
|
|
|
|
gst_element_set_state (self->decoder, GST_STATE_NULL);
|
|
|
|
GST_OBJECT_FLAG_UNSET (self->internal_sinkpad, GST_PAD_FLAG_EOS);
|
|
|
|
GST_OBJECT_FLAG_UNSET (self->internal_srcpad, GST_PAD_FLAG_EOS);
|
|
|
|
|
|
|
|
done:
|
|
|
|
g_list_free_full (self->pending_gop, (GDestroyNotify) gst_buffer_unref);
|
|
|
|
self->pending_gop = NULL;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-10-15 02:02:36 +00:00
|
|
|
static gboolean
|
|
|
|
gst_smart_encoder_force_reencoding_for_caps (GstSmartEncoder * self)
|
|
|
|
{
|
|
|
|
const gchar *profile;
|
|
|
|
GstStructure *structure = gst_caps_get_structure (self->original_caps, 0);
|
|
|
|
|
|
|
|
if (!gst_structure_has_name (structure, "video/x-vp9"))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!(profile = gst_structure_get_string (structure, "profile"))) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"No profile set on `vp9` stream, force reencoding");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_strcmp0 (profile, "0") && g_strcmp0 (profile, "2")) {
|
|
|
|
GST_INFO_OBJECT (self, "vp9 profile %s not supported for smart reencoding"
|
|
|
|
" as it might be using RGB stream which we can't handle properly"
|
|
|
|
" force reencoding", profile);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-08-13 15:36:38 +00:00
|
|
|
static GstFlowReturn
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_smart_encoder_push_pending_gop (GstSmartEncoder * self)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2011-05-16 11:48:11 +00:00
|
|
|
guint64 cstart, cstop;
|
2010-08-13 15:36:38 +00:00
|
|
|
GList *tmp;
|
2021-10-15 02:02:36 +00:00
|
|
|
gboolean force_reencoding = FALSE;
|
2010-08-13 15:36:38 +00:00
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
|
|
|
|
|
|
|
GST_DEBUG ("Pushing pending GOP (%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
|
2020-07-04 16:33:20 +00:00
|
|
|
")", GST_TIME_ARGS (self->gop_start), GST_TIME_ARGS (self->gop_stop));
|
|
|
|
|
|
|
|
if (!self->pending_gop) {
|
|
|
|
/* This might happen on EOS */
|
|
|
|
GST_INFO_OBJECT (self, "Empty gop!");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_segment_clip (&self->input_segment, GST_FORMAT_TIME, self->gop_start,
|
|
|
|
self->gop_stop, &cstart, &cstop)) {
|
2010-08-13 15:36:38 +00:00
|
|
|
/* The whole GOP is outside the segment, there's most likely
|
|
|
|
* a bug somewhere. */
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"GOP is entirely outside of the segment, upstream gave us too much data: (%"
|
|
|
|
GST_TIME_FORMAT " -- %" GST_TIME_FORMAT ")",
|
|
|
|
GST_TIME_ARGS (self->gop_start), GST_TIME_ARGS (self->gop_stop));
|
|
|
|
for (tmp = self->pending_gop; tmp; tmp = tmp->next)
|
2010-08-13 15:36:38 +00:00
|
|
|
gst_buffer_unref ((GstBuffer *) tmp->data);
|
2020-07-04 16:33:20 +00:00
|
|
|
|
|
|
|
goto done;
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 02:02:36 +00:00
|
|
|
force_reencoding = gst_smart_encoder_force_reencoding_for_caps (self);
|
2020-07-04 16:33:20 +00:00
|
|
|
if ((cstart != self->gop_start)
|
2021-10-15 02:02:36 +00:00
|
|
|
|| (cstop != self->gop_stop)
|
|
|
|
|| force_reencoding) {
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_INFO_OBJECT (self,
|
|
|
|
"GOP needs to be re-encoded from %" GST_TIME_FORMAT " to %"
|
|
|
|
GST_TIME_FORMAT " - %" GST_SEGMENT_FORMAT, GST_TIME_ARGS (cstart),
|
|
|
|
GST_TIME_ARGS (cstop), &self->input_segment);
|
|
|
|
res = gst_smart_encoder_reencode_gop (self);
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
|
|
|
|
/* Make sure we push the original caps when resuming the original stream */
|
2021-10-15 02:02:36 +00:00
|
|
|
if (!force_reencoding)
|
|
|
|
self->push_original_caps = TRUE;
|
2020-07-04 16:33:20 +00:00
|
|
|
} else {
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
if (self->push_original_caps) {
|
|
|
|
gst_pad_push_event (self->srcpad,
|
|
|
|
gst_event_new_caps (self->original_caps));
|
|
|
|
self->push_original_caps = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->output_segment.format == GST_FORMAT_UNDEFINED) {
|
|
|
|
gst_segment_init (&self->output_segment, GST_FORMAT_TIME);
|
|
|
|
|
|
|
|
/* Ensure that we can represent negative DTS in our 'single' segment */
|
|
|
|
self->output_segment.start = 60 * 60 * GST_SECOND * 1000;
|
|
|
|
if (!gst_pad_push_event (self->srcpad,
|
|
|
|
gst_event_new_segment (&self->output_segment))) {
|
|
|
|
GST_ERROR_OBJECT (self, "Could not push segment!");
|
|
|
|
|
|
|
|
GST_ELEMENT_FLOW_ERROR (self, GST_FLOW_ERROR);
|
|
|
|
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
/* The whole GOP is within the segment, push all pending buffers downstream */
|
|
|
|
GST_INFO_OBJECT (self,
|
|
|
|
"GOP doesn't need to be modified, pushing downstream: %" GST_TIME_FORMAT
|
|
|
|
" to %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart), GST_TIME_ARGS (cstop));
|
|
|
|
|
|
|
|
self->internal_segment = self->input_segment;
|
|
|
|
for (tmp = self->pending_gop; tmp; tmp = tmp->next) {
|
|
|
|
GstBuffer *buf = (GstBuffer *) tmp->data;
|
|
|
|
|
|
|
|
res = gst_smart_encoder_finish_buffer (self, buf);
|
|
|
|
if (G_UNLIKELY (res != GST_FLOW_OK))
|
|
|
|
break;
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
2020-07-04 16:33:20 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
g_list_free (self->pending_gop);
|
|
|
|
self->pending_gop = NULL;
|
|
|
|
self->gop_start = GST_CLOCK_TIME_NONE;
|
|
|
|
self->gop_stop = 0;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-11-17 11:48:25 +00:00
|
|
|
gst_smart_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2020-07-04 16:33:20 +00:00
|
|
|
GstSmartEncoder *self;
|
2010-08-13 15:36:38 +00:00
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
|
|
|
gboolean discont, keyframe;
|
2020-07-04 16:33:20 +00:00
|
|
|
GstClockTime end_time;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
self = GST_SMART_ENCODER (parent->parent);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
discont = GST_BUFFER_IS_DISCONT (buf);
|
|
|
|
keyframe = !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
|
2020-07-04 16:33:20 +00:00
|
|
|
end_time = GST_BUFFER_PTS (buf);
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (end_time))
|
|
|
|
end_time += (GST_BUFFER_DURATION_IS_VALID (buf) ? buf->duration : 0);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_DEBUG_OBJECT (pad,
|
|
|
|
"New buffer %s %s %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
|
|
|
|
discont ? "discont" : "", keyframe ? "keyframe" : "",
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_PTS (buf)), GST_TIME_ARGS (end_time));
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
if (keyframe) {
|
|
|
|
/* If there's a pending GOP, flush it out */
|
2020-07-04 16:33:20 +00:00
|
|
|
if (self->pending_gop) {
|
|
|
|
/* Mark stop of previous gop */
|
|
|
|
if (GST_BUFFER_PTS_IS_VALID (buf)) {
|
|
|
|
if (self->gop_stop > buf->pts)
|
|
|
|
GST_WARNING_OBJECT (self, "Next gop start < current gop" " end");
|
|
|
|
self->gop_stop = buf->pts;
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
/* flush pending */
|
2020-07-04 16:33:20 +00:00
|
|
|
res = gst_smart_encoder_push_pending_gop (self);
|
2010-08-13 15:36:38 +00:00
|
|
|
if (G_UNLIKELY (res != GST_FLOW_OK))
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark gop_start for new gop */
|
2020-07-04 16:33:20 +00:00
|
|
|
self->gop_start = GST_BUFFER_TIMESTAMP (buf);
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Store buffer */
|
2020-07-04 16:33:20 +00:00
|
|
|
self->pending_gop = g_list_append (self->pending_gop, buf);
|
|
|
|
|
2010-08-13 15:36:38 +00:00
|
|
|
/* Update GOP stop position */
|
2020-07-04 16:33:20 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (end_time))
|
|
|
|
self->gop_stop = MAX (self->gop_stop, end_time);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Buffer stored , Current GOP : %"
|
|
|
|
GST_TIME_FORMAT " -- %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (self->gop_start), GST_TIME_ARGS (self->gop_stop));
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
beach:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-10-05 13:46:26 +00:00
|
|
|
static GstCaps *
|
|
|
|
smart_encoder_get_caps (GstSmartEncoder * self, GstCaps * original_caps)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
GstCaps *caps, *outcaps;
|
|
|
|
GstStructure *original_struct = gst_caps_get_structure (original_caps, 0);
|
|
|
|
GstStructure *out_struct, *_struct;
|
|
|
|
GstVideoInfo info;
|
|
|
|
static const gchar *default_fields[] = {
|
|
|
|
"pixel-aspect-ratio",
|
|
|
|
"framerate",
|
|
|
|
"interlace-mode",
|
|
|
|
"colorimetry",
|
|
|
|
"chroma-site",
|
|
|
|
"multiview-mode",
|
|
|
|
"multiview-flags",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!gst_structure_has_name (original_struct, "video/x-vp8")) {
|
|
|
|
|
|
|
|
return gst_caps_ref (original_caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VP8 is always decoded into YUV colorspaces and we support VP9 profiles
|
|
|
|
* where only YUV is supported (0 and 2) so we ensure that all the
|
|
|
|
* default fields for video/x-raw are set on the caps if none provided by
|
|
|
|
* upstream. This allows us to allow renegotiating new caps downstream when
|
|
|
|
* switching from no reencoding to reencoding making sure all the fields are
|
|
|
|
* defined all the time
|
|
|
|
*/
|
|
|
|
caps = gst_caps_copy (original_caps);
|
|
|
|
_struct = gst_caps_get_structure (caps, 0);
|
|
|
|
gst_structure_set_name (_struct, "video/x-raw");
|
|
|
|
gst_structure_set (_struct,
|
|
|
|
"format", G_TYPE_STRING, "I420",
|
|
|
|
"multiview-mode", G_TYPE_STRING, "mono",
|
|
|
|
"multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET,
|
|
|
|
GST_VIDEO_MULTIVIEW_FLAGS_NONE, GST_FLAG_SET_MASK_EXACT, NULL);
|
|
|
|
|
|
|
|
gst_video_info_from_caps (&info, caps);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
caps = gst_video_info_to_caps (&info);
|
|
|
|
_struct = gst_caps_get_structure (caps, 0);
|
|
|
|
|
|
|
|
outcaps = gst_caps_copy (original_caps);
|
|
|
|
out_struct = gst_caps_get_structure (outcaps, 0);
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (default_fields); i++) {
|
|
|
|
const gchar *field = default_fields[i];
|
|
|
|
|
|
|
|
if (!gst_structure_has_field (original_struct, field)) {
|
|
|
|
const GValue *v = gst_structure_get_value (_struct, field);
|
|
|
|
g_assert (v);
|
|
|
|
gst_structure_set_value (out_struct, field, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
|
|
|
return outcaps;
|
|
|
|
}
|
|
|
|
|
2010-08-13 15:36:38 +00:00
|
|
|
static gboolean
|
2020-07-04 16:33:20 +00:00
|
|
|
smart_encoder_sink_event (GstPad * pad, GstObject * ghostpad, GstEvent * event)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
|
|
|
gboolean res = TRUE;
|
2020-07-04 16:33:20 +00:00
|
|
|
GstSmartEncoder *self = GST_SMART_ENCODER (ghostpad->parent);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
2020-07-04 16:33:20 +00:00
|
|
|
smart_encoder_reset (self);
|
|
|
|
break;
|
|
|
|
case GST_EVENT_CAPS:
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
gst_event_parse_caps (event, &caps);
|
|
|
|
if (self->original_caps)
|
|
|
|
gst_caps_unref (self->original_caps);
|
|
|
|
|
2021-10-05 13:46:26 +00:00
|
|
|
|
|
|
|
self->original_caps = smart_encoder_get_caps (self, caps);
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
self->push_original_caps = TRUE;
|
|
|
|
gst_clear_event (&event);
|
2020-07-04 16:33:20 +00:00
|
|
|
break;
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
}
|
2020-07-04 16:33:20 +00:00
|
|
|
case GST_EVENT_STREAM_START:
|
|
|
|
gst_event_replace (&self->stream_start_event, gst_event_ref (event));
|
2010-08-13 15:36:38 +00:00
|
|
|
break;
|
2011-05-16 11:48:11 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_INFO_OBJECT (self, "Pushing pending GOP on new segment");
|
|
|
|
gst_smart_encoder_push_pending_gop (self);
|
|
|
|
|
|
|
|
gst_event_copy_segment (event, &self->input_segment);
|
2011-05-16 11:48:11 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "input_segment: %" GST_SEGMENT_FORMAT,
|
|
|
|
&self->input_segment);
|
|
|
|
if (self->input_segment.format != GST_FORMAT_TIME) {
|
|
|
|
GST_ERROR_OBJECT (self, "Can't handle streams %s format",
|
|
|
|
gst_format_get_name (self->input_segment.format));
|
2016-04-25 14:16:04 +00:00
|
|
|
gst_event_unref (event);
|
2020-07-04 16:33:20 +00:00
|
|
|
|
2016-04-25 14:16:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2020-07-04 16:33:20 +00:00
|
|
|
self->segment_event = event;
|
|
|
|
event = NULL;
|
|
|
|
GST_INFO_OBJECT (self, "Eating segment");
|
2010-08-13 15:36:38 +00:00
|
|
|
break;
|
2020-07-04 16:33:20 +00:00
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
case GST_EVENT_EOS:
|
2020-07-04 16:33:20 +00:00
|
|
|
if (self->input_segment.format == GST_FORMAT_TIME)
|
|
|
|
gst_smart_encoder_push_pending_gop (self);
|
2010-08-13 15:36:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
if (event)
|
|
|
|
res = gst_pad_push_event (self->srcpad, event);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
2020-07-04 16:33:20 +00:00
|
|
|
smart_encoder_sink_getcaps (GstSmartEncoder * self, GstPad * pad,
|
|
|
|
GstCaps * filter)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
|
|
|
GstCaps *peer, *tmpl, *res;
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
tmpl = gst_static_pad_template_get_caps (&src_template);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2011-05-16 09:04:30 +00:00
|
|
|
/* Try getting it from downstream */
|
2020-07-04 16:33:20 +00:00
|
|
|
peer = gst_pad_peer_query_caps (self->srcpad, tmpl);
|
2010-08-13 15:36:38 +00:00
|
|
|
if (peer == NULL) {
|
|
|
|
res = tmpl;
|
|
|
|
} else {
|
2011-05-16 09:04:30 +00:00
|
|
|
res = peer;
|
2010-08-13 15:36:38 +00:00
|
|
|
gst_caps_unref (tmpl);
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
if (filter) {
|
|
|
|
GstCaps *filtered_res = gst_caps_intersect (res, filter);
|
|
|
|
|
|
|
|
gst_caps_unref (res);
|
|
|
|
if (!filtered_res || gst_caps_is_empty (filtered_res)) {
|
|
|
|
res = NULL;
|
|
|
|
} else {
|
|
|
|
res = filtered_res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-13 15:36:38 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-11-15 15:30:38 +00:00
|
|
|
static gboolean
|
2020-07-04 16:33:20 +00:00
|
|
|
_pad_sink_acceptcaps (GstPad * pad, GstSmartEncoder * self, GstCaps * caps)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
GstCaps *modified_caps;
|
|
|
|
GstCaps *accepted_caps;
|
|
|
|
gint i, n;
|
|
|
|
GstStructure *s;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pad, "%" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
accepted_caps = gst_pad_get_current_caps (GST_PAD (self->srcpad));
|
|
|
|
if (accepted_caps == NULL)
|
|
|
|
accepted_caps = gst_pad_get_pad_template_caps (GST_PAD (self->srcpad));
|
|
|
|
accepted_caps = gst_caps_make_writable (accepted_caps);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pad, "src caps %" GST_PTR_FORMAT, accepted_caps);
|
|
|
|
|
|
|
|
n = gst_caps_get_size (accepted_caps);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
s = gst_caps_get_structure (accepted_caps, i);
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
|
|
|
|
if (gst_structure_has_name (s, "video/x-h264") ||
|
|
|
|
gst_structure_has_name (s, "video/x-h265")) {
|
|
|
|
gst_structure_remove_fields (s, "codec_data", "tier", "profile", "level",
|
|
|
|
NULL);
|
|
|
|
} else if (gst_structure_has_name (s, "video/x-vp8")
|
|
|
|
|| gst_structure_has_name (s, "video/x-vp9")) {
|
|
|
|
gst_structure_remove_field (s, "streamheader");
|
|
|
|
}
|
2020-07-04 16:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
modified_caps = gst_caps_copy (caps);
|
|
|
|
n = gst_caps_get_size (modified_caps);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
s = gst_caps_get_structure (modified_caps, i);
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
|
|
|
|
if (gst_structure_has_name (s, "video/x-h264") ||
|
|
|
|
gst_structure_has_name (s, "video/x-h265")) {
|
|
|
|
gst_structure_remove_fields (s, "codec_data", "tier", "profile", "level",
|
|
|
|
NULL);
|
|
|
|
} else if (gst_structure_has_name (s, "video/x-vp8")
|
|
|
|
|| gst_structure_has_name (s, "video/x-vp9")) {
|
|
|
|
gst_structure_remove_field (s, "streamheader");
|
|
|
|
}
|
2020-07-04 16:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = gst_caps_can_intersect (modified_caps, accepted_caps);
|
|
|
|
GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
|
|
|
|
(ret ? "" : "Doesn't "), caps);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
smart_encoder_sink_query (GstPad * pad, GstObject * ghostpad, GstQuery * query)
|
2011-11-15 15:30:38 +00:00
|
|
|
{
|
|
|
|
gboolean res;
|
2020-07-04 16:33:20 +00:00
|
|
|
GstSmartEncoder *self = GST_SMART_ENCODER (ghostpad->parent);
|
2011-11-15 15:30:38 +00:00
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *filter, *caps;
|
|
|
|
|
|
|
|
gst_query_parse_caps (query, &filter);
|
2020-07-04 16:33:20 +00:00
|
|
|
caps = smart_encoder_sink_getcaps (self, pad, filter);
|
|
|
|
GST_DEBUG_OBJECT (self, "Got caps: %" GST_PTR_FORMAT, caps);
|
2011-11-15 15:30:38 +00:00
|
|
|
gst_query_set_caps_result (query, caps);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2020-07-04 16:33:20 +00:00
|
|
|
case GST_QUERY_ACCEPT_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
gst_query_parse_accept_caps (query, &caps);
|
|
|
|
res = _pad_sink_acceptcaps (GST_PAD (pad), self, caps);
|
|
|
|
gst_query_set_accept_caps_result (query, res);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-15 15:30:38 +00:00
|
|
|
default:
|
2020-07-04 16:33:20 +00:00
|
|
|
res = gst_pad_query_default (pad, ghostpad, query);
|
2011-11-15 15:30:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-08-13 15:36:38 +00:00
|
|
|
static gboolean
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_smart_encoder_add_parser (GstSmartEncoder * self, GstCaps * format)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2021-08-10 01:24:34 +00:00
|
|
|
const gchar *stream_format;
|
2021-10-15 01:29:19 +00:00
|
|
|
GstPad *chainpad, *internal_chainpad, *sinkpad = NULL;
|
2021-08-10 01:24:34 +00:00
|
|
|
GstStructure *structure = gst_caps_get_structure (format, 0);
|
2020-07-04 16:33:20 +00:00
|
|
|
GstElement *capsfilter = gst_element_factory_make ("capsfilter", NULL);
|
2021-10-15 01:29:19 +00:00
|
|
|
GstElement *parser = NULL;
|
2020-07-04 16:33:20 +00:00
|
|
|
|
|
|
|
gst_bin_add (GST_BIN (self), capsfilter);
|
|
|
|
g_object_set (capsfilter, "caps", format, NULL);
|
2021-08-10 01:24:34 +00:00
|
|
|
if (gst_structure_has_name (structure, "video/x-h264")) {
|
2021-10-15 01:29:19 +00:00
|
|
|
parser = gst_element_factory_make ("h264parse", NULL);
|
2020-07-04 16:33:20 +00:00
|
|
|
if (!parser) {
|
|
|
|
GST_ERROR_OBJECT (self, "`h264parse` is missing, can't encode smartly");
|
|
|
|
|
|
|
|
goto failed;
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2021-08-10 01:24:34 +00:00
|
|
|
stream_format = gst_structure_get_string (structure, "stream-format");
|
2022-01-22 01:35:36 +00:00
|
|
|
if (g_strcmp0 (stream_format, "avc"))
|
2021-08-10 01:24:34 +00:00
|
|
|
g_object_set (parser, "config-interval", -1, NULL);
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
|
|
|
|
} else if (gst_structure_has_name (gst_caps_get_structure (format, 0),
|
|
|
|
"video/x-h265")) {
|
2021-10-15 01:29:19 +00:00
|
|
|
parser = gst_element_factory_make ("h265parse", NULL);
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
if (!parser) {
|
|
|
|
GST_ERROR_OBJECT (self, "`h265parse` is missing, can't encode smartly");
|
|
|
|
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2021-08-10 01:24:34 +00:00
|
|
|
stream_format = gst_structure_get_string (structure, "stream-format");
|
|
|
|
if (g_strcmp0 (stream_format, "hvc1"))
|
|
|
|
g_object_set (parser, "config-interval", -1, NULL);
|
2021-10-15 01:29:19 +00:00
|
|
|
} else if (gst_structure_has_name (structure, "video/x-vp9")) {
|
|
|
|
parser = gst_element_factory_make ("vp9parse", NULL);
|
|
|
|
if (!parser) {
|
|
|
|
GST_ERROR_OBJECT (self, "`vp9parse` is missing, can't encode smartly");
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
|
2021-10-15 01:29:19 +00:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sinkpad = gst_element_get_static_pad (capsfilter, "sink");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser) {
|
2020-07-04 16:33:20 +00:00
|
|
|
if (!gst_bin_add (GST_BIN (self), parser)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Could not add parser.");
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
goto failed;
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
if (!gst_element_link (parser, capsfilter)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Could not link capfilter and parser.");
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
goto failed;
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
sinkpad = gst_element_get_static_pad (parser, "sink");
|
|
|
|
}
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
g_assert (sinkpad);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
/* The chainpad is the pad that is linked to the srcpad of the chain
|
|
|
|
* of element that is linked to our public sinkpad, this is the pad where
|
|
|
|
* we chain the buffers either directly to our srcpad or through the
|
|
|
|
* reencoding sub chain. */
|
|
|
|
chainpad =
|
|
|
|
GST_PAD (gst_ghost_pad_new ("chainpad", capsfilter->srcpads->data));
|
|
|
|
gst_element_add_pad (GST_ELEMENT (self), chainpad);
|
|
|
|
internal_chainpad =
|
|
|
|
GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (chainpad)));
|
|
|
|
gst_pad_set_chain_function (internal_chainpad, gst_smart_encoder_chain);
|
|
|
|
gst_pad_set_event_function (internal_chainpad, smart_encoder_sink_event);
|
|
|
|
gst_pad_set_query_function (internal_chainpad, smart_encoder_sink_query);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
gst_ghost_pad_set_target (GST_GHOST_PAD (self->sinkpad), sinkpad);
|
|
|
|
gst_object_unref (sinkpad);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
failed:
|
2021-10-15 01:29:19 +00:00
|
|
|
gst_clear_object (&parser);
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
return FALSE;
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
gboolean
|
|
|
|
gst_smart_encoder_set_encoder (GstSmartEncoder * self, GstCaps * format,
|
|
|
|
GstElement * encoder)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2020-07-04 16:33:20 +00:00
|
|
|
self->encoder = g_object_ref_sink (encoder);
|
|
|
|
gst_element_set_locked_state (self->encoder, TRUE);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
return gst_smart_encoder_add_parser (self, format);
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
* GstElement vmethod implementations *
|
|
|
|
******************************************/
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_smart_encoder_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
2020-07-04 16:33:20 +00:00
|
|
|
GstSmartEncoder *self;
|
2010-08-13 15:36:38 +00:00
|
|
|
GstStateChangeReturn ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_SMART_ENCODER (element),
|
|
|
|
GST_STATE_CHANGE_FAILURE);
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
self = GST_SMART_ENCODER (element);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
ret =
|
|
|
|
GST_ELEMENT_CLASS (gst_smart_encoder_parent_class)->change_state (element,
|
|
|
|
transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2020-07-04 16:33:20 +00:00
|
|
|
smart_encoder_reset (self);
|
2010-08-13 15:36:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-07-04 16:33:20 +00:00
|
|
|
|
|
|
|
/******************************************
|
|
|
|
* GObject vmethods *
|
|
|
|
******************************************/
|
|
|
|
static void
|
|
|
|
gst_smart_encoder_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstSmartEncoder *self = (GstSmartEncoder *) object;
|
|
|
|
g_mutex_clear (&self->internal_flow_lock);
|
|
|
|
g_cond_clear (&self->internal_flow_cond);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_smart_encoder_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_smart_encoder_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstSmartEncoder *self = (GstSmartEncoder *) object;
|
|
|
|
|
|
|
|
gst_clear_object (&self->encoder);
|
|
|
|
|
smartencoder: clean up and extend accepted formats
* Add support for H265
* Don't overwrite original codec_data / streamheader in the output
caps, but instead allow them to change and send them to the
combiner at the right moment: encoder caps, reencoded GOP,
original caps, original GOP(s), and potentially encoder caps
and rencoded last GOP.
* For H264 / H265, force usage of a format with inband SPS / PPS
(avc3 / hev1), this is cleaner than misadvertising avc1, hvc1 and
some muxers like mp4mux will actually advertise both differently.
Unfortunately, while mp4 supports updating the codec_data and using
avc1 with no in-band SPS / PPS updates, it turns out some decoders
(eg chrome / firefox) don't handle this particularly well and stop
decoding after the reencoded GOP. We could expose a switch to
force usage of avc1 / hvc1 nevertheless, but for now stick to
requiring that the parser output SPS / PPS in-band with
config-interval=-1 (that has not changed)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1249>
2021-08-10 00:09:09 +00:00
|
|
|
if (self->original_caps) {
|
|
|
|
gst_caps_unref (self->original_caps);
|
|
|
|
self->original_caps = NULL;
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:33:20 +00:00
|
|
|
G_OBJECT_CLASS (gst_smart_encoder_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_smart_encoder_class_init (GstSmartEncoderClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *element_class;
|
|
|
|
|
|
|
|
element_class = (GstElementClass *) klass;
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gst_smart_encoder_parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
|
|
|
gst_element_class_add_static_pad_template (element_class, &src_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class, &sink_template);
|
|
|
|
|
|
|
|
gst_element_class_set_static_metadata (element_class, "Smart Video Encoder",
|
|
|
|
"Codec/Recoder/Video",
|
|
|
|
"Re-encodes portions of Video that lay on segment boundaries",
|
|
|
|
"Edward Hervey <bilboed@gmail.com>");
|
|
|
|
|
|
|
|
gobject_class->dispose = (GObjectFinalizeFunc) gst_smart_encoder_dispose;
|
|
|
|
gobject_class->finalize = (GObjectFinalizeFunc) gst_smart_encoder_finalize;
|
|
|
|
element_class->change_state = gst_smart_encoder_change_state;
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (smart_encoder_debug, "smartencoder", 0,
|
|
|
|
"Smart Encoder");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_smart_encoder_init (GstSmartEncoder * self)
|
|
|
|
{
|
|
|
|
GstPadTemplate *template = gst_static_pad_template_get (&sink_template);
|
|
|
|
|
|
|
|
self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", template);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
|
|
|
|
gst_object_unref (template);
|
|
|
|
|
|
|
|
self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
|
|
|
|
gst_pad_use_fixed_caps (self->srcpad);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
|
|
|
|
|
|
|
|
g_mutex_init (&self->internal_flow_lock);
|
|
|
|
g_cond_init (&self->internal_flow_cond);
|
|
|
|
smart_encoder_reset (self);
|
|
|
|
}
|