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/base.h>
33 #include <gst/pbutils/pbutils.h>
34 #include <gst/video/video.h>
36 #include "gstvideoparserselements.h"
37 #include "gstmpeg4videoparse.h"
39 GST_DEBUG_CATEGORY (mpeg4v_parse_debug);
40 #define GST_CAT_DEFAULT mpeg4v_parse_debug
42 static GstStaticPadTemplate src_template =
43 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
45 GST_STATIC_CAPS ("video/mpeg, "
46 "mpegversion = (int) 4, "
47 "width = (int)[ 0, max ], "
48 "height = (int)[ 0, max ], "
49 "framerate = (fraction)[ 0, max ] ,"
50 "parsed = (boolean) true, " "systemstream = (boolean) false; "
51 "video/x-divx, " "divxversion = (int) [ 4, 5 ]")
54 static GstStaticPadTemplate sink_template =
55 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
57 GST_STATIC_CAPS ("video/mpeg, "
58 "mpegversion = (int) 4, " "systemstream = (boolean) false; "
59 "video/x-divx, " "divxversion = (int) [ 4, 5 ]")
63 #define DEFAULT_PROP_DROP TRUE
64 #define DEFAULT_CONFIG_INTERVAL (0)
73 #define gst_mpeg4vparse_parent_class parent_class
74 G_DEFINE_TYPE (GstMpeg4VParse, gst_mpeg4vparse, GST_TYPE_BASE_PARSE);
75 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (mpeg4videoparse, "mpeg4videoparse",
76 GST_RANK_PRIMARY + 1, GST_TYPE_MPEG4VIDEO_PARSE,
77 videoparsers_element_init (plugin));
79 static gboolean gst_mpeg4vparse_start (GstBaseParse * parse);
80 static gboolean gst_mpeg4vparse_stop (GstBaseParse * parse);
81 static GstFlowReturn gst_mpeg4vparse_handle_frame (GstBaseParse * parse,
82 GstBaseParseFrame * frame, gint * skipsize);
83 static GstFlowReturn gst_mpeg4vparse_parse_frame (GstBaseParse * parse,
84 GstBaseParseFrame * frame);
85 static GstFlowReturn gst_mpeg4vparse_pre_push_frame (GstBaseParse * parse,
86 GstBaseParseFrame * frame);
87 static gboolean gst_mpeg4vparse_set_caps (GstBaseParse * parse, GstCaps * caps);
88 static GstCaps *gst_mpeg4vparse_get_caps (GstBaseParse * parse,
91 static void gst_mpeg4vparse_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_mpeg4vparse_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
95 static gboolean gst_mpeg4vparse_event (GstBaseParse * parse, GstEvent * event);
96 static gboolean gst_mpeg4vparse_src_event (GstBaseParse * parse,
100 gst_mpeg4vparse_set_property (GObject * object, guint property_id,
101 const GValue * value, GParamSpec * pspec)
103 GstMpeg4VParse *parse = GST_MPEG4VIDEO_PARSE (object);
105 switch (property_id) {
107 parse->drop = g_value_get_boolean (value);
109 case PROP_CONFIG_INTERVAL:
110 parse->interval = g_value_get_int (value);
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
118 gst_mpeg4vparse_get_property (GObject * object, guint property_id,
119 GValue * value, GParamSpec * pspec)
121 GstMpeg4VParse *parse = GST_MPEG4VIDEO_PARSE (object);
123 switch (property_id) {
125 g_value_set_boolean (value, parse->drop);
127 case PROP_CONFIG_INTERVAL:
128 g_value_set_int (value, parse->interval);
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
136 gst_mpeg4vparse_class_init (GstMpeg4VParseClass * klass)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
140 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
142 parent_class = g_type_class_peek_parent (klass);
144 gobject_class->set_property = gst_mpeg4vparse_set_property;
145 gobject_class->get_property = gst_mpeg4vparse_get_property;
147 g_object_class_install_property (gobject_class, PROP_DROP,
148 g_param_spec_boolean ("drop", "drop",
149 "Drop data until valid configuration data is received either "
150 "in the stream or through caps", DEFAULT_PROP_DROP,
151 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153 g_object_class_install_property (gobject_class, PROP_CONFIG_INTERVAL,
154 g_param_spec_int ("config-interval",
155 "Configuration Send Interval",
156 "Send Configuration Insertion Interval in seconds (configuration headers "
157 "will be multiplexed in the data stream when detected.) "
158 "(0 = disabled, -1 = send with every IDR frame)",
159 -1, 3600, DEFAULT_CONFIG_INTERVAL,
160 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 gst_element_class_add_static_pad_template (element_class, &src_template);
163 gst_element_class_add_static_pad_template (element_class, &sink_template);
165 gst_element_class_set_static_metadata (element_class,
166 "MPEG 4 video elementary stream parser", "Codec/Parser/Video",
167 "Parses MPEG-4 Part 2 elementary video streams",
168 "Julien Moutte <julien@fluendo.com>");
170 GST_DEBUG_CATEGORY_INIT (mpeg4v_parse_debug, "mpeg4videoparse", 0,
171 "MPEG-4 video parser");
173 /* Override BaseParse vfuncs */
174 parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_start);
175 parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_stop);
176 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_handle_frame);
177 parse_class->pre_push_frame =
178 GST_DEBUG_FUNCPTR (gst_mpeg4vparse_pre_push_frame);
179 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_set_caps);
180 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_get_caps);
181 parse_class->sink_event = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_event);
182 parse_class->src_event = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_src_event);
186 gst_mpeg4vparse_init (GstMpeg4VParse * parse)
188 parse->interval = DEFAULT_CONFIG_INTERVAL;
189 parse->last_report = GST_CLOCK_TIME_NONE;
191 gst_base_parse_set_pts_interpolation (GST_BASE_PARSE (parse), FALSE);
192 gst_base_parse_set_infer_ts (GST_BASE_PARSE (parse), FALSE);
193 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (parse));
194 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (parse));
198 gst_mpeg4vparse_reset_frame (GstMpeg4VParse * mp4vparse)
200 /* done parsing; reset state */
201 mp4vparse->last_sc = -1;
202 mp4vparse->vop_offset = -1;
203 mp4vparse->vo_found = FALSE;
204 mp4vparse->config_found = FALSE;
205 mp4vparse->vol_offset = -1;
206 mp4vparse->vo_offset = -1;
210 gst_mpeg4vparse_reset (GstMpeg4VParse * mp4vparse)
212 gst_mpeg4vparse_reset_frame (mp4vparse);
213 mp4vparse->update_caps = TRUE;
214 mp4vparse->profile = NULL;
215 mp4vparse->level = NULL;
216 mp4vparse->pending_key_unit_ts = GST_CLOCK_TIME_NONE;
217 mp4vparse->force_key_unit_event = NULL;
218 mp4vparse->discont = FALSE;
220 gst_buffer_replace (&mp4vparse->config, NULL);
221 memset (&mp4vparse->vol, 0, sizeof (mp4vparse->vol));
225 gst_mpeg4vparse_start (GstBaseParse * parse)
227 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
229 GST_DEBUG_OBJECT (parse, "start");
231 gst_mpeg4vparse_reset (mp4vparse);
232 /* at least this much for a valid frame */
233 gst_base_parse_set_min_frame_size (parse, 6);
239 gst_mpeg4vparse_stop (GstBaseParse * parse)
241 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
243 GST_DEBUG_OBJECT (parse, "stop");
245 gst_mpeg4vparse_reset (mp4vparse);
251 gst_mpeg4vparse_process_config (GstMpeg4VParse * mp4vparse,
252 const guint8 * data, guint offset, gsize size)
254 GstMpeg4VisualObject *vo;
255 GstMpeg4VideoObjectLayer vol = { 0 };
257 /* only do stuff if something new */
258 if (mp4vparse->config
259 && gst_buffer_get_size (mp4vparse->config) == size
260 && !gst_buffer_memcmp (mp4vparse->config, offset, data, size))
263 if (mp4vparse->vol_offset < 0) {
264 GST_WARNING ("No video object Layer parsed in this frame, cannot accept "
269 vo = mp4vparse->vo_found ? &mp4vparse->vo : NULL;
271 /* If the parsing fail, we accept the config only if we don't have
273 if (gst_mpeg4_parse_video_object_layer (&vol,
274 vo, data + mp4vparse->vol_offset,
275 size - mp4vparse->vol_offset) != GST_MPEG4_PARSER_OK &&
279 /* ignore update if nothing meaningful changed */
280 if (vol.height == mp4vparse->vol.height &&
281 vol.width == mp4vparse->vol.width &&
282 vol.vop_time_increment_resolution ==
283 mp4vparse->vol.vop_time_increment_resolution &&
284 vol.fixed_vop_time_increment == mp4vparse->vol.fixed_vop_time_increment &&
285 vol.par_width == mp4vparse->vol.par_width &&
286 vol.par_height == mp4vparse->vol.par_height &&
287 vol.sprite_enable == mp4vparse->vol.sprite_enable &&
288 vol.no_of_sprite_warping_points ==
289 mp4vparse->vol.no_of_sprite_warping_points)
292 mp4vparse->vol = vol;
294 GST_LOG_OBJECT (mp4vparse, "Width/Height: %u/%u, "
295 "time increment resolution: %u fixed time increment: %u",
296 mp4vparse->vol.width, mp4vparse->vol.height,
297 mp4vparse->vol.vop_time_increment_resolution,
298 mp4vparse->vol.fixed_vop_time_increment);
301 GST_LOG_OBJECT (mp4vparse, "accepting parsed config size %" G_GSIZE_FORMAT,
304 if (mp4vparse->config != NULL)
305 gst_buffer_unref (mp4vparse->config);
307 mp4vparse->config = gst_buffer_new_memdup (data, size);
309 /* trigger src caps update */
310 mp4vparse->update_caps = TRUE;
315 /* caller guarantees at least start code in @buf at @off */
317 gst_mpeg4vparse_process_sc (GstMpeg4VParse * mp4vparse, GstMpeg4Packet * packet,
321 GST_LOG_OBJECT (mp4vparse, "process startcode %x", packet->type);
323 /* if we found a VOP, next start code ends it,
324 * except for final VOS end sequence code included in last VOP-frame */
325 if (mp4vparse->vop_offset >= 0 &&
326 packet->type != GST_MPEG4_VISUAL_OBJ_SEQ_END) {
327 GST_LOG_OBJECT (mp4vparse, "ending frame of size %d", packet->offset - 3);
331 if (mp4vparse->vo_offset >= 0) {
332 gst_mpeg4_parse_visual_object (&mp4vparse->vo, NULL,
333 packet->data + mp4vparse->vo_offset,
334 packet->offset - 3 - mp4vparse->vo_offset);
335 mp4vparse->vo_offset = -1;
336 mp4vparse->vo_found = TRUE;
339 switch (packet->type) {
340 case GST_MPEG4_VIDEO_OBJ_PLANE:
341 case GST_MPEG4_GROUP_OF_VOP:
342 case GST_MPEG4_USER_DATA:
344 if (packet->type == GST_MPEG4_VIDEO_OBJ_PLANE) {
345 GST_LOG_OBJECT (mp4vparse, "startcode is VOP");
346 mp4vparse->vop_offset = packet->offset;
347 } else if (packet->type == GST_MPEG4_GROUP_OF_VOP) {
348 GST_LOG_OBJECT (mp4vparse, "startcode is GOP");
350 GST_LOG_OBJECT (mp4vparse, "startcode is User Data");
352 /* parse config data ending here if proper startcodes found earlier;
353 * we should have received a visual object before. */
354 if (mp4vparse->config_found) {
355 /*Do not take care startcode into account */
356 gst_mpeg4vparse_process_config (mp4vparse,
357 packet->data, packet->offset, packet->offset - 3);
358 mp4vparse->vo_found = FALSE;
362 case GST_MPEG4_VISUAL_OBJ_SEQ_START:
363 GST_LOG_OBJECT (mp4vparse, "Visual Sequence Start");
364 mp4vparse->config_found = TRUE;
365 mp4vparse->profile = gst_codec_utils_mpeg4video_get_profile (packet->data
366 + packet->offset + 1, packet->offset);
367 mp4vparse->level = gst_codec_utils_mpeg4video_get_level (packet->data
368 + packet->offset + 1, packet->offset);
370 case GST_MPEG4_VISUAL_OBJ:
371 GST_LOG_OBJECT (mp4vparse, "Visual Object");
372 mp4vparse->vo_offset = packet->offset;
375 if (packet->type >= GST_MPEG4_VIDEO_LAYER_FIRST &&
376 packet->type <= GST_MPEG4_VIDEO_LAYER_LAST) {
378 GST_LOG_OBJECT (mp4vparse, "Video Object Layer");
380 /* we keep track of the offset to parse later on */
381 if (mp4vparse->vol_offset < 0)
382 mp4vparse->vol_offset = packet->offset;
384 /* Video Object below is merely a start code,
385 * if that is considered as config, then certainly Video Object Layer
386 * which really contains some needed data */
387 mp4vparse->config_found = TRUE;
389 /* VO (video object) cases */
390 } else if (packet->type <= GST_MPEG4_VIDEO_OBJ_LAST) {
391 GST_LOG_OBJECT (mp4vparse, "Video object");
392 mp4vparse->config_found = TRUE;
397 /* at least need to have a VOP in a frame */
402 gst_mpeg4vparse_handle_frame (GstBaseParse * parse,
403 GstBaseParseFrame * frame, gint * skipsize)
405 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
406 GstMpeg4Packet packet;
411 gboolean ret = FALSE;
414 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (frame->buffer,
415 GST_BUFFER_FLAG_DISCONT))) {
416 mp4vparse->discont = TRUE;
419 gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
424 /* at least start code and subsequent byte */
425 if (G_UNLIKELY (size - off < 5)) {
430 /* avoid stale cached parsing state */
431 if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_NEW_FRAME) {
432 GST_LOG_OBJECT (mp4vparse, "parsing new frame");
433 gst_mpeg4vparse_reset_frame (mp4vparse);
435 GST_LOG_OBJECT (mp4vparse, "resuming frame parsing");
438 /* if already found a previous start code, e.g. start of frame, go for next */
439 if (mp4vparse->last_sc >= 0) {
440 off = mp4vparse->last_sc;
444 /* didn't find anything that looks like a sync word, skip */
445 switch (gst_mpeg4_parse (&packet, FALSE, NULL, data, off, size)) {
446 case (GST_MPEG4_PARSER_NO_PACKET):
447 case (GST_MPEG4_PARSER_ERROR):
448 *skipsize = size - 3;
455 /* possible frame header, but not at offset 0? skip bytes before sync */
456 if (G_UNLIKELY (off > 3)) {
461 switch (packet.type) {
462 case GST_MPEG4_GROUP_OF_VOP:
463 case GST_MPEG4_VISUAL_OBJ_SEQ_START:
464 case GST_MPEG4_VIDEO_OBJ_PLANE:
467 if (packet.type <= GST_MPEG4_VIDEO_OBJ_LAST)
469 if (packet.type >= GST_MPEG4_VIDEO_LAYER_FIRST &&
470 packet.type <= GST_MPEG4_VIDEO_LAYER_LAST)
473 GST_LOG_OBJECT (mp4vparse, "start code is no VOS, VO, VOL, VOP or GOP");
478 mp4vparse->last_sc = 0;
480 /* examine start code, which should not end frame at present */
481 gst_mpeg4vparse_process_sc (mp4vparse, &packet, size);
484 GST_LOG_OBJECT (mp4vparse, "Looking for frame end");
486 /* start is fine as of now */
488 /* position a bit further than last sc */
491 /* so now we have start code at start of data; locate next packet */
492 switch (gst_mpeg4_parse (&packet, FALSE, NULL, data, off, size)) {
493 case (GST_MPEG4_PARSER_NO_PACKET_END):
494 ret = gst_mpeg4vparse_process_sc (mp4vparse, &packet, size);
497 case (GST_MPEG4_PARSER_NO_PACKET):
498 case (GST_MPEG4_PARSER_ERROR):
499 /* if draining, take all */
500 if (GST_BASE_PARSE_DRAINING (parse)) {
504 /* resume scan where we left it */
505 mp4vparse->last_sc = size - 3;
509 /* decide whether this startcode ends a frame */
510 ret = gst_mpeg4vparse_process_sc (mp4vparse, &packet, size);
523 gst_buffer_unmap (frame->buffer, &map);
528 g_assert (framesize <= map.size);
529 res = gst_mpeg4vparse_parse_frame (parse, frame);
530 if (res == GST_BASE_PARSE_FLOW_DROPPED)
531 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_DROP;
532 if (G_UNLIKELY (mp4vparse->discont)) {
533 GST_BUFFER_FLAG_SET (frame->buffer, GST_BUFFER_FLAG_DISCONT);
534 mp4vparse->discont = FALSE;
536 return gst_base_parse_finish_frame (parse, frame, framesize);
543 gst_mpeg4vparse_update_src_caps (GstMpeg4VParse * mp4vparse)
545 GstCaps *caps = NULL;
546 GstStructure *s = NULL;
548 /* only update if no src caps yet or explicitly triggered */
549 if (G_LIKELY (gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (mp4vparse)) &&
550 !mp4vparse->update_caps))
553 GST_LOG_OBJECT (mp4vparse, "Updating caps");
555 /* carry over input caps as much as possible; override with our own stuff */
556 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (mp4vparse));
558 GstCaps *tmp = gst_caps_copy (caps);
559 gst_caps_unref (caps);
561 s = gst_caps_get_structure (caps, 0);
563 caps = gst_caps_new_simple ("video/mpeg",
564 "mpegversion", G_TYPE_INT, 4,
565 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
568 gst_caps_set_simple (caps, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
570 if (mp4vparse->profile && mp4vparse->level) {
571 gst_caps_set_simple (caps, "profile", G_TYPE_STRING, mp4vparse->profile,
572 "level", G_TYPE_STRING, mp4vparse->level, NULL);
575 if (mp4vparse->config != NULL) {
576 gst_caps_set_simple (caps, "codec_data",
577 GST_TYPE_BUFFER, mp4vparse->config, NULL);
580 if (mp4vparse->vol.width > 0 && mp4vparse->vol.height > 0) {
581 gst_caps_set_simple (caps, "width", G_TYPE_INT, mp4vparse->vol.width,
582 "height", G_TYPE_INT, mp4vparse->vol.height, NULL);
585 /* perhaps we have a framerate */
587 gint fps_num = mp4vparse->vol.vop_time_increment_resolution;
588 gint fps_den = mp4vparse->vol.fixed_vop_time_increment;
589 GstClockTime latency;
591 /* upstream overrides */
592 if (s && gst_structure_has_field (s, "framerate"))
593 gst_structure_get_fraction (s, "framerate", &fps_num, &fps_den);
595 if (fps_den > 0 && fps_num > 0) {
596 gst_caps_set_simple (caps, "framerate",
597 GST_TYPE_FRACTION, fps_num, fps_den, NULL);
598 gst_base_parse_set_frame_rate (GST_BASE_PARSE (mp4vparse),
599 fps_num, fps_den, 0, 0);
600 latency = gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
601 gst_base_parse_set_latency (GST_BASE_PARSE (mp4vparse), latency, latency);
605 /* or pixel-aspect-ratio */
606 if (mp4vparse->vol.par_width > 0 && mp4vparse->vol.par_height > 0 &&
607 (!s || !gst_structure_has_field (s, "pixel-aspect-ratio"))) {
608 gst_caps_set_simple (caps, "pixel-aspect-ratio",
609 GST_TYPE_FRACTION, mp4vparse->vol.par_width,
610 mp4vparse->vol.par_height, NULL);
613 if (mp4vparse->vol.sprite_enable != GST_MPEG4_SPRITE_UNUSED)
614 gst_caps_set_simple (caps, "sprite-warping-points", G_TYPE_INT,
615 mp4vparse->vol.no_of_sprite_warping_points, NULL);
617 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (mp4vparse), caps);
618 gst_caps_unref (caps);
620 mp4vparse->update_caps = FALSE;
624 gst_mpeg4vparse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
626 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
627 GstBuffer *buffer = frame->buffer;
629 gboolean intra = FALSE;
631 gst_mpeg4vparse_update_src_caps (mp4vparse);
633 /* let's live up to our function name and really parse something here
634 * (which ensures it is done for all frames, whether drained or not);
635 * determine intra frame */
636 gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
637 if (G_LIKELY (map.size > mp4vparse->vop_offset + 1)) {
638 intra = ((map.data[mp4vparse->vop_offset + 1] >> 6 & 0x3) == 0);
639 GST_DEBUG_OBJECT (mp4vparse, "frame intra = %d", intra);
641 GST_WARNING_OBJECT (mp4vparse, "no data following VOP startcode");
643 gst_buffer_unmap (frame->buffer, &map);
646 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
648 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
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 (!mp4vparse->sent_codec_tag) {
736 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
737 if (G_UNLIKELY (caps == NULL)) {
738 if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
739 GST_INFO_OBJECT (parse, "Src pad is flushing");
740 return GST_FLOW_FLUSHING;
742 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
743 return GST_FLOW_NOT_NEGOTIATED;
747 taglist = gst_tag_list_new_empty ();
748 gst_pb_utils_add_codec_description_to_tag_list (taglist,
749 GST_TAG_VIDEO_CODEC, caps);
750 gst_caps_unref (caps);
752 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
753 gst_tag_list_unref (taglist);
755 /* also signals the end of first-frame processing */
756 mp4vparse->sent_codec_tag = TRUE;
759 if ((event = check_pending_key_unit_event (mp4vparse->force_key_unit_event,
760 &parse->segment, GST_BUFFER_TIMESTAMP (buffer),
761 GST_BUFFER_FLAGS (buffer), mp4vparse->pending_key_unit_ts))) {
762 gst_mpeg4vparse_prepare_key_unit (mp4vparse, event);
766 if (mp4vparse->interval == -1) {
770 /* periodic config sending */
771 if (mp4vparse->interval > 0 || push_codec) {
772 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
776 if (!GST_CLOCK_TIME_IS_VALID (mp4vparse->last_report)) {
777 mp4vparse->last_report = timestamp;
780 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
781 if (timestamp > mp4vparse->last_report)
782 diff = timestamp - mp4vparse->last_report;
786 GST_LOG_OBJECT (mp4vparse,
787 "now %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
788 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (mp4vparse->last_report));
790 GST_LOG_OBJECT (mp4vparse,
791 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
793 if (GST_TIME_AS_SECONDS (diff) >= mp4vparse->interval || push_codec) {
798 /* we need to send config now first */
799 GST_INFO_OBJECT (parse, "inserting config in stream");
800 gst_buffer_map (mp4vparse->config, &cmap, GST_MAP_READ);
801 diffconf = (gst_buffer_get_size (buffer) < cmap.size)
802 || gst_buffer_memcmp (buffer, 0, cmap.data, cmap.size);
804 gst_buffer_unmap (mp4vparse->config, &cmap);
806 /* avoid inserting duplicate config */
812 gst_buffer_append (gst_buffer_ref (mp4vparse->config),
813 gst_buffer_ref (buffer));
814 gst_buffer_copy_into (superbuf, buffer, GST_BUFFER_COPY_METADATA, 0,
816 gst_buffer_replace (&frame->out_buffer, superbuf);
817 gst_buffer_unref (superbuf);
819 GST_INFO_OBJECT (parse, "... but avoiding duplication");
822 if (G_UNLIKELY (timestamp != -1)) {
823 mp4vparse->last_report = timestamp;
833 gst_mpeg4vparse_set_caps (GstBaseParse * parse, GstCaps * caps)
835 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
843 GstMpeg4Packet packet;
844 GstMpeg4ParseResult res;
846 GST_DEBUG_OBJECT (parse, "setcaps called with %" GST_PTR_FORMAT, caps);
848 s = gst_caps_get_structure (caps, 0);
850 if ((value = gst_structure_get_value (s, "codec_data")) != NULL
851 && (buf = gst_value_get_buffer (value))) {
852 /* best possible parse attempt,
853 * src caps are based on sink caps so it will end up in there
854 * whether successful or not */
855 gst_buffer_map (buf, &map, GST_MAP_READ);
858 res = gst_mpeg4_parse (&packet, FALSE, NULL, data, 0, size);
860 while (res == GST_MPEG4_PARSER_OK || res == GST_MPEG4_PARSER_NO_PACKET_END) {
862 if (packet.type >= GST_MPEG4_VIDEO_LAYER_FIRST &&
863 packet.type <= GST_MPEG4_VIDEO_LAYER_LAST)
864 mp4vparse->vol_offset = packet.offset;
866 else if (packet.type == GST_MPEG4_VISUAL_OBJ) {
867 gst_mpeg4_parse_visual_object (&mp4vparse->vo, NULL,
868 data + packet.offset, MIN (packet.size, size));
869 mp4vparse->vo_found = TRUE;
872 res = gst_mpeg4_parse (&packet, FALSE, NULL, data, packet.offset, size);
875 /* And take it as config */
876 gst_mpeg4vparse_process_config (mp4vparse, data, 3, size);
877 gst_buffer_unmap (buf, &map);
878 gst_mpeg4vparse_reset_frame (mp4vparse);
881 /* let's not interfere and accept regardless of config parsing success */
886 remove_fields (GstCaps * caps)
890 n = gst_caps_get_size (caps);
891 for (i = 0; i < n; i++) {
892 GstStructure *s = gst_caps_get_structure (caps, i);
894 gst_structure_remove_field (s, "parsed");
900 gst_mpeg4vparse_get_caps (GstBaseParse * parse, GstCaps * filter)
902 GstCaps *peercaps, *templ;
905 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
907 GstCaps *fcopy = gst_caps_copy (filter);
908 /* Remove the fields we convert */
909 remove_fields (fcopy);
910 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
911 gst_caps_unref (fcopy);
913 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
916 /* Remove the parsed field */
917 peercaps = gst_caps_make_writable (peercaps);
918 remove_fields (peercaps);
920 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
921 gst_caps_unref (peercaps);
922 gst_caps_unref (templ);
928 GstCaps *tmp = gst_caps_intersect_full (res, filter,
929 GST_CAPS_INTERSECT_FIRST);
930 gst_caps_unref (res);
939 gst_mpeg4vparse_event (GstBaseParse * parse, GstEvent * event)
942 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
944 switch (GST_EVENT_TYPE (event)) {
945 case GST_EVENT_CUSTOM_DOWNSTREAM:
947 GstClockTime timestamp, stream_time, running_time;
948 gboolean all_headers;
951 if (gst_video_event_is_force_key_unit (event)) {
952 gst_video_event_parse_downstream_force_key_unit (event,
953 ×tamp, &stream_time, &running_time, &all_headers, &count);
955 GST_INFO_OBJECT (mp4vparse, "received downstream force key unit event, "
956 "seqnum %d running_time %" GST_TIME_FORMAT
957 " all_headers %d count %d", gst_event_get_seqnum (event),
958 GST_TIME_ARGS (running_time), all_headers, count);
960 if (mp4vparse->force_key_unit_event) {
961 GST_INFO_OBJECT (mp4vparse, "ignoring force key unit event "
962 "as one is already queued");
964 mp4vparse->pending_key_unit_ts = running_time;
965 gst_event_replace (&mp4vparse->force_key_unit_event, event);
967 gst_event_unref (event);
970 res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse, event);
975 res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse, event);
982 gst_mpeg4vparse_src_event (GstBaseParse * parse, GstEvent * event)
985 GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
987 switch (GST_EVENT_TYPE (event)) {
988 case GST_EVENT_CUSTOM_UPSTREAM:
990 GstClockTime running_time;
991 gboolean all_headers;
994 if (gst_video_event_is_force_key_unit (event)) {
995 gst_video_event_parse_upstream_force_key_unit (event,
996 &running_time, &all_headers, &count);
998 GST_INFO_OBJECT (mp4vparse, "received upstream force-key-unit event, "
999 "seqnum %d running_time %" GST_TIME_FORMAT
1000 " all_headers %d count %d", gst_event_get_seqnum (event),
1001 GST_TIME_ARGS (running_time), all_headers, count);
1004 mp4vparse->pending_key_unit_ts = running_time;
1005 gst_event_replace (&mp4vparse->force_key_unit_event, event);
1008 res = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
1012 res = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);