2 * GStreamer codec plugin for Tizen Emulator.
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved.
7 * KiTae Kim <kt920.kim@samsung.com>
8 * SeokYeon Hwang <syeon.hwang@samsung.com>
9 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
31 // #include "gstmaru.h"
32 #include "gstmarudevice.h"
33 #include "gstmaruutils.h"
34 #include "gstmaruinterface.h"
35 // #include "gstmarudevice.h"
37 #define GST_MARUDEC_PARAMS_QDATA g_quark_from_static_string("marudec-params")
39 /* indicate dts, pts, offset in the stream */
41 #define GST_TS_INFO_NONE &ts_info_none
42 static const GstTSInfo ts_info_none = { -1, -1, -1, -1 };
44 typedef struct _GstMaruDecClass
46 GstElementClass parent_class;
49 GstPadTemplate *sinktempl;
50 GstPadTemplate *srctempl;
54 static GstElementClass *parent_class = NULL;
56 static void gst_marudec_base_init (GstMaruDecClass *klass);
57 static void gst_marudec_class_init (GstMaruDecClass *klass);
58 static void gst_marudec_init (GstMaruDec *marudec);
59 static void gst_marudec_finalize (GObject *object);
61 static gboolean gst_marudec_setcaps (GstPad *pad, GstCaps *caps);
64 static gboolean gst_marudec_sink_event (GstPad *pad, GstEvent *event);
65 static GstFlowReturn gst_marudec_chain (GstPad *pad, GstBuffer *buffer);
68 static gboolean gst_marudec_src_event (GstPad *pad, GstEvent *event);
69 static GstStateChangeReturn gst_marudec_change_state (GstElement *element,
70 GstStateChange transition);
72 static gboolean gst_marudec_negotiate (GstMaruDec *dec, gboolean force);
74 static gint gst_marudec_frame (GstMaruDec *marudec, guint8 *data,
75 guint size, gint *got_data,
76 const GstTSInfo *dec_info, gint64 in_offset, GstFlowReturn *ret);
78 static gboolean gst_marudec_open (GstMaruDec *marudec);
79 static int gst_marudec_close (GstMaruDec *marudec);
82 static const GstTSInfo *
83 gst_ts_info_store (GstMaruDec *dec, GstClockTime timestamp,
84 GstClockTime duration, gint64 offset)
86 gint idx = dec->ts_idx;
87 dec->ts_info[idx].idx = idx;
88 dec->ts_info[idx].timestamp = timestamp;
89 dec->ts_info[idx].duration = duration;
90 dec->ts_info[idx].offset = offset;
91 dec->ts_idx = (idx + 1) & MAX_TS_MASK;
93 return &dec->ts_info[idx];
96 static const GstTSInfo *
97 gst_ts_info_get (GstMaruDec *dec, gint idx)
99 if (G_UNLIKELY (idx < 0 || idx > MAX_TS_MASK))
100 return GST_TS_INFO_NONE;
102 return &dec->ts_info[idx];
106 gst_marudec_reset_ts (GstMaruDec *marudec)
108 marudec->next_out = GST_CLOCK_TIME_NONE;
112 gst_marudec_update_qos (GstMaruDec *marudec, gdouble proportion,
113 GstClockTime timestamp)
115 GST_LOG_OBJECT (marudec, "update QOS: %f, %" GST_TIME_FORMAT,
116 proportion, GST_TIME_ARGS (timestamp));
118 GST_OBJECT_LOCK (marudec);
119 marudec->proportion = proportion;
120 marudec->earliest_time = timestamp;
121 GST_OBJECT_UNLOCK (marudec);
125 gst_marudec_reset_qos (GstMaruDec *marudec)
127 gst_marudec_update_qos (marudec, 0.5, GST_CLOCK_TIME_NONE);
128 marudec->processed = 0;
129 marudec->dropped = 0;
133 gst_marudec_do_qos (GstMaruDec *marudec, GstClockTime timestamp,
134 gboolean *mode_switch)
136 GstClockTimeDiff diff;
138 GstClockTime qostime, earliest_time;
141 *mode_switch = FALSE;
143 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
144 marudec->processed++;
148 proportion = marudec->proportion;
149 earliest_time = marudec->earliest_time;
151 qostime = gst_segment_to_running_time (&marudec->segment, GST_FORMAT_TIME,
154 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (qostime))) {
155 marudec->processed++;
159 diff = GST_CLOCK_DIFF (qostime, earliest_time);
161 if (proportion < 0.4 && diff < 0 ){
162 marudec->processed++;
166 // if (marudec->waiting_for_key) {
175 GstClockTime stream_time, jitter;
180 gst_segment_to_stream_time (&marudec->segment, GST_FORMAT_TIME,
182 jitter = GST_CLOCK_DIFF (qostime, earliest_time);
184 gst_message_new_qos (GST_OBJECT_CAST (marudec), FALSE, qostime,
185 stream_time, timestamp, GST_CLOCK_TIME_NONE);
186 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
187 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
188 marudec->processed, marudec->dropped);
189 gst_element_post_message (GST_ELEMENT_CAST (marudec), qos_msg);
195 marudec->processed++;
200 clear_queued (GstMaruDec *marudec)
202 g_list_foreach (marudec->queued, (GFunc) gst_mini_object_unref, NULL);
203 g_list_free (marudec->queued);
204 marudec->queued = NULL;
208 flush_queued (GstMaruDec *marudec)
210 GstFlowReturn res = GST_FLOW_OK;
212 CODEC_LOG (DEBUG, "flush queued\n");
214 while (marudec->queued) {
215 GstBuffer *buf = GST_BUFFER_CAST (marudec->queued->data);
217 GST_LOG_OBJECT (marudec, "pushing buffer %p, offset %"
218 G_GUINT64_FORMAT ", timestamp %"
219 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT, buf,
220 GST_BUFFER_OFFSET (buf),
221 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
222 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
224 res = gst_pad_push (marudec->srcpad, buf);
227 g_list_delete_link (marudec->queued, marudec->queued);
234 gst_marudec_drain (GstMaruDec *marudec)
236 GstMaruDecClass *oclass;
238 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
240 CODEC_LOG (DEBUG, "drain frame\n");
243 gint have_data, len, try = 0;
249 gst_marudec_frame (marudec, NULL, 0, &have_data, &ts_info_none, 0, &ret);
251 if (len < 0 || have_data == 0) {
254 } while (try++ < 10);
258 if (marudec->segment.rate < 0.0) {
259 CODEC_LOG (DEBUG, "reverse playback\n");
260 flush_queued (marudec);
268 gst_marudec_base_init (GstMaruDecClass *klass)
270 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
271 GstCaps *sinkcaps = NULL, *srccaps = NULL;
272 GstPadTemplate *sinktempl, *srctempl;
274 gchar *longname, *classification, *description;
277 (CodecElement *)g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
278 GST_MARUDEC_PARAMS_QDATA);
280 longname = g_strdup_printf ("%s Decoder", codec->longname);
281 classification = g_strdup_printf ("Codec/Decoder/%s",
282 (codec->media_type == AVMEDIA_TYPE_VIDEO) ?
284 description = g_strdup_printf("%s Decoder", codec->name);
286 gst_element_class_set_details_simple (element_class,
290 "Kitae Kim <kt920.kim@samsung.com>");
293 g_free (classification);
294 g_free (description);
296 sinkcaps = gst_maru_codecname_to_caps (codec->name, NULL, FALSE);
298 sinkcaps = gst_caps_from_string ("unknown/unknown");
301 switch (codec->media_type) {
302 case AVMEDIA_TYPE_VIDEO:
303 srccaps = gst_caps_from_string ("video/x-raw-rgb; video/x-raw-yuv");
305 case AVMEDIA_TYPE_AUDIO:
306 srccaps = gst_maru_codectype_to_audio_caps (NULL, codec->name, FALSE, codec);
309 GST_LOG("unknown media type.\n");
314 srccaps = gst_caps_from_string ("unknown/unknown");
317 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
318 GST_PAD_ALWAYS, sinkcaps);
319 srctempl = gst_pad_template_new ("src", GST_PAD_SRC,
320 GST_PAD_ALWAYS, srccaps);
322 gst_element_class_add_pad_template (element_class, srctempl);
323 gst_element_class_add_pad_template (element_class, sinktempl);
325 klass->codec = codec;
326 klass->sinktempl = sinktempl;
327 klass->srctempl = srctempl;
331 gst_marudec_class_init (GstMaruDecClass *klass)
333 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
334 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
336 parent_class = g_type_class_peek_parent (klass);
339 gobject_class->set_property = gst_marudec_set_property
340 gobject_class->get_property = gst_marudec_get_property
343 gobject_class->finalize = gst_marudec_finalize;
344 gstelement_class->change_state = gst_marudec_change_state;
348 gst_marudec_init (GstMaruDec *marudec)
350 GstMaruDecClass *oclass;
352 oclass = (GstMaruDecClass*) (G_OBJECT_GET_CLASS(marudec));
354 marudec->sinkpad = gst_pad_new_from_template (oclass->sinktempl, "sink");
355 gst_pad_set_setcaps_function (marudec->sinkpad,
356 GST_DEBUG_FUNCPTR(gst_marudec_setcaps));
357 gst_pad_set_event_function (marudec->sinkpad,
358 GST_DEBUG_FUNCPTR(gst_marudec_sink_event));
359 gst_pad_set_chain_function (marudec->sinkpad,
360 GST_DEBUG_FUNCPTR(gst_marudec_chain));
362 marudec->srcpad = gst_pad_new_from_template (oclass->srctempl, "src") ;
363 gst_pad_use_fixed_caps (marudec->srcpad);
364 gst_pad_set_event_function (marudec->srcpad,
365 GST_DEBUG_FUNCPTR(gst_marudec_src_event));
367 gst_element_add_pad (GST_ELEMENT(marudec), marudec->sinkpad);
368 gst_element_add_pad (GST_ELEMENT(marudec), marudec->srcpad);
370 marudec->context = g_malloc0 (sizeof(CodecContext));
371 marudec->context->video.pix_fmt = PIX_FMT_NONE;
372 marudec->context->audio.sample_fmt = SAMPLE_FMT_NONE;
374 marudec->opened = FALSE;
375 marudec->format.video.par_n = -1;
376 marudec->format.video.fps_n = -1;
377 marudec->format.video.old_fps_n = -1;
379 marudec->queued = NULL;
380 gst_segment_init (&marudec->segment, GST_FORMAT_TIME);
382 marudec->dev = g_malloc0 (sizeof(CodecDevice));
384 CODEC_LOG (ERR, "failed to allocate memory.\n");
389 gst_marudec_finalize (GObject *object)
391 GstMaruDec *marudec = (GstMaruDec *) object;
393 if (marudec->context) {
394 g_free (marudec->context);
395 marudec->context = NULL;
398 G_OBJECT_CLASS (parent_class)->finalize (object);
402 gst_marudec_src_event (GstPad *pad, GstEvent *event)
407 marudec = (GstMaruDec *) gst_pad_get_parent (pad);
409 switch (GST_EVENT_TYPE (event)) {
410 /* Quality Of Service (QOS) event contains a report
411 about the current real-time performance of the stream.*/
415 GstClockTimeDiff diff;
416 GstClockTime timestamp;
418 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
420 /* update our QoS values */
421 gst_marudec_update_qos (marudec, proportion, timestamp + diff);
428 /* forward upstream */
429 res = gst_pad_push_event (marudec->sinkpad, event);
431 gst_object_unref (marudec);
437 gst_marudec_sink_event (GstPad *pad, GstEvent *event)
440 gboolean ret = FALSE;
442 marudec = (GstMaruDec *) gst_pad_get_parent (pad);
444 GST_DEBUG_OBJECT (marudec, "Handling %s event",
445 GST_EVENT_TYPE_NAME (event));
447 switch (GST_EVENT_TYPE (event)) {
449 gst_marudec_drain (marudec);
451 case GST_EVENT_FLUSH_STOP:
453 if (marudec->opened) {
454 codec_flush_buffers (marudec->context, marudec->dev);
457 gst_marudec_reset_ts (marudec);
458 gst_marudec_reset_qos (marudec);
460 gst_marudec_flush_pcache (marudec);
461 marudec->waiting_for_key = TRUE;
463 gst_segment_init (&marudec->segment, GST_FORMAT_TIME);
464 clear_queued (marudec);
467 case GST_EVENT_NEWSEGMENT:
471 gint64 start, stop, time;
474 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
475 &start, &stop, &time);
478 case GST_FORMAT_TIME:
480 case GST_FORMAT_BYTES:
483 bit_rate = marudec->context->bit_rate;
486 GST_WARNING_OBJECT (marudec, "no bitrate to convert BYTES to TIME");
487 gst_event_unref (event);
488 gst_object_unref (marudec);
492 GST_DEBUG_OBJECT (marudec, "bitrate: %d", bit_rate);
495 start = gst_util_uint64_scale_int (start, GST_SECOND, bit_rate);
498 stop = gst_util_uint64_scale_int (stop, GST_SECOND, bit_rate);
501 time = gst_util_uint64_scale_int (time, GST_SECOND, bit_rate);
504 gst_event_unref (event);
506 format = GST_FORMAT_TIME;
509 event = gst_event_new_new_segment (update, rate, format,
514 GST_WARNING_OBJECT (marudec, "unknown format received in NEWSEGMENT");
515 gst_event_unref (event);
516 gst_object_unref (marudec);
520 if (marudec->context->codec) {
521 gst_marudec_drain (marudec);
524 GST_DEBUG_OBJECT (marudec,
525 "NEWSEGMENT in time start %" GST_TIME_FORMAT " -- stop %"
526 GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
528 gst_segment_set_newsegment_full (&marudec->segment, update,
529 rate, arate, format, start, stop, time);
536 ret = gst_pad_push_event (marudec->srcpad, event);
538 gst_object_unref (marudec);
546 gst_marudec_setcaps (GstPad *pad, GstCaps *caps)
549 GstMaruDecClass *oclass;
550 GstStructure *structure;
555 GST_DEBUG_OBJECT (pad, "setcaps called.");
557 marudec = (GstMaruDec *) (gst_pad_get_parent (pad));
558 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
560 GST_OBJECT_LOCK (marudec);
562 if (marudec->opened) {
563 GST_OBJECT_UNLOCK (marudec);
564 gst_marudec_drain (marudec);
565 GST_OBJECT_LOCK (marudec);
566 gst_marudec_close (marudec);
569 GST_LOG_OBJECT (marudec, "size %dx%d", marudec->context->video.width,
570 marudec->context->video.height);
572 if (!strcmp(oclass->codec->name, "wmv3") ||
573 !strcmp(oclass->codec->name, "vc1")) {
574 gst_maru_caps_to_codecname (caps, oclass->codec->name, NULL);
577 gst_maru_caps_with_codecname (oclass->codec->name, oclass->codec->media_type,
578 caps, marudec->context);
580 GST_LOG_OBJECT (marudec, "size after %dx%d", marudec->context->video.width,
581 marudec->context->video.height);
583 if (!marudec->context->video.fps_d || !marudec->context->video.fps_n) {
584 GST_DEBUG_OBJECT (marudec, "forcing 25/1 framerate");
585 marudec->context->video.fps_n = 1;
586 marudec->context->video.fps_d = 25;
589 structure = gst_caps_get_structure (caps, 0);
591 par = gst_structure_get_value (structure, "pixel-aspect-ratio");
593 GST_DEBUG_OBJECT (marudec, "sink caps have pixel-aspect-ratio of %d:%d",
594 gst_value_get_fraction_numerator (par),
595 gst_value_get_fraction_denominator (par));
599 g_free(marudec->par);
601 marudec->par = g_new0 (GValue, 1);
602 gst_value_init_and_copy (marudec->par, par);
606 fps = gst_structure_get_value (structure, "framerate");
607 if (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps)) {
608 marudec->format.video.fps_n = gst_value_get_fraction_numerator (fps);
609 marudec->format.video.fps_d = gst_value_get_fraction_denominator (fps);
610 GST_DEBUG_OBJECT (marudec, "Using framerate %d/%d from incoming",
611 marudec->format.video.fps_n, marudec->format.video.fps_d);
613 marudec->format.video.fps_n = -1;
614 GST_DEBUG_OBJECT (marudec, "Using framerate from codec");
618 if (strcmp (oclass->codec->name, "aac") == 0) {
619 const gchar *format = gst_structure_get_string (structure, "stream-format");
620 if (format == NULL || strcmp ("format", "raw") == 0) {
621 marudec->turnoff_parser = TRUE;
626 if (!gst_marudec_open (marudec)) {
627 GST_DEBUG_OBJECT (marudec, "Failed to open");
630 g_free(marudec->par);
634 GST_OBJECT_UNLOCK (marudec);
635 gst_object_unref (marudec);
640 gst_structure_get_int (structure, "width",
641 &marudec->format.video.clip_width);
642 gst_structure_get_int (structure, "height",
643 &marudec->format.video.clip_height);
645 GST_DEBUG_OBJECT (pad, "clipping to %dx%d",
646 marudec->format.video.clip_width, marudec->format.video.clip_height);
648 GST_OBJECT_UNLOCK (marudec);
649 gst_object_unref (marudec);
655 gst_marudec_open (GstMaruDec *marudec)
657 GstMaruDecClass *oclass;
659 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
665 if (gst_maru_avcodec_open (marudec->context,
666 oclass->codec, marudec->dev) < 0) {
667 gst_marudec_close (marudec);
668 GST_ERROR_OBJECT (marudec,
669 "maru_%sdec: Failed to open codec", oclass->codec->name);
673 marudec->opened = TRUE;
674 GST_LOG_OBJECT (marudec, "Opened codec %s", oclass->codec->name);
676 switch (oclass->codec->media_type) {
677 case AVMEDIA_TYPE_VIDEO:
678 marudec->format.video.width = 0;
679 marudec->format.video.height = 0;
680 marudec->format.video.clip_width = -1;
681 marudec->format.video.clip_height = -1;
682 marudec->format.video.pix_fmt = PIX_FMT_NB;
683 marudec->format.video.interlaced = FALSE;
685 case AVMEDIA_TYPE_AUDIO:
686 marudec->format.audio.samplerate = 0;
687 marudec->format.audio.channels = 0;
688 marudec->format.audio.depth = 0;
694 gst_marudec_reset_ts (marudec);
696 marudec->proportion = 0.0;
697 marudec->earliest_time = -1;
703 gst_marudec_close (GstMaruDec *marudec)
707 if (marudec->context->codecdata) {
708 g_free(marudec->context->codecdata);
709 marudec->context->codecdata = NULL;
716 ret = gst_maru_avcodec_close (marudec->context, marudec->dev);
719 g_free(marudec->dev);
728 gst_marudec_negotiate (GstMaruDec *marudec, gboolean force)
730 GstMaruDecClass *oclass;
733 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
735 switch (oclass->codec->media_type) {
736 case AVMEDIA_TYPE_VIDEO:
737 if (!force && marudec->format.video.width == marudec->context->video.width
738 && marudec->format.video.height == marudec->context->video.height
739 && marudec->format.video.fps_n == marudec->format.video.old_fps_n
740 && marudec->format.video.fps_d == marudec->format.video.old_fps_d
741 && marudec->format.video.pix_fmt == marudec->context->video.pix_fmt
742 && marudec->format.video.par_n == marudec->context->video.par_n
743 && marudec->format.video.par_d == marudec->context->video.par_d) {
746 marudec->format.video.width = marudec->context->video.width;
747 marudec->format.video.height = marudec->context->video.height;
748 marudec->format.video.old_fps_n = marudec->format.video.fps_n;
749 marudec->format.video.old_fps_d = marudec->format.video.fps_d;
750 marudec->format.video.pix_fmt = marudec->context->video.pix_fmt;
751 marudec->format.video.par_n = marudec->context->video.par_n;
752 marudec->format.video.par_d = marudec->context->video.par_d;
754 case AVMEDIA_TYPE_AUDIO:
756 gint depth = gst_maru_smpfmt_depth (marudec->context->audio.sample_fmt);
757 if (!force && marudec->format.audio.samplerate ==
758 marudec->context->audio.sample_rate &&
759 marudec->format.audio.channels == marudec->context->audio.channels &&
760 marudec->format.audio.depth == depth) {
763 marudec->format.audio.samplerate = marudec->context->audio.sample_rate;
764 marudec->format.audio.channels = marudec->context->audio.channels;
765 marudec->format.audio.depth = depth;
773 gst_maru_codectype_to_caps (oclass->codec->media_type, marudec->context,
774 oclass->codec->name, FALSE);
777 GST_ELEMENT_ERROR (marudec, CORE, NEGOTIATION,
778 ("Could not find GStreamer caps mapping for codec '%s'.",
779 oclass->codec->name), (NULL));
783 switch (oclass->codec->media_type) {
784 case AVMEDIA_TYPE_VIDEO:
789 width = marudec->format.video.clip_width;
790 height = marudec->format.video.clip_height;
791 interlaced = marudec->format.video.interlaced;
793 if (width != -1 && height != -1) {
794 if (width < marudec->context->video.width) {
795 gst_caps_set_simple (caps, "width", G_TYPE_INT, width, NULL);
797 if (height < marudec->context->video.height) {
798 gst_caps_set_simple (caps, "height", G_TYPE_INT, height, NULL);
800 gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, interlaced,
803 if (marudec->format.video.fps_n != -1) {
804 gst_caps_set_simple (caps, "framerate",
805 GST_TYPE_FRACTION, marudec->format.video.fps_n,
806 marudec->format.video.fps_d, NULL);
809 gst_marudec_add_pixel_aspect_ratio (marudec,
810 gst_caps_get_structure (caps, 0));
815 case AVMEDIA_TYPE_AUDIO:
821 if (!gst_pad_set_caps (marudec->srcpad, caps)) {
822 GST_ELEMENT_ERROR (marudec, CORE, NEGOTIATION, (NULL),
823 ("Could not set caps for decoder (%s), not fixed?",
824 oclass->codec->name));
825 gst_caps_unref (caps);
829 gst_caps_unref (caps);
835 new_aligned_buffer (gint size, GstCaps *caps)
839 buf = gst_buffer_new ();
840 GST_BUFFER_DATA (buf) = GST_BUFFER_MALLOCDATA (buf) = g_malloc0 (size);
841 GST_BUFFER_SIZE (buf) = size;
842 GST_BUFFER_FREE_FUNC (buf) = g_free;
845 gst_buffer_set_caps (buf, caps);
852 get_output_buffer (GstMaruDec *marudec, GstBuffer **outbuf)
861 if (G_UNLIKELY (!gst_marudec_negotiate (marudec, FALSE))) {
862 GST_DEBUG_OBJECT (marudec, "negotiate failed");
863 return GST_FLOW_NOT_NEGOTIATED;
866 pict_size = gst_maru_avpicture_size (marudec->context->video.pix_fmt,
867 marudec->context->video.width, marudec->context->video.height);
869 GST_DEBUG_OBJECT (marudec, "size of a picture is negative. "
870 "pixel format: %d, width: %d, height: %d",
871 marudec->context->video.pix_fmt, marudec->context->video.width,
872 marudec->context->video.height);
873 return GST_FLOW_ERROR;
876 CODEC_LOG (DEBUG, "outbuf size of decoded video: %d\n", pict_size);
878 gst_pad_set_element_private(GST_PAD_PEER(marudec->srcpad), (gpointer)marudec);
880 /* GstPadBufferAllocFunction is mostly overridden by elements that can
881 * provide a hardware buffer in order to avoid additional memcpy operations.
883 gst_pad_set_bufferalloc_function(
884 GST_PAD_PEER(marudec->srcpad),
885 (GstPadBufferAllocFunction) codec_buffer_alloc_and_copy);
887 ret = gst_pad_alloc_buffer_and_set_caps (marudec->srcpad,
888 GST_BUFFER_OFFSET_NONE, pict_size,
889 GST_PAD_CAPS (marudec->srcpad), outbuf);
890 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
891 GST_DEBUG_OBJECT (marudec, "pad_alloc failed %d (%s)", ret,
892 gst_flow_get_name (ret));
900 clip_video_buffer (GstMaruDec *dec, GstBuffer *buf,
901 GstClockTime in_ts, GstClockTime in_dur)
909 clip_audio_buffer (GstMaruDec *dec, GstBuffer *buf,
910 GstClockTime in_ts, GstClockTime in_dur)
913 gint64 diff, cstart, cstop;
916 if (G_UNLIKELY (dec->segment.format != GST_FORMAT_TIME)) {
917 GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
921 // in_ts: in_timestamp. check a start time.
922 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (in_ts))) {
923 GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
928 GST_CLOCK_TIME_IS_VALID (in_dur) ? (in_ts + in_dur) : GST_CLOCK_TIME_NONE;
930 res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME, in_ts,
931 stop, &cstart, &cstop);
932 if (G_UNLIKELY (!res)) {
933 GST_LOG_OBJECT (dec, "out of segment");
934 GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
938 if (G_UNLIKELY ((diff = cstart - in_ts) > 0)) {
940 gst_util_uint64_scale_int (diff, dec->format.audio.samplerate, GST_SECOND) *
941 (dec->format.audio.depth * dec->format.audio.channels);
943 GST_DEBUG_OBJECT (dec, "clipping start to %" GST_TIME_FORMAT " %"
944 G_GINT64_FORMAT " bytes", GST_TIME_ARGS (cstart), diff);
946 GST_BUFFER_SIZE (buf) -= diff;
947 GST_BUFFER_DATA (buf) += diff;
951 if (G_UNLIKELY ((diff = stop - cstop) > 0)) {
953 gst_util_uint64_scale_int (diff, dec->format.audio.samplerate, GST_SECOND) *
954 (dec->format.audio.depth * dec->format.audio.channels);
956 GST_DEBUG_OBJECT (dec, "clipping stop to %" GST_TIME_FORMAT " %"
957 G_GINT64_FORMAT " bytes", GST_TIME_ARGS (cstop), diff);
959 GST_BUFFER_SIZE (buf) -= diff;
962 GST_BUFFER_TIMESTAMP (buf) = cstart;
963 GST_BUFFER_DURATION (buf) = cstop - cstart;
965 GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
970 gst_marudec_video_frame (GstMaruDec *marudec, guint8 *data, guint size,
971 const GstTSInfo *dec_info, gint64 in_offset, GstBuffer **outbuf,
974 gint len = -1, have_data;
975 gboolean mode_switch;
977 GstClockTime out_timestamp, out_duration, out_pts;
979 const GstTSInfo *out_info;
981 decode = gst_marudec_do_qos (marudec, dec_info->timestamp, &mode_switch);
983 CODEC_LOG (DEBUG, "decode video: input buffer size: %d\n", size);
985 codec_decode_video (marudec->context, data, size,
986 dec_info->idx, in_offset, outbuf,
987 &have_data, marudec->dev);
993 GST_DEBUG_OBJECT (marudec, "after decode: len %d, have_data %d",
997 if (len < 0 && (mode_switch || marudec->context->skip_frame)) {
1001 if (len > 0 && have_data <= 0 && (mode_switch
1002 || marudec->context->skip_frame)) {
1003 marudec->last_out = -1;
1007 if (len < 0 || have_data <= 0) {
1008 // if (len < 0) { // have_data <= 0) {
1009 GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1010 *ret, *outbuf, len);
1013 "return flow %d, out %p, len %d, have_data: %d\n",
1014 *ret, *outbuf, len, have_data);
1019 out_info = gst_ts_info_get (marudec, dec_info->idx);
1020 out_pts = out_info->timestamp;
1021 out_duration = out_info->duration;
1022 out_offset = out_info->offset;
1024 *ret = get_output_buffer (marudec, outbuf);
1025 if (G_UNLIKELY (*ret != GST_FLOW_OK)) {
1026 GST_DEBUG_OBJECT (marudec, "no output buffer");
1028 GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1029 *ret, *outbuf, len);
1035 if (out_pts != -1) {
1036 out_timestamp = (GstClockTime) out_pts;
1037 GST_LOG_OBJECT (marudec, "using timestamp %" GST_TIME_FORMAT
1038 " returned by ffmpeg", GST_TIME_ARGS (out_timestamp));
1041 if (!GST_CLOCK_TIME_IS_VALID (out_timestamp) && marudec->next_out != -1) {
1042 out_timestamp = marudec->next_out;
1043 GST_LOG_OBJECT (marudec, "using next timestamp %" GST_TIME_FORMAT,
1044 GST_TIME_ARGS (out_timestamp));
1047 if (!GST_CLOCK_TIME_IS_VALID (out_timestamp)) {
1048 out_timestamp = dec_info->timestamp;
1049 GST_LOG_OBJECT (marudec, "using in timestamp %" GST_TIME_FORMAT,
1050 GST_TIME_ARGS (out_timestamp));
1052 GST_BUFFER_TIMESTAMP (*outbuf) = out_timestamp;
1055 if (out_offset != GST_BUFFER_OFFSET_NONE) {
1056 GST_LOG_OBJECT (marudec, "Using offset returned by ffmpeg");
1057 } else if (out_timestamp != GST_CLOCK_TIME_NONE) {
1058 GstFormat out_fmt = GST_FORMAT_DEFAULT;
1059 GST_LOG_OBJECT (marudec, "Using offset converted from timestamp");
1061 gst_pad_query_peer_convert (marudec->sinkpad,
1062 GST_FORMAT_TIME, out_timestamp, &out_fmt, &out_offset);
1063 } else if (dec_info->offset != GST_BUFFER_OFFSET_NONE) {
1064 GST_LOG_OBJECT (marudec, "using in_offset %" G_GINT64_FORMAT,
1066 out_offset = dec_info->offset;
1068 GST_LOG_OBJECT (marudec, "no valid offset found");
1069 out_offset = GST_BUFFER_OFFSET_NONE;
1071 GST_BUFFER_OFFSET (*outbuf) = out_offset;
1074 if (GST_CLOCK_TIME_IS_VALID (out_duration)) {
1075 GST_LOG_OBJECT (marudec, "Using duration returned by ffmpeg");
1076 } else if (GST_CLOCK_TIME_IS_VALID (dec_info->duration)) {
1077 GST_LOG_OBJECT (marudec, "Using in_duration");
1078 out_duration = dec_info->duration;
1080 } else if (GST_CLOCK_TIME_IS_VALID (marudec->last_diff)) {
1081 GST_LOG_OBJECT (marudec, "Using last-diff");
1082 out_duration = marudec->last_diff;
1085 if (marudec->format.video.fps_n != -1 &&
1086 (marudec->format.video.fps_n != 1000 &&
1087 marudec->format.video.fps_d != 1)) {
1088 GST_LOG_OBJECT (marudec, "using input framerate for duration");
1089 out_duration = gst_util_uint64_scale_int (GST_SECOND,
1090 marudec->format.video.fps_d, marudec->format.video.fps_n);
1092 if (marudec->context->video.fps_n != 0 &&
1093 (marudec->context->video.fps_d > 0 &&
1094 marudec->context->video.fps_d < 1000)) {
1095 GST_LOG_OBJECT (marudec, "using decoder's framerate for duration");
1096 out_duration = gst_util_uint64_scale_int (GST_SECOND,
1097 marudec->context->video.fps_n * 1,
1098 marudec->context->video.fps_d);
1100 GST_LOG_OBJECT (marudec, "no valid duration found");
1106 if (GST_CLOCK_TIME_IS_VALID (out_duration)) {
1107 out_duration += out_duration * marudec->picture->repeat_pict / 2;
1109 GST_BUFFER_DURATION (*outbuf) = out_duration;
1111 if (out_timestamp != -1 && out_duration != -1 && out_duration != 0) {
1112 marudec->next_out = out_timestamp + out_duration;
1114 marudec->next_out = -1;
1118 if (G_UNLIKELY (!clip_video_buffer (marudec, *outbuf, out_timestamp,
1120 GST_DEBUG_OBJECT (marudec, "buffer clipped");
1121 gst_buffer_unref (*outbuf);
1123 GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1124 *ret, *outbuf, len);
1128 GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1129 *ret, *outbuf, len);
1134 gst_marudec_audio_frame (GstMaruDec *marudec, CodecElement *codec,
1135 guint8 *data, guint size,
1136 const GstTSInfo *dec_info, GstBuffer **outbuf,
1140 gint have_data = FF_MAX_AUDIO_FRAME_SIZE;
1141 GstClockTime out_timestamp, out_duration;
1145 new_aligned_buffer (FF_MAX_AUDIO_FRAME_SIZE,
1146 GST_PAD_CAPS (marudec->srcpad));
1148 CODEC_LOG (DEBUG, "decode audio, input buffer size: %d\n", size);
1150 len = codec_decode_audio (marudec->context,
1151 (int16_t *) GST_BUFFER_DATA (*outbuf), &have_data,
1152 data, size, marudec->dev);
1154 GST_DEBUG_OBJECT (marudec,
1155 "Decode audio: len=%d, have_data=%d", len, have_data);
1157 if (len >= 0 && have_data > 0) {
1158 GST_DEBUG_OBJECT (marudec, "Creating output buffer");
1159 if (!gst_marudec_negotiate (marudec, FALSE)) {
1160 gst_buffer_unref (*outbuf);
1163 GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1164 *ret, *outbuf, len);
1168 GST_BUFFER_SIZE (*outbuf) = have_data;
1170 if (GST_CLOCK_TIME_IS_VALID (dec_info->timestamp)) {
1171 out_timestamp = dec_info->timestamp;
1173 out_timestamp = marudec->next_out;
1176 /* calculate based on number of samples */
1177 out_duration = gst_util_uint64_scale (have_data, GST_SECOND,
1178 marudec->format.audio.depth * marudec->format.audio.channels *
1179 marudec->format.audio.samplerate);
1181 out_offset = dec_info->offset;
1183 GST_DEBUG_OBJECT (marudec,
1184 "Buffer created. Size: %d, timestamp: %" GST_TIME_FORMAT
1185 ", duration: %" GST_TIME_FORMAT, have_data,
1186 GST_TIME_ARGS (out_timestamp), GST_TIME_ARGS (out_duration));
1188 GST_BUFFER_TIMESTAMP (*outbuf) = out_timestamp;
1189 GST_BUFFER_DURATION (*outbuf) = out_duration;
1190 GST_BUFFER_OFFSET (*outbuf) = out_offset;
1191 gst_buffer_set_caps (*outbuf, GST_PAD_CAPS (marudec->srcpad));
1193 if (GST_CLOCK_TIME_IS_VALID (out_timestamp)) {
1194 marudec->next_out = out_timestamp + out_duration;
1197 if (G_UNLIKELY (!clip_audio_buffer (marudec, *outbuf,
1198 out_timestamp, out_duration))) {
1199 GST_DEBUG_OBJECT (marudec, "buffer_clipped");
1200 gst_buffer_unref (*outbuf);
1202 GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d", *ret, *outbuf, len);
1206 gst_buffer_unref (*outbuf);
1210 if (len == -1 && !strcmp(codec->name, "aac")) {
1211 GST_ELEMENT_ERROR (marudec, STREAM, DECODE, (NULL),
1212 ("Decoding of AAC stream by FFMPEG failed."));
1213 *ret = GST_FLOW_ERROR;
1216 GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1217 *ret, *outbuf, len);
1222 gst_marudec_frame (GstMaruDec *marudec, guint8 *data, guint size,
1223 gint *got_data, const GstTSInfo *dec_info, gint64 in_offset, GstFlowReturn *ret)
1225 GstMaruDecClass *oclass;
1226 GstBuffer *outbuf = NULL;
1227 gint have_data = 0, len = 0;
1229 if (G_UNLIKELY (marudec->context->codec == NULL)) {
1230 GST_ERROR_OBJECT (marudec, "no codec context");
1235 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
1237 switch (oclass->codec->media_type) {
1238 case AVMEDIA_TYPE_VIDEO:
1239 len = gst_marudec_video_frame (marudec, data, size,
1240 dec_info, in_offset, &outbuf, ret);
1242 case AVMEDIA_TYPE_AUDIO:
1243 len = gst_marudec_audio_frame (marudec, oclass->codec, data, size,
1244 dec_info, &outbuf, ret);
1245 if (outbuf == NULL && marudec->discont) {
1246 GST_DEBUG_OBJECT (marudec, "no buffer but keeping timestamp");
1247 // marudec->clear_ts = FALSE;
1251 GST_ERROR_OBJECT (marudec, "Asked to decode non-audio/video frame!");
1252 g_assert_not_reached ();
1260 if (len < 0 || have_data < 0) {
1261 GST_WARNING_OBJECT (marudec,
1262 "maru_%sdec: decoding error (len: %d, have_data: %d)",
1263 oclass->codec->name, len, have_data);
1266 } else if (len == 0 && have_data == 0) {
1274 GST_LOG_OBJECT (marudec,
1275 "Decoded data, now pushing buffer %p with offset %" G_GINT64_FORMAT
1276 ", timestamp %" GST_TIME_FORMAT " and duration %" GST_TIME_FORMAT,
1277 outbuf, GST_BUFFER_OFFSET (outbuf),
1278 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1279 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
1281 if (marudec->discont) {
1282 /* GST_BUFFER_FLAG_DISCONT :
1283 * the buffer marks a data discontinuity in the stream. This typically
1284 * occurs after a seek or a dropped buffer from a live or network source.
1286 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1287 marudec->discont = FALSE;
1290 if (marudec->segment.rate > 0.0) {
1292 *ret = gst_pad_push (marudec->srcpad, outbuf);
1295 GST_DEBUG_OBJECT (marudec, "queued frame");
1296 marudec->queued = g_list_prepend (marudec->queued, outbuf);
1300 GST_DEBUG_OBJECT (marudec, "Didn't get a decoded buffer");
1306 static GstFlowReturn
1307 gst_marudec_chain (GstPad *pad, GstBuffer *buffer)
1309 GstMaruDec *marudec;
1310 GstMaruDecClass *oclass;
1312 gint in_size, len, have_data;
1313 GstFlowReturn ret = GST_FLOW_OK;
1314 GstClockTime in_timestamp;
1315 GstClockTime in_duration;
1318 const GstTSInfo *in_info;
1319 const GstTSInfo *dec_info;
1321 marudec = (GstMaruDec *) (GST_PAD_PARENT (pad));
1323 if (G_UNLIKELY (!marudec->opened)) {
1325 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
1326 GST_ELEMENT_ERROR (marudec, CORE, NEGOTIATION, (NULL),
1327 ("maru_%sdec: input format was not set before data start",
1328 oclass->codec->name));
1329 gst_buffer_unref (buffer);
1330 return GST_FLOW_NOT_NEGOTIATED;
1333 discont = GST_BUFFER_IS_DISCONT (buffer);
1336 if (G_UNLIKELY (discont)) {
1337 GST_DEBUG_OBJECT (marudec, "received DISCONT");
1338 gst_marudec_drain (marudec);
1339 // gst_marudec_flush_pcache (marudec);
1340 codec_flush_buffers (marudec->context, marudec->dev);
1341 marudec->discont = TRUE;
1342 gst_marudec_reset_ts (marudec);
1344 // marudec->clear_ts = TRUE;
1346 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
1348 if (G_UNLIKELY (marudec->waiting_for_key)) {
1349 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT) &&
1350 oclass->codec->media_type != AVMEDIA_TYPE_AUDIO) {
1353 marudec->waiting_for_key = FALSE;
1356 if (marudec->pcache) {
1357 GST_LOG_OBJECT (marudec, "join parse cache");
1358 buffer = gst_buffer_join (marudec->pcache, buffer);
1359 marudec->pcache = NULL;
1363 in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
1364 in_duration = GST_BUFFER_DURATION (buffer);
1365 in_offset = GST_BUFFER_OFFSET (buffer);
1367 in_info = gst_ts_info_store (marudec, in_timestamp, in_duration, in_offset);
1370 if (in_timestamp != -1) {
1371 if (!marudec->reordered_in && marudec->last_in != -1) {
1372 if (in_timestamp < marudec->last_in) {
1373 GST_LOG_OBJECT (marudec, "detected reordered input timestamps");
1374 marudec->reordered_in = TRUE;
1375 marudec->last_diff = GST_CLOCK_TIME_NONE;
1376 } else if (in_timestamp > marudec->last_in) {
1378 diff = in_timestamp - marudec->last_in;
1379 if (marudec->last_frames) {
1380 diff /= marudec->last_frames;
1383 GST_LOG_OBJECT (marudec, "estimated duration %" GST_TIME_FORMAT " %u",
1384 GST_TIME_ARGS (diff), marudec->last_frames);
1386 marudec->last_diff = diff;
1389 marudec->last_in = in_timestamp;
1390 marudec->last_frames;
1394 GST_LOG_OBJECT (marudec,
1395 "Received new data of size %u, offset: %" G_GUINT64_FORMAT ", ts:%"
1396 GST_TIME_FORMAT ", dur: %" GST_TIME_FORMAT ", info %d",
1397 GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (buffer),
1398 GST_TIME_ARGS (in_timestamp), GST_TIME_ARGS (in_duration), in_info->idx);
1400 in_buf = GST_BUFFER_DATA (buffer);
1401 in_size = GST_BUFFER_SIZE (buffer);
1406 gst_marudec_frame (marudec, in_buf, in_size, &have_data, dec_info, in_offset, &ret);
1409 if (marudec->clear_ts) {
1410 in_timestamp = GST_CLOCK_TIME_NONE;
1411 in_duration = GST_CLOCK_TIME_NONE;
1412 in_offset = GST_BUFFER_OFFSET_NONE;
1413 in_info = GST_TS_INFO_NONE;
1415 marudec->clear_ts = TRUE;
1419 gst_buffer_unref (buffer);
1424 static GstStateChangeReturn
1425 gst_marudec_change_state (GstElement *element, GstStateChange transition)
1427 GstMaruDec *marudec = (GstMaruDec *) element;
1428 GstStateChangeReturn ret;
1430 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1432 switch (transition) {
1433 case GST_STATE_CHANGE_PAUSED_TO_READY:
1434 GST_OBJECT_LOCK (marudec);
1435 gst_marudec_close (marudec);
1436 GST_OBJECT_UNLOCK (marudec);
1439 clear_queued (marudec);
1449 gst_marudec_register (GstPlugin *plugin, GList *element)
1451 GTypeInfo typeinfo = {
1452 sizeof (GstMaruDecClass),
1453 (GBaseInitFunc) gst_marudec_base_init,
1455 (GClassInitFunc) gst_marudec_class_init,
1458 sizeof (GstMaruDec),
1460 (GInstanceInitFunc) gst_marudec_init,
1465 gint rank = GST_RANK_PRIMARY;
1466 GList *elem = element;
1467 CodecElement *codec = NULL;
1473 /* register element */
1475 codec = (CodecElement *)(elem->data);
1480 if (codec->codec_type != CODEC_TYPE_DECODE) {
1484 type_name = g_strdup_printf ("maru_%sdec", codec->name);
1485 type = g_type_from_name (type_name);
1487 type = g_type_register_static (GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
1488 g_type_set_qdata (type, GST_MARUDEC_PARAMS_QDATA, (gpointer) codec);
1491 if (!gst_element_register (plugin, type_name, rank, type)) {
1496 } while ((elem = elem->next));