1 /* GStreamer
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>
10  *
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.
15  *
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.
20  *
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.
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <string.h>
32 #include <gst/base/base.h>
33 #include <gst/pbutils/pbutils.h>
34 #include <gst/video/video.h>
35 
36 #include "gstmpeg4videoparse.h"
37 
38 GST_DEBUG_CATEGORY (mpeg4v_parse_debug);
39 #define GST_CAT_DEFAULT mpeg4v_parse_debug
40 
41 static GstStaticPadTemplate src_template =
42     GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
43     GST_PAD_ALWAYS,
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 ]")
51     );
52 
53 static GstStaticPadTemplate sink_template =
54     GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("video/mpeg, "
57         "mpegversion = (int) 4, " "systemstream = (boolean) false; "
58         "video/x-divx, " "divxversion = (int) [ 4, 5 ]")
59     );
60 
61 /* Properties */
62 #define DEFAULT_PROP_DROP TRUE
63 #define DEFAULT_CONFIG_INTERVAL (0)
64 
65 enum
66 {
67   PROP_0,
68   PROP_DROP,
69   PROP_CONFIG_INTERVAL
70 };
71 
72 #define gst_mpeg4vparse_parent_class parent_class
73 G_DEFINE_TYPE (GstMpeg4VParse, gst_mpeg4vparse, GST_TYPE_BASE_PARSE);
74 
75 static gboolean gst_mpeg4vparse_start (GstBaseParse * parse);
76 static gboolean gst_mpeg4vparse_stop (GstBaseParse * parse);
77 static GstFlowReturn gst_mpeg4vparse_handle_frame (GstBaseParse * parse,
78     GstBaseParseFrame * frame, gint * skipsize);
79 static GstFlowReturn gst_mpeg4vparse_parse_frame (GstBaseParse * parse,
80     GstBaseParseFrame * frame);
81 static GstFlowReturn gst_mpeg4vparse_pre_push_frame (GstBaseParse * parse,
82     GstBaseParseFrame * frame);
83 static gboolean gst_mpeg4vparse_set_caps (GstBaseParse * parse, GstCaps * caps);
84 static GstCaps *gst_mpeg4vparse_get_caps (GstBaseParse * parse,
85     GstCaps * filter);
86 
87 static void gst_mpeg4vparse_set_property (GObject * object, guint prop_id,
88     const GValue * value, GParamSpec * pspec);
89 static void gst_mpeg4vparse_get_property (GObject * object, guint prop_id,
90     GValue * value, GParamSpec * pspec);
91 static gboolean gst_mpeg4vparse_event (GstBaseParse * parse, GstEvent * event);
92 static gboolean gst_mpeg4vparse_src_event (GstBaseParse * parse,
93     GstEvent * event);
94 
95 static void
gst_mpeg4vparse_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)96 gst_mpeg4vparse_set_property (GObject * object, guint property_id,
97     const GValue * value, GParamSpec * pspec)
98 {
99   GstMpeg4VParse *parse = GST_MPEG4VIDEO_PARSE (object);
100 
101   switch (property_id) {
102     case PROP_DROP:
103       parse->drop = g_value_get_boolean (value);
104       break;
105     case PROP_CONFIG_INTERVAL:
106       parse->interval = g_value_get_uint (value);
107       break;
108     default:
109       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
110   }
111 }
112 
113 static void
gst_mpeg4vparse_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)114 gst_mpeg4vparse_get_property (GObject * object, guint property_id,
115     GValue * value, GParamSpec * pspec)
116 {
117   GstMpeg4VParse *parse = GST_MPEG4VIDEO_PARSE (object);
118 
119   switch (property_id) {
120     case PROP_DROP:
121       g_value_set_boolean (value, parse->drop);
122       break;
123     case PROP_CONFIG_INTERVAL:
124       g_value_set_uint (value, parse->interval);
125       break;
126     default:
127       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
128   }
129 }
130 
131 static void
gst_mpeg4vparse_class_init(GstMpeg4VParseClass * klass)132 gst_mpeg4vparse_class_init (GstMpeg4VParseClass * klass)
133 {
134   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
135   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
136   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
137 
138   parent_class = g_type_class_peek_parent (klass);
139 
140   gobject_class->set_property = gst_mpeg4vparse_set_property;
141   gobject_class->get_property = gst_mpeg4vparse_get_property;
142 
143   g_object_class_install_property (gobject_class, PROP_DROP,
144       g_param_spec_boolean ("drop", "drop",
145           "Drop data untill valid configuration data is received either "
146           "in the stream or through caps", DEFAULT_PROP_DROP,
147           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148 
149   g_object_class_install_property (gobject_class, PROP_CONFIG_INTERVAL,
150       g_param_spec_uint ("config-interval",
151           "Configuration Send Interval",
152           "Send Configuration Insertion Interval in seconds (configuration headers "
153           "will be multiplexed in the data stream when detected.) (0 = disabled)",
154           0, 3600, DEFAULT_CONFIG_INTERVAL,
155           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
156 
157   gst_element_class_add_static_pad_template (element_class, &src_template);
158   gst_element_class_add_static_pad_template (element_class, &sink_template);
159 
160   gst_element_class_set_static_metadata (element_class,
161       "MPEG 4 video elementary stream parser", "Codec/Parser/Video",
162       "Parses MPEG-4 Part 2 elementary video streams",
163       "Julien Moutte <julien@fluendo.com>");
164 
165   GST_DEBUG_CATEGORY_INIT (mpeg4v_parse_debug, "mpeg4videoparse", 0,
166       "MPEG-4 video parser");
167 
168   /* Override BaseParse vfuncs */
169   parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_start);
170   parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_stop);
171   parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_handle_frame);
172   parse_class->pre_push_frame =
173       GST_DEBUG_FUNCPTR (gst_mpeg4vparse_pre_push_frame);
174   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_set_caps);
175   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_get_caps);
176   parse_class->sink_event = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_event);
177   parse_class->src_event = GST_DEBUG_FUNCPTR (gst_mpeg4vparse_src_event);
178 }
179 
180 static void
gst_mpeg4vparse_init(GstMpeg4VParse * parse)181 gst_mpeg4vparse_init (GstMpeg4VParse * parse)
182 {
183   parse->interval = DEFAULT_CONFIG_INTERVAL;
184   parse->last_report = GST_CLOCK_TIME_NONE;
185 
186   gst_base_parse_set_pts_interpolation (GST_BASE_PARSE (parse), FALSE);
187   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (parse));
188   GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (parse));
189 }
190 
191 static void
gst_mpeg4vparse_reset_frame(GstMpeg4VParse * mp4vparse)192 gst_mpeg4vparse_reset_frame (GstMpeg4VParse * mp4vparse)
193 {
194   /* done parsing; reset state */
195   mp4vparse->last_sc = -1;
196   mp4vparse->vop_offset = -1;
197   mp4vparse->vo_found = FALSE;
198   mp4vparse->config_found = FALSE;
199   mp4vparse->vol_offset = -1;
200   mp4vparse->vo_offset = -1;
201 }
202 
203 static void
gst_mpeg4vparse_reset(GstMpeg4VParse * mp4vparse)204 gst_mpeg4vparse_reset (GstMpeg4VParse * mp4vparse)
205 {
206   gst_mpeg4vparse_reset_frame (mp4vparse);
207   mp4vparse->update_caps = TRUE;
208   mp4vparse->profile = NULL;
209   mp4vparse->level = NULL;
210   mp4vparse->pending_key_unit_ts = GST_CLOCK_TIME_NONE;
211   mp4vparse->force_key_unit_event = NULL;
212   mp4vparse->discont = FALSE;
213 
214   gst_buffer_replace (&mp4vparse->config, NULL);
215   memset (&mp4vparse->vol, 0, sizeof (mp4vparse->vol));
216 }
217 
218 static gboolean
gst_mpeg4vparse_start(GstBaseParse * parse)219 gst_mpeg4vparse_start (GstBaseParse * parse)
220 {
221   GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
222 
223   GST_DEBUG_OBJECT (parse, "start");
224 
225   gst_mpeg4vparse_reset (mp4vparse);
226   /* at least this much for a valid frame */
227   gst_base_parse_set_min_frame_size (parse, 6);
228 
229   return TRUE;
230 }
231 
232 static gboolean
gst_mpeg4vparse_stop(GstBaseParse * parse)233 gst_mpeg4vparse_stop (GstBaseParse * parse)
234 {
235   GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
236 
237   GST_DEBUG_OBJECT (parse, "stop");
238 
239   gst_mpeg4vparse_reset (mp4vparse);
240 
241   return TRUE;
242 }
243 
244 static gboolean
gst_mpeg4vparse_process_config(GstMpeg4VParse * mp4vparse,const guint8 * data,guint offset,gsize size)245 gst_mpeg4vparse_process_config (GstMpeg4VParse * mp4vparse,
246     const guint8 * data, guint offset, gsize size)
247 {
248   GstMpeg4VisualObject *vo;
249   GstMpeg4VideoObjectLayer vol = { 0 };
250 
251   /* only do stuff if something new */
252   if (mp4vparse->config
253       && gst_buffer_get_size (mp4vparse->config) == size
254       && !gst_buffer_memcmp (mp4vparse->config, offset, data, size))
255     return TRUE;
256 
257   if (mp4vparse->vol_offset < 0) {
258     GST_WARNING ("No video object Layer parsed in this frame, cannot accept "
259         "config");
260     return FALSE;
261   }
262 
263   vo = mp4vparse->vo_found ? &mp4vparse->vo : NULL;
264 
265   /* If the parsing fail, we accept the config only if we don't have
266    * any config yet. */
267   if (gst_mpeg4_parse_video_object_layer (&vol,
268           vo, data + mp4vparse->vol_offset,
269           size - mp4vparse->vol_offset) != GST_MPEG4_PARSER_OK &&
270       mp4vparse->config)
271     return FALSE;
272 
273   /* ignore update if nothing meaningful changed */
274   if (vol.height == mp4vparse->vol.height &&
275       vol.width == mp4vparse->vol.width &&
276       vol.vop_time_increment_resolution ==
277       mp4vparse->vol.vop_time_increment_resolution &&
278       vol.fixed_vop_time_increment == mp4vparse->vol.fixed_vop_time_increment &&
279       vol.par_width == mp4vparse->vol.par_width &&
280       vol.par_height == mp4vparse->vol.par_height &&
281       vol.sprite_enable == mp4vparse->vol.sprite_enable &&
282       vol.no_of_sprite_warping_points ==
283       mp4vparse->vol.no_of_sprite_warping_points)
284     return TRUE;
285 
286   mp4vparse->vol = vol;
287 
288   GST_LOG_OBJECT (mp4vparse, "Width/Height: %u/%u, "
289       "time increment resolution: %u fixed time increment: %u",
290       mp4vparse->vol.width, mp4vparse->vol.height,
291       mp4vparse->vol.vop_time_increment_resolution,
292       mp4vparse->vol.fixed_vop_time_increment);
293 
294 
295   GST_LOG_OBJECT (mp4vparse, "accepting parsed config size %" G_GSIZE_FORMAT,
296       size);
297 
298   if (mp4vparse->config != NULL)
299     gst_buffer_unref (mp4vparse->config);
300 
301   mp4vparse->config = gst_buffer_new_wrapped (g_memdup (data, size), size);
302 
303   /* trigger src caps update */
304   mp4vparse->update_caps = TRUE;
305 
306   return TRUE;
307 }
308 
309 /* caller guarantees at least start code in @buf at @off */
310 static gboolean
gst_mpeg4vparse_process_sc(GstMpeg4VParse * mp4vparse,GstMpeg4Packet * packet,gsize size)311 gst_mpeg4vparse_process_sc (GstMpeg4VParse * mp4vparse, GstMpeg4Packet * packet,
312     gsize size)
313 {
314 
315   GST_LOG_OBJECT (mp4vparse, "process startcode %x", packet->type);
316 
317   /* if we found a VOP, next start code ends it,
318    * except for final VOS end sequence code included in last VOP-frame */
319   if (mp4vparse->vop_offset >= 0 &&
320       packet->type != GST_MPEG4_VISUAL_OBJ_SEQ_END) {
321     GST_LOG_OBJECT (mp4vparse, "ending frame of size %d", packet->offset - 3);
322     return TRUE;
323   }
324 
325   if (mp4vparse->vo_offset >= 0) {
326     gst_mpeg4_parse_visual_object (&mp4vparse->vo, NULL,
327         packet->data + mp4vparse->vo_offset,
328         packet->offset - 3 - mp4vparse->vo_offset);
329     mp4vparse->vo_offset = -1;
330     mp4vparse->vo_found = TRUE;
331   }
332 
333   switch (packet->type) {
334     case GST_MPEG4_VIDEO_OBJ_PLANE:
335     case GST_MPEG4_GROUP_OF_VOP:
336     case GST_MPEG4_USER_DATA:
337     {
338       if (packet->type == GST_MPEG4_VIDEO_OBJ_PLANE) {
339         GST_LOG_OBJECT (mp4vparse, "startcode is VOP");
340         mp4vparse->vop_offset = packet->offset;
341       } else if (packet->type == GST_MPEG4_GROUP_OF_VOP) {
342         GST_LOG_OBJECT (mp4vparse, "startcode is GOP");
343       } else {
344         GST_LOG_OBJECT (mp4vparse, "startcode is User Data");
345       }
346       /* parse config data ending here if proper startcodes found earlier;
347        * we should have received a visual object before. */
348       if (mp4vparse->config_found) {
349         /*Do not take care startcode into account */
350         gst_mpeg4vparse_process_config (mp4vparse,
351             packet->data, packet->offset, packet->offset - 3);
352         mp4vparse->vo_found = FALSE;
353       }
354       break;
355     }
356     case GST_MPEG4_VISUAL_OBJ_SEQ_START:
357       GST_LOG_OBJECT (mp4vparse, "Visual Sequence Start");
358       mp4vparse->config_found = TRUE;
359       mp4vparse->profile = gst_codec_utils_mpeg4video_get_profile (packet->data
360           + packet->offset + 1, packet->offset);
361       mp4vparse->level = gst_codec_utils_mpeg4video_get_level (packet->data
362           + packet->offset + 1, packet->offset);
363       break;
364     case GST_MPEG4_VISUAL_OBJ:
365       GST_LOG_OBJECT (mp4vparse, "Visual Object");
366       mp4vparse->vo_offset = packet->offset;
367       break;
368     default:
369       if (packet->type >= GST_MPEG4_VIDEO_LAYER_FIRST &&
370           packet->type <= GST_MPEG4_VIDEO_LAYER_LAST) {
371 
372         GST_LOG_OBJECT (mp4vparse, "Video Object Layer");
373 
374         /* we keep track of the offset to parse later on */
375         if (mp4vparse->vol_offset < 0)
376           mp4vparse->vol_offset = packet->offset;
377 
378         /* Video Object below is merely a start code,
379          * if that is considered as config, then certainly Video Object Layer
380          * which really contains some needed data */
381         mp4vparse->config_found = TRUE;
382 
383         /* VO (video object) cases */
384       } else if (packet->type <= GST_MPEG4_VIDEO_OBJ_LAST) {
385         GST_LOG_OBJECT (mp4vparse, "Video object");
386         mp4vparse->config_found = TRUE;
387       }
388       break;
389   }
390 
391   /* at least need to have a VOP in a frame */
392   return FALSE;
393 }
394 
395 static GstFlowReturn
gst_mpeg4vparse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)396 gst_mpeg4vparse_handle_frame (GstBaseParse * parse,
397     GstBaseParseFrame * frame, gint * skipsize)
398 {
399   GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
400   GstMpeg4Packet packet;
401   GstMapInfo map;
402   guint8 *data = NULL;
403   gsize size;
404   gint off = 0;
405   gboolean ret = FALSE;
406   guint framesize = 0;
407 
408   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (frame->buffer,
409               GST_BUFFER_FLAG_DISCONT))) {
410     mp4vparse->discont = TRUE;
411   }
412 
413   gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
414   data = map.data;
415   size = map.size;
416 
417 retry:
418   /* at least start code and subsequent byte */
419   if (G_UNLIKELY (size - off < 5)) {
420     *skipsize = 1;
421     goto out;
422   }
423 
424   /* avoid stale cached parsing state */
425   if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_NEW_FRAME) {
426     GST_LOG_OBJECT (mp4vparse, "parsing new frame");
427     gst_mpeg4vparse_reset_frame (mp4vparse);
428   } else {
429     GST_LOG_OBJECT (mp4vparse, "resuming frame parsing");
430   }
431 
432   /* if already found a previous start code, e.g. start of frame, go for next */
433   if (mp4vparse->last_sc >= 0) {
434     off = mp4vparse->last_sc;
435     goto next;
436   }
437 
438   /* didn't find anything that looks like a sync word, skip */
439   switch (gst_mpeg4_parse (&packet, FALSE, NULL, data, off, size)) {
440     case (GST_MPEG4_PARSER_NO_PACKET):
441     case (GST_MPEG4_PARSER_ERROR):
442       *skipsize = size - 3;
443       goto out;
444     default:
445       break;
446   }
447   off = packet.offset;
448 
449   /* possible frame header, but not at offset 0? skip bytes before sync */
450   if (G_UNLIKELY (off > 3)) {
451     *skipsize = off - 3;
452     goto out;
453   }
454 
455   switch (packet.type) {
456     case GST_MPEG4_GROUP_OF_VOP:
457     case GST_MPEG4_VISUAL_OBJ_SEQ_START:
458     case GST_MPEG4_VIDEO_OBJ_PLANE:
459       break;
460     default:
461       if (packet.type <= GST_MPEG4_VIDEO_OBJ_LAST)
462         break;
463       if (packet.type >= GST_MPEG4_VIDEO_LAYER_FIRST &&
464           packet.type <= GST_MPEG4_VIDEO_LAYER_LAST)
465         break;
466       /* undesirable sc */
467       GST_LOG_OBJECT (mp4vparse, "start code is no VOS, VO, VOL, VOP or GOP");
468       goto retry;
469   }
470 
471   /* found sc */
472   mp4vparse->last_sc = 0;
473 
474   /* examine start code, which should not end frame at present */
475   gst_mpeg4vparse_process_sc (mp4vparse, &packet, size);
476 
477 next:
478   GST_LOG_OBJECT (mp4vparse, "Looking for frame end");
479 
480   /* start is fine as of now */
481   *skipsize = 0;
482   /* position a bit further than last sc */
483   off++;
484 
485   /* so now we have start code at start of data; locate next packet */
486   switch (gst_mpeg4_parse (&packet, FALSE, NULL, data, off, size)) {
487     case (GST_MPEG4_PARSER_NO_PACKET_END):
488       ret = gst_mpeg4vparse_process_sc (mp4vparse, &packet, size);
489       if (ret)
490         break;
491     case (GST_MPEG4_PARSER_NO_PACKET):
492     case (GST_MPEG4_PARSER_ERROR):
493       /* if draining, take all */
494       if (GST_BASE_PARSE_DRAINING (parse)) {
495         framesize = size;
496         ret = TRUE;
497       } else {
498         /* resume scan where we left it */
499         mp4vparse->last_sc = size - 3;
500       }
501       goto out;
502     default:
503       /* decide whether this startcode ends a frame */
504       ret = gst_mpeg4vparse_process_sc (mp4vparse, &packet, size);
505       break;
506   }
507 
508   off = packet.offset;
509 
510   if (ret) {
511     framesize = off - 3;
512   } else {
513     goto next;
514   }
515 
516 out:
517   gst_buffer_unmap (frame->buffer, &map);
518 
519   if (ret) {
520     GstFlowReturn res;
521 
522     g_assert (framesize <= map.size);
523     res = gst_mpeg4vparse_parse_frame (parse, frame);
524     if (res == GST_BASE_PARSE_FLOW_DROPPED)
525       frame->flags |= GST_BASE_PARSE_FRAME_FLAG_DROP;
526     if (G_UNLIKELY (mp4vparse->discont)) {
527       GST_BUFFER_FLAG_SET (frame->buffer, GST_BUFFER_FLAG_DISCONT);
528       mp4vparse->discont = FALSE;
529     }
530     return gst_base_parse_finish_frame (parse, frame, framesize);
531   }
532 
533   return GST_FLOW_OK;
534 }
535 
536 static void
gst_mpeg4vparse_update_src_caps(GstMpeg4VParse * mp4vparse)537 gst_mpeg4vparse_update_src_caps (GstMpeg4VParse * mp4vparse)
538 {
539   GstCaps *caps = NULL;
540   GstStructure *s = NULL;
541 
542   /* only update if no src caps yet or explicitly triggered */
543   if (G_LIKELY (gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (mp4vparse)) &&
544           !mp4vparse->update_caps))
545     return;
546 
547   GST_LOG_OBJECT (mp4vparse, "Updating caps");
548 
549   /* carry over input caps as much as possible; override with our own stuff */
550   caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (mp4vparse));
551   if (caps) {
552     GstCaps *tmp = gst_caps_copy (caps);
553     gst_caps_unref (caps);
554     caps = tmp;
555     s = gst_caps_get_structure (caps, 0);
556   } else {
557     caps = gst_caps_new_simple ("video/mpeg",
558         "mpegversion", G_TYPE_INT, 4,
559         "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
560   }
561 
562   gst_caps_set_simple (caps, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
563 
564   if (mp4vparse->profile && mp4vparse->level) {
565     gst_caps_set_simple (caps, "profile", G_TYPE_STRING, mp4vparse->profile,
566         "level", G_TYPE_STRING, mp4vparse->level, NULL);
567   }
568 
569   if (mp4vparse->config != NULL) {
570     gst_caps_set_simple (caps, "codec_data",
571         GST_TYPE_BUFFER, mp4vparse->config, NULL);
572   }
573 
574   if (mp4vparse->vol.width > 0 && mp4vparse->vol.height > 0) {
575     gst_caps_set_simple (caps, "width", G_TYPE_INT, mp4vparse->vol.width,
576         "height", G_TYPE_INT, mp4vparse->vol.height, NULL);
577   }
578 
579   /* perhaps we have a framerate */
580   {
581     gint fps_num = mp4vparse->vol.vop_time_increment_resolution;
582     gint fps_den = mp4vparse->vol.fixed_vop_time_increment;
583     GstClockTime latency;
584 
585     /* upstream overrides */
586     if (s && gst_structure_has_field (s, "framerate"))
587       gst_structure_get_fraction (s, "framerate", &fps_num, &fps_den);
588 
589     if (fps_den > 0 && fps_num > 0) {
590       gst_caps_set_simple (caps, "framerate",
591           GST_TYPE_FRACTION, fps_num, fps_den, NULL);
592       gst_base_parse_set_frame_rate (GST_BASE_PARSE (mp4vparse),
593           fps_num, fps_den, 0, 0);
594       latency = gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
595       gst_base_parse_set_latency (GST_BASE_PARSE (mp4vparse), latency, latency);
596     }
597   }
598 
599   /* or pixel-aspect-ratio */
600   if (mp4vparse->vol.par_width > 0 && mp4vparse->vol.par_height > 0 &&
601       (!s || !gst_structure_has_field (s, "pixel-aspect-ratio"))) {
602     gst_caps_set_simple (caps, "pixel-aspect-ratio",
603         GST_TYPE_FRACTION, mp4vparse->vol.par_width,
604         mp4vparse->vol.par_height, NULL);
605   }
606 
607   if (mp4vparse->vol.sprite_enable != GST_MPEG4_SPRITE_UNUSED)
608     gst_caps_set_simple (caps, "sprite-warping-points", G_TYPE_INT,
609         mp4vparse->vol.no_of_sprite_warping_points, NULL);
610 
611   gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (mp4vparse), caps);
612   gst_caps_unref (caps);
613 
614   mp4vparse->update_caps = FALSE;
615 }
616 
617 static GstFlowReturn
gst_mpeg4vparse_parse_frame(GstBaseParse * parse,GstBaseParseFrame * frame)618 gst_mpeg4vparse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
619 {
620   GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
621   GstBuffer *buffer = frame->buffer;
622   GstMapInfo map;
623   gboolean intra = FALSE;
624 
625   gst_mpeg4vparse_update_src_caps (mp4vparse);
626 
627   /* let's live up to our function name and really parse something here
628    * (which ensures it is done for all frames, whether drained or not);
629    * determine intra frame */
630   gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
631   if (G_LIKELY (map.size > mp4vparse->vop_offset + 1)) {
632     intra = ((map.data[mp4vparse->vop_offset + 1] >> 6 & 0x3) == 0);
633     GST_DEBUG_OBJECT (mp4vparse, "frame intra = %d", intra);
634   } else {
635     GST_WARNING_OBJECT (mp4vparse, "no data following VOP startcode");
636   }
637   gst_buffer_unmap (frame->buffer, &map);
638 
639   if (intra)
640     GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
641   else
642     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
643 
644   if (G_UNLIKELY (mp4vparse->drop && !mp4vparse->config)) {
645     GST_LOG_OBJECT (mp4vparse, "dropping frame as no config yet");
646     return GST_BASE_PARSE_FLOW_DROPPED;
647   } else
648     return GST_FLOW_OK;
649 }
650 
651 static GstEvent *
check_pending_key_unit_event(GstEvent * pending_event,GstSegment * segment,GstClockTime timestamp,guint flags,GstClockTime pending_key_unit_ts)652 check_pending_key_unit_event (GstEvent * pending_event, GstSegment * segment,
653     GstClockTime timestamp, guint flags, GstClockTime pending_key_unit_ts)
654 {
655   GstClockTime running_time, stream_time;
656   gboolean all_headers;
657   guint count;
658   GstEvent *event = NULL;
659 
660   g_return_val_if_fail (segment != NULL, NULL);
661 
662   if (pending_event == NULL)
663     goto out;
664 
665   if (GST_CLOCK_TIME_IS_VALID (pending_key_unit_ts) &&
666       timestamp == GST_CLOCK_TIME_NONE)
667     goto out;
668 
669   running_time = gst_segment_to_running_time (segment,
670       GST_FORMAT_TIME, timestamp);
671 
672   GST_INFO ("now %" GST_TIME_FORMAT " wanted %" GST_TIME_FORMAT,
673       GST_TIME_ARGS (running_time), GST_TIME_ARGS (pending_key_unit_ts));
674   if (GST_CLOCK_TIME_IS_VALID (pending_key_unit_ts) &&
675       running_time < pending_key_unit_ts)
676     goto out;
677 
678   if (flags & GST_BUFFER_FLAG_DELTA_UNIT) {
679     GST_DEBUG ("pending force key unit, waiting for keyframe");
680     goto out;
681   }
682 
683   stream_time = gst_segment_to_stream_time (segment,
684       GST_FORMAT_TIME, timestamp);
685 
686   gst_video_event_parse_upstream_force_key_unit (pending_event,
687       NULL, &all_headers, &count);
688 
689   event =
690       gst_video_event_new_downstream_force_key_unit (timestamp, stream_time,
691       running_time, all_headers, count);
692   gst_event_set_seqnum (event, gst_event_get_seqnum (pending_event));
693 
694 out:
695   return event;
696 }
697 
698 static void
gst_mpeg4vparse_prepare_key_unit(GstMpeg4VParse * parse,GstEvent * event)699 gst_mpeg4vparse_prepare_key_unit (GstMpeg4VParse * parse, GstEvent * event)
700 {
701   GstClockTime running_time;
702   guint count;
703 
704   parse->pending_key_unit_ts = GST_CLOCK_TIME_NONE;
705   gst_event_replace (&parse->force_key_unit_event, NULL);
706 
707   gst_video_event_parse_downstream_force_key_unit (event,
708       NULL, NULL, &running_time, NULL, &count);
709 
710   GST_INFO_OBJECT (parse, "pushing downstream force-key-unit event %d "
711       "%" GST_TIME_FORMAT " count %d", gst_event_get_seqnum (event),
712       GST_TIME_ARGS (running_time), count);
713   gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (parse), event);
714 }
715 
716 
717 static GstFlowReturn
gst_mpeg4vparse_pre_push_frame(GstBaseParse * parse,GstBaseParseFrame * frame)718 gst_mpeg4vparse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
719 {
720   GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
721   GstBuffer *buffer = frame->buffer;
722   gboolean push_codec = FALSE;
723   GstEvent *event = NULL;
724 
725   if (!mp4vparse->sent_codec_tag) {
726     GstTagList *taglist;
727     GstCaps *caps;
728 
729     /* codec tag */
730     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
731     if (G_UNLIKELY (caps == NULL)) {
732       if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
733         GST_INFO_OBJECT (parse, "Src pad is flushing");
734         return GST_FLOW_FLUSHING;
735       } else {
736         GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
737         return GST_FLOW_NOT_NEGOTIATED;
738       }
739     }
740 
741     taglist = gst_tag_list_new_empty ();
742     gst_pb_utils_add_codec_description_to_tag_list (taglist,
743         GST_TAG_VIDEO_CODEC, caps);
744     gst_caps_unref (caps);
745 
746     gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
747     gst_tag_list_unref (taglist);
748 
749     /* also signals the end of first-frame processing */
750     mp4vparse->sent_codec_tag = TRUE;
751   }
752 
753   if ((event = check_pending_key_unit_event (mp4vparse->force_key_unit_event,
754               &parse->segment, GST_BUFFER_TIMESTAMP (buffer),
755               GST_BUFFER_FLAGS (buffer), mp4vparse->pending_key_unit_ts))) {
756     gst_mpeg4vparse_prepare_key_unit (mp4vparse, event);
757     push_codec = TRUE;
758   }
759 
760   /* periodic config sending */
761   if (mp4vparse->interval > 0 || push_codec) {
762     GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
763     guint64 diff;
764 
765     /* init */
766     if (!GST_CLOCK_TIME_IS_VALID (mp4vparse->last_report)) {
767       mp4vparse->last_report = timestamp;
768     }
769 
770     if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
771       if (timestamp > mp4vparse->last_report)
772         diff = timestamp - mp4vparse->last_report;
773       else
774         diff = 0;
775 
776       GST_LOG_OBJECT (mp4vparse,
777           "now %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
778           GST_TIME_ARGS (timestamp), GST_TIME_ARGS (mp4vparse->last_report));
779 
780       GST_LOG_OBJECT (mp4vparse,
781           "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
782 
783       if (GST_TIME_AS_SECONDS (diff) >= mp4vparse->interval || push_codec) {
784         GstMapInfo cmap;
785         gsize csize;
786         gboolean diffconf;
787 
788         /* we need to send config now first */
789         GST_INFO_OBJECT (parse, "inserting config in stream");
790         gst_buffer_map (mp4vparse->config, &cmap, GST_MAP_READ);
791         diffconf = (gst_buffer_get_size (buffer) < cmap.size)
792             || gst_buffer_memcmp (buffer, 0, cmap.data, cmap.size);
793         csize = cmap.size;
794         gst_buffer_unmap (mp4vparse->config, &cmap);
795 
796         /* avoid inserting duplicate config */
797         if (diffconf) {
798           GstBuffer *superbuf;
799 
800           /* insert header */
801           superbuf =
802               gst_buffer_append (gst_buffer_ref (mp4vparse->config),
803               gst_buffer_ref (buffer));
804           gst_buffer_copy_into (superbuf, buffer, GST_BUFFER_COPY_METADATA, 0,
805               csize);
806           gst_buffer_replace (&frame->out_buffer, superbuf);
807           gst_buffer_unref (superbuf);
808         } else {
809           GST_INFO_OBJECT (parse, "... but avoiding duplication");
810         }
811 
812         if (G_UNLIKELY (timestamp != -1)) {
813           mp4vparse->last_report = timestamp;
814         }
815       }
816     }
817   }
818 
819   return GST_FLOW_OK;
820 }
821 
822 static gboolean
gst_mpeg4vparse_set_caps(GstBaseParse * parse,GstCaps * caps)823 gst_mpeg4vparse_set_caps (GstBaseParse * parse, GstCaps * caps)
824 {
825   GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
826   GstStructure *s;
827   const GValue *value;
828   GstBuffer *buf;
829   GstMapInfo map;
830   guint8 *data;
831   gsize size;
832 
833   GstMpeg4Packet packet;
834   GstMpeg4ParseResult res;
835 
836   GST_DEBUG_OBJECT (parse, "setcaps called with %" GST_PTR_FORMAT, caps);
837 
838   s = gst_caps_get_structure (caps, 0);
839 
840   if ((value = gst_structure_get_value (s, "codec_data")) != NULL
841       && (buf = gst_value_get_buffer (value))) {
842     /* best possible parse attempt,
843      * src caps are based on sink caps so it will end up in there
844      * whether sucessful or not */
845     gst_buffer_map (buf, &map, GST_MAP_READ);
846     data = map.data;
847     size = map.size;
848     res = gst_mpeg4_parse (&packet, FALSE, NULL, data, 0, size);
849 
850     while (res == GST_MPEG4_PARSER_OK || res == GST_MPEG4_PARSER_NO_PACKET_END) {
851 
852       if (packet.type >= GST_MPEG4_VIDEO_LAYER_FIRST &&
853           packet.type <= GST_MPEG4_VIDEO_LAYER_LAST)
854         mp4vparse->vol_offset = packet.offset;
855 
856       else if (packet.type == GST_MPEG4_VISUAL_OBJ) {
857         gst_mpeg4_parse_visual_object (&mp4vparse->vo, NULL,
858             data + packet.offset, MIN (packet.size, size));
859         mp4vparse->vo_found = TRUE;
860       }
861 
862       res = gst_mpeg4_parse (&packet, FALSE, NULL, data, packet.offset, size);
863     }
864 
865     /* And take it as config */
866     gst_mpeg4vparse_process_config (mp4vparse, data, 3, size);
867     gst_buffer_unmap (buf, &map);
868     gst_mpeg4vparse_reset_frame (mp4vparse);
869   }
870 
871   /* let's not interfere and accept regardless of config parsing success */
872   return TRUE;
873 }
874 
875 static void
remove_fields(GstCaps * caps)876 remove_fields (GstCaps * caps)
877 {
878   guint i, n;
879 
880   n = gst_caps_get_size (caps);
881   for (i = 0; i < n; i++) {
882     GstStructure *s = gst_caps_get_structure (caps, i);
883 
884     gst_structure_remove_field (s, "parsed");
885   }
886 }
887 
888 
889 static GstCaps *
gst_mpeg4vparse_get_caps(GstBaseParse * parse,GstCaps * filter)890 gst_mpeg4vparse_get_caps (GstBaseParse * parse, GstCaps * filter)
891 {
892   GstCaps *peercaps, *templ;
893   GstCaps *res;
894 
895   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
896   if (filter) {
897     GstCaps *fcopy = gst_caps_copy (filter);
898     /* Remove the fields we convert */
899     remove_fields (fcopy);
900     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
901     gst_caps_unref (fcopy);
902   } else
903     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
904 
905   if (peercaps) {
906     /* Remove the parsed field */
907     peercaps = gst_caps_make_writable (peercaps);
908     remove_fields (peercaps);
909 
910     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
911     gst_caps_unref (peercaps);
912     gst_caps_unref (templ);
913   } else {
914     res = templ;
915   }
916 
917   if (filter) {
918     GstCaps *tmp = gst_caps_intersect_full (res, filter,
919         GST_CAPS_INTERSECT_FIRST);
920     gst_caps_unref (res);
921     res = tmp;
922   }
923 
924 
925   return res;
926 }
927 
928 static gboolean
gst_mpeg4vparse_event(GstBaseParse * parse,GstEvent * event)929 gst_mpeg4vparse_event (GstBaseParse * parse, GstEvent * event)
930 {
931   gboolean res;
932   GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
933 
934   switch (GST_EVENT_TYPE (event)) {
935     case GST_EVENT_CUSTOM_DOWNSTREAM:
936     {
937       GstClockTime timestamp, stream_time, running_time;
938       gboolean all_headers;
939       guint count;
940 
941       if (gst_video_event_is_force_key_unit (event)) {
942         gst_video_event_parse_downstream_force_key_unit (event,
943             &timestamp, &stream_time, &running_time, &all_headers, &count);
944 
945         GST_INFO_OBJECT (mp4vparse, "received downstream force key unit event, "
946             "seqnum %d running_time %" GST_TIME_FORMAT
947             " all_headers %d count %d", gst_event_get_seqnum (event),
948             GST_TIME_ARGS (running_time), all_headers, count);
949 
950         if (mp4vparse->force_key_unit_event) {
951           GST_INFO_OBJECT (mp4vparse, "ignoring force key unit event "
952               "as one is already queued");
953         } else {
954           mp4vparse->pending_key_unit_ts = running_time;
955           gst_event_replace (&mp4vparse->force_key_unit_event, event);
956         }
957         gst_event_unref (event);
958         res = TRUE;
959       } else {
960         res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse, event);
961       }
962       break;
963     }
964     default:
965       res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse, event);
966       break;
967   }
968   return res;
969 }
970 
971 static gboolean
gst_mpeg4vparse_src_event(GstBaseParse * parse,GstEvent * event)972 gst_mpeg4vparse_src_event (GstBaseParse * parse, GstEvent * event)
973 {
974   gboolean res;
975   GstMpeg4VParse *mp4vparse = GST_MPEG4VIDEO_PARSE (parse);
976 
977   switch (GST_EVENT_TYPE (event)) {
978     case GST_EVENT_CUSTOM_UPSTREAM:
979     {
980       GstClockTime running_time;
981       gboolean all_headers;
982       guint count;
983 
984       if (gst_video_event_is_force_key_unit (event)) {
985         gst_video_event_parse_upstream_force_key_unit (event,
986             &running_time, &all_headers, &count);
987 
988         GST_INFO_OBJECT (mp4vparse, "received upstream force-key-unit event, "
989             "seqnum %d running_time %" GST_TIME_FORMAT
990             " all_headers %d count %d", gst_event_get_seqnum (event),
991             GST_TIME_ARGS (running_time), all_headers, count);
992 
993         if (all_headers) {
994           mp4vparse->pending_key_unit_ts = running_time;
995           gst_event_replace (&mp4vparse->force_key_unit_event, event);
996         }
997       }
998       res = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
999       break;
1000     }
1001     default:
1002       res = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
1003       break;
1004   }
1005 
1006   return res;
1007 }
1008