Debian Codegraph @

gst/videoparsers/gstmpegvideoparse.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) <2007> Jan Schmidt <[email protected]>
3 * Copyright (C) <2011> Mark Nauwelaerts <[email protected]>
4 * Copyright (C) <2011> Thibault Saunier <[email protected]>
5 * Copyright (C) <2011> Collabora ltd
6 * Copyright (C) <2011> Nokia Corporation
7 * Copyright (C) <2011> Intel Corporation
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <string.h>
30 #include <gst/base/base.h>
31 #include <gst/pbutils/pbutils.h>
32 #include <gst/codecparsers/gstmpegvideometa.h>
33
34 #include "gstvideoparserselements.h"
35 #include "gstmpegvideoparse.h"
36
37 GST_DEBUG_CATEGORY (mpegv_parse_debug);
38 #define GST_CAT_DEFAULT mpegv_parse_debug
39
40 static GstStaticPadTemplate src_template =
41 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
42 GST_PAD_ALWAYS,
43 GST_STATIC_CAPS ("video/mpeg, "
44 "mpegversion = (int) [1, 2], "
45 "parsed = (boolean) true, " "systemstream = (boolean) false")
46 );
47
48 static GstStaticPadTemplate sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
50 GST_PAD_ALWAYS,
51 GST_STATIC_CAPS ("video/mpeg, "
52 "mpegversion = (int) [1, 2], " "systemstream = (boolean) false")
53 );
54
55 /* Properties */
56 #define DEFAULT_PROP_DROP TRUE
57 #define DEFAULT_PROP_GOP_SPLIT FALSE
58
59 enum
60 {
61 PROP_0,
62 PROP_DROP,
63 PROP_GOP_SPLIT
64 };
65
66 #define parent_class gst_mpegv_parse_parent_class
67 G_DEFINE_TYPE (GstMpegvParse, gst_mpegv_parse, GST_TYPE_BASE_PARSE);
68 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (mpegvideoparse, "mpegvideoparse",
69 GST_RANK_PRIMARY + 1, GST_TYPE_MPEGVIDEO_PARSE,
70 videoparsers_element_init (plugin));
71
72 static gboolean gst_mpegv_parse_start (GstBaseParse * parse);
73 static gboolean gst_mpegv_parse_stop (GstBaseParse * parse);
74 static GstFlowReturn gst_mpegv_parse_handle_frame (GstBaseParse * parse,
75 GstBaseParseFrame * frame, gint * skipsize);
76 static GstFlowReturn gst_mpegv_parse_parse_frame (GstBaseParse * parse,
77 GstBaseParseFrame * frame);
78 static gboolean gst_mpegv_parse_set_caps (GstBaseParse * parse, GstCaps * caps);
79 static GstCaps *gst_mpegv_parse_get_caps (GstBaseParse * parse,
80 GstCaps * filter);
81 static GstFlowReturn gst_mpegv_parse_pre_push_frame (GstBaseParse * parse,
82 GstBaseParseFrame * frame);
83 static gboolean gst_mpegv_parse_sink_query (GstBaseParse * parse,
84 GstQuery * query);
85
86 static void gst_mpegv_parse_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_mpegv_parse_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
90
91 static void
92 gst_mpegv_parse_set_property (GObject * object, guint property_id,
93 const GValue * value, GParamSpec * pspec)
94 {
95 GstMpegvParse *parse = GST_MPEGVIDEO_PARSE (object);
96
97 switch (property_id) {
98 case PROP_DROP:
99 parse->drop = g_value_get_boolean (value);
100 break;
101 case PROP_GOP_SPLIT:
102 parse->gop_split = g_value_get_boolean (value);
103 break;
104 default:
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
106 }
107 }
108
109 static void
110 gst_mpegv_parse_get_property (GObject * object, guint property_id,
111 GValue * value, GParamSpec * pspec)
112 {
113 GstMpegvParse *parse = GST_MPEGVIDEO_PARSE (object);
114
115 switch (property_id) {
116 case PROP_DROP:
117 g_value_set_boolean (value, parse->drop);
118 break;
119 case PROP_GOP_SPLIT:
120 g_value_set_boolean (value, parse->gop_split);
121 break;
122 default:
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
124 }
125 }
126
127 static void
128 gst_mpegv_parse_class_init (GstMpegvParseClass * klass)
129 {
130 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
131 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
132 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
133
134 GST_DEBUG_CATEGORY_INIT (mpegv_parse_debug, "mpegvideoparse", 0,
135 "MPEG-1/2 video parser");
136
137 parent_class = g_type_class_peek_parent (klass);
138
139 gobject_class->set_property = gst_mpegv_parse_set_property;
140 gobject_class->get_property = gst_mpegv_parse_get_property;
141
142 g_object_class_install_property (gobject_class, PROP_DROP,
143 g_param_spec_boolean ("drop", "drop",
144 "Drop data until valid configuration data is received either "
145 "in the stream or through caps", DEFAULT_PROP_DROP,
146 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
147
148 g_object_class_install_property (gobject_class, PROP_GOP_SPLIT,
149 g_param_spec_boolean ("gop-split", "gop-split",
150 "Split frame when encountering GOP", DEFAULT_PROP_GOP_SPLIT,
151 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152
153 gst_element_class_add_static_pad_template (element_class, &src_template);
154 gst_element_class_add_static_pad_template (element_class, &sink_template);
155
156 gst_element_class_set_static_metadata (element_class,
157 "MPEG video elementary stream parser",
158 "Codec/Parser/Video",
159 "Parses and frames MPEG-1 and MPEG-2 elementary video streams",
160 "Wim Taymans <[email protected]>, "
161 "Jan Schmidt <[email protected]>, "
162 "Mark Nauwelaerts <[email protected]>");
163
164 /* Override BaseParse vfuncs */
165 parse_class->start = GST_DEBUG_FUNCPTR (gst_mpegv_parse_start);
166 parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpegv_parse_stop);
167 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_mpegv_parse_handle_frame);
168 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_mpegv_parse_set_caps);
169 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_mpegv_parse_get_caps);
170 parse_class->pre_push_frame =
171 GST_DEBUG_FUNCPTR (gst_mpegv_parse_pre_push_frame);
172 parse_class->sink_query = GST_DEBUG_FUNCPTR (gst_mpegv_parse_sink_query);
173 }
174
175 static void
176 gst_mpegv_parse_init (GstMpegvParse * parse)
177 {
178 parse->config_flags = FLAG_NONE;
179
180 gst_base_parse_set_pts_interpolation (GST_BASE_PARSE (parse), FALSE);
181 gst_base_parse_set_infer_ts (GST_BASE_PARSE (parse), FALSE);
182 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (parse));
183 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (parse));
184 }
185
186 static void
187 gst_mpegv_parse_reset_frame (GstMpegvParse * mpvparse)
188 {
189 /* done parsing; reset state */
190 mpvparse->last_sc = -1;
191 mpvparse->seq_size = 0;
192 mpvparse->seq_offset = -1;
193 mpvparse->pic_offset = -1;
194 mpvparse->frame_repeat_count = 0;
195 memset (mpvparse->ext_offsets, 0, sizeof (mpvparse->ext_offsets));
196 mpvparse->ext_count = 0;
197 mpvparse->slice_count = 0;
198 mpvparse->slice_offset = 0;
199 gst_video_clear_user_data (&mpvparse->user_data, FALSE);
200 }
201
202 static void
203 gst_mpegv_parse_reset (GstMpegvParse * mpvparse)
204 {
205 gst_mpegv_parse_reset_frame (mpvparse);
206 mpvparse->profile = 0;
207 mpvparse->update_caps = TRUE;
208 mpvparse->send_codec_tag = TRUE;
209 mpvparse->send_mpeg_meta = TRUE;
210
211 gst_buffer_replace (&mpvparse->config, NULL);
212 memset (&mpvparse->sequencehdr, 0, sizeof (mpvparse->sequencehdr));
213 memset (&mpvparse->sequenceext, 0, sizeof (mpvparse->sequenceext));
214 memset (&mpvparse->sequencedispext, 0, sizeof (mpvparse->sequencedispext));
215 memset (&mpvparse->pichdr, 0, sizeof (mpvparse->pichdr));
216 memset (&mpvparse->picext, 0, sizeof (mpvparse->picext));
217
218 mpvparse->seqhdr_updated = FALSE;
219 mpvparse->seqext_updated = FALSE;
220 mpvparse->seqdispext_updated = FALSE;
221 mpvparse->picext_updated = FALSE;
222 mpvparse->quantmatrext_updated = FALSE;
223 }
224
225 static gboolean
226 gst_mpegv_parse_sink_query (GstBaseParse * parse, GstQuery * query)
227 {
228 gboolean res;
229 GstMpegvParse *mpvparse = GST_MPEGVIDEO_PARSE (parse);
230
231 res = GST_BASE_PARSE_CLASS (parent_class)->sink_query (parse, query);
232
233 if (res && GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
234 mpvparse->send_mpeg_meta =
235 gst_query_find_allocation_meta (query, GST_MPEG_VIDEO_META_API_TYPE,
236 NULL);
237
238 GST_DEBUG_OBJECT (parse, "Downstream can handle GstMpegVideo GstMeta : %d",
239 mpvparse->send_mpeg_meta);
240 }
241
242 return res;
243 }
244
245 static gboolean
246 gst_mpegv_parse_start (GstBaseParse * parse)
247 {
248 GstMpegvParse *mpvparse = GST_MPEGVIDEO_PARSE (parse);
249
250 GST_DEBUG_OBJECT (parse, "start");
251
252 gst_mpegv_parse_reset (mpvparse);
253 /* at least this much for a valid frame */
254 gst_base_parse_set_min_frame_size (parse, 6);
255
256 return TRUE;
257 }
258
259 static gboolean
260 gst_mpegv_parse_stop (GstBaseParse * parse)
261 {
262 GstMpegvParse *mpvparse = GST_MPEGVIDEO_PARSE (parse);
263
264 GST_DEBUG_OBJECT (parse, "stop");
265
266 gst_mpegv_parse_reset (mpvparse);
267
268 return TRUE;
269 }
270
271 static gboolean
272 gst_mpegv_parse_process_config (GstMpegvParse * mpvparse, GstMapInfo * info,
273 guint size)
274 {
275 GstMpegVideoPacket packet;
276 guint8 *data_with_prefix;
277 gint i;
278
279 if (mpvparse->seq_offset < 4) {
280 /* This shouldn't happen, but just in case... */
281 GST_WARNING_OBJECT (mpvparse, "Sequence header start code missing.");
282 return FALSE;
283 }
284
285 packet.data = info->data;
286 packet.type = GST_MPEG_VIDEO_PACKET_SEQUENCE;
287 packet.offset = mpvparse->seq_offset;
288 packet.size = size - mpvparse->seq_offset;
289 /* pointer to sequence header data including the start code prefix -
290 used for codec private data */
291 data_with_prefix = (guint8 *) packet.data + packet.offset - 4;
292
293 /* only do stuff if something new; only compare first 8 bytes, changes in
294 quantiser matrix or bitrate don't matter here. Also changing the
295 matrices in codec_data seems to cause problem with decoders */
296 if (mpvparse->config &&
297 gst_buffer_memcmp (mpvparse->config, 0, data_with_prefix, MIN (size,
298 8)) == 0) {
299 return TRUE;
300 }
301
302 if (!gst_mpeg_video_packet_parse_sequence_header (&packet,
303 &mpvparse->sequencehdr)) {
304 GST_DEBUG_OBJECT (mpvparse,
305 "failed to parse config data (size %d) at offset %d",
306 size, mpvparse->seq_offset);
307 return FALSE;
308 }
309
310 mpvparse->seqhdr_updated = TRUE;
311
312 GST_LOG_OBJECT (mpvparse, "accepting parsed config size %d", size);
313
314 /* Set mpeg version, and parse sequence extension */
315 mpvparse->config_flags = FLAG_NONE;
316 for (i = 0; i < mpvparse->ext_count; ++i) {
317 packet.type = GST_MPEG_VIDEO_PACKET_EXTENSION;
318 packet.offset = mpvparse->ext_offsets[i];
319 packet.size = (gint) size - mpvparse->ext_offsets[i];
320 mpvparse->config_flags |= FLAG_MPEG2;
321 if (packet.size > 1) {
322 switch (packet.data[packet.offset] >> 4) {
323 case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE:
324 if (gst_mpeg_video_packet_parse_sequence_extension (&packet,
325 &mpvparse->sequenceext)) {
326 GST_LOG_OBJECT (mpvparse, "Read Sequence Extension");
327 mpvparse->config_flags |= FLAG_SEQUENCE_EXT;
328 mpvparse->seqext_updated = TRUE;
329 }
330 break;
331 case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE_DISPLAY:
332 if (gst_mpeg_video_packet_parse_sequence_display_extension (&packet,
333 &mpvparse->sequencedispext)) {
334 GST_LOG_OBJECT (mpvparse, "Read Sequence Display Extension");
335 mpvparse->config_flags |= FLAG_SEQUENCE_DISPLAY_EXT;
336 mpvparse->seqdispext_updated = TRUE;
337 }
338 break;
339 case GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX:
340 if (gst_mpeg_video_packet_parse_quant_matrix_extension (&packet,
341 &mpvparse->quantmatrext)) {
342 GST_LOG_OBJECT (mpvparse, "Read Quantization Matrix Extension");
343 mpvparse->quantmatrext_updated = TRUE;
344 }
345 break;
346 }
347 }
348 }
349 if (mpvparse->config_flags & FLAG_MPEG2) {
350 /* Update the sequence header based on extensions */
351 GstMpegVideoSequenceExt *seqext = NULL;
352 GstMpegVideoSequenceDisplayExt *seqdispext = NULL;
353
354 if (mpvparse->config_flags & FLAG_SEQUENCE_EXT)
355 seqext = &mpvparse->sequenceext;
356 if (mpvparse->config_flags & FLAG_SEQUENCE_DISPLAY_EXT)
357 seqdispext = &mpvparse->sequencedispext;
358
359 gst_mpeg_video_finalise_mpeg2_sequence_header (&mpvparse->sequencehdr,
360 seqext, seqdispext);
361 }
362
363 if (mpvparse->fps_num == 0 || mpvparse->fps_den == 0) {
364 mpvparse->fps_num = mpvparse->sequencehdr.fps_n;
365 mpvparse->fps_den = mpvparse->sequencehdr.fps_d;
366 }
367
368 /* parsing ok, so accept it as new config */
369 if (mpvparse->config != NULL)
370 gst_buffer_unref (mpvparse->config);
371
372 mpvparse->config = gst_buffer_new_and_alloc (size);
373 gst_buffer_fill (mpvparse->config, 0, data_with_prefix, size);
374
375 /* trigger src caps update */
376 mpvparse->update_caps = TRUE;
377
378 return TRUE;
379 }
380
381 /* FIXME : Move these functions to libgstcodecparser for usage by
382 * more elements/code */
383 #ifndef GST_DISABLE_GST_DEBUG
384 static const gchar *
385 picture_start_code_name (guint8 psc)
386 {
387 guint i;
388 const struct
389 {
390 guint8 psc;
391 const gchar *name;
392 } psc_names[] = {
393 {
394 0x00, "Picture Start"}, {
395 0xb0, "Reserved"}, {
396 0xb1, "Reserved"}, {
397 0xb2, "User Data Start"}, {
398 0xb3, "Sequence Header Start"}, {
399 0xb4, "Sequence Error"}, {
400 0xb5, "Extension Start"}, {
401 0xb6, "Reserved"}, {
402 0xb7, "Sequence End"}, {
403 0xb8, "Group Start"}, {
404 0xb9, "Program End"}
405 };
406 if (psc < 0xB0 && psc > 0)
407 return "Slice Start";
408
409 for (i = 0; i < G_N_ELEMENTS (psc_names); i++)
410 if (psc_names[i].psc == psc)
411 return psc_names[i].name;
412
413 return "UNKNOWN";
414 };
415
416 static const gchar *
417 picture_type_name (guint8 pct)
418 {
419 guint i;
420 const struct
421 {
422 guint8 pct;
423 const gchar *name;
424 } pct_names[] = {
425 {
426 0, "Forbidden"}, {
427 1, "I Frame"}, {
428 2, "P Frame"}, {
429 3, "B Frame"}, {
430 4, "DC Intra Coded (Shall Not Be Used!)"}
431 };
432
433 for (i = 0; i < G_N_ELEMENTS (pct_names); i++)
434 if (pct_names[i].pct == pct)
435 return pct_names[i].name;
436
437 return "Reserved/Unknown";
438 }
439 #endif /* GST_DISABLE_GST_DEBUG */
440
441 static void
442 parse_packet_extension (GstMpegvParse * mpvparse, GstMapInfo * info, guint off)
443 {
444 GstMpegVideoPacket packet;
445
446 packet.data = info->data;
447 packet.type = GST_MPEG_VIDEO_PACKET_EXTENSION;
448 packet.offset = off;
449 packet.size = info->size - off;
450
451 /* FIXME : WE ARE ASSUMING IT IS A *PICTURE* EXTENSION */
452 if (gst_mpeg_video_packet_parse_picture_extension (&packet,
453 &mpvparse->picext)) {
454 mpvparse->frame_repeat_count = 1;
455
456 if (mpvparse->picext.repeat_first_field) {
457 if (mpvparse->sequenceext.progressive) {
458 if (mpvparse->picext.top_field_first)
459 mpvparse->frame_repeat_count = 5;
460 else
461 mpvparse->frame_repeat_count = 3;
462 } else if (mpvparse->picext.progressive_frame) {
463 mpvparse->frame_repeat_count = 2;
464 }
465 }
466 mpvparse->picext_updated = TRUE;
467 }
468 }
469
470 static void
471 parse_user_data_packet (GstMpegvParse * mpvparse, const guint8 * data,
472 guint size)
473 {
474 GstByteReader br;
475 GstVideoParseUtilsField field = GST_VIDEO_PARSE_UTILS_FIELD_1;
476 gst_byte_reader_init (&br, data, size);
477
478 if (mpvparse->picext.picture_structure ==
479 (guint8) GST_MPEG_VIDEO_PICTURE_STRUCTURE_BOTTOM_FIELD)
480 field = GST_VIDEO_PARSE_UTILS_FIELD_2;
481 gst_video_parse_user_data ((GstElement *) mpvparse, &mpvparse->user_data, &br,
482 field, ITU_T_T35_MANUFACTURER_US_ATSC);
483
484 }
485
486 /* caller guarantees at least start code in @buf at @off ( - 4)*/
487 /* for off == 4 initial code; returns TRUE if code starts a frame
488 * otherwise returns TRUE if code terminates preceding frame */
489 static gboolean
490 gst_mpegv_parse_process_sc (GstMpegvParse * mpvparse,
491 GstMapInfo * info, gint off, GstMpegVideoPacket * packet,
492 gboolean * need_more)
493 {
494 gboolean ret = FALSE, checkconfig = TRUE;
495
496 GST_LOG_OBJECT (mpvparse, "process startcode %x (%s) offset:%d", packet->type,
497 picture_start_code_name (packet->type), off);
498
499 *need_more = FALSE;
500
501 switch (packet->type) {
502 case GST_MPEG_VIDEO_PACKET_PICTURE:
503 GST_LOG_OBJECT (mpvparse, "startcode is PICTURE");
504 /* picture is aggregated with preceding sequence/gop, if any.
505 * so, picture start code only ends if already a previous one */
506 if (mpvparse->pic_offset < 0)
507 mpvparse->pic_offset = off;
508 else
509 ret = (off != mpvparse->pic_offset);
510 /* but it's a valid starting one */
511 if (off == 4)
512 ret = TRUE;
513 break;
514 case GST_MPEG_VIDEO_PACKET_SEQUENCE:
515 GST_LOG_OBJECT (mpvparse, "startcode is SEQUENCE");
516 if (mpvparse->seq_offset < 0)
517 mpvparse->seq_offset = off;
518 ret = TRUE;
519 break;
520 case GST_MPEG_VIDEO_PACKET_GOP:
521 GST_LOG_OBJECT (mpvparse, "startcode is GOP");
522 if (mpvparse->seq_offset >= 0)
523 ret = mpvparse->gop_split;
524 else
525 ret = TRUE;
526 break;
527 case GST_MPEG_VIDEO_PACKET_EXTENSION:
528 mpvparse->config_flags |= FLAG_MPEG2;
529 GST_LOG_OBJECT (mpvparse, "startcode is VIDEO PACKET EXTENSION");
530 if (mpvparse->pic_offset >= 0) {
531 GST_LOG_OBJECT (mpvparse, "... considered PICTURE EXTENSION");
532 parse_packet_extension (mpvparse, info, off);
533 } else {
534 GST_LOG_OBJECT (mpvparse, "... considered SEQUENCE EXTENSION");
535 if (mpvparse->ext_count < G_N_ELEMENTS (mpvparse->ext_offsets))
536 mpvparse->ext_offsets[mpvparse->ext_count++] = off;
537 }
538 checkconfig = FALSE;
539 break;
540 case GST_MPEG_VIDEO_PACKET_USER_DATA:
541 GST_LOG_OBJECT (mpvparse, "USER_DATA packet of %d bytes", packet->size);
542
543 if (packet->size < 0) {
544 GST_LOG_OBJECT (mpvparse, "no size yet, need more data");
545 *need_more = TRUE;
546 return FALSE;
547 }
548
549 parse_user_data_packet (mpvparse, info->data + off, packet->size);
550 checkconfig = FALSE;
551 break;
552 default:
553 if (GST_MPEG_VIDEO_PACKET_IS_SLICE (packet->type)) {
554 mpvparse->slice_count++;
555 if (mpvparse->slice_offset == 0)
556 mpvparse->slice_offset = off - 4;
557 }
558 checkconfig = FALSE;
559 break;
560 }
561
562 /* set size to avoid processing config again */
563 if (checkconfig && mpvparse->seq_offset >= 0 && off != mpvparse->seq_offset &&
564 !mpvparse->seq_size) {
565 /* should always be at start */
566 g_assert (mpvparse->seq_offset <= 4);
567 gst_mpegv_parse_process_config (mpvparse, info, off - mpvparse->seq_offset);
568 mpvparse->seq_size = off - mpvparse->seq_offset;
569 }
570
571 /* extract some picture info if there is any in the frame being terminated */
572 if (ret && mpvparse->pic_offset >= 0 && mpvparse->pic_offset < off) {
573 GstMpegVideoPacket header;
574
575 header.data = info->data;
576 header.type = GST_MPEG_VIDEO_PACKET_PICTURE;
577 header.offset = mpvparse->pic_offset;
578 header.size = info->size - mpvparse->pic_offset;
579 if (gst_mpeg_video_packet_parse_picture_header (&header, &mpvparse->pichdr))
580 GST_LOG_OBJECT (mpvparse, "picture_coding_type %d (%s), ending"
581 "frame of size %d", mpvparse->pichdr.pic_type,
582 picture_type_name (mpvparse->pichdr.pic_type), off - 4);
583 else
584 GST_LOG_OBJECT (mpvparse, "Couldn't parse picture at offset %d",
585 mpvparse->pic_offset);
586
587 /* if terminating packet is a picture, we need to check if it has same TSN as the picture that is being
588 terminated. If it does, we need to keep those together, as these packets are two fields of the same
589 frame */
590 if (packet->type == GST_MPEG_VIDEO_PACKET_PICTURE
591 && (mpvparse->config_flags & FLAG_SEQUENCE_EXT)
592 && !mpvparse->sequenceext.progressive) {
593 if (info->size - off < 2) { /* we need at least two bytes to read the TSN */
594 *need_more = TRUE;
595 ret = FALSE;
596 } else {
597 /* TSN is stored in first 10 bits */
598 int tsn = info->data[off] << 2 | (info->data[off + 1] & 0xC0) >> 6;
599
600 if (tsn == mpvparse->pichdr.tsn) /* prevent termination if TSN is same */
601 ret = FALSE;
602 }
603 }
604 }
605
606 return ret;
607 }
608
609 /* FIXME move into baseparse, or anything equivalent;
610 * see https://bugzilla.gnome.org/show_bug.cgi?id=650093
611 * #define GST_BASE_PARSE_FRAME_FLAG_PARSING 0x100000 */
612
613 static inline void
614 update_frame_parsing_status (GstMpegvParse * mpvparse,
615 GstBaseParseFrame * frame)
616 {
617 /* avoid stale cached parsing state */
618 if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_NEW_FRAME) {
619 GST_LOG_OBJECT (mpvparse, "parsing new frame");
620 gst_mpegv_parse_reset_frame (mpvparse);
621 } else {
622 GST_LOG_OBJECT (mpvparse, "resuming frame parsing");
623 }
624 }
625
626 static GstFlowReturn
627 gst_mpegv_parse_handle_frame (GstBaseParse * parse,
628 GstBaseParseFrame * frame, gint * skipsize)
629 {
630 GstFlowReturn flowret = GST_FLOW_OK;
631 GstMpegvParse *mpvparse = GST_MPEGVIDEO_PARSE (parse);
632 GstBuffer *buf = frame->buffer;
633 gboolean ret = FALSE;
634 gint off = 0;
635 GstMpegVideoPacket packet;
636 guint8 *data;
637 gsize size;
638 gboolean need_more = FALSE;
639 GstMapInfo map;
640
641 update_frame_parsing_status (mpvparse, frame);
642
643 gst_buffer_map (buf, &map, GST_MAP_READ);
644 data = map.data;
645 size = map.size;
646
647 retry:
648 /* at least start code and subsequent byte */
649 if (G_UNLIKELY (size < 5 + off))
650 goto exit;
651
652 /* if already found a previous start code, e.g. start of frame, go for next */
653 if (mpvparse->last_sc >= 0) {
654 packet.offset = mpvparse->last_sc;
655 packet.size = 0;
656 goto next;
657 }
658
659 if (!gst_mpeg_video_parse (&packet, data, size, off)) {
660 /* didn't find anything that looks like a sync word, skip */
661 GST_LOG_OBJECT (mpvparse, "no start code found");
662 *skipsize = size - 3;
663 goto exit;
664 }
665
666 off = packet.offset - 4;
667 GST_LOG_OBJECT (mpvparse, "possible sync at buffer offset %d", off);
668
669 /* possible frame header, but not at offset 0? skip bytes before sync */
670 if (G_UNLIKELY (off > 0)) {
671 *skipsize = off;
672 goto exit;
673 }
674
675 /* note: initial start code is assumed at offset 0 by subsequent code */
676
677 /* examine start code, see if it looks like an initial start code */
678 if (gst_mpegv_parse_process_sc (mpvparse, &map, 4, &packet, &need_more)) {
679 /* found sc */
680 GST_LOG_OBJECT (mpvparse, "valid start code found");
681 mpvparse->last_sc = 0;
682 } else {
683 off++;
684 gst_mpegv_parse_reset_frame (mpvparse);
685 GST_LOG_OBJECT (mpvparse, "invalid start code");
686 goto retry;
687 }
688
689 next:
690 /* start is fine as of now */
691 *skipsize = 0;
692 /* terminating start code may have been found in prev scan already */
693 if (((gint) packet.size) >= 0) {
694 off = packet.offset + packet.size;
695 /* so now we have start code at start of data; locate next start code */
696 if (!gst_mpeg_video_parse (&packet, data, size, off)) {
697 off = -1;
698 } else {
699 g_assert (packet.offset >= 4);
700 off = packet.offset - 4;
701 }
702 } else {
703 off = -1;
704 }
705
706 GST_LOG_OBJECT (mpvparse, "next start code at %d", off);
707 if (off < 0) {
708 off = size - 3;
709 goto need_more;
710 } else {
711 /* decide whether this startcode ends a frame */
712 ret = gst_mpegv_parse_process_sc (mpvparse, &map, off + 4, &packet,
713 &need_more);
714 /* in rare cases, might need to peek past start code */
715 if (need_more) {
716 GST_LOG_OBJECT (mpvparse, "need more data (past start code");
717 ret = FALSE;
718 goto need_more;
719 }
720 }
721
722 if (!ret)
723 goto next;
724
725 exit:
726 gst_buffer_unmap (buf, &map);
727
728 if (ret) {
729 GstFlowReturn res;
730
731 *skipsize = 0;
732 g_assert (off <= size);
733 res = gst_mpegv_parse_parse_frame (parse, frame);
734 if (res == GST_BASE_PARSE_FLOW_DROPPED)
735 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_DROP;
736 flowret = gst_base_parse_finish_frame (parse, frame, off);
737 /* Reset local information */
738 mpvparse->seqhdr_updated = FALSE;
739 mpvparse->seqext_updated = FALSE;
740 mpvparse->seqdispext_updated = FALSE;
741 mpvparse->picext_updated = FALSE;
742 mpvparse->quantmatrext_updated = FALSE;
743 }
744
745 return flowret;
746
747 need_more:
748 /* if draining, take all */
749 if (GST_BASE_PARSE_DRAINING (parse)) {
750 GST_LOG_OBJECT (mpvparse, "draining, accepting all data");
751 off = size;
752 ret = TRUE;
753 } else {
754 GST_LOG_OBJECT (mpvparse, "need more data");
755 /* resume scan where we left it */
756 mpvparse->last_sc = off;
757 /* request best next available */
758 off = G_MAXUINT;
759 }
760 goto exit;
761 }
762
763 static void
764 gst_mpegv_parse_update_src_caps (GstMpegvParse * mpvparse)
765 {
766 GstCaps *caps = NULL;
767 GstStructure *s = NULL;
768
769 /* only update if no src caps yet or explicitly triggered */
770 if (G_LIKELY (gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (mpvparse)) &&
771 !mpvparse->update_caps))
772 return;
773
774 /* carry over input caps as much as possible; override with our own stuff */
775 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (mpvparse));
776 if (caps) {
777 caps = gst_caps_make_writable (caps);
778 s = gst_caps_get_structure (caps, 0);
779 } else {
780 caps = gst_caps_new_empty_simple ("video/mpeg");
781 }
782
783 /* typically we don't output buffers until we have properly parsed some
784 * config data, so we should at least know about version.
785 * If not, it means it has been requested not to drop data, and
786 * upstream and/or app must know what they are doing ... */
787 gst_caps_set_simple (caps,
788 "mpegversion", G_TYPE_INT, (mpvparse->config_flags & FLAG_MPEG2) ? 2 : 1,
789 NULL);
790
791 gst_caps_set_simple (caps, "systemstream", G_TYPE_BOOLEAN, FALSE,
792 "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
793
794 if (mpvparse->sequencehdr.width > 0 && mpvparse->sequencehdr.height > 0) {
795 GstMpegVideoSequenceDisplayExt *seqdispext;
796 gint width, height;
797
798 width = mpvparse->sequencehdr.width;
799 height = mpvparse->sequencehdr.height;
800
801 if (mpvparse->config_flags & FLAG_SEQUENCE_DISPLAY_EXT) {
802 seqdispext = &mpvparse->sequencedispext;
803
804 if (seqdispext->display_horizontal_size <= width
805 && seqdispext->display_vertical_size <= height) {
806 width = seqdispext->display_horizontal_size;
807 height = seqdispext->display_vertical_size;
808 GST_INFO_OBJECT (mpvparse,
809 "stream has display extension: display_width=%d display_height=%d",
810 width, height);
811 }
812 }
813 gst_caps_set_simple (caps, "width", G_TYPE_INT, width,
814 "height", G_TYPE_INT, height, NULL);
815 }
816
817 /* perhaps we have a framerate */
818 {
819 gint fps_num = mpvparse->fps_num;
820 gint fps_den = mpvparse->fps_den;
821 GstClockTime latency;
822
823 /* upstream overrides */
824 if (s && gst_structure_has_field (s, "framerate"))
825 gst_structure_get_fraction (s, "framerate", &fps_num, &fps_den);
826
827 if (fps_den > 0 && fps_num > 0) {
828 gst_caps_set_simple (caps, "framerate",
829 GST_TYPE_FRACTION, fps_num, fps_den, NULL);
830 gst_base_parse_set_frame_rate (GST_BASE_PARSE (mpvparse),
831 fps_num, fps_den, 0, 0);
832 latency = gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
833 gst_base_parse_set_latency (GST_BASE_PARSE (mpvparse), latency, latency);
834 }
835 }
836
837 /* or pixel-aspect-ratio */
838 if (mpvparse->sequencehdr.par_w && mpvparse->sequencehdr.par_h > 0 &&
839 (!s || !gst_structure_has_field (s, "pixel-aspect-ratio"))) {
840 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
841 mpvparse->sequencehdr.par_w, mpvparse->sequencehdr.par_h, NULL);
842 }
843
844 if (mpvparse->config != NULL) {
845 gst_caps_set_simple (caps, "codec_data",
846 GST_TYPE_BUFFER, mpvparse->config, NULL);
847 }
848
849 if (mpvparse->config_flags & FLAG_SEQUENCE_EXT) {
850 guint8 escape = mpvparse->sequenceext.profile_level_escape_bit;
851 const guint profile_c = mpvparse->sequenceext.profile;
852 const guint level_c = mpvparse->sequenceext.level;
853 const gchar *profile = NULL, *level = NULL;
854 /*
855 * Profile indication - 1 => High, 2 => Spatially Scalable,
856 * 3 => SNR Scalable, 4 => Main, 5 => Simple
857 * 4:2:2 and Multi-view have profile = 0, with the escape bit set to 1
858 */
859 const gchar *const profiles[] =
860 { "4:2:2", "high", "spatial", "snr", "main", "simple" };
861 /*
862 * Level indication - 4 => High, 6 => High-1440, 8 => Main, 10 => Low,
863 * except in the case of profile = 0
864 */
865 const gchar *const levels[] = { "high", "high-1440", "main", "low" };
866
867 if (escape) {
868 /* Non-hierarchical profile */
869 switch (level_c) {
870 case 2:
871 level = levels[0];
872 /* FALLTHROUGH */
873 case 5:
874 if (!level)
875 level = levels[2];
876 profile = "4:2:2";
877 break;
878 case 10:
879 level = levels[0];
880 /* FALLTHROUGH */
881 case 11:
882 if (!level)
883 level = levels[1];
884 /* FALLTHROUGH */
885 case 13:
886 if (!level)
887 level = levels[2];
888 /* FALLTHROUGH */
889 case 14:
890 if (!level)
891 level = levels[3];
892 profile = "multiview";
893 break;
894 default:
895 break;
896 }
897
898 } else {
899 if (profile_c < 6)
900 profile = profiles[profile_c];
901
902 if ((level_c > 3) && (level_c < 11) && (level_c % 2 == 0))
903 level = levels[(level_c >> 1) - 2];
904 }
905
906 GST_DEBUG_OBJECT (mpvparse, "profile:'%s' level:'%s'", profile, level);
907
908 if (profile)
909 gst_caps_set_simple (caps, "profile", G_TYPE_STRING, profile, NULL);
910 else
911 GST_DEBUG_OBJECT (mpvparse, "Invalid profile - %u", profile_c);
912
913 if (level)
914 gst_caps_set_simple (caps, "level", G_TYPE_STRING, level, NULL);
915 else
916 GST_DEBUG_OBJECT (mpvparse, "Invalid level - %u", level_c);
917
918 if (!s || !gst_structure_has_field (s, "interlace-mode"))
919 gst_caps_set_simple (caps, "interlace-mode",
920 G_TYPE_STRING,
921 (mpvparse->sequenceext.progressive ? "progressive" : "mixed"), NULL);
922 }
923
924 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (mpvparse), caps);
925 gst_caps_unref (caps);
926
927 mpvparse->update_caps = FALSE;
928 }
929
930 static GstFlowReturn
931 gst_mpegv_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
932 {
933 GstMpegvParse *mpvparse = GST_MPEGVIDEO_PARSE (parse);
934 GstBuffer *buffer = frame->buffer;
935
936 if (G_UNLIKELY (mpvparse->pichdr.pic_type == GST_MPEG_VIDEO_PICTURE_TYPE_I))
937 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
938 else if (mpvparse->pichdr.pic_type != 0)
939 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
940
941 /* maybe only sequence in this buffer, though not recommended,
942 * so mark it as such and force 0 duration */
943 if (G_UNLIKELY (mpvparse->pic_offset < 0)) {
944 GST_DEBUG_OBJECT (mpvparse, "frame holds no picture data");
945 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
946 GST_BUFFER_DURATION (buffer) = 0;
947 }
948
949 if (mpvparse->pic_offset > 4) {
950 gst_base_parse_set_ts_at_offset (parse, mpvparse->pic_offset - 4);
951 }
952
953 if (mpvparse->frame_repeat_count
954 && GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) {
955 GST_BUFFER_DURATION (buffer) =
956 (1 + mpvparse->frame_repeat_count) * GST_BUFFER_DURATION (buffer) / 2;
957 }
958
959 if (G_UNLIKELY (mpvparse->drop && !mpvparse->config)) {
960 GST_DEBUG_OBJECT (mpvparse, "dropping frame as no config yet");
961 return GST_BASE_PARSE_FLOW_DROPPED;
962 }
963
964 gst_mpegv_parse_update_src_caps (mpvparse);
965 return GST_FLOW_OK;
966 }
967
968 static GstFlowReturn
969 gst_mpegv_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
970 {
971 GstMpegvParse *mpvparse = GST_MPEGVIDEO_PARSE (parse);
972 GstTagList *taglist;
973 GstMpegVideoMeta *meta;
974 GstMpegVideoSequenceHdr *seq_hdr = NULL;
975 GstMpegVideoSequenceExt *seq_ext = NULL;
976 GstMpegVideoSequenceDisplayExt *disp_ext = NULL;
977 GstMpegVideoPictureHdr *pic_hdr = NULL;
978 GstMpegVideoPictureExt *pic_ext = NULL;
979 GstMpegVideoQuantMatrixExt *quant_ext = NULL;
980 GstBuffer *parse_buffer = NULL;
981
982 /* tag sending done late enough in hook to ensure pending events
983 * have already been sent */
984
985 if (G_UNLIKELY (mpvparse->send_codec_tag)) {
986 GstCaps *caps;
987
988 /* codec tag */
989 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
990 if (G_UNLIKELY (caps == NULL)) {
991 if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
992 GST_INFO_OBJECT (parse, "Src pad is flushing");
993 return GST_FLOW_FLUSHING;
994 } else {
995 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
996 return GST_FLOW_NOT_NEGOTIATED;
997 }
998 }
999
1000 taglist = gst_tag_list_new_empty ();
1001 gst_pb_utils_add_codec_description_to_tag_list (taglist,
1002 GST_TAG_VIDEO_CODEC, caps);
1003 gst_caps_unref (caps);
1004
1005 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1006 gst_tag_list_unref (taglist);
1007
1008 mpvparse->send_codec_tag = FALSE;
1009 }
1010
1011 if (mpvparse->send_mpeg_meta) {
1012 GstBuffer *buf;
1013
1014 if (mpvparse->seqhdr_updated)
1015 seq_hdr = &mpvparse->sequencehdr;
1016 if (mpvparse->seqext_updated)
1017 seq_ext = &mpvparse->sequenceext;
1018 if (mpvparse->seqdispext_updated)
1019 disp_ext = &mpvparse->sequencedispext;
1020 if (mpvparse->picext_updated)
1021 pic_ext = &mpvparse->picext;
1022 if (mpvparse->quantmatrext_updated)
1023 quant_ext = &mpvparse->quantmatrext;
1024 pic_hdr = &mpvparse->pichdr;
1025
1026 GST_DEBUG_OBJECT (mpvparse,
1027 "Adding GstMpegVideoMeta (slice_count:%d, slice_offset:%d)",
1028 mpvparse->slice_count, mpvparse->slice_offset);
1029
1030 if (frame->out_buffer) {
1031 buf = frame->out_buffer = gst_buffer_make_writable (frame->out_buffer);
1032 } else {
1033 buf = frame->buffer = gst_buffer_make_writable (frame->buffer);
1034 }
1035
1036 meta =
1037 gst_buffer_add_mpeg_video_meta (buf, seq_hdr, seq_ext, disp_ext,
1038 pic_hdr, pic_ext, quant_ext);
1039 meta->num_slices = mpvparse->slice_count;
1040 meta->slice_offset = mpvparse->slice_offset;
1041 }
1042
1043 if (frame->out_buffer) {
1044 parse_buffer = frame->out_buffer =
1045 gst_buffer_make_writable (frame->out_buffer);
1046 } else {
1047 parse_buffer = frame->buffer = gst_buffer_make_writable (frame->buffer);
1048 }
1049
1050 if (pic_ext && !pic_ext->progressive_frame) {
1051 GST_BUFFER_FLAG_SET (parse_buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1052 if (pic_ext->top_field_first)
1053 GST_BUFFER_FLAG_SET (parse_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1054 }
1055 gst_video_push_user_data ((GstElement *) mpvparse, &mpvparse->user_data,
1056 parse_buffer);
1057
1058 return GST_FLOW_OK;
1059 }
1060
1061 static gboolean
1062 gst_mpegv_parse_set_caps (GstBaseParse * parse, GstCaps * caps)
1063 {
1064 GstMpegvParse *mpvparse = GST_MPEGVIDEO_PARSE (parse);
1065 GstStructure *s;
1066 const GValue *value;
1067 GstBuffer *buf;
1068
1069 GST_DEBUG_OBJECT (parse, "setcaps called with %" GST_PTR_FORMAT, caps);
1070
1071 s = gst_caps_get_structure (caps, 0);
1072
1073 if ((value = gst_structure_get_value (s, "codec_data")) != NULL
1074 && (buf = gst_value_get_buffer (value))) {
1075 GstMapInfo map;
1076 gst_buffer_map (buf, &map, GST_MAP_READ);
1077 /* best possible parse attempt,
1078 * src caps are based on sink caps so it will end up in there
1079 * whether successful or not */
1080 mpvparse->seq_offset = 4;
1081 gst_mpegv_parse_process_config (mpvparse, &map, gst_buffer_get_size (buf));
1082 gst_buffer_unmap (buf, &map);
1083 gst_mpegv_parse_reset_frame (mpvparse);
1084 }
1085
1086 /* let's not interfere and accept regardless of config parsing success */
1087 return TRUE;
1088 }
1089
1090 static void
1091 remove_fields (GstCaps * caps)
1092 {
1093 guint i, n;
1094
1095 n = gst_caps_get_size (caps);
1096 for (i = 0; i < n; i++) {
1097 GstStructure *s = gst_caps_get_structure (caps, i);
1098
1099 gst_structure_remove_field (s, "parsed");
1100 }
1101 }
1102
1103 static GstCaps *
1104 gst_mpegv_parse_get_caps (GstBaseParse * parse, GstCaps * filter)
1105 {
1106 GstCaps *peercaps, *templ;
1107 GstCaps *res;
1108
1109 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1110 if (filter) {
1111 GstCaps *fcopy = gst_caps_copy (filter);
1112 /* Remove the fields we convert */
1113 remove_fields (fcopy);
1114 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1115 gst_caps_unref (fcopy);
1116 } else
1117 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1118
1119 if (peercaps) {
1120 /* Remove the parsed field */
1121 peercaps = gst_caps_make_writable (peercaps);
1122 remove_fields (peercaps);
1123
1124 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1125 gst_caps_unref (peercaps);
1126 gst_caps_unref (templ);
1127 } else {
1128 res = templ;
1129 }
1130
1131 if (filter) {
1132 GstCaps *intersection;
1133
1134 intersection =
1135 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1136 gst_caps_unref (res);
1137 res = intersection;
1138 }
1139
1140 return res;
1141 }