1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/* GStreamer
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/**
* SECTION:gstbasevideocodec
* @short_description: Base class for video codecs
* @see_also: #GstBaseVideoDecoder , #GstBaseVideoEncoder
*/
/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
* with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#include "gstbasevideocodec.h"
#include <string.h>
#include <math.h>
GST_DEBUG_CATEGORY (basevideocodec_debug);
#define GST_CAT_DEFAULT basevideocodec_debug
/* 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);
GType
gst_video_frame_get_type (void)
{
static volatile gsize type = 0;
if (g_once_init_enter (&type)) {
GType _type;
_type = g_boxed_type_register_static ("GstVideoFrame",
(GBoxedCopyFunc) gst_video_frame_ref,
(GBoxedFreeFunc) gst_video_frame_unref);
g_once_init_leave (&type, _type);
}
return (GType) type;
}
GST_BOILERPLATE (GstBaseVideoCodec, gst_base_video_codec, GstElement,
GST_TYPE_ELEMENT);
static void
gst_base_video_codec_base_init (gpointer g_class)
{
GST_DEBUG_CATEGORY_INIT (basevideocodec_debug, "basevideocodec", 0,
"Base Video Codec");
}
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);
gobject_class->finalize = gst_base_video_codec_finalize;
element_class->change_state = gst_base_video_codec_change_state;
}
static void
gst_base_video_codec_init (GstBaseVideoCodec * base_video_codec,
GstBaseVideoCodecClass * klass)
{
GstPadTemplate *pad_template;
GST_DEBUG_OBJECT (base_video_codec, "gst_base_video_codec_init");
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);
gst_segment_init (&base_video_codec->segment, GST_FORMAT_TIME);
g_static_rec_mutex_init (&base_video_codec->stream_lock);
}
static void
gst_base_video_codec_reset (GstBaseVideoCodec * base_video_codec)
{
GList *g;
GST_DEBUG_OBJECT (base_video_codec, "reset");
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_codec);
for (g = base_video_codec->frames; g; g = g_list_next (g)) {
gst_video_frame_unref ((GstVideoFrame *) g->data);
}
g_list_free (base_video_codec->frames);
base_video_codec->frames = NULL;
base_video_codec->bytes = 0;
base_video_codec->time = 0;
gst_buffer_replace (&base_video_codec->state.codec_data, NULL);
gst_caps_replace (&base_video_codec->state.caps, NULL);
memset (&base_video_codec->state, 0, sizeof (GstVideoState));
base_video_codec->state.format = GST_VIDEO_FORMAT_UNKNOWN;
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_codec);
}
static void
gst_base_video_codec_finalize (GObject * object)
{
GstBaseVideoCodec *base_video_codec = GST_BASE_VIDEO_CODEC (object);
g_static_rec_mutex_free (&base_video_codec->stream_lock);
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;
}
/**
* gst_base_video_codec_new_frame:
* @base_video_codec: a #GstBaseVideoCodec
*
* Create a new blank #GstVideoFrame.
*
* Returns: (transfer full): a new #GstVideoFrame
*/
GstVideoFrame *
gst_base_video_codec_new_frame (GstBaseVideoCodec * base_video_codec)
{
GstVideoFrame *frame;
frame = g_slice_new0 (GstVideoFrame);
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;
}
static void
_gst_video_frame_free (GstVideoFrame * frame)
{
g_return_if_fail (frame != NULL);
if (frame->sink_buffer) {
gst_buffer_unref (frame->sink_buffer);
}
if (frame->src_buffer) {
gst_buffer_unref (frame->src_buffer);
}
g_list_foreach (frame->events, (GFunc) gst_event_unref, NULL);
g_list_free (frame->events);
if (frame->coder_hook_destroy_notify && frame->coder_hook)
frame->coder_hook_destroy_notify (frame->coder_hook);
g_slice_free (GstVideoFrame, frame);
}
/**
* gst_video_frame_ref:
* @frame: a #GstVideoFrame
*
* Increases the refcount of the given frame by one.
*
* Returns: @buf
*/
GstVideoFrame *
gst_video_frame_ref (GstVideoFrame * frame)
{
g_return_val_if_fail (frame != NULL, NULL);
g_atomic_int_inc (&frame->ref_count);
return frame;
}
/**
* gst_video_frame_unref:
* @frame: a #GstVideoFrame
*
* Decreases the refcount of the frame. If the refcount reaches 0, the frame
* will be freed.
*/
void
gst_video_frame_unref (GstVideoFrame * 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_free (frame);
}
}
|