5867462e55dc06cc5f9ba5a5c646e5747f605ee0
1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <string.h>
25 #include <inttypes.h>
27 #include "gstmpeg2dec.h"
29 /* 16byte-aligns a buffer for libmpeg2 */
30 #define ALIGN_16(p) ((void *)(((uintptr_t)(p) + 15) & ~((uintptr_t)15)))
32 /* mpeg2dec changed a struct name after 0.3.1, here's a workaround */
33 /* mpeg2dec also only defined MPEG2_RELEASE after 0.3.1
34 #if MPEG2_RELEASE < MPEG2_VERSION(0,3,2)
35 */
36 #ifndef MPEG2_RELEASE
37 #define MPEG2_VERSION(a,b,c) ((((a)&0xff)<<16)|(((b)&0xff)<<8)|((c)&0xff))
38 #define MPEG2_RELEASE MPEG2_VERSION(0,3,1)
39 typedef picture_t mpeg2_picture_t;
40 typedef gint mpeg2_state_t;
42 #define STATE_BUFFER 0
43 #endif
45 GST_DEBUG_CATEGORY_STATIC (mpeg2dec_debug);
46 #define GST_CAT_DEFAULT (mpeg2dec_debug)
48 /* Send a warning message about decoding errors after receiving this many
49 * STATE_INVALID return values from mpeg2_parse. -1 means never.
50 */
51 #define WARN_THRESHOLD (5)
53 //#define enable_user_data
54 #ifdef enable_user_data
55 static GstStaticPadTemplate user_data_template_factory =
56 GST_STATIC_PAD_TEMPLATE ("user_data",
57 GST_PAD_SRC,
58 GST_PAD_ALWAYS,
59 GST_STATIC_CAPS_ANY);
60 #endif
62 static GstStaticPadTemplate sink_template_factory =
63 GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_PAD_SINK,
65 GST_PAD_ALWAYS,
66 GST_STATIC_CAPS ("video/mpeg, "
67 "mpegversion = (int) [ 1, 2 ], " "systemstream = (boolean) false")
68 );
70 static GstStaticPadTemplate src_template_factory =
71 GST_STATIC_PAD_TEMPLATE ("src",
72 GST_PAD_SRC,
73 GST_PAD_ALWAYS,
74 GST_STATIC_CAPS ("video/x-raw-yuv, "
75 "format = (fourcc) { YV12, I420, Y42B, Y444 }, "
76 "width = (int) [ 16, 4096 ], "
77 "height = (int) [ 16, 4096 ], "
78 "framerate = (fraction) [ 0/1, 2147483647/1 ]")
79 );
81 static void gst_mpeg2dec_base_init (gpointer g_class);
82 static void gst_mpeg2dec_class_init (GstMpeg2decClass * klass);
83 static void gst_mpeg2dec_init (GstMpeg2dec * mpeg2dec);
85 static void gst_mpeg2dec_finalize (GObject * object);
86 static void gst_mpeg2dec_reset (GstMpeg2dec * mpeg2dec);
88 static void gst_mpeg2dec_set_index (GstElement * element, GstIndex * index);
89 static GstIndex *gst_mpeg2dec_get_index (GstElement * element);
91 static gboolean gst_mpeg2dec_src_event (GstPad * pad, GstEvent * event);
92 static const GstQueryType *gst_mpeg2dec_get_src_query_types (GstPad * pad);
94 static gboolean gst_mpeg2dec_src_query (GstPad * pad, GstQuery * query);
96 static gboolean gst_mpeg2dec_sink_convert (GstPad * pad, GstFormat src_format,
97 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
98 static gboolean gst_mpeg2dec_src_convert (GstPad * pad, GstFormat src_format,
99 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
101 static GstStateChangeReturn gst_mpeg2dec_change_state (GstElement * element,
102 GstStateChange transition);
104 static gboolean gst_mpeg2dec_sink_event (GstPad * pad, GstEvent * event);
105 static gboolean gst_mpeg2dec_setcaps (GstPad * pad, GstCaps * caps);
106 static GstFlowReturn gst_mpeg2dec_chain (GstPad * pad, GstBuffer * buf);
108 static void clear_buffers (GstMpeg2dec * mpeg2dec);
110 //static gboolean gst_mpeg2dec_sink_query (GstPad * pad, GstQuery * query);
112 #if 0
113 static const GstFormat *gst_mpeg2dec_get_formats (GstPad * pad);
114 #endif
116 #if 0
117 static const GstEventMask *gst_mpeg2dec_get_event_masks (GstPad * pad);
118 #endif
120 static GstElementClass *parent_class = NULL;
122 static gboolean gst_mpeg2dec_crop_buffer (GstMpeg2dec * dec, GstBuffer ** buf);
124 /*static guint gst_mpeg2dec_signals[LAST_SIGNAL] = { 0 };*/
126 GType
127 gst_mpeg2dec_get_type (void)
128 {
129 static GType mpeg2dec_type = 0;
131 if (!mpeg2dec_type) {
132 static const GTypeInfo mpeg2dec_info = {
133 sizeof (GstMpeg2decClass),
134 gst_mpeg2dec_base_init,
135 NULL,
136 (GClassInitFunc) gst_mpeg2dec_class_init,
137 NULL,
138 NULL,
139 sizeof (GstMpeg2dec),
140 0,
141 (GInstanceInitFunc) gst_mpeg2dec_init,
142 };
144 mpeg2dec_type =
145 g_type_register_static (GST_TYPE_ELEMENT, "GstMpeg2dec", &mpeg2dec_info,
146 0);
147 }
149 GST_DEBUG_CATEGORY_INIT (mpeg2dec_debug, "mpeg2dec", 0,
150 "MPEG2 decoder element");
152 return mpeg2dec_type;
153 }
155 static void
156 gst_mpeg2dec_base_init (gpointer g_class)
157 {
158 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
160 gst_element_class_add_pad_template (element_class,
161 gst_static_pad_template_get (&src_template_factory));
162 gst_element_class_add_pad_template (element_class,
163 gst_static_pad_template_get (&sink_template_factory));
164 #ifdef enable_user_data
165 gst_element_class_add_pad_template (element_class,
166 gst_static_pad_template_get (&user_data_template_factory));
167 #endif
168 gst_element_class_set_details_simple (element_class,
169 "mpeg1 and mpeg2 video decoder", "Codec/Decoder/Video",
170 "Uses libmpeg2 to decode MPEG video streams",
171 "Wim Taymans <wim.taymans@chello.be>");
172 }
174 static void
175 gst_mpeg2dec_class_init (GstMpeg2decClass * klass)
176 {
177 GObjectClass *gobject_class;
178 GstElementClass *gstelement_class;
180 gobject_class = (GObjectClass *) klass;
181 gstelement_class = (GstElementClass *) klass;
183 parent_class = g_type_class_peek_parent (klass);
185 gobject_class->finalize = gst_mpeg2dec_finalize;
187 gstelement_class->change_state = gst_mpeg2dec_change_state;
188 gstelement_class->set_index = gst_mpeg2dec_set_index;
189 gstelement_class->get_index = gst_mpeg2dec_get_index;
190 }
192 static void
193 gst_mpeg2dec_init (GstMpeg2dec * mpeg2dec)
194 {
195 /* create the sink and src pads */
196 mpeg2dec->sinkpad =
197 gst_pad_new_from_static_template (&sink_template_factory, "sink");
198 gst_pad_set_chain_function (mpeg2dec->sinkpad,
199 GST_DEBUG_FUNCPTR (gst_mpeg2dec_chain));
200 #if 0
201 gst_pad_set_query_function (mpeg2dec->sinkpad,
202 GST_DEBUG_FUNCPTR (gst_mpeg2dec_get_sink_query));
203 #endif
204 gst_pad_set_event_function (mpeg2dec->sinkpad,
205 GST_DEBUG_FUNCPTR (gst_mpeg2dec_sink_event));
206 gst_pad_set_setcaps_function (mpeg2dec->sinkpad,
207 GST_DEBUG_FUNCPTR (gst_mpeg2dec_setcaps));
208 gst_element_add_pad (GST_ELEMENT (mpeg2dec), mpeg2dec->sinkpad);
210 mpeg2dec->srcpad =
211 gst_pad_new_from_static_template (&src_template_factory, "src");
212 gst_pad_set_event_function (mpeg2dec->srcpad,
213 GST_DEBUG_FUNCPTR (gst_mpeg2dec_src_event));
214 gst_pad_set_query_type_function (mpeg2dec->srcpad,
215 GST_DEBUG_FUNCPTR (gst_mpeg2dec_get_src_query_types));
216 gst_pad_set_query_function (mpeg2dec->srcpad,
217 GST_DEBUG_FUNCPTR (gst_mpeg2dec_src_query));
218 gst_pad_use_fixed_caps (mpeg2dec->srcpad);
219 gst_element_add_pad (GST_ELEMENT (mpeg2dec), mpeg2dec->srcpad);
221 #ifdef enable_user_data
222 mpeg2dec->userdatapad =
223 gst_pad_new_from_static_template (&user_data_template_factory,
224 "user_data");
225 gst_element_add_pad (GST_ELEMENT (mpeg2dec), mpeg2dec->userdatapad);
226 #endif
228 mpeg2dec->error_count = 0;
229 mpeg2dec->can_allocate_aligned = TRUE;
231 /* initialize the mpeg2dec acceleration */
232 }
234 static void
235 gst_mpeg2dec_finalize (GObject * object)
236 {
237 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (object);
239 if (mpeg2dec->index) {
240 gst_object_unref (mpeg2dec->index);
241 mpeg2dec->index = NULL;
242 mpeg2dec->index_id = 0;
243 }
245 if (mpeg2dec->decoder) {
246 GST_DEBUG_OBJECT (mpeg2dec, "closing decoder");
247 mpeg2_close (mpeg2dec->decoder);
248 mpeg2dec->decoder = NULL;
249 }
250 clear_buffers (mpeg2dec);
251 g_free (mpeg2dec->dummybuf[3]);
252 mpeg2dec->dummybuf[3] = NULL;
254 G_OBJECT_CLASS (parent_class)->finalize (object);
255 }
257 static void
258 gst_mpeg2dec_reset (GstMpeg2dec * mpeg2dec)
259 {
260 if (mpeg2dec->index) {
261 gst_object_unref (mpeg2dec->index);
262 mpeg2dec->index = NULL;
263 mpeg2dec->index_id = 0;
264 }
266 /* reset the initial video state */
267 mpeg2dec->format = GST_VIDEO_FORMAT_UNKNOWN;
268 mpeg2dec->width = -1;
269 mpeg2dec->height = -1;
270 gst_segment_init (&mpeg2dec->segment, GST_FORMAT_UNDEFINED);
271 mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
272 mpeg2dec->frame_period = 0;
273 mpeg2dec->need_sequence = TRUE;
274 mpeg2dec->next_time = -1;
275 mpeg2dec->offset = 0;
276 mpeg2dec->error_count = 0;
277 mpeg2dec->can_allocate_aligned = TRUE;
278 mpeg2_reset (mpeg2dec->decoder, 1);
279 }
281 static void
282 gst_mpeg2dec_qos_reset (GstMpeg2dec * mpeg2dec)
283 {
284 GST_OBJECT_LOCK (mpeg2dec);
285 mpeg2dec->proportion = 1.0;
286 mpeg2dec->earliest_time = -1;
287 GST_OBJECT_UNLOCK (mpeg2dec);
288 }
290 static void
291 gst_mpeg2dec_set_index (GstElement * element, GstIndex * index)
292 {
293 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (element);
295 GST_OBJECT_LOCK (mpeg2dec);
296 if (mpeg2dec->index)
297 gst_object_unref (mpeg2dec->index);
298 mpeg2dec->index = NULL;
299 mpeg2dec->index_id = 0;
300 if (index) {
301 mpeg2dec->index = gst_object_ref (index);
302 gst_index_get_writer_id (index, GST_OBJECT (element), &mpeg2dec->index_id);
303 }
305 GST_OBJECT_UNLOCK (mpeg2dec);
306 }
308 static GstIndex *
309 gst_mpeg2dec_get_index (GstElement * element)
310 {
311 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (element);
313 return (mpeg2dec->index) ? gst_object_ref (mpeg2dec->index) : NULL;
314 }
316 static GstFlowReturn
317 gst_mpeg2dec_crop_buffer (GstMpeg2dec * dec, GstBuffer ** buf)
318 {
319 GstFlowReturn flow_ret;
320 GstBuffer *inbuf = *buf;
321 GstBuffer *outbuf;
322 guint outsize, c;
324 outsize = gst_video_format_get_size (dec->format, dec->width, dec->height);
326 GST_LOG_OBJECT (dec, "Copying input buffer %ux%u (%u) to output buffer "
327 "%ux%u (%u)", dec->decoded_width, dec->decoded_height,
328 GST_BUFFER_SIZE (inbuf), dec->width, dec->height, outsize);
330 flow_ret = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
331 GST_BUFFER_OFFSET_NONE, outsize, GST_PAD_CAPS (dec->srcpad), &outbuf);
333 if (G_UNLIKELY (flow_ret != GST_FLOW_OK))
334 return flow_ret;
336 for (c = 0; c < 3; c++) {
337 const guint8 *src;
338 guint8 *dest;
339 guint stride_in, stride_out;
340 guint c_height, c_width, line;
342 src =
343 GST_BUFFER_DATA (inbuf) +
344 gst_video_format_get_component_offset (dec->format, c,
345 dec->decoded_width, dec->decoded_height);
346 dest =
347 GST_BUFFER_DATA (outbuf) +
348 gst_video_format_get_component_offset (dec->format, c, dec->width,
349 dec->height);
350 stride_out = gst_video_format_get_row_stride (dec->format, c, dec->width);
351 stride_in =
352 gst_video_format_get_row_stride (dec->format, c, dec->decoded_width);
353 c_height =
354 gst_video_format_get_component_height (dec->format, c, dec->height);
355 c_width = gst_video_format_get_component_width (dec->format, c, dec->width);
357 for (line = 0; line < c_height; line++) {
358 memcpy (dest, src, c_width);
359 dest += stride_out;
360 src += stride_in;
361 }
362 }
364 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dec->srcpad));
365 gst_buffer_copy_metadata (outbuf, inbuf,
366 GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS);
368 gst_buffer_unref (*buf);
369 *buf = outbuf;
371 return GST_FLOW_OK;
372 }
374 static GstFlowReturn
375 gst_mpeg2dec_alloc_sized_buf (GstMpeg2dec * mpeg2dec, guint size,
376 GstBuffer ** obuf)
377 {
378 if (mpeg2dec->can_allocate_aligned
379 && mpeg2dec->decoded_width == mpeg2dec->width
380 && mpeg2dec->decoded_height == mpeg2dec->height) {
381 GstFlowReturn ret;
383 ret = gst_pad_alloc_buffer_and_set_caps (mpeg2dec->srcpad,
384 GST_BUFFER_OFFSET_NONE, size, GST_PAD_CAPS (mpeg2dec->srcpad), obuf);
385 if (ret != GST_FLOW_OK) {
386 return ret;
387 }
389 /* libmpeg2 needs 16 byte aligned buffers... test for this here
390 * and if it fails only a single time create our own buffers from
391 * there on below that are correctly aligned */
392 if (((uintptr_t) GST_BUFFER_DATA (*obuf)) % 16 == 0) {
393 GST_LOG_OBJECT (mpeg2dec, "return 16 byte aligned buffer");
394 return ret;
395 }
397 GST_DEBUG_OBJECT (mpeg2dec,
398 "can't get 16 byte aligned buffers, creating our own ones");
399 gst_buffer_unref (*obuf);
400 mpeg2dec->can_allocate_aligned = FALSE;
401 }
403 /* can't use gst_pad_alloc_buffer() here because the output buffer will
404 * either be cropped later or be bigger than expected (for the alignment),
405 * and basetransform-based elements will complain about the wrong unit size
406 * when not operating in passthrough mode */
407 *obuf = gst_buffer_new_and_alloc (size + 15);
408 GST_BUFFER_DATA (*obuf) = (guint8 *) ALIGN_16 (GST_BUFFER_DATA (*obuf));
409 GST_BUFFER_SIZE (*obuf) = size;
410 gst_buffer_set_caps (*obuf, GST_PAD_CAPS (mpeg2dec->srcpad));
412 return GST_FLOW_OK;
413 }
415 static GstFlowReturn
416 gst_mpeg2dec_alloc_buffer (GstMpeg2dec * mpeg2dec, gint64 offset,
417 GstBuffer ** obuf)
418 {
419 GstBuffer *outbuf = NULL;
420 guint8 *buf[3];
421 GstFlowReturn ret = GST_FLOW_OK;
423 ret = gst_mpeg2dec_alloc_sized_buf (mpeg2dec, mpeg2dec->size, &outbuf);
424 if (ret != GST_FLOW_OK)
425 goto no_buffer;
427 buf[0] = GST_BUFFER_DATA (outbuf);
428 buf[1] = buf[0] + mpeg2dec->u_offs;
429 buf[2] = buf[0] + mpeg2dec->v_offs;
431 GST_DEBUG_OBJECT (mpeg2dec, "set_buf: %p %p %p, outbuf %p",
432 buf[0], buf[1], buf[2], outbuf);
434 mpeg2_set_buf (mpeg2dec->decoder, buf, outbuf);
436 /* we store the original byteoffset of this picture in the stream here
437 * because we need it for indexing */
438 GST_BUFFER_OFFSET (outbuf) = offset;
440 *obuf = outbuf;
441 return ret;
443 /* ERRORS */
444 no_buffer:
445 {
446 if (ret != GST_FLOW_WRONG_STATE && ret != GST_FLOW_UNEXPECTED &&
447 ret != GST_FLOW_NOT_LINKED) {
448 GST_ELEMENT_ERROR (mpeg2dec, RESOURCE, FAILED, (NULL),
449 ("Failed to allocate memory for buffer, reason %s",
450 gst_flow_get_name (ret)));
451 }
452 GST_DEBUG_OBJECT (mpeg2dec, "no output buffer, reason %s",
453 gst_flow_get_name (ret));
454 mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
455 *obuf = NULL;
456 return ret;
457 }
458 }
460 static gboolean
461 gst_mpeg2dec_negotiate_format (GstMpeg2dec * mpeg2dec)
462 {
463 GstCaps *caps;
464 guint32 fourcc;
465 const mpeg2_info_t *info;
466 const mpeg2_sequence_t *sequence;
468 info = mpeg2_info (mpeg2dec->decoder);
469 sequence = info->sequence;
471 if (sequence->width != sequence->chroma_width &&
472 sequence->height != sequence->chroma_height) {
473 mpeg2dec->format = GST_VIDEO_FORMAT_I420;
474 } else if ((sequence->width == sequence->chroma_width &&
475 sequence->height != sequence->chroma_height) ||
476 (sequence->width != sequence->chroma_width &&
477 sequence->height == sequence->chroma_height)) {
478 mpeg2dec->format = GST_VIDEO_FORMAT_Y42B;
479 } else {
480 mpeg2dec->format = GST_VIDEO_FORMAT_Y444;
481 }
483 fourcc = gst_video_format_to_fourcc (mpeg2dec->format);
484 mpeg2dec->size = gst_video_format_get_size (mpeg2dec->format,
485 mpeg2dec->decoded_width, mpeg2dec->decoded_height);
486 mpeg2dec->u_offs = gst_video_format_get_component_offset (mpeg2dec->format, 1,
487 mpeg2dec->decoded_width, mpeg2dec->decoded_height);
488 mpeg2dec->v_offs = gst_video_format_get_component_offset (mpeg2dec->format, 2,
489 mpeg2dec->decoded_width, mpeg2dec->decoded_height);
491 if (mpeg2dec->pixel_width == 0 || mpeg2dec->pixel_height == 0) {
492 GValue par = { 0, };
493 GValue dar = { 0, };
494 GValue dimensions = { 0, };
496 /* assume display aspect ratio (DAR) of 4:3 */
497 g_value_init (&dar, GST_TYPE_FRACTION);
498 gst_value_set_fraction (&dar, 4, 3);
499 g_value_init (&dimensions, GST_TYPE_FRACTION);
500 gst_value_set_fraction (&dimensions, mpeg2dec->height, mpeg2dec->width);
502 g_value_init (&par, GST_TYPE_FRACTION);
503 if (!gst_value_fraction_multiply (&par, &dar, &dimensions)) {
504 gst_value_set_fraction (&dimensions, 1, 1);
505 }
507 mpeg2dec->pixel_width = gst_value_get_fraction_numerator (&par);
508 mpeg2dec->pixel_height = gst_value_get_fraction_denominator (&par);
510 GST_WARNING_OBJECT (mpeg2dec, "Unknown pixel-aspect-ratio, assuming %d:%d",
511 mpeg2dec->pixel_width, mpeg2dec->pixel_height);
513 g_value_unset (&par);
514 g_value_unset (&dar);
515 g_value_unset (&dimensions);
516 }
518 caps = gst_caps_new_simple ("video/x-raw-yuv",
519 "format", GST_TYPE_FOURCC, fourcc,
520 "width", G_TYPE_INT, mpeg2dec->width,
521 "height", G_TYPE_INT, mpeg2dec->height,
522 "pixel-aspect-ratio", GST_TYPE_FRACTION, mpeg2dec->pixel_width,
523 mpeg2dec->pixel_height,
524 "framerate", GST_TYPE_FRACTION, mpeg2dec->fps_n, mpeg2dec->fps_d,
525 "interlaced", G_TYPE_BOOLEAN, mpeg2dec->interlaced, NULL);
527 gst_pad_set_caps (mpeg2dec->srcpad, caps);
528 gst_caps_unref (caps);
530 return TRUE;
531 }
533 static void
534 init_dummybuf (GstMpeg2dec * mpeg2dec)
535 {
536 g_free (mpeg2dec->dummybuf[3]);
538 /* libmpeg2 needs 16 byte aligned buffers... care for this here */
539 mpeg2dec->dummybuf[3] = g_malloc0 (mpeg2dec->size + 15);
540 mpeg2dec->dummybuf[0] = ALIGN_16 (mpeg2dec->dummybuf[3]);
541 mpeg2dec->dummybuf[1] = mpeg2dec->dummybuf[0] + mpeg2dec->u_offs;
542 mpeg2dec->dummybuf[2] = mpeg2dec->dummybuf[0] + mpeg2dec->v_offs;
543 }
545 static GstFlowReturn
546 handle_sequence (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
547 {
548 GstFlowReturn ret = GST_FLOW_OK;
550 if (info->sequence->frame_period == 0) {
551 GST_WARNING_OBJECT (mpeg2dec, "Frame period is 0!");
552 ret = GST_FLOW_ERROR;
553 goto done;
554 }
556 mpeg2dec->width = info->sequence->picture_width;
557 mpeg2dec->height = info->sequence->picture_height;
558 mpeg2dec->decoded_width = info->sequence->width;
559 mpeg2dec->decoded_height = info->sequence->height;
560 mpeg2dec->total_frames = 0;
562 /* don't take the sequence PAR if we already have one from the sink caps */
563 if (!mpeg2dec->have_par) {
564 mpeg2dec->pixel_width = info->sequence->pixel_width;
565 mpeg2dec->pixel_height = info->sequence->pixel_height;
566 }
568 /* mpeg2 video can only be from 16x16 to 4096x4096. Everything
569 * else is a corrupted files */
570 if (mpeg2dec->width > 4096 || mpeg2dec->width < 16 ||
571 mpeg2dec->height > 4096 || mpeg2dec->height < 16) {
572 GST_ERROR_OBJECT (mpeg2dec, "Invalid frame dimensions: %d x %d",
573 mpeg2dec->width, mpeg2dec->height);
574 return GST_FLOW_ERROR;
575 }
577 /* set framerate */
578 mpeg2dec->fps_n = 27000000;
579 mpeg2dec->fps_d = info->sequence->frame_period;
580 mpeg2dec->frame_period = info->sequence->frame_period * GST_USECOND / 27;
582 mpeg2dec->interlaced =
583 !(info->sequence->flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE);
585 GST_DEBUG_OBJECT (mpeg2dec,
586 "sequence flags: %d, frame period: %d (%g), frame rate: %d/%d",
587 info->sequence->flags, info->sequence->frame_period,
588 (double) (mpeg2dec->frame_period) / GST_SECOND, mpeg2dec->fps_n,
589 mpeg2dec->fps_d);
590 GST_DEBUG_OBJECT (mpeg2dec, "profile: %02x, colour_primaries: %d",
591 info->sequence->profile_level_id, info->sequence->colour_primaries);
592 GST_DEBUG_OBJECT (mpeg2dec, "transfer chars: %d, matrix coef: %d",
593 info->sequence->transfer_characteristics,
594 info->sequence->matrix_coefficients);
595 GST_DEBUG_OBJECT (mpeg2dec,
596 "FLAGS: CONSTRAINED_PARAMETERS:%d, PROGRESSIVE_SEQUENCE:%d",
597 info->sequence->flags & SEQ_FLAG_CONSTRAINED_PARAMETERS,
598 info->sequence->flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE);
599 GST_DEBUG_OBJECT (mpeg2dec, "FLAGS: LOW_DELAY:%d, COLOUR_DESCRIPTION:%d",
600 info->sequence->flags & SEQ_FLAG_LOW_DELAY,
601 info->sequence->flags & SEQ_FLAG_COLOUR_DESCRIPTION);
603 if (!gst_mpeg2dec_negotiate_format (mpeg2dec))
604 goto negotiate_failed;
606 mpeg2_custom_fbuf (mpeg2dec->decoder, 1);
608 init_dummybuf (mpeg2dec);
610 /* Pump in some null buffers, because otherwise libmpeg2 doesn't
611 * initialise the discard_fbuf->id */
612 mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
613 mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
614 mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
616 mpeg2dec->need_sequence = FALSE;
618 done:
619 return ret;
621 negotiate_failed:
622 {
623 GST_ELEMENT_ERROR (mpeg2dec, CORE, NEGOTIATION, (NULL), (NULL));
624 ret = GST_FLOW_NOT_NEGOTIATED;
625 goto done;
626 }
627 }
629 static void
630 clear_buffers (GstMpeg2dec * mpeg2dec)
631 {
632 gint i;
633 GstBuffer **bufpen;
635 for (i = 0; i < 4; i++) {
636 bufpen = &mpeg2dec->ip_buffers[i];
637 if (*bufpen)
638 gst_buffer_unref (*bufpen);
639 *bufpen = NULL;
640 }
641 bufpen = &mpeg2dec->b_buffer;
642 if (*bufpen)
643 gst_buffer_unref (*bufpen);
644 *bufpen = NULL;
645 }
647 static void
648 clear_queued (GstMpeg2dec * mpeg2dec)
649 {
650 g_list_foreach (mpeg2dec->queued, (GFunc) gst_mini_object_unref, NULL);
651 g_list_free (mpeg2dec->queued);
652 mpeg2dec->queued = NULL;
653 }
655 static GstFlowReturn
656 flush_queued (GstMpeg2dec * mpeg2dec)
657 {
658 GstFlowReturn res = GST_FLOW_OK;
660 while (mpeg2dec->queued) {
661 GstBuffer *buf = GST_BUFFER_CAST (mpeg2dec->queued->data);
663 GST_LOG_OBJECT (mpeg2dec, "pushing buffer %p, timestamp %"
664 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT, buf,
665 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
666 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
668 /* iterate ouput queue an push downstream */
669 res = gst_pad_push (mpeg2dec->srcpad, buf);
671 mpeg2dec->queued = g_list_delete_link (mpeg2dec->queued, mpeg2dec->queued);
672 }
673 return res;
674 }
676 static GstFlowReturn
677 handle_picture (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
678 {
679 gboolean key_frame = FALSE;
680 GstBuffer *outbuf, **bufpen;
681 GstFlowReturn ret;
682 gint type;
684 ret = gst_mpeg2dec_alloc_buffer (mpeg2dec, mpeg2dec->offset, &outbuf);
685 if (ret != GST_FLOW_OK)
686 goto no_buffer;
688 if (info->current_picture) {
689 type = info->current_picture->flags & PIC_MASK_CODING_TYPE;
690 } else {
691 type = 0;
692 }
694 GST_DEBUG_OBJECT (mpeg2dec, "handle picture type %d", type);
696 key_frame = type == PIC_FLAG_CODING_TYPE_I;
698 switch (type) {
699 case PIC_FLAG_CODING_TYPE_I:
700 mpeg2_skip (mpeg2dec->decoder, 0);
701 if (mpeg2dec->segment.rate < 0.0) {
702 /* negative rate, flush the queued pictures in reverse */
703 GST_DEBUG_OBJECT (mpeg2dec, "flushing queued buffers");
704 flush_queued (mpeg2dec);
705 }
706 case PIC_FLAG_CODING_TYPE_P:
707 bufpen = &mpeg2dec->ip_buffers[mpeg2dec->ip_bufpos];
708 GST_DEBUG_OBJECT (mpeg2dec, "I/P unref %p, ref %p", *bufpen, outbuf);
709 if (*bufpen)
710 gst_buffer_unref (*bufpen);
711 *bufpen = outbuf;
712 mpeg2dec->ip_bufpos = (mpeg2dec->ip_bufpos + 1) & 3;
713 break;
714 case PIC_FLAG_CODING_TYPE_B:
715 bufpen = &mpeg2dec->b_buffer;
716 GST_DEBUG_OBJECT (mpeg2dec, "B unref %p, ref %p", *bufpen, outbuf);
717 if (*bufpen)
718 gst_buffer_unref (*bufpen);
719 *bufpen = outbuf;
720 break;
721 default:
722 break;
723 }
725 GST_DEBUG_OBJECT (mpeg2dec, "picture %s, outbuf %p, offset %"
726 G_GINT64_FORMAT,
727 key_frame ? ", kf," : " ", outbuf, GST_BUFFER_OFFSET (outbuf)
728 );
730 if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_PICTURE && key_frame) {
731 mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_KEYFRAME;
732 }
734 return ret;
736 no_buffer:
737 {
738 return ret;
739 }
740 }
742 /* try to clip the buffer to the segment boundaries */
743 static gboolean
744 clip_buffer (GstMpeg2dec * dec, GstBuffer * buf)
745 {
746 gboolean res = TRUE;
747 GstClockTime in_ts, in_dur, stop;
748 gint64 cstart, cstop;
750 in_ts = GST_BUFFER_TIMESTAMP (buf);
751 in_dur = GST_BUFFER_DURATION (buf);
753 GST_LOG_OBJECT (dec,
754 "timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT,
755 GST_TIME_ARGS (in_ts), GST_TIME_ARGS (in_dur));
757 /* can't clip without TIME segment */
758 if (dec->segment.format != GST_FORMAT_TIME)
759 goto beach;
761 /* we need a start time */
762 if (!GST_CLOCK_TIME_IS_VALID (in_ts))
763 goto beach;
765 /* generate valid stop, if duration unknown, we have unknown stop */
766 stop =
767 GST_CLOCK_TIME_IS_VALID (in_dur) ? (in_ts + in_dur) : GST_CLOCK_TIME_NONE;
769 /* now clip */
770 if (!(res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
771 in_ts, stop, &cstart, &cstop)))
772 goto beach;
774 /* update timestamp and possibly duration if the clipped stop time is
775 * valid */
776 GST_BUFFER_TIMESTAMP (buf) = cstart;
777 if (GST_CLOCK_TIME_IS_VALID (cstop))
778 GST_BUFFER_DURATION (buf) = cstop - cstart;
780 beach:
781 GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
782 return res;
783 }
785 static GstFlowReturn
786 handle_slice (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
787 {
788 GstBuffer *outbuf = NULL;
789 GstFlowReturn ret = GST_FLOW_OK;
790 const mpeg2_picture_t *picture;
791 gboolean key_frame = FALSE;
792 GstClockTime time;
794 GST_DEBUG_OBJECT (mpeg2dec, "picture slice/end %p %p %p %p",
795 info->display_fbuf,
796 info->display_picture, info->current_picture,
797 (info->display_fbuf ? info->display_fbuf->id : NULL));
799 if (!info->display_fbuf || !info->display_fbuf->id)
800 goto no_display;
802 outbuf = GST_BUFFER (info->display_fbuf->id);
804 picture = info->display_picture;
806 key_frame = (picture->flags & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_I;
808 GST_DEBUG_OBJECT (mpeg2dec, "picture flags: %d, type: %d, keyframe: %d",
809 picture->flags, picture->flags & PIC_MASK_CODING_TYPE, key_frame);
811 if (key_frame) {
812 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
813 mpeg2_skip (mpeg2dec->decoder, 0);
814 } else {
815 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
816 }
818 if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_KEYFRAME && key_frame)
819 mpeg2dec->discont_state = MPEG2DEC_DISC_NONE;
821 time = GST_CLOCK_TIME_NONE;
823 #if MPEG2_RELEASE < MPEG2_VERSION(0,4,0)
824 if (picture->flags & PIC_FLAG_PTS) {
825 time = MPEG_TIME_TO_GST_TIME (picture->pts);
826 GST_DEBUG_OBJECT (mpeg2dec, "picture pts %" G_GUINT64_FORMAT
827 ", time %" GST_TIME_FORMAT, picture->pts, GST_TIME_ARGS (time));
828 }
829 #else
830 if (picture->flags & PIC_FLAG_TAGS) {
831 guint64 pts = (((guint64) picture->tag2) << 32) | picture->tag;
833 time = MPEG_TIME_TO_GST_TIME (pts);
834 GST_DEBUG_OBJECT (mpeg2dec, "picture tags %" G_GUINT64_FORMAT
835 ", time %" GST_TIME_FORMAT, pts, GST_TIME_ARGS (time));
836 }
837 #endif
839 if (time == GST_CLOCK_TIME_NONE) {
840 time = mpeg2dec->next_time;
841 GST_DEBUG_OBJECT (mpeg2dec, "picture didn't have pts");
842 } else {
843 GST_DEBUG_OBJECT (mpeg2dec,
844 "picture had pts %" GST_TIME_FORMAT ", we had %"
845 GST_TIME_FORMAT, GST_TIME_ARGS (time),
846 GST_TIME_ARGS (mpeg2dec->next_time));
847 mpeg2dec->next_time = time;
848 }
849 GST_BUFFER_TIMESTAMP (outbuf) = time;
851 /* TODO set correct offset here based on frame number */
852 if (info->display_picture_2nd) {
853 GST_BUFFER_DURATION (outbuf) = (picture->nb_fields +
854 info->display_picture_2nd->nb_fields) * mpeg2dec->frame_period / 2;
855 } else {
856 GST_BUFFER_DURATION (outbuf) =
857 picture->nb_fields * mpeg2dec->frame_period / 2;
858 }
859 mpeg2dec->next_time += GST_BUFFER_DURATION (outbuf);
861 if (picture->flags & PIC_FLAG_TOP_FIELD_FIRST)
862 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_TFF);
864 #if MPEG2_RELEASE >= MPEG2_VERSION(0,5,0)
865 /* repeat field introduced in 0.5.0 */
866 if (picture->flags & PIC_FLAG_REPEAT_FIRST_FIELD)
867 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_RFF);
868 #endif
870 GST_DEBUG_OBJECT (mpeg2dec,
871 "picture: %s %s %s %s %s fields:%d off:%" G_GINT64_FORMAT " ts:%"
872 GST_TIME_FORMAT,
873 (picture->flags & PIC_FLAG_PROGRESSIVE_FRAME ? "prog" : " "),
874 (picture->flags & PIC_FLAG_TOP_FIELD_FIRST ? "tff" : " "),
875 #if MPEG2_RELEASE >= MPEG2_VERSION(0,5,0)
876 (picture->flags & PIC_FLAG_REPEAT_FIRST_FIELD ? "rff" : " "),
877 #else
878 "unknown rff",
879 #endif
880 (picture->flags & PIC_FLAG_SKIP ? "skip" : " "),
881 (picture->flags & PIC_FLAG_COMPOSITE_DISPLAY ? "composite" : " "),
882 picture->nb_fields, GST_BUFFER_OFFSET (outbuf),
883 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
885 if (mpeg2dec->index) {
886 gst_index_add_association (mpeg2dec->index, mpeg2dec->index_id,
887 (key_frame ? GST_ASSOCIATION_FLAG_KEY_UNIT :
888 GST_ASSOCIATION_FLAG_DELTA_UNIT),
889 GST_FORMAT_BYTES, GST_BUFFER_OFFSET (outbuf),
890 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (outbuf), 0);
891 }
893 if (picture->flags & PIC_FLAG_SKIP)
894 goto skip;
896 if (mpeg2dec->discont_state != MPEG2DEC_DISC_NONE)
897 goto drop;
899 /* check for clipping */
900 if (!clip_buffer (mpeg2dec, outbuf))
901 goto clipped;
903 if (GST_CLOCK_TIME_IS_VALID (time)) {
904 gboolean need_skip;
905 GstClockTime qostime;
907 /* qos needs to be done on running time */
908 qostime = gst_segment_to_running_time (&mpeg2dec->segment, GST_FORMAT_TIME,
909 time);
911 GST_OBJECT_LOCK (mpeg2dec);
912 /* check for QoS, don't perform the last steps of getting and
913 * pushing the buffers that are known to be late. */
914 /* FIXME, we can also entirely skip decoding if the next valid buffer is
915 * known to be after a keyframe (using the granule_shift) */
916 need_skip = mpeg2dec->earliest_time != -1
917 && qostime <= mpeg2dec->earliest_time;
918 GST_OBJECT_UNLOCK (mpeg2dec);
920 if (need_skip)
921 goto dropping_qos;
922 }
924 /* ref before pushing it out, so we still have the ref in our
925 * array of buffers */
926 gst_buffer_ref (outbuf);
928 /* do cropping if the target region is smaller than the input one */
929 if (mpeg2dec->decoded_width != mpeg2dec->width ||
930 mpeg2dec->decoded_height != mpeg2dec->height) {
931 GST_DEBUG_OBJECT (mpeg2dec, "cropping buffer");
932 ret = gst_mpeg2dec_crop_buffer (mpeg2dec, &outbuf);
933 if (ret != GST_FLOW_OK)
934 goto done;
935 }
937 if (mpeg2dec->segment.rate >= 0.0) {
938 /* forward: push right away */
939 GST_LOG_OBJECT (mpeg2dec, "pushing buffer %p, timestamp %"
940 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
941 outbuf,
942 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
943 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
944 GST_LOG_OBJECT (mpeg2dec, "... with flags %x", GST_BUFFER_FLAGS (outbuf));
946 ret = gst_pad_push (mpeg2dec->srcpad, outbuf);
947 GST_DEBUG_OBJECT (mpeg2dec, "pushed with result %s",
948 gst_flow_get_name (ret));
949 } else {
950 /* reverse: queue, we'll push in reverse when we receive the next (previous)
951 * keyframe. */
952 GST_DEBUG_OBJECT (mpeg2dec, "queued frame");
953 mpeg2dec->queued = g_list_prepend (mpeg2dec->queued, outbuf);
954 ret = GST_FLOW_OK;
955 }
957 done:
959 return ret;
961 /* special cases */
962 no_display:
963 {
964 GST_DEBUG_OBJECT (mpeg2dec, "no picture to display");
965 return GST_FLOW_OK;
966 }
967 skip:
968 {
969 GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer because of skip flag");
970 return GST_FLOW_OK;
971 }
972 drop:
973 {
974 GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer, discont state %d",
975 mpeg2dec->discont_state);
976 return GST_FLOW_OK;
977 }
978 clipped:
979 {
980 GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer, clipped");
981 return GST_FLOW_OK;
982 }
983 dropping_qos:
984 {
985 GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer because of QoS");
986 return GST_FLOW_OK;
987 }
988 }
990 #if 0
991 static void
992 update_streaminfo (GstMpeg2dec * mpeg2dec)
993 {
994 GstCaps *caps;
995 GstProps *props;
996 GstPropsEntry *entry;
997 const mpeg2_info_t *info;
999 info = mpeg2_info (mpeg2dec->decoder);
1001 props = gst_props_empty_new ();
1003 entry =
1004 gst_props_entry_new ("framerate",
1005 G_TYPE_DOUBLE (GST_SECOND / (float) mpeg2dec->frame_period));
1006 gst_props_add_entry (props, entry);
1007 entry =
1008 gst_props_entry_new ("bitrate",
1009 G_TYPE_INT (info->sequence->byte_rate * 8));
1010 gst_props_add_entry (props, entry);
1012 caps = gst_caps_new ("mpeg2dec_streaminfo",
1013 "application/x-gst-streaminfo", props);
1015 gst_caps_replace_sink (&mpeg2dec->streaminfo, caps);
1016 g_object_notify (G_OBJECT (mpeg2dec), "streaminfo");
1017 }
1018 #endif
1020 static GstFlowReturn
1021 gst_mpeg2dec_chain (GstPad * pad, GstBuffer * buf)
1022 {
1023 GstMpeg2dec *mpeg2dec;
1024 guint32 size;
1025 guint8 *data, *end;
1026 GstClockTime pts;
1027 const mpeg2_info_t *info;
1028 mpeg2_state_t state;
1029 gboolean done = FALSE;
1030 GstFlowReturn ret = GST_FLOW_OK;
1032 mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1034 size = GST_BUFFER_SIZE (buf);
1035 data = GST_BUFFER_DATA (buf);
1036 pts = GST_BUFFER_TIMESTAMP (buf);
1038 if (GST_BUFFER_IS_DISCONT (buf)) {
1039 GST_LOG_OBJECT (mpeg2dec, "DISCONT, reset decoder");
1040 /* when we receive a discont, reset our state as to not create too much
1041 * distortion in the picture due to missing packets */
1042 mpeg2_reset (mpeg2dec->decoder, 0);
1043 mpeg2_skip (mpeg2dec->decoder, 1);
1044 mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
1045 }
1047 GST_LOG_OBJECT (mpeg2dec, "received buffer, timestamp %"
1048 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
1049 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1050 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1052 info = mpeg2dec->info;
1053 end = data + size;
1055 mpeg2dec->offset = GST_BUFFER_OFFSET (buf);
1057 if (pts != GST_CLOCK_TIME_NONE) {
1058 gint64 mpeg_pts = GST_TIME_TO_MPEG_TIME (pts);
1060 GST_DEBUG_OBJECT (mpeg2dec,
1061 "have pts: %" G_GINT64_FORMAT " (%" GST_TIME_FORMAT ")",
1062 mpeg_pts, GST_TIME_ARGS (MPEG_TIME_TO_GST_TIME (mpeg_pts)));
1064 #if MPEG2_RELEASE >= MPEG2_VERSION(0,4,0)
1065 mpeg2_tag_picture (mpeg2dec->decoder, mpeg_pts & 0xffffffff,
1066 mpeg_pts >> 32);
1067 #else
1068 mpeg2_pts (mpeg2dec->decoder, mpeg_pts);
1069 #endif
1070 } else {
1071 GST_LOG ("no pts");
1072 }
1074 GST_LOG_OBJECT (mpeg2dec, "calling mpeg2_buffer");
1075 mpeg2_buffer (mpeg2dec->decoder, data, end);
1076 GST_LOG_OBJECT (mpeg2dec, "calling mpeg2_buffer done");
1078 while (!done) {
1079 GST_LOG_OBJECT (mpeg2dec, "calling parse");
1080 state = mpeg2_parse (mpeg2dec->decoder);
1081 GST_DEBUG_OBJECT (mpeg2dec, "parse state %d", state);
1083 switch (state) {
1084 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
1085 case STATE_SEQUENCE_MODIFIED:
1086 GST_DEBUG_OBJECT (mpeg2dec, "sequence modified");
1087 /* fall through */
1088 #endif
1089 case STATE_SEQUENCE:
1090 ret = handle_sequence (mpeg2dec, info);
1091 /* if there is an error handling the sequence
1092 * reset the decoder, maybe something more elegant
1093 * could be done.
1094 */
1095 if (ret == GST_FLOW_ERROR) {
1096 mpeg2dec->error_count++;
1097 GST_WARNING_OBJECT (mpeg2dec, "Decoding error #%d",
1098 mpeg2dec->error_count);
1099 if (mpeg2dec->error_count >= WARN_THRESHOLD && WARN_THRESHOLD > 0) {
1100 GST_ELEMENT_WARNING (mpeg2dec, STREAM, DECODE,
1101 ("%d consecutive decoding errors", mpeg2dec->error_count),
1102 (NULL));
1103 }
1104 mpeg2_reset (mpeg2dec->decoder, 0);
1105 mpeg2_skip (mpeg2dec->decoder, 1);
1106 mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
1108 goto exit;
1109 }
1110 break;
1111 case STATE_SEQUENCE_REPEATED:
1112 GST_DEBUG_OBJECT (mpeg2dec, "sequence repeated");
1113 break;
1114 case STATE_GOP:
1115 GST_DEBUG_OBJECT (mpeg2dec, "gop");
1116 break;
1117 case STATE_PICTURE:
1118 ret = handle_picture (mpeg2dec, info);
1119 break;
1120 case STATE_SLICE_1ST:
1121 GST_LOG_OBJECT (mpeg2dec, "1st slice of frame encountered");
1122 break;
1123 case STATE_PICTURE_2ND:
1124 GST_LOG_OBJECT (mpeg2dec,
1125 "Second picture header encountered. Decoding 2nd field");
1126 break;
1127 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 4, 0)
1128 case STATE_INVALID_END:
1129 GST_DEBUG_OBJECT (mpeg2dec, "invalid end");
1130 #endif
1131 case STATE_END:
1132 GST_DEBUG_OBJECT (mpeg2dec, "end");
1133 mpeg2dec->need_sequence = TRUE;
1134 case STATE_SLICE:
1135 ret = handle_slice (mpeg2dec, info);
1136 break;
1137 case STATE_BUFFER:
1138 done = TRUE;
1139 break;
1140 /* error */
1141 case STATE_INVALID:
1142 /* FIXME: at some point we should probably send newsegment events to
1143 * let downstream know that parts of the stream are missing */
1144 mpeg2dec->error_count++;
1145 GST_WARNING_OBJECT (mpeg2dec, "Decoding error #%d",
1146 mpeg2dec->error_count);
1147 if (mpeg2dec->error_count >= WARN_THRESHOLD && WARN_THRESHOLD > 0) {
1148 GST_ELEMENT_WARNING (mpeg2dec, STREAM, DECODE,
1149 ("%d consecutive decoding errors", mpeg2dec->error_count),
1150 (NULL));
1151 }
1152 goto exit;
1153 default:
1154 GST_ERROR_OBJECT (mpeg2dec, "Unknown libmpeg2 state %d, FIXME", state);
1155 goto exit;
1156 }
1158 mpeg2dec->error_count = 0;
1160 /*
1161 * FIXME: should pass more information such as state the user data is from
1162 */
1163 #ifdef enable_user_data
1164 if (info->user_data_len > 0) {
1165 GstBuffer *udbuf = gst_buffer_new_and_alloc (info->user_data_len);
1167 memcpy (GST_BUFFER_DATA (udbuf), info->user_data, info->user_data_len);
1169 gst_pad_push (mpeg2dec->userdatapad, udbuf);
1170 }
1171 #endif
1173 if (ret != GST_FLOW_OK) {
1174 GST_DEBUG_OBJECT (mpeg2dec, "exit loop, reason %s",
1175 gst_flow_get_name (ret));
1176 break;
1177 }
1178 }
1179 done:
1180 gst_buffer_unref (buf);
1181 return ret;
1183 /* errors */
1184 exit:
1185 {
1186 ret = GST_FLOW_OK;
1187 goto done;
1188 }
1189 }
1191 static gboolean
1192 gst_mpeg2dec_sink_event (GstPad * pad, GstEvent * event)
1193 {
1194 GstMpeg2dec *mpeg2dec;
1195 gboolean ret = TRUE;
1197 mpeg2dec = GST_MPEG2DEC (gst_pad_get_parent (pad));
1199 GST_DEBUG_OBJECT (mpeg2dec, "Got %s event on sink pad",
1200 GST_EVENT_TYPE_NAME (event));
1202 switch (GST_EVENT_TYPE (event)) {
1203 case GST_EVENT_NEWSEGMENT:
1204 {
1205 gboolean update;
1206 GstFormat format;
1207 gdouble rate, arate;
1208 gint64 start, stop, time;
1210 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1211 &start, &stop, &time);
1213 /* we need TIME and a positive rate */
1214 if (format != GST_FORMAT_TIME)
1215 goto newseg_wrong_format;
1217 /* now configure the values */
1218 gst_segment_set_newsegment_full (&mpeg2dec->segment, update,
1219 rate, arate, format, start, stop, time);
1221 GST_DEBUG_OBJECT (mpeg2dec,
1222 "Pushing newseg rate %g, applied rate %g, format %d, start %"
1223 G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", pos %" G_GINT64_FORMAT,
1224 rate, arate, format, start, stop, time);
1226 ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1227 break;
1228 }
1229 case GST_EVENT_FLUSH_START:
1230 ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1231 break;
1232 case GST_EVENT_FLUSH_STOP:
1233 {
1234 mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
1235 mpeg2dec->next_time = -1;;
1236 gst_mpeg2dec_qos_reset (mpeg2dec);
1237 mpeg2_reset (mpeg2dec->decoder, 0);
1238 mpeg2_skip (mpeg2dec->decoder, 1);
1239 clear_queued (mpeg2dec);
1240 ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1241 break;
1242 }
1243 case GST_EVENT_EOS:
1244 if (mpeg2dec->index && mpeg2dec->closed) {
1245 gst_index_commit (mpeg2dec->index, mpeg2dec->index_id);
1246 }
1247 ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1248 break;
1249 default:
1250 ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1251 break;
1252 }
1254 done:
1255 gst_object_unref (mpeg2dec);
1257 return ret;
1259 /* ERRORS */
1260 newseg_wrong_format:
1261 {
1262 GST_DEBUG_OBJECT (mpeg2dec, "received non TIME newsegment");
1263 gst_event_unref (event);
1264 goto done;
1265 }
1266 }
1268 static gboolean
1269 gst_mpeg2dec_setcaps (GstPad * pad, GstCaps * caps)
1270 {
1271 GstMpeg2dec *mpeg2dec;
1272 GstStructure *s;
1274 mpeg2dec = GST_MPEG2DEC (gst_pad_get_parent (pad));
1276 s = gst_caps_get_structure (caps, 0);
1278 /* parse the par, this overrides the encoded par */
1279 mpeg2dec->have_par = gst_structure_get_fraction (s, "pixel-aspect-ratio",
1280 &mpeg2dec->pixel_width, &mpeg2dec->pixel_height);
1282 gst_object_unref (mpeg2dec);
1284 return TRUE;
1285 }
1287 static gboolean
1288 gst_mpeg2dec_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
1289 GstFormat * dest_format, gint64 * dest_value)
1290 {
1291 gboolean res = TRUE;
1292 GstMpeg2dec *mpeg2dec;
1293 const mpeg2_info_t *info;
1295 mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1297 if (mpeg2dec->decoder == NULL)
1298 return FALSE;
1300 if (src_format == *dest_format) {
1301 *dest_value = src_value;
1302 return TRUE;
1303 }
1305 info = mpeg2_info (mpeg2dec->decoder);
1307 switch (src_format) {
1308 case GST_FORMAT_BYTES:
1309 switch (*dest_format) {
1310 case GST_FORMAT_TIME:
1311 if (info->sequence && info->sequence->byte_rate) {
1312 *dest_value =
1313 gst_util_uint64_scale (GST_SECOND, src_value,
1314 info->sequence->byte_rate);
1315 GST_WARNING_OBJECT (mpeg2dec, "dest_value:%" GST_TIME_FORMAT,
1316 GST_TIME_ARGS (*dest_value));
1317 break;
1318 } else if (info->sequence)
1319 GST_WARNING_OBJECT (mpeg2dec,
1320 "Cannot convert from BYTES to TIME since we don't know the bitrate at this point.");
1321 default:
1322 res = FALSE;
1323 }
1324 break;
1325 case GST_FORMAT_TIME:
1326 switch (*dest_format) {
1327 case GST_FORMAT_BYTES:
1328 if (info->sequence && info->sequence->byte_rate) {
1329 *dest_value =
1330 gst_util_uint64_scale_int (src_value, info->sequence->byte_rate,
1331 GST_SECOND);
1332 break;
1333 } else if (info->sequence)
1334 GST_WARNING_OBJECT (mpeg2dec,
1335 "Cannot convert from TIME to BYTES since we don't know the bitrate at this point.");
1336 default:
1337 res = FALSE;
1338 }
1339 break;
1340 default:
1341 res = FALSE;
1342 }
1343 return res;
1344 }
1347 static gboolean
1348 gst_mpeg2dec_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
1349 GstFormat * dest_format, gint64 * dest_value)
1350 {
1351 gboolean res = TRUE;
1352 GstMpeg2dec *mpeg2dec;
1353 const mpeg2_info_t *info;
1354 guint64 scale = 1;
1356 mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1358 if (mpeg2dec->decoder == NULL)
1359 return FALSE;
1361 if (src_format == *dest_format) {
1362 *dest_value = src_value;
1363 return TRUE;
1364 }
1366 info = mpeg2_info (mpeg2dec->decoder);
1368 switch (src_format) {
1369 case GST_FORMAT_BYTES:
1370 switch (*dest_format) {
1371 case GST_FORMAT_TIME:
1372 default:
1373 res = FALSE;
1374 }
1375 break;
1376 case GST_FORMAT_TIME:
1377 switch (*dest_format) {
1378 case GST_FORMAT_BYTES:
1379 scale = 6 * (mpeg2dec->width * mpeg2dec->height >> 2);
1380 case GST_FORMAT_DEFAULT:
1381 if (info->sequence && mpeg2dec->frame_period) {
1382 *dest_value =
1383 gst_util_uint64_scale_int (src_value, scale,
1384 mpeg2dec->frame_period);
1385 break;
1386 }
1387 default:
1388 res = FALSE;
1389 }
1390 break;
1391 case GST_FORMAT_DEFAULT:
1392 switch (*dest_format) {
1393 case GST_FORMAT_TIME:
1394 *dest_value = src_value * mpeg2dec->frame_period;
1395 break;
1396 case GST_FORMAT_BYTES:
1397 *dest_value =
1398 src_value * 6 * ((mpeg2dec->width * mpeg2dec->height) >> 2);
1399 break;
1400 default:
1401 res = FALSE;
1402 }
1403 break;
1404 default:
1405 res = FALSE;
1406 }
1407 return res;
1408 }
1410 static const GstQueryType *
1411 gst_mpeg2dec_get_src_query_types (GstPad * pad)
1412 {
1413 static const GstQueryType types[] = {
1414 GST_QUERY_POSITION,
1415 GST_QUERY_DURATION,
1416 0
1417 };
1419 return types;
1420 }
1422 static gboolean
1423 gst_mpeg2dec_src_query (GstPad * pad, GstQuery * query)
1424 {
1425 gboolean res = TRUE;
1426 GstMpeg2dec *mpeg2dec;
1428 mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1430 switch (GST_QUERY_TYPE (query)) {
1431 case GST_QUERY_POSITION:
1432 {
1433 GstFormat format;
1434 gint64 cur;
1436 /* First, we try to ask upstream, which might know better, especially in
1437 * the case of DVDs, with multiple chapter */
1438 if ((res = gst_pad_peer_query (mpeg2dec->sinkpad, query)))
1439 break;
1441 /* save requested format */
1442 gst_query_parse_position (query, &format, NULL);
1444 /* and convert to the requested format */
1445 if (!gst_mpeg2dec_src_convert (pad, GST_FORMAT_TIME,
1446 mpeg2dec->next_time, &format, &cur))
1447 goto error;
1449 cur = gst_segment_to_stream_time (&mpeg2dec->segment, format, cur);
1450 if (cur == -1)
1451 goto error;
1453 gst_query_set_position (query, format, cur);
1455 GST_LOG_OBJECT (mpeg2dec,
1456 "position query: we return %" G_GUINT64_FORMAT " (format %u)", cur,
1457 format);
1458 break;
1459 }
1460 case GST_QUERY_DURATION:
1461 {
1462 GstFormat format;
1463 GstFormat rformat;
1464 gint64 total, total_bytes;
1465 GstPad *peer;
1467 if ((peer = gst_pad_get_peer (mpeg2dec->sinkpad)) == NULL)
1468 goto error;
1470 /* save requested format */
1471 gst_query_parse_duration (query, &format, NULL);
1473 /* send to peer */
1474 if ((res = gst_pad_query (peer, query))) {
1475 gst_object_unref (peer);
1476 goto done;
1477 } else {
1478 GST_LOG_OBJECT (mpeg2dec, "query on peer pad failed, trying bytes");
1479 }
1481 /* query peer for total length in bytes */
1482 gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
1484 if (!(res = gst_pad_query (peer, query))) {
1485 GST_LOG_OBJECT (mpeg2dec, "query on peer pad failed");
1486 gst_object_unref (peer);
1487 goto error;
1488 }
1489 gst_object_unref (peer);
1491 /* get the returned format */
1492 gst_query_parse_duration (query, &rformat, &total_bytes);
1493 GST_LOG_OBJECT (mpeg2dec,
1494 "peer pad returned total=%" G_GINT64_FORMAT " bytes", total_bytes);
1496 if (total_bytes != -1) {
1497 if (!gst_mpeg2dec_sink_convert (pad, GST_FORMAT_BYTES, total_bytes,
1498 &format, &total))
1499 goto error;
1500 } else {
1501 total = -1;
1502 }
1504 gst_query_set_duration (query, format, total);
1506 GST_LOG_OBJECT (mpeg2dec,
1507 "position query: we return %" G_GUINT64_FORMAT " (format %u)", total,
1508 format);
1509 break;
1510 }
1511 default:
1512 res = gst_pad_query_default (pad, query);
1513 break;
1514 }
1515 done:
1516 return res;
1518 error:
1520 GST_DEBUG ("error handling query");
1521 return FALSE;
1522 }
1525 #if 0
1526 static const GstEventMask *
1527 gst_mpeg2dec_get_event_masks (GstPad * pad)
1528 {
1529 static const GstEventMask masks[] = {
1530 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
1531 {GST_EVENT_NAVIGATION, GST_EVENT_FLAG_NONE},
1532 {0,}
1533 };
1535 return masks;
1536 }
1537 #endif
1539 static gboolean
1540 index_seek (GstPad * pad, GstEvent * event)
1541 {
1542 GstIndexEntry *entry;
1543 GstMpeg2dec *mpeg2dec;
1544 gdouble rate;
1545 GstFormat format;
1546 GstSeekFlags flags;
1547 GstSeekType cur_type, stop_type;
1548 gint64 cur, stop;
1550 mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1552 gst_event_parse_seek (event, &rate, &format, &flags,
1553 &cur_type, &cur, &stop_type, &stop);
1555 entry = gst_index_get_assoc_entry (mpeg2dec->index, mpeg2dec->index_id,
1556 GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_KEY_UNIT, format, cur);
1558 if ((entry) && gst_pad_is_linked (mpeg2dec->sinkpad)) {
1559 const GstFormat *peer_formats, *try_formats;
1561 /* since we know the exact byteoffset of the frame, make sure to seek on bytes first */
1562 const GstFormat try_all_formats[] = {
1563 GST_FORMAT_BYTES,
1564 GST_FORMAT_TIME,
1565 0
1566 };
1568 try_formats = try_all_formats;
1570 #if 0
1571 peer_formats = gst_pad_get_formats (GST_PAD_PEER (mpeg2dec->sinkpad));
1572 #else
1573 peer_formats = try_all_formats; /* FIXE ME */
1574 #endif
1576 while (gst_formats_contains (peer_formats, *try_formats)) {
1577 gint64 value;
1579 if (gst_index_entry_assoc_map (entry, *try_formats, &value)) {
1580 GstEvent *seek_event;
1582 GST_DEBUG_OBJECT (mpeg2dec, "index %s %" G_GINT64_FORMAT
1583 " -> %s %" G_GINT64_FORMAT,
1584 gst_format_get_details (format)->nick,
1585 cur, gst_format_get_details (*try_formats)->nick, value);
1587 /* lookup succeeded, create the seek */
1588 seek_event =
1589 gst_event_new_seek (rate, *try_formats, flags, cur_type, value,
1590 stop_type, stop);
1591 /* do the seek */
1592 if (gst_pad_push_event (mpeg2dec->sinkpad, seek_event)) {
1593 /* seek worked, we're done, loop will exit */
1594 #if 0
1595 mpeg2dec->segment_start = GST_EVENT_SEEK_OFFSET (event);
1596 #endif
1597 return TRUE;
1598 }
1599 }
1600 try_formats++;
1601 }
1602 }
1603 return FALSE;
1604 }
1606 static gboolean
1607 normal_seek (GstPad * pad, GstEvent * event)
1608 {
1609 gdouble rate;
1610 GstFormat format, conv;
1611 GstSeekFlags flags;
1612 GstSeekType cur_type, stop_type;
1613 gint64 cur, stop;
1614 gint64 time_cur, bytes_cur;
1615 gint64 time_stop, bytes_stop;
1616 gboolean res;
1617 GstMpeg2dec *mpeg2dec;
1618 GstEvent *peer_event;
1620 mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1622 GST_DEBUG ("normal seek");
1624 gst_event_parse_seek (event, &rate, &format, &flags,
1625 &cur_type, &cur, &stop_type, &stop);
1627 conv = GST_FORMAT_TIME;
1628 if (!gst_mpeg2dec_src_convert (pad, format, cur, &conv, &time_cur))
1629 goto convert_failed;
1630 if (!gst_mpeg2dec_src_convert (pad, format, stop, &conv, &time_stop))
1631 goto convert_failed;
1633 GST_DEBUG ("seek to time %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1634 GST_TIME_ARGS (time_cur), GST_TIME_ARGS (time_stop));
1636 peer_event = gst_event_new_seek (rate, GST_FORMAT_TIME, flags,
1637 cur_type, time_cur, stop_type, time_stop);
1639 /* try seek on time then */
1640 if ((res = gst_pad_push_event (mpeg2dec->sinkpad, peer_event)))
1641 goto done;
1643 /* else we try to seek on bytes */
1644 conv = GST_FORMAT_BYTES;
1645 if (!gst_mpeg2dec_sink_convert (pad, GST_FORMAT_TIME, time_cur,
1646 &conv, &bytes_cur))
1647 goto convert_failed;
1648 if (!gst_mpeg2dec_sink_convert (pad, GST_FORMAT_TIME, time_stop,
1649 &conv, &bytes_stop))
1650 goto convert_failed;
1652 /* conversion succeeded, create the seek */
1653 peer_event =
1654 gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
1655 cur_type, bytes_cur, stop_type, bytes_stop);
1657 /* do the seek */
1658 res = gst_pad_push_event (mpeg2dec->sinkpad, peer_event);
1660 done:
1661 return res;
1663 /* ERRORS */
1664 convert_failed:
1665 {
1666 /* probably unsupported seek format */
1667 GST_DEBUG_OBJECT (mpeg2dec,
1668 "failed to convert format %u into GST_FORMAT_TIME", format);
1669 return FALSE;
1670 }
1671 }
1674 static gboolean
1675 gst_mpeg2dec_src_event (GstPad * pad, GstEvent * event)
1676 {
1677 gboolean res;
1678 GstMpeg2dec *mpeg2dec;
1680 mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1682 if (mpeg2dec->decoder == NULL)
1683 goto no_decoder;
1685 switch (GST_EVENT_TYPE (event)) {
1686 /* the all-formats seek logic */
1687 case GST_EVENT_SEEK:{
1688 gst_event_ref (event);
1689 if (!(res = gst_pad_push_event (mpeg2dec->sinkpad, event))) {
1690 if (mpeg2dec->index)
1691 res = index_seek (pad, event);
1692 else
1693 res = normal_seek (pad, event);
1694 }
1695 gst_event_unref (event);
1696 break;
1697 }
1698 case GST_EVENT_QOS:
1699 {
1700 gdouble proportion;
1701 GstClockTimeDiff diff;
1702 GstClockTime timestamp;
1704 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1706 GST_OBJECT_LOCK (mpeg2dec);
1707 mpeg2dec->proportion = proportion;
1708 mpeg2dec->earliest_time = timestamp + diff;
1709 GST_OBJECT_UNLOCK (mpeg2dec);
1711 GST_DEBUG_OBJECT (mpeg2dec,
1712 "got QoS %" GST_TIME_FORMAT ", %" G_GINT64_FORMAT,
1713 GST_TIME_ARGS (timestamp), diff);
1715 res = gst_pad_push_event (mpeg2dec->sinkpad, event);
1716 break;
1717 }
1718 case GST_EVENT_NAVIGATION:
1719 /* Forward a navigation event unchanged */
1720 default:
1721 res = gst_pad_push_event (mpeg2dec->sinkpad, event);
1722 break;
1723 }
1724 return res;
1726 no_decoder:
1727 {
1728 GST_DEBUG_OBJECT (mpeg2dec, "no decoder, cannot handle event");
1729 gst_event_unref (event);
1730 return FALSE;
1731 }
1732 }
1734 static GstStateChangeReturn
1735 gst_mpeg2dec_change_state (GstElement * element, GstStateChange transition)
1736 {
1737 GstStateChangeReturn ret;
1738 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (element);
1740 switch (transition) {
1741 case GST_STATE_CHANGE_NULL_TO_READY:
1742 mpeg2_accel (MPEG2_ACCEL_DETECT);
1743 if ((mpeg2dec->decoder = mpeg2_init ()) == NULL)
1744 goto init_failed;
1745 mpeg2dec->info = mpeg2_info (mpeg2dec->decoder);
1746 break;
1747 case GST_STATE_CHANGE_READY_TO_PAUSED:
1748 gst_mpeg2dec_reset (mpeg2dec);
1749 gst_mpeg2dec_qos_reset (mpeg2dec);
1750 break;
1751 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1752 default:
1753 break;
1754 }
1756 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1758 switch (transition) {
1759 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1760 break;
1761 case GST_STATE_CHANGE_PAUSED_TO_READY:
1762 gst_mpeg2dec_qos_reset (mpeg2dec);
1763 clear_queued (mpeg2dec);
1764 break;
1765 case GST_STATE_CHANGE_READY_TO_NULL:
1766 if (mpeg2dec->decoder) {
1767 mpeg2_close (mpeg2dec->decoder);
1768 mpeg2dec->decoder = NULL;
1769 mpeg2dec->info = NULL;
1770 }
1771 clear_buffers (mpeg2dec);
1772 break;
1773 default:
1774 break;
1775 }
1776 return ret;
1778 /* ERRORS */
1779 init_failed:
1780 {
1781 GST_ELEMENT_ERROR (mpeg2dec, LIBRARY, INIT,
1782 (NULL), ("Failed to initialize libmpeg2 library"));
1783 return GST_STATE_CHANGE_FAILURE;
1784 }
1785 }
1787 static gboolean
1788 plugin_init (GstPlugin * plugin)
1789 {
1790 if (!gst_element_register (plugin, "mpeg2dec", GST_RANK_PRIMARY,
1791 GST_TYPE_MPEG2DEC))
1792 return FALSE;
1794 return TRUE;
1795 }
1797 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1798 GST_VERSION_MINOR,
1799 "mpeg2dec",
1800 "LibMpeg2 decoder", plugin_init, VERSION, "GPL", GST_PACKAGE_NAME,
1801 GST_PACKAGE_ORIGIN);