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)
237 GstMaruDecClass *oclass;
238 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
241 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);
257 if (marudec->segment.rate < 0.0) {
258 CODEC_LOG (DEBUG, "reverse playback\n");
259 flush_queued (marudec);
267 gst_marudec_base_init (GstMaruDecClass *klass)
269 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
270 GstCaps *sinkcaps = NULL, *srccaps = NULL;
271 GstPadTemplate *sinktempl, *srctempl;
273 gchar *longname, *classification, *description;
276 (CodecElement *)g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
277 GST_MARUDEC_PARAMS_QDATA);
279 longname = g_strdup_printf ("%s Decoder", codec->longname);
280 classification = g_strdup_printf ("Codec/Decoder/%s",
281 (codec->media_type == AVMEDIA_TYPE_VIDEO) ?
283 description = g_strdup_printf("%s Decoder", codec->name);
285 gst_element_class_set_details_simple (element_class,
289 "Kitae Kim <kt920.kim@samsung.com>");
292 g_free (classification);
293 g_free (description);
295 sinkcaps = gst_maru_codecname_to_caps (codec->name, NULL, FALSE);
297 sinkcaps = gst_caps_from_string ("unknown/unknown");
300 switch (codec->media_type) {
301 case AVMEDIA_TYPE_VIDEO:
302 srccaps = gst_caps_from_string ("video/x-raw-rgb; video/x-raw-yuv");
304 case AVMEDIA_TYPE_AUDIO:
305 srccaps = gst_maru_codectype_to_audio_caps (NULL, codec->name, FALSE, codec);
308 GST_LOG("unknown media type.\n");
313 srccaps = gst_caps_from_string ("unknown/unknown");
316 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
317 GST_PAD_ALWAYS, sinkcaps);
318 srctempl = gst_pad_template_new ("src", GST_PAD_SRC,
319 GST_PAD_ALWAYS, srccaps);
321 gst_element_class_add_pad_template (element_class, srctempl);
322 gst_element_class_add_pad_template (element_class, sinktempl);
324 klass->codec = codec;
325 klass->sinktempl = sinktempl;
326 klass->srctempl = srctempl;
330 gst_marudec_class_init (GstMaruDecClass *klass)
332 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
333 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
335 parent_class = g_type_class_peek_parent (klass);
338 gobject_class->set_property = gst_marudec_set_property
339 gobject_class->get_property = gst_marudec_get_property
342 gobject_class->finalize = gst_marudec_finalize;
343 gstelement_class->change_state = gst_marudec_change_state;
347 gst_marudec_init (GstMaruDec *marudec)
349 GstMaruDecClass *oclass;
351 oclass = (GstMaruDecClass*) (G_OBJECT_GET_CLASS(marudec));
353 marudec->sinkpad = gst_pad_new_from_template (oclass->sinktempl, "sink");
354 gst_pad_set_setcaps_function (marudec->sinkpad,
355 GST_DEBUG_FUNCPTR(gst_marudec_setcaps));
356 gst_pad_set_event_function (marudec->sinkpad,
357 GST_DEBUG_FUNCPTR(gst_marudec_sink_event));
358 gst_pad_set_chain_function (marudec->sinkpad,
359 GST_DEBUG_FUNCPTR(gst_marudec_chain));
361 marudec->srcpad = gst_pad_new_from_template (oclass->srctempl, "src") ;
362 gst_pad_use_fixed_caps (marudec->srcpad);
363 gst_pad_set_event_function (marudec->srcpad,
364 GST_DEBUG_FUNCPTR(gst_marudec_src_event));
366 gst_element_add_pad (GST_ELEMENT(marudec), marudec->sinkpad);
367 gst_element_add_pad (GST_ELEMENT(marudec), marudec->srcpad);
369 marudec->context = g_malloc0 (sizeof(CodecContext));
370 marudec->context->video.pix_fmt = PIX_FMT_NONE;
371 marudec->context->audio.sample_fmt = SAMPLE_FMT_NONE;
373 marudec->opened = FALSE;
374 marudec->format.video.par_n = -1;
375 marudec->format.video.fps_n = -1;
376 marudec->format.video.old_fps_n = -1;
378 marudec->queued = NULL;
379 gst_segment_init (&marudec->segment, GST_FORMAT_TIME);
381 marudec->dev = g_malloc0 (sizeof(CodecDevice));
383 CODEC_LOG (ERR, "failed to allocate memory.\n");
388 gst_marudec_finalize (GObject *object)
390 GstMaruDec *marudec = (GstMaruDec *) object;
392 if (marudec->context) {
393 g_free (marudec->context);
394 marudec->context = NULL;
397 G_OBJECT_CLASS (parent_class)->finalize (object);
401 gst_marudec_src_event (GstPad *pad, GstEvent *event)
406 marudec = (GstMaruDec *) gst_pad_get_parent (pad);
408 switch (GST_EVENT_TYPE (event)) {
409 /* Quality Of Service (QOS) event contains a report
410 about the current real-time performance of the stream.*/
414 GstClockTimeDiff diff;
415 GstClockTime timestamp;
417 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
419 /* update our QoS values */
420 gst_marudec_update_qos (marudec, proportion, timestamp + diff);
427 /* forward upstream */
428 res = gst_pad_push_event (marudec->sinkpad, event);
430 gst_object_unref (marudec);
436 gst_marudec_sink_event (GstPad *pad, GstEvent *event)
439 gboolean ret = FALSE;
441 marudec = (GstMaruDec *) gst_pad_get_parent (pad);
443 GST_DEBUG_OBJECT (marudec, "Handling %s event",
444 GST_EVENT_TYPE_NAME (event));
446 switch (GST_EVENT_TYPE (event)) {
448 gst_marudec_drain (marudec);
450 case GST_EVENT_FLUSH_STOP:
452 if (marudec->opened) {
453 codec_flush_buffers (marudec->context, marudec->dev);
456 gst_marudec_reset_ts (marudec);
457 gst_marudec_reset_qos (marudec);
459 gst_marudec_flush_pcache (marudec);
460 marudec->waiting_for_key = TRUE;
462 gst_segment_init (&marudec->segment, GST_FORMAT_TIME);
463 clear_queued (marudec);
466 case GST_EVENT_NEWSEGMENT:
470 gint64 start, stop, time;
473 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
474 &start, &stop, &time);
477 case GST_FORMAT_TIME:
479 case GST_FORMAT_BYTES:
482 bit_rate = marudec->context->bit_rate;
485 GST_WARNING_OBJECT (marudec, "no bitrate to convert BYTES to TIME");
486 gst_event_unref (event);
487 gst_object_unref (marudec);
491 GST_DEBUG_OBJECT (marudec, "bitrate: %d", bit_rate);
494 start = gst_util_uint64_scale_int (start, GST_SECOND, bit_rate);
497 stop = gst_util_uint64_scale_int (stop, GST_SECOND, bit_rate);
500 time = gst_util_uint64_scale_int (time, GST_SECOND, bit_rate);
503 gst_event_unref (event);
505 format = GST_FORMAT_TIME;
508 event = gst_event_new_new_segment (update, rate, format,
513 GST_WARNING_OBJECT (marudec, "unknown format received in NEWSEGMENT");
514 gst_event_unref (event);
515 gst_object_unref (marudec);
519 if (marudec->context->codec) {
520 gst_marudec_drain (marudec);
523 GST_DEBUG_OBJECT (marudec,
524 "NEWSEGMENT in time start %" GST_TIME_FORMAT " -- stop %"
525 GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
527 gst_segment_set_newsegment_full (&marudec->segment, update,
528 rate, arate, format, start, stop, time);
535 ret = gst_pad_push_event (marudec->srcpad, event);
537 gst_object_unref (marudec);
545 gst_marudec_setcaps (GstPad *pad, GstCaps *caps)
548 GstMaruDecClass *oclass;
549 GstStructure *structure;
554 GST_DEBUG_OBJECT (pad, "setcaps called.");
556 marudec = (GstMaruDec *) (gst_pad_get_parent (pad));
557 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
559 GST_OBJECT_LOCK (marudec);
561 if (marudec->opened) {
562 GST_OBJECT_UNLOCK (marudec);
563 gst_marudec_drain (marudec);
564 GST_OBJECT_LOCK (marudec);
565 gst_marudec_close (marudec);
568 GST_LOG_OBJECT (marudec, "size %dx%d", marudec->context->video.width,
569 marudec->context->video.height);
571 if (!strcmp(oclass->codec->name, "wmv3") ||
572 !strcmp(oclass->codec->name, "vc1")) {
573 gst_maru_caps_to_codecname (caps, oclass->codec->name, NULL);
576 gst_maru_caps_with_codecname (oclass->codec->name, oclass->codec->media_type,
577 caps, marudec->context);
579 GST_LOG_OBJECT (marudec, "size after %dx%d", marudec->context->video.width,
580 marudec->context->video.height);
582 if (!marudec->context->video.fps_d || !marudec->context->video.fps_n) {
583 GST_DEBUG_OBJECT (marudec, "forcing 25/1 framerate");
584 marudec->context->video.fps_n = 1;
585 marudec->context->video.fps_d = 25;
588 structure = gst_caps_get_structure (caps, 0);
590 par = gst_structure_get_value (structure, "pixel-aspect-ratio");
592 GST_DEBUG_OBJECT (marudec, "sink caps have pixel-aspect-ratio of %d:%d",
593 gst_value_get_fraction_numerator (par),
594 gst_value_get_fraction_denominator (par));
598 g_free(marudec->par);
600 marudec->par = g_new0 (GValue, 1);
601 gst_value_init_and_copy (marudec->par, par);
605 fps = gst_structure_get_value (structure, "framerate");
606 if (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps)) {
607 marudec->format.video.fps_n = gst_value_get_fraction_numerator (fps);
608 marudec->format.video.fps_d = gst_value_get_fraction_denominator (fps);
609 GST_DEBUG_OBJECT (marudec, "Using framerate %d/%d from incoming",
610 marudec->format.video.fps_n, marudec->format.video.fps_d);
612 marudec->format.video.fps_n = -1;
613 GST_DEBUG_OBJECT (marudec, "Using framerate from codec");
617 if (strcmp (oclass->codec->name, "aac") == 0) {
618 const gchar *format = gst_structure_get_string (structure, "stream-format");
619 if (format == NULL || strcmp ("format", "raw") == 0) {
620 marudec->turnoff_parser = TRUE;
625 if (!gst_marudec_open (marudec)) {
626 GST_DEBUG_OBJECT (marudec, "Failed to open");
629 g_free(marudec->par);
633 GST_OBJECT_UNLOCK (marudec);
634 gst_object_unref (marudec);
639 gst_structure_get_int (structure, "width",
640 &marudec->format.video.clip_width);
641 gst_structure_get_int (structure, "height",
642 &marudec->format.video.clip_height);
644 GST_DEBUG_OBJECT (pad, "clipping to %dx%d",
645 marudec->format.video.clip_width, marudec->format.video.clip_height);
647 GST_OBJECT_UNLOCK (marudec);
648 gst_object_unref (marudec);
654 gst_marudec_open (GstMaruDec *marudec)
656 GstMaruDecClass *oclass;
658 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
664 if (gst_maru_avcodec_open (marudec->context,
665 oclass->codec, marudec->dev) < 0) {
666 gst_marudec_close (marudec);
667 GST_ERROR_OBJECT (marudec,
668 "maru_%sdec: Failed to open codec", oclass->codec->name);
672 marudec->opened = TRUE;
673 GST_LOG_OBJECT (marudec, "Opened codec %s", oclass->codec->name);
675 switch (oclass->codec->media_type) {
676 case AVMEDIA_TYPE_VIDEO:
677 marudec->format.video.width = 0;
678 marudec->format.video.height = 0;
679 marudec->format.video.clip_width = -1;
680 marudec->format.video.clip_height = -1;
681 marudec->format.video.pix_fmt = PIX_FMT_NB;
682 marudec->format.video.interlaced = FALSE;
684 case AVMEDIA_TYPE_AUDIO:
685 marudec->format.audio.samplerate = 0;
686 marudec->format.audio.channels = 0;
687 marudec->format.audio.depth = 0;
693 gst_marudec_reset_ts (marudec);
695 marudec->proportion = 0.0;
696 marudec->earliest_time = -1;
702 gst_marudec_close (GstMaruDec *marudec)
706 if (marudec->context->codecdata) {
707 g_free(marudec->context->codecdata);
708 marudec->context->codecdata = NULL;
715 ret = gst_maru_avcodec_close (marudec->context, marudec->dev);
718 g_free(marudec->dev);
727 gst_marudec_negotiate (GstMaruDec *marudec, gboolean force)
729 GstMaruDecClass *oclass;
732 oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
734 switch (oclass->codec->media_type) {
735 case AVMEDIA_TYPE_VIDEO:
736 if (!force && marudec->format.video.width == marudec->context->video.width
737 && marudec->format.video.height == marudec->context->video.height
738 && marudec->format.video.fps_n == marudec->format.video.old_fps_n
739 && marudec->format.video.fps_d == marudec->format.video.old_fps_d
740 && marudec->format.video.pix_fmt == marudec->context->video.pix_fmt
741 && marudec->format.video.par_n == marudec->context->video.par_n
742 && marudec->format.video.par_d == marudec->context->video.par_d) {
745 marudec->format.video.width = marudec->context->video.width;
746 marudec->format.video.height = marudec->context->video.height;
747 marudec->format.video.old_fps_n = marudec->format.video.fps_n;
748 marudec->format.video.old_fps_d = marudec->format.video.fps_d;
749 marudec->format.video.pix_fmt = marudec->context->video.pix_fmt;
750 marudec->format.video.par_n = marudec->context->video.par_n;
751 marudec->format.video.par_d = marudec->context->video.par_d;
753 case AVMEDIA_TYPE_AUDIO:
755 gint depth = gst_maru_smpfmt_depth (marudec->context->audio.sample_fmt);
756 if (!force && marudec->format.audio.samplerate ==
757 marudec->context->audio.sample_rate &&
758 marudec->format.audio.channels == marudec->context->audio.channels &&
759 marudec->format.audio.depth == depth) {
762 marudec->format.audio.samplerate = marudec->context->audio.sample_rate;
763 marudec->format.audio.channels = marudec->context->audio.channels;
764 marudec->format.audio.depth = depth;
772 gst_maru_codectype_to_caps (oclass->codec->media_type, marudec->context,
773 oclass->codec->name, FALSE);
776 GST_ELEMENT_ERROR (marudec, CORE, NEGOTIATION,
777 ("Could not find GStreamer caps mapping for codec '%s'.",
778 oclass->codec->name), (NULL));
782 switch (oclass->codec->media_type) {
783 case AVMEDIA_TYPE_VIDEO:
788 width = marudec->format.video.clip_width;
789 height = marudec->format.video.clip_height;
790 interlaced = marudec->format.video.interlaced;
792 if (width != -1 && height != -1) {
793 if (width < marudec->context->video.width) {
794 gst_caps_set_simple (caps, "width", G_TYPE_INT, width, NULL);
796 if (height < marudec->context->video.height) {
797 gst_caps_set_simple (caps, "height", G_TYPE_INT, height, NULL);
799 gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, interlaced,
802 if (marudec->format.video.fps_n != -1) {
803 gst_caps_set_simple (caps, "framerate",
804 GST_TYPE_FRACTION, marudec->format.video.fps_n,
805 marudec->format.video.fps_d, NULL);
808 gst_marudec_add_pixel_aspect_ratio (marudec,
809 gst_caps_get_structure (caps, 0));
814 case AVMEDIA_TYPE_AUDIO:
820 if (!gst_pad_set_caps (marudec->srcpad, caps)) {
821 GST_ELEMENT_ERROR (marudec, CORE, NEGOTIATION, (NULL),
822 ("Could not set caps for decoder (%s), not fixed?",
823 oclass->codec->name));
824 gst_caps_unref (caps);
828 gst_caps_unref (caps);
834 new_aligned_buffer (gint size, GstCaps *caps)
838 buf = gst_buffer_new ();
839 GST_BUFFER_DATA (buf) = GST_BUFFER_MALLOCDATA (buf) = g_malloc0 (size);
840 GST_BUFFER_SIZE (buf) = size;
841 GST_BUFFER_FREE_FUNC (buf) = g_free;
844 gst_buffer_set_caps (buf, caps);
851 get_output_buffer (GstMaruDec *marudec, GstBuffer **outbuf)
860 if (G_UNLIKELY (!gst_marudec_negotiate (marudec, FALSE))) {
861 GST_DEBUG_OBJECT (marudec, "negotiate failed");
862 return GST_FLOW_NOT_NEGOTIATED;
865 pict_size = gst_maru_avpicture_size (marudec->context->video.pix_fmt,
866 marudec->context->video.width, marudec->context->video.height);
868 GST_DEBUG_OBJECT (marudec, "size of a picture is negative. "
869 "pixel format: %d, width: %d, height: %d",
870 marudec->context->video.pix_fmt, marudec->context->video.width,
871 marudec->context->video.height);
872 return GST_FLOW_ERROR;
875 CODEC_LOG (DEBUG, "outbuf size of decoded video: %d\n", pict_size);
877 gst_pad_set_element_private(GST_PAD_PEER(marudec->srcpad), (gpointer)marudec);
879 /* GstPadBufferAllocFunction is mostly overridden by elements that can
880 * provide a hardware buffer in order to avoid additional memcpy operations.
882 gst_pad_set_bufferalloc_function(
883 GST_PAD_PEER(marudec->srcpad),
884 (GstPadBufferAllocFunction) codec_buffer_alloc_and_copy);
886 ret = gst_pad_alloc_buffer_and_set_caps (marudec->srcpad,
887 GST_BUFFER_OFFSET_NONE, pict_size,
888 GST_PAD_CAPS (marudec->srcpad), outbuf);
889 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
890 GST_DEBUG_OBJECT (marudec, "pad_alloc failed %d (%s)", ret,
891 gst_flow_get_name (ret));
899 clip_video_buffer (GstMaruDec *dec, GstBuffer *buf,
900 GstClockTime in_ts, GstClockTime in_dur)
908 clip_audio_buffer (GstMaruDec *dec, GstBuffer *buf,
909 GstClockTime in_ts, GstClockTime in_dur)
912 gint64 diff, cstart, cstop;
915 if (G_UNLIKELY (dec->segment.format != GST_FORMAT_TIME)) {
916 GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
920 // in_ts: in_timestamp. check a start time.
921 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (in_ts))) {
922 GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
927 GST_CLOCK_TIME_IS_VALID (in_dur) ? (in_ts + in_dur) : GST_CLOCK_TIME_NONE;
929 res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME, in_ts,
930 stop, &cstart, &cstop);
931 if (G_UNLIKELY (!res)) {
932 GST_LOG_OBJECT (dec, "out of segment");
933 GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
937 if (G_UNLIKELY ((diff = cstart - in_ts) > 0)) {
939 gst_util_uint64_scale_int (diff, dec->format.audio.samplerate, GST_SECOND) *
940 (dec->format.audio.depth * dec->format.audio.channels);
942 GST_DEBUG_OBJECT (dec, "clipping start to %" GST_TIME_FORMAT " %"
943 G_GINT64_FORMAT " bytes", GST_TIME_ARGS (cstart), diff);
945 GST_BUFFER_SIZE (buf) -= diff;
946 GST_BUFFER_DATA (buf) += diff;
950 if (G_UNLIKELY ((diff = stop - cstop) > 0)) {
952 gst_util_uint64_scale_int (diff, dec->format.audio.samplerate, GST_SECOND) *
953 (dec->format.audio.depth * dec->format.audio.channels);
955 GST_DEBUG_OBJECT (dec, "clipping stop to %" GST_TIME_FORMAT " %"
956 G_GINT64_FORMAT " bytes", GST_TIME_ARGS (cstop), diff);
958 GST_BUFFER_SIZE (buf) -= diff;
961 GST_BUFFER_TIMESTAMP (buf) = cstart;
962 GST_BUFFER_DURATION (buf) = cstop - cstart;
964 GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
969 gst_marudec_video_frame (GstMaruDec *marudec, guint8 *data, guint size,
970 const GstTSInfo *dec_info, gint64 in_offset, GstBuffer **outbuf,
973 gint len = -1, have_data;
974 gboolean mode_switch;
976 GstClockTime out_timestamp, out_duration, out_pts;
978 const GstTSInfo *out_info;
980 decode = gst_marudec_do_qos (marudec, dec_info->timestamp, &mode_switch);
981 CODEC_LOG (DEBUG, "decode: %d\n", decode);
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, 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);
1405 gst_marudec_frame (marudec, in_buf, in_size, &have_data, dec_info, in_offset, &ret);
1408 if (marudec->clear_ts) {
1409 in_timestamp = GST_CLOCK_TIME_NONE;
1410 in_duration = GST_CLOCK_TIME_NONE;
1411 in_offset = GST_BUFFER_OFFSET_NONE;
1412 in_info = GST_TS_INFO_NONE;
1414 marudec->clear_ts = TRUE;
1418 gst_buffer_unref (buffer);
1423 static GstStateChangeReturn
1424 gst_marudec_change_state (GstElement *element, GstStateChange transition)
1426 GstMaruDec *marudec = (GstMaruDec *) element;
1427 GstStateChangeReturn ret;
1429 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1431 switch (transition) {
1432 case GST_STATE_CHANGE_PAUSED_TO_READY:
1433 GST_OBJECT_LOCK (marudec);
1434 gst_marudec_close (marudec);
1435 GST_OBJECT_UNLOCK (marudec);
1438 clear_queued (marudec);
1448 gst_marudec_register (GstPlugin *plugin, GList *element)
1450 GTypeInfo typeinfo = {
1451 sizeof (GstMaruDecClass),
1452 (GBaseInitFunc) gst_marudec_base_init,
1454 (GClassInitFunc) gst_marudec_class_init,
1457 sizeof (GstMaruDec),
1459 (GInstanceInitFunc) gst_marudec_init,
1464 gint rank = GST_RANK_PRIMARY;
1465 GList *elem = element;
1466 CodecElement *codec = NULL;
1472 /* register element */
1474 codec = (CodecElement *)(elem->data);
1479 if (codec->codec_type != CODEC_TYPE_DECODE) {
1483 type_name = g_strdup_printf ("maru_%sdec", codec->name);
1484 type = g_type_from_name (type_name);
1486 type = g_type_register_static (GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
1487 g_type_set_qdata (type, GST_MARUDEC_PARAMS_QDATA, (gpointer) codec);
1490 if (!gst_element_register (plugin, type_name, rank, type)) {
1495 } while ((elem = elem->next));