2 * Copyright (C) <2008> Mindfruit B.V.
3 * @author Sjoerd Simons <sjoerd@luon.net>
4 * Copyright (C) <2007> Julien Moutte <julien@fluendo.com>
5 * Copyright (C) <2011> Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
6 * Copyright (C) <2011> Nokia Corporation
7 * Copyright (C) <2011> Intel
8 * Copyright (C) <2011> Collabora Ltd.
9 * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
32 #include <gst/base/gstbytereader.h>
33 #include <gst/pbutils/codec-utils.h>
34 #include <gst/video/video.h>
36 #include "gstmpeg4videoparse.h"
38 GST_DEBUG_CATEGORY (mpeg4v_parse_debug);
39 #define GST_CAT_DEFAULT mpeg4v_parse_debug
41 static GstStaticPadTemplate src_template =
42 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
44 GST_STATIC_CAPS ("video/mpeg, "
45 "mpegversion = (int) 4, "
46 "width = (int)[ 0, max ], "
47 "height = (int)[ 0, max ], "
48 "framerate = (fraction)[ 0, max ] ,"
49 "parsed = (boolean) true, " "systemstream = (boolean) false; "
50 "video/x-divx, " "divxversion = (int) [ 4, 5 ]")
53 static GstStaticPadTemplate sink_template =
54 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
56 GST_STATIC_CAPS ("video/mpeg, "
57 "mpegversion = (int) 4, " "systemstream = (boolean) false; "
58 "video/x-divx, " "divxversion = (int) [ 4, 5 ]")
62 #define DEFAULT_PROP_DROP TRUE
63 #define DEFAULT_CONFIG_INTERVAL (0)
73 #define gst_mpeg4vparse_parent_class parent_class
74 G_DEFINE_TYPE (GstMpeg4VParse, gst_mpeg4vparse, GST_TYPE_BASE_PARSE);
76 static gboolean gst_mpeg4vparse_start (GstBaseParse * parse);
77 static gboolean gst_mpeg4vparse_stop (GstBaseParse * parse);
78 static GstFlowReturn gst_mpeg4vparse_handle_frame (GstBaseParse * parse,
79 GstBaseParseFrame * frame, gint * skipsize);
80 static GstFlowReturn gst_mpeg4vparse_parse_frame (GstBaseParse * parse,
81 GstBaseParseFrame * frame);
82 static GstFlowReturn gst_mpeg4vparse_pre_push_frame (GstBaseParse * parse,
83 GstBaseParseFrame * frame);
84 static gboolean gst_mpeg4vparse_set_caps (GstBaseParse * parse, GstCaps * caps);
85 static GstCaps *gst_mpeg4vparse_get_caps (GstBaseParse * parse,
88 static void gst_mpeg4vparse_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec);
90 static void gst_mpeg4vparse_get_property (GObject * object, guint prop_id,
91 GValue * value, GParamSpec * pspec);
92 static gboolean gst_mpeg4vparse_event (GstBaseParse * parse, GstEvent * event);
93 static gboolean gst_mpeg4vparse_src_event (GstBaseParse * parse,
97 gst_mpeg4vparse_set_property (GObject * object, guint property_id,
98 const GValue * value, GParamSpec * pspec)
100 GstMpeg4VParse *parse = GST_MPEG4VIDEO_PARSE (object);
102 switch (property_id) {
104 parse->drop = g_value_get_boolean (value);
106 case PROP_CONFIG_INTERVAL:
107 parse->interval = g_value_get_uint (value);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
115 gst_mpeg4vparse_get_property (GObject * object, guint property_id,
116 GValue * value, GParamSpec * pspec)
118 GstMpeg4VParse *parse = GST_MPEG4VIDEO_PARSE (object);
120 switch (property_id) {
122 g_value_set_boolean (value, parse->drop);
124 case PROP_CONFIG_INTERVAL:
125 g_value_set_uint (value, parse->interval);
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
133 gst_mpeg4vparse_class_init (GstMpeg4VParseClass * klass)
135 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
136 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
137 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
139 parent_class = g_type_class_peek_parent (klass);
141 gobject_class->set_property = gst_mpeg4vparse_set_property;
142 gobject_class->get_property = gst_mpeg4vparse_get_property;
144 g_object_class_install_property (gobject_class, PROP_DROP,
145 g_param_spec_boolean ("drop", "drop",
146 "Drop data untill valid configuration data is received either "
147 "in the stream or through caps", DEFAULT_PROP_DROP,
148 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
150 g_object_class_install_property (gobject_class, PROP_CONFIG_INTERVAL,
151 g_param_spec_uint ("config-interval",
152 "Configuration Send Interval",
153 "Send Configuration Insertion Interval in seconds (configuration headers "
154 "will be multiplexed in the data stream when detected.) (0 = disabled)",
155 0, 3600, DEFAULT_CONFIG_INTERVAL,
156 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 gst_element_class_add_pad_template (element_class,
159 gst_static_pad_template_get (&src_template));
160 gst_element_class_add_pad_template (element_class,
161 gst_static_pad_template_get (&sink_template));
163 gst_element_class_set_static_metadata (element_class,
164 "MPEG 4 video elementary stream parser", "Codec/Parser/Video",
165 "Parses MPEG-4 Part 2 elementary video streams",
166 "Julien Moutte <julien@fluendo.com>");
168 GST_DEBUG_CATEGORY_INIT (mpeg4v_parse_debug, "mpeg4videoparse", 0,
169 "MPEG-4 video parser");
171 /* Override BaseParse vfuncs */
172 parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_start);
173 parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_stop);
174 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_handle_frame);
175 parse_class->pre_push_frame =
176 GST_DEBUG_FUNCPTR (gst_mpeg4vparse_pre_push_frame);
177 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_set_caps);
178 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_get_caps);
179 parse_class->sink_event = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_event);
180 parse_class->src_event = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_src_event);
184 gst_mpeg4vparse_init (GstMpeg4VParse * parse)
186 parse->interval = DEFAULT_CONFIG_INTERVAL;
187 parse->last_report = GST_CLOCK_TIME_NONE;
189 gst_base_parse_set_pts_interpolation (GST_BASE_PARSE (parse), FALSE);
193 gst_mpeg4vparse_reset_frame (GstMpeg4VParse * mp4vparse)
195 /* done parsing; reset state */
196 mp4vparse->last_sc = -1;
197 mp4vparse->vop_offset = -1;
198 mp4vparse->vo_found = FALSE;
199 mp4vparse->config_found = FALSE;
200 mp4vparse->vol_offset = -1;
201 mp4vparse->vo_offset = -1;
205 gst_mpeg4vparse_reset (GstMpeg4VParse * mp4vparse)
207 gst_mpeg4vparse_reset_frame (mp4vparse);
208 mp4vparse->update_caps = TRUE;
209 mp4vparse->profile = NULL;
210 mp4vparse->level = NULL;
211 mp4vparse->pending_key_unit_ts = GST_CLOCK_TIME_NONE;
212 mp4vparse->force_key_unit_event = NULL;
214 gst_buffer_replace (&mp4vparse->config, NULL);
215 memset (&mp4vparse->vol, 0, sizeof (mp4vparse->vol));
219 gst_mpeg4vparse_start (GstBaseParse * parse)
221 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
223 GST_DEBUG_OBJECT (parse, "start");
225 gst_mpeg4vparse_reset (mp4vparse);
226 /* at least this much for a valid frame */
227 gst_base_parse_set_min_frame_size (parse, 6);
233 gst_mpeg4vparse_stop (GstBaseParse * parse)
235 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
237 GST_DEBUG_OBJECT (parse, "stop");
239 gst_mpeg4vparse_reset (mp4vparse);
245 gst_mpeg4vparse_process_config (GstMpeg4VParse * mp4vparse,
246 const guint8 * data, guint offset, gsize size)
248 GstMpeg4VisualObject *vo;
250 /* only do stuff if something new */
251 if (mp4vparse->config
252 && !gst_buffer_memcmp (mp4vparse->config, offset, data, size))
255 if (mp4vparse->vol_offset < 0) {
256 GST_WARNING ("No video object Layer parsed in this frame, cannot accept "
261 vo = mp4vparse->vo_found ? &mp4vparse->vo : NULL;
263 /* If the parsing fail, we accept the config only if we don't have
265 if (gst_mpeg4_parse_video_object_layer (&mp4vparse->vol,
266 vo, data + mp4vparse->vol_offset,
267 size - mp4vparse->vol_offset) != GST_MPEG4_PARSER_OK &&
271 GST_LOG_OBJECT (mp4vparse, "Width/Height: %u/%u, "
272 "time increment resolution: %u fixed time increment: %u",
273 mp4vparse->vol.width, mp4vparse->vol.height,
274 mp4vparse->vol.vop_time_increment_resolution,
275 mp4vparse->vol.fixed_vop_time_increment);
278 GST_LOG_OBJECT (mp4vparse, "accepting parsed config size %" G_GSSIZE_FORMAT,
281 if (mp4vparse->config != NULL)
282 gst_buffer_unref (mp4vparse->config);
284 mp4vparse->config = gst_buffer_new_wrapped (g_memdup (data, size), size);
286 /* trigger src caps update */
287 mp4vparse->update_caps = TRUE;
293 gst_mpeg4vparse_get_vop_coded (GstMpeg4VParse * mp4vparse, const guint8 * data,
294 gint vop_offset, gsize size, gsize frame_size)
296 if (frame_size > 9) { /* assuming bigger frame will always have vop_coded (saves some parsing) */
298 } else if (size > vop_offset + 3) {
302 gst_bit_reader_init (&reader, data + vop_offset + 1, size - vop_offset);
303 gst_bit_reader_skip (&reader, 2); /* VOP_coding_type */
305 /* modulo_time_base (ends with 0) */
306 while (gst_bit_reader_get_bits_uint8 (&reader, &value, 1) && value);
309 g_return_val_if_fail (gst_bit_reader_get_bits_uint8 (&reader, &value, 1)
312 /* VOP_time_increment */
313 gst_bit_reader_skip (&reader, mp4vparse->vol.vop_time_increment_bits);
316 g_return_val_if_fail (gst_bit_reader_get_bits_uint8 (&reader, &value, 1)
320 if (!gst_bit_reader_get_bits_uint8 (&reader, &value, 1)) {
330 /* caller guarantees at least start code in @buf at @off */
332 gst_mpeg4vparse_process_sc (GstMpeg4VParse * mp4vparse, GstMpeg4Packet * packet,
336 GST_LOG_OBJECT (mp4vparse, "process startcode %x", packet->type);
338 /* if we found a VOP, next start code ends it,
339 * except for final VOS end sequence code included in last VOP-frame */
340 if (mp4vparse->vop_offset >= 0 &&
341 packet->type != GST_MPEG4_VISUAL_OBJ_SEQ_END) {
342 if (G_LIKELY (size > mp4vparse->vop_offset + 1)) {
343 mp4vparse->intra_frame =
344 ((packet->data[mp4vparse->vop_offset + 1] >> 6 & 0x3) == 0);
346 GST_WARNING_OBJECT (mp4vparse, "no data following VOP startcode");
347 mp4vparse->intra_frame = FALSE;
349 mp4vparse->vop_coded =
350 gst_mpeg4vparse_get_vop_coded (mp4vparse, packet->data,
351 mp4vparse->vop_offset, size, packet->offset - 3);
352 GST_LOG_OBJECT (mp4vparse,
353 "ending frame of size %d, is intra %d, vop_coded %d",
354 packet->offset - 3, mp4vparse->intra_frame, mp4vparse->vop_coded);
358 if (mp4vparse->vo_offset >= 0) {
359 gst_mpeg4_parse_visual_object (&mp4vparse->vo, NULL,
360 packet->data + mp4vparse->vo_offset,
361 packet->offset - 3 - mp4vparse->vo_offset);
362 mp4vparse->vo_offset = -1;
363 mp4vparse->vo_found = TRUE;
366 switch (packet->type) {
367 case GST_MPEG4_VIDEO_OBJ_PLANE:
368 case GST_MPEG4_GROUP_OF_VOP:
369 case GST_MPEG4_USER_DATA:
371 if (packet->type == GST_MPEG4_VIDEO_OBJ_PLANE) {
372 GST_LOG_OBJECT (mp4vparse, "startcode is VOP");
373 mp4vparse->vop_offset = packet->offset;
374 } else if (packet->type == GST_MPEG4_GROUP_OF_VOP) {
375 GST_LOG_OBJECT (mp4vparse, "startcode is GOP");
377 GST_LOG_OBJECT (mp4vparse, "startcode is User Data");
379 /* parse config data ending here if proper startcodes found earlier;
380 * we should have received a visual object before. */
381 if (mp4vparse->config_found) {
382 /*Do not take care startcode into account */
383 gst_mpeg4vparse_process_config (mp4vparse,
384 packet->data, packet->offset, packet->offset - 3);
385 mp4vparse->vo_found = FALSE;
389 case GST_MPEG4_VISUAL_OBJ_SEQ_START:
390 GST_LOG_OBJECT (mp4vparse, "Visual Sequence Start");
391 mp4vparse->config_found = TRUE;
392 mp4vparse->profile = gst_codec_utils_mpeg4video_get_profile (packet->data
393 + packet->offset + 1, packet->offset);
394 mp4vparse->level = gst_codec_utils_mpeg4video_get_level (packet->data
395 + packet->offset + 1, packet->offset);
397 case GST_MPEG4_VISUAL_OBJ:
398 GST_LOG_OBJECT (mp4vparse, "Visual Object");
399 mp4vparse->vo_offset = packet->offset;
402 if (packet->type >= GST_MPEG4_VIDEO_LAYER_FIRST &&
403 packet->type <= GST_MPEG4_VIDEO_LAYER_LAST) {
405 GST_LOG_OBJECT (mp4vparse, "Video Object Layer");
407 /* wee keep track of the offset to parse later on */
408 if (mp4vparse->vol_offset < 0)
409 mp4vparse->vol_offset = packet->offset;
411 /* VO (video object) cases */
412 } else if (packet->type <= GST_MPEG4_VIDEO_OBJ_LAST) {
413 GST_LOG_OBJECT (mp4vparse, "Video object");
414 mp4vparse->config_found = TRUE;
419 /* at least need to have a VOP in a frame */
424 gst_mpeg4vparse_handle_frame (GstBaseParse * parse,
425 GstBaseParseFrame * frame, gint * skipsize)
427 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
428 GstMpeg4Packet packet;
433 gboolean ret = FALSE;
436 gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
441 /* at least start code and subsequent byte */
442 if (G_UNLIKELY (size - off < 5)) {
447 /* avoid stale cached parsing state */
448 if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_NEW_FRAME) {
449 GST_LOG_OBJECT (mp4vparse, "parsing new frame");
450 gst_mpeg4vparse_reset_frame (mp4vparse);
452 GST_LOG_OBJECT (mp4vparse, "resuming frame parsing");
455 /* if already found a previous start code, e.g. start of frame, go for next */
456 if (mp4vparse->last_sc >= 0) {
457 off = mp4vparse->last_sc;
461 /* didn't find anything that looks like a sync word, skip */
462 switch (gst_mpeg4_parse (&packet, FALSE, NULL, data, off, size)) {
463 case (GST_MPEG4_PARSER_NO_PACKET):
464 case (GST_MPEG4_PARSER_ERROR):
465 *skipsize = size - 3;
472 /* possible frame header, but not at offset 0? skip bytes before sync */
473 if (G_UNLIKELY (off > 3)) {
478 switch (packet.type) {
479 case GST_MPEG4_GROUP_OF_VOP:
480 case GST_MPEG4_VISUAL_OBJ_SEQ_START:
481 case GST_MPEG4_VIDEO_OBJ_PLANE:
484 if (packet.type <= GST_MPEG4_VIDEO_OBJ_LAST)
487 GST_LOG_OBJECT (mp4vparse, "start code is no VOS, VO, VOP or GOP");
492 mp4vparse->last_sc = 0;
494 /* examine start code, which should not end frame at present */
495 gst_mpeg4vparse_process_sc (mp4vparse, &packet, size);
498 GST_LOG_OBJECT (mp4vparse, "Looking for frame end");
500 /* start is fine as of now */
502 /* position a bit further than last sc */
505 /* so now we have start code at start of data; locate next packet */
506 switch (gst_mpeg4_parse (&packet, FALSE, NULL, data, off, size)) {
507 case (GST_MPEG4_PARSER_NO_PACKET_END):
508 ret = gst_mpeg4vparse_process_sc (mp4vparse, &packet, size);
511 case (GST_MPEG4_PARSER_NO_PACKET):
512 case (GST_MPEG4_PARSER_ERROR):
513 /* if draining, take all */
514 if (GST_BASE_PARSE_DRAINING (parse)) {
518 /* resume scan where we left it */
519 mp4vparse->last_sc = size - 3;
524 /* decide whether this startcode ends a frame */
525 ret = gst_mpeg4vparse_process_sc (mp4vparse, &packet, size);
538 gst_buffer_unmap (frame->buffer, &map);
543 g_assert (framesize <= map.size);
544 res = gst_mpeg4vparse_parse_frame (parse, frame);
545 if (res == GST_BASE_PARSE_FLOW_DROPPED)
546 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_DROP;
547 return gst_base_parse_finish_frame (parse, frame, framesize);
554 gst_mpeg4vparse_update_src_caps (GstMpeg4VParse * mp4vparse)
556 GstCaps *caps = NULL;
557 GstStructure *s = NULL;
559 GST_LOG_OBJECT (mp4vparse, "Updating caps");
561 /* only update if no src caps yet or explicitly triggered */
562 if (G_LIKELY (gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (mp4vparse)) &&
563 !mp4vparse->update_caps))
566 /* carry over input caps as much as possible; override with our own stuff */
567 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (mp4vparse));
569 GstCaps *tmp = gst_caps_copy (caps);
570 gst_caps_unref (caps);
572 s = gst_caps_get_structure (caps, 0);
574 caps = gst_caps_new_simple ("video/mpeg",
575 "mpegversion", G_TYPE_INT, 4,
576 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
579 gst_caps_set_simple (caps, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
581 if (mp4vparse->profile && mp4vparse->level) {
582 gst_caps_set_simple (caps, "profile", G_TYPE_STRING, mp4vparse->profile,
583 "level", G_TYPE_STRING, mp4vparse->level, NULL);
586 if (mp4vparse->config != NULL) {
587 gst_caps_set_simple (caps, "codec_data",
588 GST_TYPE_BUFFER, mp4vparse->config, NULL);
591 if (mp4vparse->vol.width > 0 && mp4vparse->vol.height > 0) {
592 gst_caps_set_simple (caps, "width", G_TYPE_INT, mp4vparse->vol.width,
593 "height", G_TYPE_INT, mp4vparse->vol.height, NULL);
596 /* perhaps we have a framerate */
598 gint fps_num = mp4vparse->vol.vop_time_increment_resolution;
599 gint fps_den = mp4vparse->vol.fixed_vop_time_increment;
600 GstClockTime latency;
602 /* upstream overrides */
603 if (s && gst_structure_has_field (s, "framerate"))
604 gst_structure_get_fraction (s, "framerate", &fps_num, &fps_den);
606 if (fps_den > 0 && fps_num > 0) {
607 gst_caps_set_simple (caps, "framerate",
608 GST_TYPE_FRACTION, fps_num, fps_den, NULL);
609 gst_base_parse_set_frame_rate (GST_BASE_PARSE (mp4vparse),
610 fps_num, fps_den, 0, 0);
611 latency = gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
612 gst_base_parse_set_latency (GST_BASE_PARSE (mp4vparse), latency, latency);
616 /* or pixel-aspect-ratio */
617 if (mp4vparse->vol.par_width > 0 && mp4vparse->vol.par_height > 0 &&
618 (!s || !gst_structure_has_field (s, "pixel-aspect-ratio"))) {
619 gst_caps_set_simple (caps, "pixel-aspect-ratio",
620 GST_TYPE_FRACTION, mp4vparse->vol.par_width,
621 mp4vparse->vol.par_height, NULL);
624 if (mp4vparse->vol.sprite_enable != GST_MPEG4_SPRITE_UNUSED)
625 gst_caps_set_simple (caps, "sprite-warping-points", G_TYPE_INT,
626 mp4vparse->vol.no_of_sprite_warping_points, NULL);
628 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (mp4vparse), caps);
629 gst_caps_unref (caps);
631 mp4vparse->update_caps = FALSE;
635 gst_mpeg4vparse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
637 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
638 GstBuffer *buffer = frame->buffer;
640 gst_mpeg4vparse_update_src_caps (mp4vparse);
642 if (mp4vparse->intra_frame)
643 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
645 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
647 if (!mp4vparse->vop_coded) /* buffer without VOP_coded has no data */
648 GST_BUFFER_DURATION (buffer) = 0;
650 if (G_UNLIKELY (mp4vparse->drop && !mp4vparse->config)) {
651 GST_LOG_OBJECT (mp4vparse, "dropping frame as no config yet");
652 return GST_BASE_PARSE_FLOW_DROPPED;
658 check_pending_key_unit_event (GstEvent * pending_event, GstSegment * segment,
659 GstClockTime timestamp, guint flags, GstClockTime pending_key_unit_ts)
661 GstClockTime running_time, stream_time;
662 gboolean all_headers;
664 GstEvent *event = NULL;
666 g_return_val_if_fail (segment != NULL, NULL);
668 if (pending_event == NULL)
671 if (GST_CLOCK_TIME_IS_VALID (pending_key_unit_ts) &&
672 timestamp == GST_CLOCK_TIME_NONE)
675 running_time = gst_segment_to_running_time (segment,
676 GST_FORMAT_TIME, timestamp);
678 GST_INFO ("now %" GST_TIME_FORMAT " wanted %" GST_TIME_FORMAT,
679 GST_TIME_ARGS (running_time), GST_TIME_ARGS (pending_key_unit_ts));
680 if (GST_CLOCK_TIME_IS_VALID (pending_key_unit_ts) &&
681 running_time < pending_key_unit_ts)
684 if (flags & GST_BUFFER_FLAG_DELTA_UNIT) {
685 GST_DEBUG ("pending force key unit, waiting for keyframe");
689 stream_time = gst_segment_to_stream_time (segment,
690 GST_FORMAT_TIME, timestamp);
692 gst_video_event_parse_upstream_force_key_unit (pending_event,
693 NULL, &all_headers, &count);
696 gst_video_event_new_downstream_force_key_unit (timestamp, stream_time,
697 running_time, all_headers, count);
698 gst_event_set_seqnum (event, gst_event_get_seqnum (pending_event));
705 gst_mpeg4vparse_prepare_key_unit (GstMpeg4VParse * parse, GstEvent * event)
707 GstClockTime running_time;
710 parse->pending_key_unit_ts = GST_CLOCK_TIME_NONE;
711 gst_event_replace (&parse->force_key_unit_event, NULL);
713 gst_video_event_parse_downstream_force_key_unit (event,
714 NULL, NULL, &running_time, NULL, &count);
716 GST_INFO_OBJECT (parse, "pushing downstream force-key-unit event %d "
717 "%" GST_TIME_FORMAT " count %d", gst_event_get_seqnum (event),
718 GST_TIME_ARGS (running_time), count);
719 gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (parse), event);
724 gst_mpeg4vparse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
726 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
727 GstBuffer *buffer = frame->buffer;
728 gboolean push_codec = FALSE;
729 GstEvent *event = NULL;
731 if ((event = check_pending_key_unit_event (mp4vparse->force_key_unit_event,
732 &parse->segment, GST_BUFFER_TIMESTAMP (buffer),
733 GST_BUFFER_FLAGS (buffer), mp4vparse->pending_key_unit_ts))) {
734 gst_mpeg4vparse_prepare_key_unit (mp4vparse, event);
738 /* periodic config sending */
739 if (mp4vparse->interval > 0 || push_codec) {
740 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
744 if (!GST_CLOCK_TIME_IS_VALID (mp4vparse->last_report)) {
745 mp4vparse->last_report = timestamp;
748 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
749 if (timestamp > mp4vparse->last_report)
750 diff = timestamp - mp4vparse->last_report;
754 GST_LOG_OBJECT (mp4vparse,
755 "now %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
756 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (mp4vparse->last_report));
758 GST_LOG_OBJECT (mp4vparse,
759 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
761 if (GST_TIME_AS_SECONDS (diff) >= mp4vparse->interval || push_codec) {
766 /* we need to send config now first */
767 GST_INFO_OBJECT (parse, "inserting config in stream");
768 gst_buffer_map (mp4vparse->config, &cmap, GST_MAP_READ);
769 diffconf = (gst_buffer_get_size (buffer) < cmap.size)
770 || gst_buffer_memcmp (buffer, 0, cmap.data, cmap.size);
772 gst_buffer_unmap (mp4vparse->config, &cmap);
774 /* avoid inserting duplicate config */
780 gst_buffer_append (gst_buffer_ref (mp4vparse->config),
781 gst_buffer_ref (buffer));
782 gst_buffer_copy_into (superbuf, buffer, GST_BUFFER_COPY_METADATA, 0,
784 gst_buffer_replace (&frame->out_buffer, superbuf);
785 gst_buffer_unref (superbuf);
787 GST_INFO_OBJECT (parse, "... but avoiding duplication");
790 if (G_UNLIKELY (timestamp != -1)) {
791 mp4vparse->last_report = timestamp;
801 gst_mpeg4vparse_set_caps (GstBaseParse * parse, GstCaps * caps)
803 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
811 GstMpeg4Packet packet;
812 GstMpeg4ParseResult res;
814 GST_DEBUG_OBJECT (parse, "setcaps called with %" GST_PTR_FORMAT, caps);
816 s = gst_caps_get_structure (caps, 0);
818 if ((value = gst_structure_get_value (s, "codec_data")) != NULL
819 && (buf = gst_value_get_buffer (value))) {
820 /* best possible parse attempt,
821 * src caps are based on sink caps so it will end up in there
822 * whether sucessful or not */
823 gst_buffer_map (buf, &map, GST_MAP_READ);
826 res = gst_mpeg4_parse (&packet, FALSE, NULL, data, 0, size);
828 while (res == GST_MPEG4_PARSER_OK || res == GST_MPEG4_PARSER_NO_PACKET_END) {
830 if (packet.type >= GST_MPEG4_VIDEO_LAYER_FIRST &&
831 packet.type <= GST_MPEG4_VIDEO_LAYER_LAST)
832 mp4vparse->vol_offset = packet.offset;
834 else if (packet.type == GST_MPEG4_VISUAL_OBJ) {
835 gst_mpeg4_parse_visual_object (&mp4vparse->vo, NULL,
836 data + packet.offset, MIN (packet.size, size));
837 mp4vparse->vo_found = TRUE;
840 res = gst_mpeg4_parse (&packet, FALSE, NULL, data, packet.offset, size);
843 /* And take it as config */
844 gst_mpeg4vparse_process_config (mp4vparse, data, 3, size);
845 gst_buffer_unmap (buf, &map);
846 gst_mpeg4vparse_reset_frame (mp4vparse);
849 /* let's not interfere and accept regardless of config parsing success */
855 gst_mpeg4vparse_get_caps (GstBaseParse * parse, GstCaps * filter)
857 GstCaps *peercaps, *templ;
860 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
861 peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
866 /* Remove the parsed field */
867 peercaps = gst_caps_make_writable (peercaps);
868 n = gst_caps_get_size (peercaps);
869 for (i = 0; i < n; i++) {
870 GstStructure *s = gst_caps_get_structure (peercaps, i);
872 gst_structure_remove_field (s, "parsed");
875 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
876 gst_caps_unref (peercaps);
878 /* Append the template caps because we still want to accept
879 * caps without any fields in the case upstream does not
882 gst_caps_append (res, templ);
888 GstCaps *tmp = gst_caps_intersect_full (res, filter,
889 GST_CAPS_INTERSECT_FIRST);
890 gst_caps_unref (res);
899 gst_mpeg4vparse_event (GstBaseParse * parse, GstEvent * event)
902 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
904 switch (GST_EVENT_TYPE (event)) {
905 case GST_EVENT_CUSTOM_DOWNSTREAM:
907 GstClockTime timestamp, stream_time, running_time;
908 gboolean all_headers;
911 if (gst_video_event_is_force_key_unit (event)) {
912 gst_video_event_parse_downstream_force_key_unit (event,
913 ×tamp, &stream_time, &running_time, &all_headers, &count);
915 GST_INFO_OBJECT (mp4vparse, "received downstream force key unit event, "
916 "seqnum %d running_time %" GST_TIME_FORMAT
917 " all_headers %d count %d", gst_event_get_seqnum (event),
918 GST_TIME_ARGS (running_time), all_headers, count);
920 if (mp4vparse->force_key_unit_event) {
921 GST_INFO_OBJECT (mp4vparse, "ignoring force key unit event "
922 "as one is already queued");
924 mp4vparse->pending_key_unit_ts = running_time;
925 gst_event_replace (&mp4vparse->force_key_unit_event, event);
927 gst_event_unref (event);
930 res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse, event);
935 res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse, event);
942 gst_mpeg4vparse_src_event (GstBaseParse * parse, GstEvent * event)
945 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
947 switch (GST_EVENT_TYPE (event)) {
948 case GST_EVENT_CUSTOM_UPSTREAM:
950 GstClockTime running_time;
951 gboolean all_headers;
954 if (gst_video_event_is_force_key_unit (event)) {
955 gst_video_event_parse_upstream_force_key_unit (event,
956 &running_time, &all_headers, &count);
958 GST_INFO_OBJECT (mp4vparse, "received upstream force-key-unit event, "
959 "seqnum %d running_time %" GST_TIME_FORMAT
960 " all_headers %d count %d", gst_event_get_seqnum (event),
961 GST_TIME_ARGS (running_time), all_headers, count);
964 mp4vparse->pending_key_unit_ts = running_time;
965 gst_event_replace (&mp4vparse->force_key_unit_event, event);
968 res = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
972 res = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);