2011-03-21 17:02:15 +00:00
|
|
|
/* GStreamer
|
2009-07-01 17:55:12 +00:00
|
|
|
* Copyright (C) 2006 David Schleef <ds@schleef.org>
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2012-02-21 15:53:37 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstbasevideocodec
|
|
|
|
* @short_description: Base class and objects for video codecs
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-01-27 14:49:58 +00:00
|
|
|
/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
|
|
|
|
* with newer GLib versions (>= 2.31.0) */
|
|
|
|
#define GLIB_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
#include "gstbasevideocodec.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2010-06-02 23:01:28 +00:00
|
|
|
GST_DEBUG_CATEGORY (basevideocodec_debug);
|
|
|
|
#define GST_CAT_DEFAULT basevideocodec_debug
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
/* GstBaseVideoCodec signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ARG_0
|
|
|
|
};
|
|
|
|
|
|
|
|
static void gst_base_video_codec_finalize (GObject * object);
|
|
|
|
|
|
|
|
static GstStateChangeReturn gst_base_video_codec_change_state (GstElement *
|
|
|
|
element, GstStateChange transition);
|
|
|
|
|
2011-10-04 12:27:32 +00:00
|
|
|
static GstElementClass *parent_class = NULL;
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-12-05 17:57:01 +00:00
|
|
|
G_DEFINE_BOXED_TYPE (GstVideoFrameState, gst_video_frame_state,
|
|
|
|
(GBoxedCopyFunc) gst_video_frame_state_ref,
|
2011-12-30 10:41:17 +00:00
|
|
|
(GBoxedFreeFunc) gst_video_frame_state_unref);
|
2011-12-05 17:57:01 +00:00
|
|
|
|
2011-11-03 12:56:49 +00:00
|
|
|
/* NOTE (Edward): Do not use G_DEFINE_* because we need to have
|
|
|
|
* a GClassInitFunc called with the target class (which the macros
|
2011-12-30 10:41:17 +00:00
|
|
|
* don't handle).
|
|
|
|
*/
|
|
|
|
static void gst_base_video_codec_class_init (GstBaseVideoCodecClass * klass);
|
|
|
|
static void gst_base_video_codec_init (GstBaseVideoCodec * dec,
|
2011-10-04 12:27:32 +00:00
|
|
|
GstBaseVideoCodecClass * klass);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-10-04 12:27:32 +00:00
|
|
|
GType
|
|
|
|
gst_base_video_codec_get_type (void)
|
2009-07-01 17:55:12 +00:00
|
|
|
{
|
2011-10-04 12:27:32 +00:00
|
|
|
static volatile gsize base_video_codec_type = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&base_video_codec_type)) {
|
|
|
|
GType _type;
|
|
|
|
static const GTypeInfo base_video_codec_info = {
|
|
|
|
sizeof (GstBaseVideoCodecClass),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_base_video_codec_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstBaseVideoCodec),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_base_video_codec_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
_type = g_type_register_static (GST_TYPE_ELEMENT,
|
|
|
|
"GstBaseVideoCodec", &base_video_codec_info, G_TYPE_FLAG_ABSTRACT);
|
|
|
|
g_once_init_leave (&base_video_codec_type, _type);
|
|
|
|
}
|
|
|
|
return base_video_codec_type;
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_base_video_codec_class_init (GstBaseVideoCodecClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *element_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
2011-11-03 12:56:49 +00:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
gobject_class->finalize = gst_base_video_codec_finalize;
|
|
|
|
|
|
|
|
element_class->change_state = gst_base_video_codec_change_state;
|
2011-10-04 12:27:32 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (basevideocodec_debug, "basevideocodec", 0,
|
|
|
|
"Base Video Codec");
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_base_video_codec_init (GstBaseVideoCodec * base_video_codec,
|
|
|
|
GstBaseVideoCodecClass * klass)
|
|
|
|
{
|
|
|
|
GstPadTemplate *pad_template;
|
|
|
|
|
2011-03-22 12:19:17 +00:00
|
|
|
GST_DEBUG_OBJECT (base_video_codec, "gst_base_video_codec_init");
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
pad_template =
|
|
|
|
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
|
|
|
|
g_return_if_fail (pad_template != NULL);
|
|
|
|
|
|
|
|
base_video_codec->sinkpad = gst_pad_new_from_template (pad_template, "sink");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (base_video_codec),
|
|
|
|
base_video_codec->sinkpad);
|
|
|
|
|
|
|
|
pad_template =
|
|
|
|
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
|
|
|
|
g_return_if_fail (pad_template != NULL);
|
|
|
|
|
|
|
|
base_video_codec->srcpad = gst_pad_new_from_template (pad_template, "src");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (base_video_codec),
|
|
|
|
base_video_codec->srcpad);
|
|
|
|
|
2010-10-10 00:36:07 +00:00
|
|
|
gst_segment_init (&base_video_codec->segment, GST_FORMAT_TIME);
|
|
|
|
|
2012-01-19 10:34:26 +00:00
|
|
|
g_rec_mutex_init (&base_video_codec->stream_lock);
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_base_video_codec_reset (GstBaseVideoCodec * base_video_codec)
|
|
|
|
{
|
2010-10-10 00:36:07 +00:00
|
|
|
GList *g;
|
|
|
|
|
2011-03-22 12:19:17 +00:00
|
|
|
GST_DEBUG_OBJECT (base_video_codec, "reset");
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_codec);
|
2010-10-10 00:36:07 +00:00
|
|
|
for (g = base_video_codec->frames; g; g = g_list_next (g)) {
|
2011-12-05 17:57:01 +00:00
|
|
|
gst_video_frame_state_unref ((GstVideoFrameState *) g->data);
|
2010-10-10 00:36:07 +00:00
|
|
|
}
|
|
|
|
g_list_free (base_video_codec->frames);
|
2011-05-12 23:03:27 +00:00
|
|
|
base_video_codec->frames = NULL;
|
2010-10-10 00:36:07 +00:00
|
|
|
|
2011-03-28 11:32:17 +00:00
|
|
|
base_video_codec->bytes = 0;
|
|
|
|
base_video_codec->time = 0;
|
2011-06-27 07:41:40 +00:00
|
|
|
|
|
|
|
gst_buffer_replace (&base_video_codec->state.codec_data, NULL);
|
|
|
|
gst_caps_replace (&base_video_codec->state.caps, NULL);
|
2011-12-30 03:02:46 +00:00
|
|
|
memset (&base_video_codec->state, 0, sizeof (GstVideoState));
|
|
|
|
base_video_codec->state.format = GST_VIDEO_FORMAT_UNKNOWN;
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_codec);
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_base_video_codec_finalize (GObject * object)
|
|
|
|
{
|
2011-08-18 08:02:50 +00:00
|
|
|
GstBaseVideoCodec *base_video_codec = GST_BASE_VIDEO_CODEC (object);
|
|
|
|
|
2012-01-19 10:34:26 +00:00
|
|
|
g_rec_mutex_clear (&base_video_codec->stream_lock);
|
2011-08-18 08:02:50 +00:00
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_base_video_codec_change_state (GstElement * element,
|
|
|
|
GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstBaseVideoCodec *base_video_codec = GST_BASE_VIDEO_CODEC (element);
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
gst_base_video_codec_reset (base_video_codec);
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = parent_class->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
gst_base_video_codec_reset (base_video_codec);
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-21 15:53:37 +00:00
|
|
|
/**
|
|
|
|
* gst_base_video_codec_append_frame:
|
|
|
|
* @codec: a #GstBaseVideoCodec
|
|
|
|
* @frame: the #GstVideoFrameState to append
|
|
|
|
*
|
|
|
|
* Appends a frame to the list of frames handled by the codec.
|
|
|
|
*
|
|
|
|
* Note: This should normally not be used by implementations.
|
|
|
|
**/
|
2011-12-05 17:57:01 +00:00
|
|
|
void
|
|
|
|
gst_base_video_codec_append_frame (GstBaseVideoCodec * codec,
|
|
|
|
GstVideoFrameState * frame)
|
2009-07-01 17:55:12 +00:00
|
|
|
{
|
2011-12-05 17:57:01 +00:00
|
|
|
g_return_if_fail (frame != NULL);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-12-05 17:57:01 +00:00
|
|
|
gst_video_frame_state_ref (frame);
|
|
|
|
codec->frames = g_list_append (codec->frames, frame);
|
|
|
|
}
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-12-05 17:57:01 +00:00
|
|
|
void
|
|
|
|
gst_base_video_codec_remove_frame (GstBaseVideoCodec * codec,
|
|
|
|
GstVideoFrameState * frame)
|
|
|
|
{
|
|
|
|
GList *link;
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-12-05 17:57:01 +00:00
|
|
|
g_return_if_fail (frame != NULL);
|
2011-11-03 12:56:49 +00:00
|
|
|
|
2011-12-05 17:57:01 +00:00
|
|
|
link = g_list_find (codec->frames, frame);
|
|
|
|
if (link) {
|
|
|
|
gst_video_frame_state_unref ((GstVideoFrameState *) link->data);
|
|
|
|
codec->frames = g_list_delete_link (codec->frames, link);
|
|
|
|
}
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2011-12-05 17:57:01 +00:00
|
|
|
static void
|
|
|
|
_gst_video_frame_state_free (GstVideoFrameState * frame)
|
2009-07-01 17:55:12 +00:00
|
|
|
{
|
2011-07-09 09:09:12 +00:00
|
|
|
g_return_if_fail (frame != NULL);
|
|
|
|
|
2011-11-03 12:56:49 +00:00
|
|
|
GST_LOG ("Freeing frame %p (sfn:%d)", frame, frame->system_frame_number);
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
if (frame->sink_buffer) {
|
|
|
|
gst_buffer_unref (frame->sink_buffer);
|
|
|
|
}
|
|
|
|
|
2011-03-29 13:41:55 +00:00
|
|
|
if (frame->src_buffer) {
|
|
|
|
gst_buffer_unref (frame->src_buffer);
|
|
|
|
}
|
|
|
|
|
2011-06-28 09:47:25 +00:00
|
|
|
g_list_foreach (frame->events, (GFunc) gst_event_unref, NULL);
|
|
|
|
g_list_free (frame->events);
|
|
|
|
|
2011-07-09 08:57:52 +00:00
|
|
|
if (frame->coder_hook_destroy_notify && frame->coder_hook)
|
|
|
|
frame->coder_hook_destroy_notify (frame->coder_hook);
|
|
|
|
|
2011-10-04 12:27:32 +00:00
|
|
|
g_slice_free (GstVideoFrameState, frame);
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
2011-12-05 17:57:01 +00:00
|
|
|
|
2012-02-21 15:53:37 +00:00
|
|
|
/**
|
|
|
|
* gst_base_video_codec_new_frame:
|
|
|
|
* @base_video_codec: a #GstBaseVideoCodec
|
|
|
|
*
|
|
|
|
* Creates a new #GstVideoFrameState for usage in decoders or encoders.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): The new #GstVideoFrameState, call
|
|
|
|
* #gst_video_frame_state_unref() when done with it.
|
|
|
|
*/
|
2011-12-05 17:57:01 +00:00
|
|
|
GstVideoFrameState *
|
|
|
|
gst_base_video_codec_new_frame (GstBaseVideoCodec * base_video_codec)
|
|
|
|
{
|
|
|
|
GstVideoFrameState *frame;
|
|
|
|
|
|
|
|
frame = g_slice_new0 (GstVideoFrameState);
|
|
|
|
|
|
|
|
frame->ref_count = 1;
|
|
|
|
|
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_codec);
|
|
|
|
frame->system_frame_number = base_video_codec->system_frame_number;
|
|
|
|
base_video_codec->system_frame_number++;
|
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_codec);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (base_video_codec, "Created new frame %p (sfn:%d)",
|
|
|
|
frame, frame->system_frame_number);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstVideoFrameState *
|
|
|
|
gst_video_frame_state_ref (GstVideoFrameState * frame)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (frame != NULL, NULL);
|
|
|
|
|
|
|
|
g_atomic_int_inc (&frame->ref_count);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_video_frame_state_unref (GstVideoFrameState * frame)
|
|
|
|
{
|
|
|
|
g_return_if_fail (frame != NULL);
|
|
|
|
g_return_if_fail (frame->ref_count > 0);
|
|
|
|
|
|
|
|
if (g_atomic_int_dec_and_test (&frame->ref_count)) {
|
|
|
|
_gst_video_frame_state_free (frame);
|
|
|
|
}
|
|
|
|
}
|