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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/* GStreamer H.263 Parser
* Copyright (C) <2010> Arun Raghavan <arun.raghavan@collabora.co.uk>
* Copyright (C) <2010> Edward Hervey <edward.hervey@collabora.co.uk>
* Copyright (C) <2010> Collabora Multimedia
* Copyright (C) <2010> Nokia Corporation
*
* Some bits C-c,C-v'ed and s/4/3 from h264parse:
* (C) 2005 Michal Benes <michal.benes@itonis.tv>
* (C) 2008 Wim Taymans <wim.taymans@gmail.com>
* (C) 2009 Mark Nauwelaerts <mnauw users sf net>
*
* 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
#include <gst/base/gstbytereader.h>
#include "gsth263parse.h"
GST_DEBUG_CATEGORY (h263_parse_debug);
#define GST_CAT_DEFAULT h263_parse_debug
static GstStaticPadTemplate srctemplate =
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/x-h263, variant = (string) itu, "
"parsed = (boolean) true")
);
static GstStaticPadTemplate sinktemplate =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/x-h263, variant = (string) itu, "
"parsed = (boolean) false")
);
GST_BOILERPLATE (GstH263Parse, gst_h263_parse, GstElement, GST_TYPE_BASE_PARSE);
static gboolean gst_h263_parse_start (GstBaseParse * parse);
static gboolean gst_h263_parse_stop (GstBaseParse * parse);
static gboolean gst_h263_parse_sink_event (GstBaseParse * parse,
GstEvent * event);
static gboolean gst_h263_parse_check_valid_frame (GstBaseParse * parse,
GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
static GstFlowReturn gst_h263_parse_parse_frame (GstBaseParse * parse,
GstBaseParseFrame * frame);
static void
gst_h263_parse_base_init (gpointer g_class)
{
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&srctemplate));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&sinktemplate));
gst_element_class_set_details_simple (gstelement_class, "H.263 parser",
"Codec/Parser/Video",
"Parses H.263 streams",
"Arun Raghavan <arun.raghavan@collabora.co.uk>,"
"Edward Hervey <edward.hervey@collabora.co.uk>");
GST_DEBUG_CATEGORY_INIT (h263_parse_debug, "h263parse", 0, "h263 parser");
}
static void
gst_h263_parse_class_init (GstH263ParseClass * klass)
{
GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
/* Override BaseParse vfuncs */
parse_class->start = GST_DEBUG_FUNCPTR (gst_h263_parse_start);
parse_class->stop = GST_DEBUG_FUNCPTR (gst_h263_parse_stop);
parse_class->event = GST_DEBUG_FUNCPTR (gst_h263_parse_sink_event);
parse_class->check_valid_frame =
GST_DEBUG_FUNCPTR (gst_h263_parse_check_valid_frame);
parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_h263_parse_parse_frame);
}
static void
gst_h263_parse_init (GstH263Parse * h263parse, GstH263ParseClass * g_class)
{
}
static gboolean
gst_h263_parse_start (GstBaseParse * parse)
{
GstH263Parse *h263parse = GST_H263_PARSE (parse);
GST_DEBUG ("Start");
h263parse->bitrate = 0;
h263parse->profile = -1;
h263parse->level = -1;
h263parse->state = PARSING;
gst_base_parse_set_min_frame_size (parse, 4);
return TRUE;
}
static gboolean
gst_h263_parse_stop (GstBaseParse * parse)
{
GST_DEBUG ("Stop");
return TRUE;
}
static gboolean
gst_h263_parse_sink_event (GstBaseParse * parse, GstEvent * event)
{
GstH263Parse *h263parse;
gboolean res = FALSE;
h263parse = GST_H263_PARSE (parse);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_TAG:
{
GstTagList *taglist;
gst_event_parse_tag (event, &taglist);
if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &h263parse->bitrate))
GST_DEBUG ("Got bitrate tag: %u", h263parse->bitrate);
break;
}
default:
break;
}
return res;
}
static guint
find_psc (GstBuffer * buffer, guint skip)
{
GstByteReader br;
guint psc_pos = -1, psc;
gst_byte_reader_init_from_buffer (&br, buffer);
if (!gst_byte_reader_set_pos (&br, skip))
goto out;
gst_byte_reader_peek_uint24_be (&br, &psc);
/* Scan for the picture start code (22 bits - 0x0020) */
while ((gst_byte_reader_get_remaining (&br) >= 3)) {
if (gst_byte_reader_peek_uint24_be (&br, &psc) &&
((psc & 0xffffc0) == 0x000080)) {
psc_pos = gst_byte_reader_get_pos (&br);
break;
} else
gst_byte_reader_skip (&br, 1);
}
out:
return psc_pos;
}
static void
gst_h263_parse_set_src_caps (GstH263Parse * h263parse,
const H263Params * params)
{
GstStructure *st;
GstCaps *caps, *sink_caps;
gint fr_num, fr_denom;
g_assert (h263parse->state == PASSTHROUGH || h263parse->state == GOT_HEADER);
caps = GST_PAD_CAPS (GST_BASE_PARSE_SINK_PAD (h263parse));
if (caps) {
caps = gst_caps_copy (caps);
} else {
caps = gst_caps_new_simple ("video/x-h263",
"variant", G_TYPE_STRING, "itu", NULL);
}
gst_caps_set_simple (caps, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
sink_caps = GST_PAD_CAPS (GST_BASE_PARSE_SINK_PAD (h263parse));
if (sink_caps && (st = gst_caps_get_structure (sink_caps, 0)) &&
gst_structure_get_fraction (st, "framerate", &fr_num, &fr_denom)) {
/* Got it in caps - nothing more to do */
GST_DEBUG ("Sink caps override framerate from headers");
} else {
/* Caps didn't have the framerate - get it from params */
gst_h263_parse_get_framerate (params, &fr_num, &fr_denom);
}
gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fr_num, fr_denom,
NULL);
if (params->width && params->height)
gst_caps_set_simple (caps, "width", G_TYPE_INT, params->width,
"height", G_TYPE_INT, params->height, NULL);
if (h263parse->state == GOT_HEADER) {
gst_caps_set_simple (caps,
"annex-d", G_TYPE_BOOLEAN, (params->features & H263_OPTION_UMV_MODE),
"annex-e", G_TYPE_BOOLEAN, (params->features & H263_OPTION_SAC_MODE),
"annex-f", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AP_MODE),
"annex-g", G_TYPE_BOOLEAN, (params->features & H263_OPTION_PB_MODE),
"annex-i", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AIC_MODE),
"annex-j", G_TYPE_BOOLEAN, (params->features & H263_OPTION_DF_MODE),
"annex-k", G_TYPE_BOOLEAN, (params->features & H263_OPTION_SS_MODE),
"annex-m", G_TYPE_BOOLEAN, (params->type == PICTURE_IMPROVED_PB),
"annex-n", G_TYPE_BOOLEAN, (params->features & H263_OPTION_RPS_MODE),
"annex-q", G_TYPE_BOOLEAN, (params->features & H263_OPTION_RRU_MODE),
"annex-r", G_TYPE_BOOLEAN, (params->features & H263_OPTION_ISD_MODE),
"annex-s", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AIV_MODE),
"annex-t", G_TYPE_BOOLEAN, (params->features & H263_OPTION_MQ_MODE),
"annex-u", G_TYPE_BOOLEAN, (params->features & H263_OPTION_ERPS_MODE),
"annex-v", G_TYPE_BOOLEAN, (params->features & H263_OPTION_DPS_MODE),
NULL);
h263parse->profile = gst_h263_parse_get_profile (params);
if (h263parse->profile != -1)
gst_caps_set_simple (caps, "profile", G_TYPE_UINT, h263parse->profile,
NULL);
h263parse->level = gst_h263_parse_get_level (params, h263parse->profile,
h263parse->bitrate, fr_num, fr_denom);
if (h263parse->level != -1)
gst_caps_set_simple (caps, "level", G_TYPE_UINT, h263parse->level, NULL);
}
gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (h263parse)), caps);
gst_caps_unref (caps);
}
static gboolean
gst_h263_parse_check_valid_frame (GstBaseParse * parse,
GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
{
GstH263Parse *h263parse;
GstBuffer *buffer;
guint psc_pos, next_psc_pos;
h263parse = GST_H263_PARSE (parse);
buffer = frame->buffer;
if (GST_BUFFER_SIZE (buffer) < 3)
return FALSE;
psc_pos = find_psc (buffer, 0);
if (psc_pos == -1) {
/* PSC not found, need more data */
if (GST_BUFFER_SIZE (buffer) > 3)
psc_pos = GST_BUFFER_SIZE (buffer) - 3;
else
psc_pos = 0;
goto more;
}
/* Found the start of the frame, now try to find the end */
next_psc_pos = psc_pos + 3;
next_psc_pos = find_psc (buffer, next_psc_pos);
if (next_psc_pos == -1) {
if (GST_BASE_PARSE_DRAINING (parse))
/* FLUSH/EOS, it's okay if we can't find the next frame */
next_psc_pos = GST_BUFFER_SIZE (buffer);
else
goto more;
}
/* We should now have a complete frame */
/* If this is the first frame, parse and set srcpad caps */
if (h263parse->state == PARSING) {
H263Params params = { 0, };
GstFlowReturn res;
res = gst_h263_parse_get_params (¶ms, buffer, FALSE, &h263parse->state);
if (res != GST_FLOW_OK || h263parse->state != GOT_HEADER) {
GST_WARNING ("Couldn't parse header - setting passthrough mode");
gst_base_parse_set_passthrough (parse, TRUE);
} else {
/* Set srcpad caps since we now have sufficient information to do so */
gst_h263_parse_set_src_caps (h263parse, ¶ms);
gst_base_parse_set_passthrough (parse, FALSE);
}
}
*skipsize = psc_pos;
*framesize = next_psc_pos - psc_pos;
/* XXX: After getting a keyframe, should we adjust min_frame_size to
* something smaller so we don't end up collecting too many non-keyframes? */
GST_DEBUG ("Found a frame of size %d at pos %d", *framesize, *skipsize);
return TRUE;
more:
/* ask for best next available */
*framesize = G_MAXUINT;
*skipsize = psc_pos;
return FALSE;
}
static GstFlowReturn
gst_h263_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
{
GstH263Parse *h263parse;
GstBuffer *buffer;
GstFlowReturn res;
H263Params params = { 0, };
h263parse = GST_H263_PARSE (parse);
buffer = frame->buffer;
res = gst_h263_parse_get_params (¶ms, buffer, TRUE, &h263parse->state);
if (res != GST_FLOW_OK)
goto out;
if (h263parse->state == PASSTHROUGH || h263parse->state == PARSING) {
/* There's a feature we don't support, or we didn't have enough data to
* parse the header, which should not be possible. Either way, go into
* passthrough mode and let downstream handle it if it can. */
GST_WARNING ("Couldn't parse header - setting passthrough mode");
gst_base_parse_set_passthrough (parse, TRUE);
goto out;
}
/* h263parse->state is now GOT_HEADER */
gst_buffer_set_caps (buffer,
GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (h263parse))));
if (gst_h263_parse_is_delta_unit (¶ms))
GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
else
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
out:
return res;
}
|