Debian Codegraph @

gst/videoparsers/gstmpeg4videoparse.c

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