1 /* GStreamer H.263 Parser
2 * Copyright (C) <2010> Arun Raghavan <arun.raghavan@collabora.co.uk>
3 * Copyright (C) <2010> Edward Hervey <edward.hervey@collabora.co.uk>
4 * Copyright (C) <2010> Collabora Multimedia
5 * Copyright (C) <2010> Nokia Corporation
7 * Some bits C-c,C-v'ed and s/4/3 from h264parse:
8 * (C) 2005 Michal Benes <michal.benes@itonis.tv>
9 * (C) 2008 Wim Taymans <wim.taymans@gmail.com>
10 * (C) 2009 Mark Nauwelaerts <mnauw users sf net>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
32 #include <gst/base/gstbytereader.h>
33 #include "gsth263parse.h"
35 GST_DEBUG_CATEGORY (h263_parse_debug);
36 #define GST_CAT_DEFAULT h263_parse_debug
38 static GstStaticPadTemplate srctemplate =
39 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
41 GST_STATIC_CAPS ("video/x-h263, variant = (string) itu, "
42 "parsed = (boolean) true, framerate=(fraction)[0/1,MAX]")
45 static GstStaticPadTemplate sinktemplate =
46 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
48 GST_STATIC_CAPS ("video/x-h263, variant = (string) itu")
51 GST_BOILERPLATE (GstH263Parse, gst_h263_parse, GstElement, GST_TYPE_BASE_PARSE);
53 static gboolean gst_h263_parse_start (GstBaseParse * parse);
54 static gboolean gst_h263_parse_stop (GstBaseParse * parse);
55 static gboolean gst_h263_parse_sink_event (GstBaseParse * parse,
57 static gboolean gst_h263_parse_check_valid_frame (GstBaseParse * parse,
58 GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
59 static GstFlowReturn gst_h263_parse_parse_frame (GstBaseParse * parse,
60 GstBaseParseFrame * frame);
61 static GstCaps *gst_h263_parse_get_sink_caps (GstBaseParse * parse);
64 gst_h263_parse_base_init (gpointer g_class)
66 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
68 gst_element_class_add_static_pad_template (gstelement_class,
70 gst_element_class_add_static_pad_template (gstelement_class,
72 gst_element_class_set_details_simple (gstelement_class, "H.263 parser",
74 "Parses H.263 streams",
75 "Arun Raghavan <arun.raghavan@collabora.co.uk>,"
76 "Edward Hervey <edward.hervey@collabora.co.uk>");
78 GST_DEBUG_CATEGORY_INIT (h263_parse_debug, "h263parse", 0, "h263 parser");
82 gst_h263_parse_class_init (GstH263ParseClass * klass)
84 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
86 /* Override BaseParse vfuncs */
87 parse_class->start = GST_DEBUG_FUNCPTR (gst_h263_parse_start);
88 parse_class->stop = GST_DEBUG_FUNCPTR (gst_h263_parse_stop);
89 parse_class->event = GST_DEBUG_FUNCPTR (gst_h263_parse_sink_event);
90 parse_class->check_valid_frame =
91 GST_DEBUG_FUNCPTR (gst_h263_parse_check_valid_frame);
92 parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_h263_parse_parse_frame);
93 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_h263_parse_get_sink_caps);
97 gst_h263_parse_init (GstH263Parse * h263parse, GstH263ParseClass * g_class)
102 gst_h263_parse_start (GstBaseParse * parse)
104 GstH263Parse *h263parse = GST_H263_PARSE (parse);
106 GST_DEBUG_OBJECT (h263parse, "start");
108 h263parse->bitrate = 0;
109 h263parse->profile = -1;
110 h263parse->level = -1;
112 h263parse->state = PARSING;
114 gst_base_parse_set_min_frame_size (parse, 4);
120 gst_h263_parse_stop (GstBaseParse * parse)
122 GST_DEBUG_OBJECT (parse, "stop");
128 gst_h263_parse_sink_event (GstBaseParse * parse, GstEvent * event)
130 GstH263Parse *h263parse;
131 gboolean res = FALSE;
133 h263parse = GST_H263_PARSE (parse);
135 switch (GST_EVENT_TYPE (event)) {
140 gst_event_parse_tag (event, &taglist);
142 if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &h263parse->bitrate))
143 GST_DEBUG_OBJECT (h263parse, "got bitrate tag: %u", h263parse->bitrate);
156 find_psc (GstBuffer * buffer, guint skip)
159 guint psc_pos = -1, psc;
161 gst_byte_reader_init_from_buffer (&br, buffer);
163 if (!gst_byte_reader_set_pos (&br, skip))
166 gst_byte_reader_peek_uint24_be (&br, &psc);
168 /* Scan for the picture start code (22 bits - 0x0020) */
169 while ((gst_byte_reader_get_remaining (&br) >= 3)) {
170 if (gst_byte_reader_peek_uint24_be (&br, &psc) &&
171 ((psc & 0xffffc0) == 0x000080)) {
172 psc_pos = gst_byte_reader_get_pos (&br);
175 gst_byte_reader_skip (&br, 1);
183 gst_h263_parse_set_src_caps (GstH263Parse * h263parse,
184 const H263Params * params)
187 GstCaps *caps, *sink_caps;
188 gint fr_num, fr_denom;
190 g_assert (h263parse->state == PASSTHROUGH || h263parse->state == GOT_HEADER);
192 caps = GST_PAD_CAPS (GST_BASE_PARSE_SINK_PAD (h263parse));
194 caps = gst_caps_copy (caps);
196 caps = gst_caps_new_simple ("video/x-h263",
197 "variant", G_TYPE_STRING, "itu", NULL);
199 gst_caps_set_simple (caps, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
201 sink_caps = GST_PAD_CAPS (GST_BASE_PARSE_SINK_PAD (h263parse));
202 if (sink_caps && (st = gst_caps_get_structure (sink_caps, 0)) &&
203 gst_structure_get_fraction (st, "framerate", &fr_num, &fr_denom)) {
204 /* Got it in caps - nothing more to do */
205 GST_DEBUG_OBJECT (h263parse, "sink caps override framerate from headers");
207 /* Caps didn't have the framerate - get it from params */
208 gst_h263_parse_get_framerate (params, &fr_num, &fr_denom);
210 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fr_num, fr_denom,
213 if (params->width && params->height)
214 gst_caps_set_simple (caps, "width", G_TYPE_INT, params->width,
215 "height", G_TYPE_INT, params->height, NULL);
217 if (h263parse->state == GOT_HEADER) {
218 gst_caps_set_simple (caps,
219 "annex-d", G_TYPE_BOOLEAN, (params->features & H263_OPTION_UMV_MODE),
220 "annex-e", G_TYPE_BOOLEAN, (params->features & H263_OPTION_SAC_MODE),
221 "annex-f", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AP_MODE),
222 "annex-g", G_TYPE_BOOLEAN, (params->features & H263_OPTION_PB_MODE),
223 "annex-i", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AIC_MODE),
224 "annex-j", G_TYPE_BOOLEAN, (params->features & H263_OPTION_DF_MODE),
225 "annex-k", G_TYPE_BOOLEAN, (params->features & H263_OPTION_SS_MODE),
226 "annex-m", G_TYPE_BOOLEAN, (params->type == PICTURE_IMPROVED_PB),
227 "annex-n", G_TYPE_BOOLEAN, (params->features & H263_OPTION_RPS_MODE),
228 "annex-q", G_TYPE_BOOLEAN, (params->features & H263_OPTION_RRU_MODE),
229 "annex-r", G_TYPE_BOOLEAN, (params->features & H263_OPTION_ISD_MODE),
230 "annex-s", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AIV_MODE),
231 "annex-t", G_TYPE_BOOLEAN, (params->features & H263_OPTION_MQ_MODE),
232 "annex-u", G_TYPE_BOOLEAN, (params->features & H263_OPTION_ERPS_MODE),
233 "annex-v", G_TYPE_BOOLEAN, (params->features & H263_OPTION_DPS_MODE),
236 h263parse->profile = gst_h263_parse_get_profile (params);
237 if (h263parse->profile != -1)
238 gst_caps_set_simple (caps, "profile", G_TYPE_UINT, h263parse->profile,
241 h263parse->level = gst_h263_parse_get_level (params, h263parse->profile,
242 h263parse->bitrate, fr_num, fr_denom);
243 if (h263parse->level != -1)
244 gst_caps_set_simple (caps, "level", G_TYPE_UINT, h263parse->level, NULL);
247 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (h263parse)), caps);
248 gst_caps_unref (caps);
252 gst_h263_parse_check_valid_frame (GstBaseParse * parse,
253 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
255 GstH263Parse *h263parse;
257 guint psc_pos, next_psc_pos;
259 h263parse = GST_H263_PARSE (parse);
260 buffer = frame->buffer;
262 if (GST_BUFFER_SIZE (buffer) < 3)
265 psc_pos = find_psc (buffer, 0);
268 /* PSC not found, need more data */
269 if (GST_BUFFER_SIZE (buffer) > 3)
270 psc_pos = GST_BUFFER_SIZE (buffer) - 3;
276 /* Found the start of the frame, now try to find the end */
277 next_psc_pos = psc_pos + 3;
278 next_psc_pos = find_psc (buffer, next_psc_pos);
280 if (next_psc_pos == -1) {
281 if (GST_BASE_PARSE_DRAINING (parse))
282 /* FLUSH/EOS, it's okay if we can't find the next frame */
283 next_psc_pos = GST_BUFFER_SIZE (buffer);
288 /* We should now have a complete frame */
290 /* If this is the first frame, parse and set srcpad caps */
291 if (h263parse->state == PARSING) {
292 H263Params params = { 0, };
295 res = gst_h263_parse_get_params (¶ms, buffer, FALSE, &h263parse->state);
296 if (res != GST_FLOW_OK || h263parse->state != GOT_HEADER) {
297 GST_WARNING ("Couldn't parse header - setting passthrough mode");
298 gst_base_parse_set_passthrough (parse, TRUE);
300 /* Set srcpad caps since we now have sufficient information to do so */
301 gst_h263_parse_set_src_caps (h263parse, ¶ms);
302 gst_base_parse_set_passthrough (parse, FALSE);
307 *framesize = next_psc_pos - psc_pos;
309 /* XXX: After getting a keyframe, should we adjust min_frame_size to
310 * something smaller so we don't end up collecting too many non-keyframes? */
312 GST_DEBUG_OBJECT (h263parse, "found a frame of size %d at pos %d",
313 *framesize, *skipsize);
318 /* ask for best next available */
319 *framesize = G_MAXUINT;
327 gst_h263_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
329 GstH263Parse *h263parse;
332 H263Params params = { 0, };
334 h263parse = GST_H263_PARSE (parse);
335 buffer = frame->buffer;
337 res = gst_h263_parse_get_params (¶ms, buffer, TRUE, &h263parse->state);
338 if (res != GST_FLOW_OK)
341 if (h263parse->state == PASSTHROUGH || h263parse->state == PARSING) {
342 /* There's a feature we don't support, or we didn't have enough data to
343 * parse the header, which should not be possible. Either way, go into
344 * passthrough mode and let downstream handle it if it can. */
345 GST_WARNING ("Couldn't parse header - setting passthrough mode");
346 gst_base_parse_set_passthrough (parse, TRUE);
350 /* h263parse->state is now GOT_HEADER */
352 #ifdef GST_H263PARSE_MODIFICATION /* do not set caps in every frame */
353 if (buffer != NULL && GST_BUFFER_CAPS (buffer) == NULL) {
354 GST_WARNING_OBJECT (h263parse, "buffer caps is null. gst_buffer_set_caps from src_caps");
355 gst_buffer_set_caps (buffer,
356 GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (h263parse))));
359 gst_buffer_set_caps (buffer,
360 GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (h263parse))));
363 if (gst_h263_parse_is_delta_unit (¶ms))
364 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
366 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
374 gst_h263_parse_get_sink_caps (GstBaseParse * parse)
379 peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
383 /* Remove parsed field */
384 peercaps = gst_caps_make_writable (peercaps);
385 n = gst_caps_get_size (peercaps);
386 for (i = 0; i < n; i++) {
387 GstStructure *s = gst_caps_get_structure (peercaps, i);
388 gst_structure_remove_field (s, "parsed");
392 gst_caps_intersect_full (peercaps,
393 gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse)),
394 GST_CAPS_INTERSECT_FIRST);
395 gst_caps_unref (peercaps);
398 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD