Removed compile warnings.
[platform/adaptation/emulator/gst-plugins-emulator.git] / src / gstmarudec.c
1 /*
2  * GStreamer codec plugin for Tizen Emulator.
3  *
4  * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * KiTae Kim <kt920.kim@samsung.com>
8  * SeokYeon Hwang <syeon.hwang@samsung.com>
9  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  *
29  */
30
31 // #include "gstmaru.h"
32 #include "gstmarudevice.h"
33 #include "gstmaruutils.h"
34 #include "gstmaruinterface.h"
35 // #include "gstmarudevice.h"
36
37 #define GST_MARUDEC_PARAMS_QDATA g_quark_from_static_string("marudec-params")
38
39 /* indicate dts, pts, offset in the stream */
40
41 #define GST_TS_INFO_NONE &ts_info_none
42 static const GstTSInfo ts_info_none = { -1, -1, -1, -1 };
43
44 typedef struct _GstMaruDecClass
45 {
46   GstElementClass parent_class;
47
48   CodecElement *codec;
49   GstPadTemplate *sinktempl;
50   GstPadTemplate *srctempl;
51 } GstMaruDecClass;
52
53
54 static GstElementClass *parent_class = NULL;
55
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);
60
61 static gboolean gst_marudec_setcaps (GstPad *pad, GstCaps *caps);
62
63 // sinkpad
64 static gboolean gst_marudec_sink_event (GstPad *pad, GstEvent *event);
65 static GstFlowReturn gst_marudec_chain (GstPad *pad, GstBuffer *buffer);
66
67 // srcpad
68 static gboolean gst_marudec_src_event (GstPad *pad, GstEvent *event);
69 static GstStateChangeReturn gst_marudec_change_state (GstElement *element,
70                                                 GstStateChange transition);
71
72 static gboolean gst_marudec_negotiate (GstMaruDec *dec, gboolean force);
73
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);
77
78 static gboolean gst_marudec_open (GstMaruDec *marudec);
79 static int gst_marudec_close (GstMaruDec *marudec);
80
81
82 static const GstTSInfo *
83 gst_ts_info_store (GstMaruDec *dec, GstClockTime timestamp,
84     GstClockTime duration, gint64 offset)
85 {
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;
92
93   return &dec->ts_info[idx];
94 }
95
96 static const GstTSInfo *
97 gst_ts_info_get (GstMaruDec *dec, gint idx)
98 {
99   if (G_UNLIKELY (idx < 0 || idx > MAX_TS_MASK))
100     return GST_TS_INFO_NONE;
101
102   return &dec->ts_info[idx];
103 }
104
105 static void
106 gst_marudec_reset_ts (GstMaruDec *marudec)
107 {
108   marudec->next_out = GST_CLOCK_TIME_NONE;
109 }
110
111 static void
112 gst_marudec_update_qos (GstMaruDec *marudec, gdouble proportion,
113   GstClockTime timestamp)
114 {
115   GST_LOG_OBJECT (marudec, "update QOS: %f, %" GST_TIME_FORMAT,
116       proportion, GST_TIME_ARGS (timestamp));
117
118   GST_OBJECT_LOCK (marudec);
119   marudec->proportion = proportion;
120   marudec->earliest_time = timestamp;
121   GST_OBJECT_UNLOCK (marudec);
122 }
123
124 static void
125 gst_marudec_reset_qos (GstMaruDec *marudec)
126 {
127   gst_marudec_update_qos (marudec, 0.5, GST_CLOCK_TIME_NONE);
128   marudec->processed = 0;
129   marudec->dropped = 0;
130 }
131
132 static gboolean
133 gst_marudec_do_qos (GstMaruDec *marudec, GstClockTime timestamp,
134   gboolean *mode_switch)
135 {
136   GstClockTimeDiff diff;
137   gdouble proportion;
138   GstClockTime qostime, earliest_time;
139   gboolean res = TRUE;
140
141   *mode_switch = FALSE;
142
143   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
144     marudec->processed++;
145     return TRUE;
146   }
147
148   proportion = marudec->proportion;
149   earliest_time = marudec->earliest_time;
150
151   qostime = gst_segment_to_running_time (&marudec->segment, GST_FORMAT_TIME,
152     timestamp);
153
154   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (qostime))) {
155     marudec->processed++;
156     return TRUE;
157   }
158
159   diff = GST_CLOCK_DIFF (qostime, earliest_time);
160
161   if (proportion < 0.4 && diff < 0 ){
162     marudec->processed++;
163     return TRUE;
164   } else {
165     if (diff >= 0) {
166 //      if (marudec->waiting_for_key) {
167       if (0) {
168         res = FALSE;
169       }
170 #if 0
171       else {
172       }
173 #endif
174
175       GstClockTime stream_time, jitter;
176       GstMessage *qos_msg;
177
178       marudec->dropped++;
179       stream_time =
180           gst_segment_to_stream_time (&marudec->segment, GST_FORMAT_TIME,
181                   timestamp);
182       jitter = GST_CLOCK_DIFF (qostime, earliest_time);
183       qos_msg =
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);
190
191       return res;
192     }
193   }
194
195   marudec->processed++;
196   return TRUE;
197 }
198
199 static void
200 clear_queued (GstMaruDec *marudec)
201 {
202   g_list_foreach (marudec->queued, (GFunc) gst_mini_object_unref, NULL);
203   g_list_free (marudec->queued);
204   marudec->queued = NULL;
205 }
206
207 static GstFlowReturn
208 flush_queued (GstMaruDec *marudec)
209 {
210   GstFlowReturn res = GST_FLOW_OK;
211
212   CODEC_LOG (DEBUG, "flush queued\n");
213
214   while (marudec->queued) {
215     GstBuffer *buf = GST_BUFFER_CAST (marudec->queued->data);
216
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)));
223
224     res = gst_pad_push (marudec->srcpad, buf);
225
226     marudec->queued =
227       g_list_delete_link (marudec->queued, marudec->queued);
228   }
229
230   return res;
231 }
232
233 static void
234 gst_marudec_drain (GstMaruDec *marudec)
235 {
236 #if 0
237   GstMaruDecClass *oclass;
238   oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
239 #endif
240
241   CODEC_LOG (DEBUG, "drain frame\n");
242   {
243     gint have_data, len, try = 0;
244
245     do {
246       GstFlowReturn ret;
247
248       len =
249         gst_marudec_frame (marudec, NULL, 0, &have_data, &ts_info_none, 0, &ret);
250
251       if (len < 0 || have_data == 0) {
252         break;
253       }
254     } while (try++ < 10);
255   }
256
257   if (marudec->segment.rate < 0.0) {
258     CODEC_LOG (DEBUG, "reverse playback\n");
259     flush_queued (marudec);
260   }
261 }
262
263 /*
264  * Implementation
265  */
266 static void
267 gst_marudec_base_init (GstMaruDecClass *klass)
268 {
269   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
270   GstCaps *sinkcaps = NULL, *srccaps = NULL;
271   GstPadTemplate *sinktempl, *srctempl;
272   CodecElement *codec;
273   gchar *longname, *classification, *description;
274
275   codec =
276       (CodecElement *)g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
277                                       GST_MARUDEC_PARAMS_QDATA);
278
279   longname = g_strdup_printf ("%s Decoder", codec->longname);
280   classification = g_strdup_printf ("Codec/Decoder/%s",
281                     (codec->media_type == AVMEDIA_TYPE_VIDEO) ?
282                     "Video" : "Audio");
283   description = g_strdup_printf("%s Decoder", codec->name);
284
285   gst_element_class_set_details_simple (element_class,
286             longname,
287             classification,
288             description,
289             "Kitae Kim <kt920.kim@samsung.com>");
290
291   g_free (longname);
292   g_free (classification);
293   g_free (description);
294
295   sinkcaps = gst_maru_codecname_to_caps (codec->name, NULL, FALSE);
296   if (!sinkcaps) {
297     sinkcaps = gst_caps_from_string ("unknown/unknown");
298   }
299
300   switch (codec->media_type) {
301   case AVMEDIA_TYPE_VIDEO:
302     srccaps = gst_caps_from_string ("video/x-raw-rgb; video/x-raw-yuv");
303     break;
304   case AVMEDIA_TYPE_AUDIO:
305     srccaps = gst_maru_codectype_to_audio_caps (NULL, codec->name, FALSE, codec);
306     break;
307   default:
308     GST_LOG("unknown media type.\n");
309     break;
310   }
311
312   if (!srccaps) {
313     srccaps = gst_caps_from_string ("unknown/unknown");
314   }
315
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);
320
321   gst_element_class_add_pad_template (element_class, srctempl);
322   gst_element_class_add_pad_template (element_class, sinktempl);
323
324   klass->codec = codec;
325   klass->sinktempl = sinktempl;
326   klass->srctempl = srctempl;
327 }
328
329 static void
330 gst_marudec_class_init (GstMaruDecClass *klass)
331 {
332   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
333   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
334
335   parent_class = g_type_class_peek_parent (klass);
336
337 #if 0
338   gobject_class->set_property = gst_marudec_set_property
339   gobject_class->get_property = gst_marudec_get_property
340 #endif
341
342   gobject_class->finalize = gst_marudec_finalize;
343   gstelement_class->change_state = gst_marudec_change_state;
344 }
345
346 static void
347 gst_marudec_init (GstMaruDec *marudec)
348 {
349   GstMaruDecClass *oclass;
350
351   oclass = (GstMaruDecClass*) (G_OBJECT_GET_CLASS(marudec));
352
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));
360
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));
365
366   gst_element_add_pad (GST_ELEMENT(marudec), marudec->sinkpad);
367   gst_element_add_pad (GST_ELEMENT(marudec), marudec->srcpad);
368
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;
372
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;
377
378   marudec->queued = NULL;
379   gst_segment_init (&marudec->segment, GST_FORMAT_TIME);
380
381   marudec->dev = g_malloc0 (sizeof(CodecDevice));
382   if (!marudec->dev) {
383     CODEC_LOG (ERR, "failed to allocate memory.\n");
384   }
385 }
386
387 static void
388 gst_marudec_finalize (GObject *object)
389 {
390   GstMaruDec *marudec = (GstMaruDec *) object;
391
392   if (marudec->context) {
393     g_free (marudec->context);
394     marudec->context = NULL;
395   }
396
397   G_OBJECT_CLASS (parent_class)->finalize (object);
398 }
399
400 static gboolean
401 gst_marudec_src_event (GstPad *pad, GstEvent *event)
402 {
403   GstMaruDec *marudec;
404   gboolean res;
405
406   marudec = (GstMaruDec *) gst_pad_get_parent (pad);
407
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.*/
411   case GST_EVENT_QOS:
412   {
413     gdouble proportion;
414     GstClockTimeDiff diff;
415     GstClockTime timestamp;
416
417     gst_event_parse_qos (event, &proportion, &diff, &timestamp);
418
419     /* update our QoS values */
420     gst_marudec_update_qos (marudec, proportion, timestamp + diff);
421     break;
422   }
423   default:
424     break;
425   }
426
427   /* forward upstream */
428   res = gst_pad_push_event (marudec->sinkpad, event);
429
430   gst_object_unref (marudec);
431
432   return res;
433 }
434
435 static gboolean
436 gst_marudec_sink_event (GstPad *pad, GstEvent *event)
437 {
438   GstMaruDec *marudec;
439   gboolean ret = FALSE;
440
441   marudec = (GstMaruDec *) gst_pad_get_parent (pad);
442
443   GST_DEBUG_OBJECT (marudec, "Handling %s event",
444     GST_EVENT_TYPE_NAME (event));
445
446   switch (GST_EVENT_TYPE (event)) {
447   case GST_EVENT_EOS:
448     gst_marudec_drain (marudec);
449     break;
450   case GST_EVENT_FLUSH_STOP:
451   {
452     if (marudec->opened) {
453       codec_flush_buffers (marudec->context, marudec->dev);
454     }
455
456     gst_marudec_reset_ts (marudec);
457     gst_marudec_reset_qos (marudec);
458 #if 0
459     gst_marudec_flush_pcache (marudec);
460     marudec->waiting_for_key = TRUE;
461 #endif
462     gst_segment_init (&marudec->segment, GST_FORMAT_TIME);
463     clear_queued (marudec);
464   }
465     break;
466   case GST_EVENT_NEWSEGMENT:
467   {
468     gboolean update;
469     GstFormat format;
470     gint64 start, stop, time;
471     gdouble rate, arate;
472
473     gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
474         &start, &stop, &time);
475
476     switch (format) {
477     case GST_FORMAT_TIME:
478       break;
479     case GST_FORMAT_BYTES:
480     {
481       gint bit_rate;
482       bit_rate = marudec->context->bit_rate;
483
484       if (!bit_rate) {
485         GST_WARNING_OBJECT (marudec, "no bitrate to convert BYTES to TIME");
486         gst_event_unref (event);
487         gst_object_unref (marudec);
488         return ret;
489       }
490
491       GST_DEBUG_OBJECT (marudec, "bitrate: %d", bit_rate);
492
493       if (start != -1) {
494         start = gst_util_uint64_scale_int (start, GST_SECOND, bit_rate);
495       }
496       if (stop != -1) {
497         stop = gst_util_uint64_scale_int (stop, GST_SECOND, bit_rate);
498       }
499       if (time != -1) {
500         time = gst_util_uint64_scale_int (time, GST_SECOND, bit_rate);
501       }
502
503       gst_event_unref (event);
504
505       format = GST_FORMAT_TIME;
506
507       stop = -1;
508       event = gst_event_new_new_segment (update, rate, format,
509           start, stop, time);
510       break;
511     }
512     default:
513       GST_WARNING_OBJECT (marudec, "unknown format received in NEWSEGMENT");
514       gst_event_unref (event);
515       gst_object_unref (marudec);
516       return ret;
517     }
518
519     if (marudec->context->codec) {
520       gst_marudec_drain (marudec);
521     }
522
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));
526
527     gst_segment_set_newsegment_full (&marudec->segment, update,
528         rate, arate, format, start, stop, time);
529     break;
530   }
531   default:
532     break;
533   }
534
535   ret = gst_pad_push_event (marudec->srcpad, event);
536
537   gst_object_unref (marudec);
538
539   return ret;
540 }
541
542
543
544 static gboolean
545 gst_marudec_setcaps (GstPad *pad, GstCaps *caps)
546 {
547   GstMaruDec *marudec;
548   GstMaruDecClass *oclass;
549   GstStructure *structure;
550   const GValue *par;
551   const GValue *fps;
552   gboolean ret = TRUE;
553
554   GST_DEBUG_OBJECT (pad, "setcaps called.");
555
556   marudec = (GstMaruDec *) (gst_pad_get_parent (pad));
557   oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
558
559   GST_OBJECT_LOCK (marudec);
560
561   if (marudec->opened) {
562     GST_OBJECT_UNLOCK (marudec);
563     gst_marudec_drain (marudec);
564     GST_OBJECT_LOCK (marudec);
565     gst_marudec_close (marudec);
566   }
567
568   GST_LOG_OBJECT (marudec, "size %dx%d", marudec->context->video.width,
569       marudec->context->video.height);
570
571   if (!strcmp(oclass->codec->name, "wmv3") ||
572       !strcmp(oclass->codec->name, "vc1")) {
573     gst_maru_caps_to_codecname (caps, oclass->codec->name, NULL);
574   }
575
576   gst_maru_caps_with_codecname (oclass->codec->name, oclass->codec->media_type,
577                                 caps, marudec->context);
578
579   GST_LOG_OBJECT (marudec, "size after %dx%d", marudec->context->video.width,
580       marudec->context->video.height);
581
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;
586   }
587
588   structure = gst_caps_get_structure (caps, 0);
589
590   par = gst_structure_get_value (structure, "pixel-aspect-ratio");
591   if (par) {
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));
595
596 #if 0 // TODO
597     if (marudec->par) {
598       g_free(marudec->par);
599     }
600     marudec->par = g_new0 (GValue, 1);
601     gst_value_init_and_copy (marudec->par, par);
602 #endif
603   }
604
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);
611   } else {
612     marudec->format.video.fps_n = -1;
613     GST_DEBUG_OBJECT (marudec, "Using framerate from codec");
614   }
615
616 #if 0
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;
621     }
622   }
623 #endif
624
625   if (!gst_marudec_open (marudec)) {
626     GST_DEBUG_OBJECT (marudec, "Failed to open");
627 #if 0
628     if (marudec->par) {
629       g_free(marudec->par);
630       marudec->par = NULL;
631     }
632 #endif
633     GST_OBJECT_UNLOCK (marudec);
634     gst_object_unref (marudec);
635
636     return FALSE;
637   }
638
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);
643
644   GST_DEBUG_OBJECT (pad, "clipping to %dx%d",
645     marudec->format.video.clip_width, marudec->format.video.clip_height);
646
647   GST_OBJECT_UNLOCK (marudec);
648   gst_object_unref (marudec);
649
650   return ret;
651 }
652
653 static gboolean
654 gst_marudec_open (GstMaruDec *marudec)
655 {
656   GstMaruDecClass *oclass;
657
658   oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
659
660   if (!marudec->dev) {
661     return FALSE;
662   }
663
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);
669     return FALSE;
670   }
671
672   marudec->opened = TRUE;
673   GST_LOG_OBJECT (marudec, "Opened codec %s", oclass->codec->name);
674
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;
683     break;
684   case AVMEDIA_TYPE_AUDIO:
685     marudec->format.audio.samplerate = 0;
686     marudec->format.audio.channels = 0;
687     marudec->format.audio.depth = 0;
688     break;
689   default:
690     break;
691   }
692
693   gst_marudec_reset_ts (marudec);
694
695   marudec->proportion = 0.0;
696   marudec->earliest_time = -1;
697
698   return TRUE;
699 }
700
701 static int
702 gst_marudec_close (GstMaruDec *marudec)
703 {
704   int ret = 0;
705
706   if (marudec->context->codecdata) {
707     g_free(marudec->context->codecdata);
708     marudec->context->codecdata = NULL;
709   }
710
711   if (!marudec->dev) {
712     return -1;
713   }
714
715   ret = gst_maru_avcodec_close (marudec->context, marudec->dev);
716
717   if (marudec->dev) {
718     g_free(marudec->dev);
719     marudec->dev = NULL;
720   }
721
722   return ret;
723 }
724
725
726 static gboolean
727 gst_marudec_negotiate (GstMaruDec *marudec, gboolean force)
728 {
729   GstMaruDecClass *oclass;
730   GstCaps *caps;
731
732   oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
733
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) {
743       return TRUE;
744     }
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;
752     break;
753   case AVMEDIA_TYPE_AUDIO:
754   {
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) {
760       return TRUE;
761     }
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;
765   }
766     break;
767   default:
768     break;
769   }
770
771   caps =
772     gst_maru_codectype_to_caps (oclass->codec->media_type, marudec->context,
773       oclass->codec->name, FALSE);
774
775   if (caps == NULL) {
776     GST_ELEMENT_ERROR (marudec, CORE, NEGOTIATION,
777       ("Could not find GStreamer caps mapping for codec '%s'.",
778       oclass->codec->name), (NULL));
779     return FALSE;
780   }
781
782   switch (oclass->codec->media_type) {
783   case AVMEDIA_TYPE_VIDEO:
784   {
785     gint width, height;
786     gboolean interlaced;
787
788     width = marudec->format.video.clip_width;
789     height = marudec->format.video.clip_height;
790     interlaced = marudec->format.video.interlaced;
791
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);
795       }
796       if (height < marudec->context->video.height) {
797           gst_caps_set_simple (caps, "height", G_TYPE_INT, height, NULL);
798       }
799       gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, interlaced,
800         NULL);
801
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);
806       }
807 #if 0
808       gst_marudec_add_pixel_aspect_ratio (marudec,
809         gst_caps_get_structure (caps, 0));
810 #endif
811     }
812   }
813     break;
814   case AVMEDIA_TYPE_AUDIO:
815     break;
816   default:
817     break;
818   }
819
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);
825     return FALSE;
826   }
827
828   gst_caps_unref (caps);
829
830   return TRUE;
831 }
832
833 GstBuffer *
834 new_aligned_buffer (gint size, GstCaps *caps)
835 {
836   GstBuffer *buf;
837
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;
842
843   if (caps) {
844     gst_buffer_set_caps (buf, caps);
845   }
846
847   return buf;
848 }
849
850 static GstFlowReturn
851 get_output_buffer (GstMaruDec *marudec, GstBuffer **outbuf)
852 {
853   gint pict_size;
854   GstFlowReturn ret;
855
856   ret = GST_FLOW_OK;
857
858   *outbuf = NULL;
859
860   if (G_UNLIKELY (!gst_marudec_negotiate (marudec, FALSE))) {
861     GST_DEBUG_OBJECT (marudec, "negotiate failed");
862     return GST_FLOW_NOT_NEGOTIATED;
863   }
864
865   pict_size = gst_maru_avpicture_size (marudec->context->video.pix_fmt,
866     marudec->context->video.width, marudec->context->video.height);
867   if (pict_size < 0) {
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;
873   }
874
875   CODEC_LOG (DEBUG, "outbuf size of decoded video: %d\n", pict_size);
876
877   gst_pad_set_element_private(GST_PAD_PEER(marudec->srcpad), (gpointer)marudec);
878
879   /* GstPadBufferAllocFunction is mostly overridden by elements that can
880    * provide a hardware buffer in order to avoid additional memcpy operations.
881    */
882   gst_pad_set_bufferalloc_function(
883     GST_PAD_PEER(marudec->srcpad),
884     (GstPadBufferAllocFunction) codec_buffer_alloc_and_copy);
885
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));
892     return ret;
893   }
894
895   return ret;
896 }
897
898 static gboolean
899 clip_video_buffer (GstMaruDec *dec, GstBuffer *buf,
900     GstClockTime in_ts, GstClockTime in_dur)
901 {
902   gboolean res = TRUE;
903
904   return res;
905 }
906
907 static gboolean
908 clip_audio_buffer (GstMaruDec *dec, GstBuffer *buf,
909     GstClockTime in_ts, GstClockTime in_dur)
910 {
911   GstClockTime stop;
912   gint64 diff, cstart, cstop;
913   gboolean res = TRUE;
914
915   if (G_UNLIKELY (dec->segment.format != GST_FORMAT_TIME)) {
916     GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
917     return res;
918   }
919
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 " : ""));
923     return res;
924   }
925
926   stop =
927     GST_CLOCK_TIME_IS_VALID (in_dur) ? (in_ts + in_dur) : GST_CLOCK_TIME_NONE;
928
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 " : ""));
934     return res;
935   }
936
937   if (G_UNLIKELY ((diff = cstart - in_ts) > 0)) {
938     diff =
939       gst_util_uint64_scale_int (diff, dec->format.audio.samplerate, GST_SECOND) *
940         (dec->format.audio.depth * dec->format.audio.channels);
941
942     GST_DEBUG_OBJECT (dec, "clipping start to %" GST_TIME_FORMAT " %"
943         G_GINT64_FORMAT " bytes", GST_TIME_ARGS (cstart), diff);
944
945     GST_BUFFER_SIZE (buf) -= diff;
946     GST_BUFFER_DATA (buf) += diff;
947
948   }
949
950   if (G_UNLIKELY ((diff = stop - cstop) > 0)) {
951     diff =
952       gst_util_uint64_scale_int (diff, dec->format.audio.samplerate, GST_SECOND) *
953         (dec->format.audio.depth * dec->format.audio.channels);
954
955     GST_DEBUG_OBJECT (dec, "clipping stop to %" GST_TIME_FORMAT " %"
956         G_GINT64_FORMAT " bytes", GST_TIME_ARGS (cstop), diff);
957
958     GST_BUFFER_SIZE (buf) -= diff;
959   }
960
961   GST_BUFFER_TIMESTAMP (buf) = cstart;
962   GST_BUFFER_DURATION (buf) = cstop - cstart;
963
964   GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
965   return res;
966 }
967
968 static gint
969 gst_marudec_video_frame (GstMaruDec *marudec, guint8 *data, guint size,
970     const GstTSInfo *dec_info, gint64 in_offset, GstBuffer **outbuf,
971     GstFlowReturn *ret)
972 {
973   gint len = -1, have_data;
974   gboolean mode_switch;
975   gboolean decode;
976   GstClockTime out_timestamp, out_duration, out_pts;
977   gint64 out_offset;
978   const GstTSInfo *out_info;
979
980   decode = gst_marudec_do_qos (marudec, dec_info->timestamp, &mode_switch);
981   CODEC_LOG (DEBUG, "decode: %d\n", decode);
982
983   CODEC_LOG (DEBUG, "decode video: input buffer size: %d\n", size);
984   len =
985     codec_decode_video (marudec->context, data, size,
986                           dec_info->idx, in_offset, outbuf,
987                           &have_data, marudec->dev);
988 #if 0
989   // skip_frame
990   if (!decode) {
991   }
992 #endif
993   GST_DEBUG_OBJECT (marudec, "after decode: len %d, have_data %d",
994     len, have_data);
995
996 #if 0
997   if (len < 0 && (mode_switch || marudec->context->skip_frame)) {
998     len = 0;
999   }
1000
1001   if (len > 0 && have_data <= 0 && (mode_switch
1002       || marudec->context->skip_frame)) {
1003     marudec->last_out = -1;
1004   }
1005 #endif
1006
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);
1011
1012     CODEC_LOG (DEBUG,
1013       "return flow %d, out %p, len %d, have_data: %d\n",
1014       *ret, *outbuf, len, have_data);
1015
1016     return len;
1017   }
1018
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;
1023
1024   *ret = get_output_buffer (marudec, outbuf);
1025   if (G_UNLIKELY (*ret != GST_FLOW_OK)) {
1026     GST_DEBUG_OBJECT (marudec, "no output buffer");
1027     len = -1;
1028     GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1029       *ret, *outbuf, len);
1030     return len;
1031   }
1032
1033   /* Timestamps */
1034   out_timestamp = -1;
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));
1039   }
1040
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));
1045   }
1046
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));
1051   }
1052   GST_BUFFER_TIMESTAMP (*outbuf) = out_timestamp;
1053
1054   /* Offset */
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");
1060
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,
1065       dec_info->offset);
1066     out_offset = dec_info->offset;
1067   } else {
1068     GST_LOG_OBJECT (marudec, "no valid offset found");
1069     out_offset = GST_BUFFER_OFFSET_NONE;
1070   }
1071   GST_BUFFER_OFFSET (*outbuf) = out_offset;
1072
1073   /* Duration */
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;
1079 #if 0
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;
1083 #endif
1084   } else {
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);
1091     } else {
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);
1099       } else {
1100         GST_LOG_OBJECT (marudec, "no valid duration found");
1101       }
1102     }
1103   }
1104
1105 #if 0
1106   if (GST_CLOCK_TIME_IS_VALID (out_duration)) {
1107     out_duration += out_duration * marudec->picture->repeat_pict / 2;
1108   }
1109   GST_BUFFER_DURATION (*outbuf) = out_duration;
1110
1111   if (out_timestamp != -1 && out_duration != -1 && out_duration != 0) {
1112     marudec->next_out = out_timestamp + out_duration;
1113   } else {
1114     marudec->next_out = -1;
1115   }
1116 #endif
1117
1118   if (G_UNLIKELY (!clip_video_buffer (marudec, *outbuf, out_timestamp,
1119       out_duration))) {
1120     GST_DEBUG_OBJECT (marudec, "buffer clipped");
1121     gst_buffer_unref (*outbuf);
1122     *outbuf = NULL;
1123     GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1124       *ret, *outbuf, len);
1125     return len;
1126   }
1127
1128   GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1129     *ret, *outbuf, len);
1130   return len;
1131 }
1132
1133 static gint
1134 gst_marudec_audio_frame (GstMaruDec *marudec, CodecElement *codec,
1135                           guint8 *data, guint size,
1136                           const GstTSInfo *dec_info, GstBuffer **outbuf,
1137                           GstFlowReturn *ret)
1138 {
1139   gint len = -1;
1140   gint have_data = FF_MAX_AUDIO_FRAME_SIZE;
1141   GstClockTime out_timestamp, out_duration;
1142   gint64 out_offset;
1143
1144   *outbuf =
1145       new_aligned_buffer (FF_MAX_AUDIO_FRAME_SIZE,
1146           GST_PAD_CAPS (marudec->srcpad));
1147
1148   CODEC_LOG (DEBUG, "decode audio, input buffer size: %d\n", size);
1149
1150   len = codec_decode_audio (marudec->context,
1151       (int16_t *) GST_BUFFER_DATA (*outbuf), &have_data,
1152       data, size, marudec->dev);
1153
1154   GST_DEBUG_OBJECT (marudec,
1155     "Decode audio: len=%d, have_data=%d", len, have_data);
1156
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);
1161       *outbuf = NULL;
1162       len = -1;
1163       GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1164         *ret, *outbuf, len);
1165       return len;
1166     }
1167
1168     GST_BUFFER_SIZE (*outbuf) = have_data;
1169
1170     if (GST_CLOCK_TIME_IS_VALID (dec_info->timestamp)) {
1171       out_timestamp = dec_info->timestamp;
1172     } else {
1173       out_timestamp = marudec->next_out;
1174     }
1175
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);
1180
1181     out_offset = dec_info->offset;
1182
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));
1187
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));
1192
1193     if (GST_CLOCK_TIME_IS_VALID (out_timestamp)) {
1194       marudec->next_out = out_timestamp + out_duration;
1195     }
1196
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);
1201       *outbuf = NULL;
1202       GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d", *ret, *outbuf, len);
1203       return len;
1204     }
1205   } else {
1206     gst_buffer_unref (*outbuf);
1207     *outbuf = NULL;
1208   }
1209
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;
1214   }
1215
1216   GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
1217     *ret, *outbuf, len);
1218   return len;
1219 }
1220
1221 static gint
1222 gst_marudec_frame (GstMaruDec *marudec, guint8 *data, guint size,
1223     gint *got_data, const GstTSInfo *dec_info, gint64 in_offset, GstFlowReturn *ret)
1224 {
1225   GstMaruDecClass *oclass;
1226   GstBuffer *outbuf = NULL;
1227   gint have_data = 0, len = 0;
1228
1229   if (G_UNLIKELY (marudec->context->codec == NULL)) {
1230     GST_ERROR_OBJECT (marudec, "no codec context");
1231     return -1;
1232   }
1233
1234   *ret = GST_FLOW_OK;
1235   oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
1236
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);
1241     break;
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;
1248     }
1249     break;
1250   default:
1251     GST_ERROR_OBJECT (marudec, "Asked to decode non-audio/video frame!");
1252     g_assert_not_reached ();
1253     break;
1254   }
1255
1256   if (outbuf) {
1257     have_data = 1;
1258   }
1259
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);
1264     *got_data = 0;
1265     return len;
1266   } else if (len == 0 && have_data == 0) {
1267     *got_data = 0;
1268     return len;
1269   } else {
1270     *got_data = 1;
1271   }
1272
1273   if (outbuf) {
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)));
1280
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.
1285        */
1286       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1287       marudec->discont = FALSE;
1288     }
1289
1290     if (marudec->segment.rate > 0.0) {
1291       // push forward
1292       *ret = gst_pad_push (marudec->srcpad, outbuf);
1293     } else {
1294       // push reverse
1295       GST_DEBUG_OBJECT (marudec, "queued frame");
1296       marudec->queued = g_list_prepend (marudec->queued, outbuf);
1297       *ret = GST_FLOW_OK;
1298     }
1299   } else {
1300     GST_DEBUG_OBJECT (marudec, "Didn't get a decoded buffer");
1301   }
1302
1303   return len;
1304 }
1305
1306 static GstFlowReturn
1307 gst_marudec_chain (GstPad *pad, GstBuffer *buffer)
1308 {
1309   GstMaruDec *marudec;
1310   GstMaruDecClass *oclass;
1311   guint8 *in_buf;
1312   gint in_size, have_data;
1313   GstFlowReturn ret = GST_FLOW_OK;
1314   GstClockTime in_timestamp;
1315   GstClockTime in_duration;
1316   gboolean discont;
1317   gint64 in_offset;
1318   const GstTSInfo *in_info;
1319   const GstTSInfo *dec_info;
1320
1321   marudec = (GstMaruDec *) (GST_PAD_PARENT (pad));
1322
1323   if (G_UNLIKELY (!marudec->opened)) {
1324     // not_negotiated
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;
1331   }
1332
1333   discont = GST_BUFFER_IS_DISCONT (buffer);
1334
1335   // FIXME
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);
1343   }
1344 //  marudec->clear_ts = TRUE;
1345
1346   oclass = (GstMaruDecClass *) (G_OBJECT_GET_CLASS (marudec));
1347 #if 0
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) {
1351       // skip_keyframe
1352     }
1353     marudec->waiting_for_key = FALSE;
1354   }
1355
1356   if (marudec->pcache) {
1357     GST_LOG_OBJECT (marudec, "join parse cache");
1358     buffer = gst_buffer_join (marudec->pcache, buffer);
1359     marudec->pcache = NULL;
1360   }
1361 #endif
1362
1363   in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
1364   in_duration = GST_BUFFER_DURATION (buffer);
1365   in_offset = GST_BUFFER_OFFSET (buffer);
1366
1367   in_info = gst_ts_info_store (marudec, in_timestamp, in_duration, in_offset);
1368
1369 #if 0
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) {
1377         GstClockTime diff;
1378         diff = in_timestamp - marudec->last_in;
1379         if (marudec->last_frames) {
1380           diff /= marudec->last_frames;
1381         }
1382
1383         GST_LOG_OBJECT (marudec, "estimated duration %" GST_TIME_FORMAT " %u",
1384           GST_TIME_ARGS (diff), marudec->last_frames);
1385
1386         marudec->last_diff = diff;
1387       }
1388     }
1389     marudec->last_in = in_timestamp;
1390     marudec->last_frames;
1391   }
1392 #endif
1393
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);
1399
1400   in_buf = GST_BUFFER_DATA (buffer);
1401   in_size = GST_BUFFER_SIZE (buffer);
1402
1403   dec_info = in_info;
1404
1405   gst_marudec_frame (marudec, in_buf, in_size, &have_data, dec_info, in_offset, &ret);
1406
1407 #if 0
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;
1413   } else {
1414     marudec->clear_ts = TRUE;
1415   }
1416 #endif
1417
1418   gst_buffer_unref (buffer);
1419
1420   return ret;
1421 }
1422
1423 static GstStateChangeReturn
1424 gst_marudec_change_state (GstElement *element, GstStateChange transition)
1425 {
1426   GstMaruDec *marudec = (GstMaruDec *) element;
1427   GstStateChangeReturn ret;
1428
1429   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1430
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);
1436
1437     /* clear queue */
1438     clear_queued (marudec);
1439     break;
1440   default:
1441     break;
1442   }
1443
1444   return ret;
1445 }
1446
1447 gboolean
1448 gst_marudec_register (GstPlugin *plugin, GList *element)
1449 {
1450   GTypeInfo typeinfo = {
1451       sizeof (GstMaruDecClass),
1452       (GBaseInitFunc) gst_marudec_base_init,
1453       NULL,
1454       (GClassInitFunc) gst_marudec_class_init,
1455       NULL,
1456       NULL,
1457       sizeof (GstMaruDec),
1458       0,
1459       (GInstanceInitFunc) gst_marudec_init,
1460   };
1461
1462   GType type;
1463   gchar *type_name;
1464   gint rank = GST_RANK_PRIMARY;
1465   GList *elem = element;
1466   CodecElement *codec = NULL;
1467
1468   if (!elem) {
1469     return FALSE;
1470   }
1471
1472   /* register element */
1473   do {
1474     codec = (CodecElement *)(elem->data);
1475     if (!codec) {
1476       return FALSE;
1477     }
1478
1479     if (codec->codec_type != CODEC_TYPE_DECODE) {
1480       continue;
1481     }
1482
1483     type_name = g_strdup_printf ("maru_%sdec", codec->name);
1484     type = g_type_from_name (type_name);
1485     if (!type) {
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);
1488     }
1489
1490     if (!gst_element_register (plugin, type_name, rank, type)) {
1491       g_free (type_name);
1492       return FALSE;
1493     }
1494     g_free (type_name);
1495   } while ((elem = elem->next));
1496
1497   return TRUE;
1498 }