enabling omx decoder
[platform/upstream/gstreamer.git] / omx / gstomxaudiodec.c
1 /*
2  * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
3  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
4  * Copyright (C) 2013, Collabora Ltd.
5  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  * Copyright (C) 2014, Sebastian Dröge <sebastian@centricular.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation
11  * version 2.1 of the License.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst.h>
29
30 #include <string.h>
31
32 #include "gstomxaudiodec.h"
33
34 GST_DEBUG_CATEGORY_STATIC (gst_omx_audio_dec_debug_category);
35 #define GST_CAT_DEFAULT gst_omx_audio_dec_debug_category
36
37 /* prototypes */
38 static void gst_omx_audio_dec_finalize (GObject * object);
39
40 static GstStateChangeReturn
41 gst_omx_audio_dec_change_state (GstElement * element,
42     GstStateChange transition);
43
44 static gboolean gst_omx_audio_dec_open (GstAudioDecoder * decoder);
45 static gboolean gst_omx_audio_dec_close (GstAudioDecoder * decoder);
46 static gboolean gst_omx_audio_dec_start (GstAudioDecoder * decoder);
47 static gboolean gst_omx_audio_dec_stop (GstAudioDecoder * decoder);
48 static gboolean gst_omx_audio_dec_set_format (GstAudioDecoder * decoder,
49     GstCaps * caps);
50 static void gst_omx_audio_dec_flush (GstAudioDecoder * decoder, gboolean hard);
51 static GstFlowReturn gst_omx_audio_dec_handle_frame (GstAudioDecoder * decoder,
52     GstBuffer * buffer);
53 static GstFlowReturn gst_omx_audio_dec_drain (GstOMXAudioDec * self);
54
55 enum
56 {
57   PROP_0
58 };
59
60 /* class initialization */
61
62 #define DEBUG_INIT \
63   GST_DEBUG_CATEGORY_INIT (gst_omx_audio_dec_debug_category, "omxaudiodec", 0, \
64       "debug category for gst-omx audio decoder base class");
65
66
67 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstOMXAudioDec, gst_omx_audio_dec,
68     GST_TYPE_AUDIO_DECODER, DEBUG_INIT);
69
70 static void
71 gst_omx_audio_dec_class_init (GstOMXAudioDecClass * klass)
72 {
73   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
74   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
75   GstAudioDecoderClass *audio_decoder_class = GST_AUDIO_DECODER_CLASS (klass);
76
77   gobject_class->finalize = gst_omx_audio_dec_finalize;
78
79   element_class->change_state =
80       GST_DEBUG_FUNCPTR (gst_omx_audio_dec_change_state);
81
82   audio_decoder_class->open = GST_DEBUG_FUNCPTR (gst_omx_audio_dec_open);
83   audio_decoder_class->close = GST_DEBUG_FUNCPTR (gst_omx_audio_dec_close);
84   audio_decoder_class->start = GST_DEBUG_FUNCPTR (gst_omx_audio_dec_start);
85   audio_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_omx_audio_dec_stop);
86   audio_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_omx_audio_dec_flush);
87   audio_decoder_class->set_format =
88       GST_DEBUG_FUNCPTR (gst_omx_audio_dec_set_format);
89   audio_decoder_class->handle_frame =
90       GST_DEBUG_FUNCPTR (gst_omx_audio_dec_handle_frame);
91
92   klass->cdata.type = GST_OMX_COMPONENT_TYPE_FILTER;
93   klass->cdata.default_src_template_caps =
94       "audio/x-raw, "
95       "rate = (int) [ 1, MAX ], "
96       "channels = (int) [ 1, " G_STRINGIFY (OMX_AUDIO_MAXCHANNELS) " ], "
97       "format = (string) " GST_AUDIO_FORMATS_ALL;
98 }
99
100 static void
101 gst_omx_audio_dec_init (GstOMXAudioDec * self)
102 {
103   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (self), TRUE);
104   gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (self), TRUE);
105   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
106       (self), TRUE);
107   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (self));
108
109   g_mutex_init (&self->drain_lock);
110   g_cond_init (&self->drain_cond);
111
112   self->output_adapter = gst_adapter_new ();
113 }
114
115 static gboolean
116 gst_omx_audio_dec_open (GstAudioDecoder * decoder)
117 {
118   GstOMXAudioDec *self = GST_OMX_AUDIO_DEC (decoder);
119   GstOMXAudioDecClass *klass = GST_OMX_AUDIO_DEC_GET_CLASS (self);
120   gint in_port_index, out_port_index;
121
122   GST_DEBUG_OBJECT (self, "Opening decoder");
123
124   self->dec =
125       gst_omx_component_new (GST_OBJECT_CAST (self), klass->cdata.core_name,
126       klass->cdata.component_name, klass->cdata.component_role,
127       klass->cdata.hacks);
128   self->started = FALSE;
129
130   if (!self->dec)
131     return FALSE;
132
133   if (gst_omx_component_get_state (self->dec,
134           GST_CLOCK_TIME_NONE) != OMX_StateLoaded)
135     return FALSE;
136
137   in_port_index = klass->cdata.in_port_index;
138   out_port_index = klass->cdata.out_port_index;
139
140   if (in_port_index == -1 || out_port_index == -1) {
141     OMX_PORT_PARAM_TYPE param;
142     OMX_ERRORTYPE err;
143
144     GST_OMX_INIT_STRUCT (&param);
145
146     err =
147         gst_omx_component_get_parameter (self->dec, OMX_IndexParamAudioInit,
148         &param);
149     if (err != OMX_ErrorNone) {
150       GST_WARNING_OBJECT (self, "Couldn't get port information: %s (0x%08x)",
151           gst_omx_error_to_string (err), err);
152       /* Fallback */
153       in_port_index = 0;
154       out_port_index = 1;
155     } else {
156       GST_DEBUG_OBJECT (self, "Detected %u ports, starting at %u",
157           (guint) param.nPorts, (guint) param.nStartPortNumber);
158       in_port_index = param.nStartPortNumber + 0;
159       out_port_index = param.nStartPortNumber + 1;
160     }
161   }
162   self->dec_in_port = gst_omx_component_add_port (self->dec, in_port_index);
163   self->dec_out_port = gst_omx_component_add_port (self->dec, out_port_index);
164
165   if (!self->dec_in_port || !self->dec_out_port)
166     return FALSE;
167
168   GST_DEBUG_OBJECT (self, "Opened decoder");
169
170   return TRUE;
171 }
172
173 static gboolean
174 gst_omx_audio_dec_shutdown (GstOMXAudioDec * self)
175 {
176   OMX_STATETYPE state;
177
178   GST_DEBUG_OBJECT (self, "Shutting down decoder");
179
180   state = gst_omx_component_get_state (self->dec, 0);
181   if (state > OMX_StateLoaded || state == OMX_StateInvalid) {
182     if (state > OMX_StateIdle) {
183       gst_omx_component_set_state (self->dec, OMX_StateIdle);
184       gst_omx_component_get_state (self->dec, 5 * GST_SECOND);
185     }
186     gst_omx_component_set_state (self->dec, OMX_StateLoaded);
187     gst_omx_port_deallocate_buffers (self->dec_in_port);
188     gst_omx_port_deallocate_buffers (self->dec_out_port);
189     if (state > OMX_StateLoaded)
190       gst_omx_component_get_state (self->dec, 5 * GST_SECOND);
191   }
192
193   return TRUE;
194 }
195
196 static gboolean
197 gst_omx_audio_dec_close (GstAudioDecoder * decoder)
198 {
199   GstOMXAudioDec *self = GST_OMX_AUDIO_DEC (decoder);
200
201   GST_DEBUG_OBJECT (self, "Closing decoder");
202
203   if (!gst_omx_audio_dec_shutdown (self))
204     return FALSE;
205
206   self->dec_in_port = NULL;
207   self->dec_out_port = NULL;
208   if (self->dec)
209     gst_omx_component_free (self->dec);
210   self->dec = NULL;
211
212   self->started = FALSE;
213
214   GST_DEBUG_OBJECT (self, "Closed decoder");
215
216   return TRUE;
217 }
218
219 static void
220 gst_omx_audio_dec_finalize (GObject * object)
221 {
222   GstOMXAudioDec *self = GST_OMX_AUDIO_DEC (object);
223
224   g_mutex_clear (&self->drain_lock);
225   g_cond_clear (&self->drain_cond);
226
227   if (self->output_adapter)
228     gst_object_unref (self->output_adapter);
229   self->output_adapter = NULL;
230
231   G_OBJECT_CLASS (gst_omx_audio_dec_parent_class)->finalize (object);
232 }
233
234 static GstStateChangeReturn
235 gst_omx_audio_dec_change_state (GstElement * element, GstStateChange transition)
236 {
237   GstOMXAudioDec *self;
238   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
239
240   g_return_val_if_fail (GST_IS_OMX_AUDIO_DEC (element),
241       GST_STATE_CHANGE_FAILURE);
242   self = GST_OMX_AUDIO_DEC (element);
243
244   switch (transition) {
245     case GST_STATE_CHANGE_NULL_TO_READY:
246       break;
247     case GST_STATE_CHANGE_READY_TO_PAUSED:
248       self->downstream_flow_ret = GST_FLOW_OK;
249       self->draining = FALSE;
250       self->started = FALSE;
251       break;
252     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
253       break;
254     case GST_STATE_CHANGE_PAUSED_TO_READY:
255       if (self->dec_in_port)
256         gst_omx_port_set_flushing (self->dec_in_port, 5 * GST_SECOND, TRUE);
257       if (self->dec_out_port)
258         gst_omx_port_set_flushing (self->dec_out_port, 5 * GST_SECOND, TRUE);
259
260       g_mutex_lock (&self->drain_lock);
261       self->draining = FALSE;
262       g_cond_broadcast (&self->drain_cond);
263       g_mutex_unlock (&self->drain_lock);
264       break;
265     default:
266       break;
267   }
268
269   ret =
270       GST_ELEMENT_CLASS (gst_omx_audio_dec_parent_class)->change_state
271       (element, transition);
272
273   if (ret == GST_STATE_CHANGE_FAILURE)
274     return ret;
275
276   switch (transition) {
277     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
278       break;
279     case GST_STATE_CHANGE_PAUSED_TO_READY:
280       self->downstream_flow_ret = GST_FLOW_FLUSHING;
281       self->started = FALSE;
282
283       if (!gst_omx_audio_dec_shutdown (self))
284         ret = GST_STATE_CHANGE_FAILURE;
285       break;
286     case GST_STATE_CHANGE_READY_TO_NULL:
287       break;
288     default:
289       break;
290   }
291
292   return ret;
293 }
294
295 static void
296 gst_omx_audio_dec_loop (GstOMXAudioDec * self)
297 {
298   GstOMXAudioDecClass *klass = GST_OMX_AUDIO_DEC_GET_CLASS (self);
299   GstOMXPort *port = self->dec_out_port;
300   GstOMXBuffer *buf = NULL;
301   GstFlowReturn flow_ret = GST_FLOW_OK;
302   GstOMXAcquireBufferReturn acq_return;
303   OMX_ERRORTYPE err;
304   gint spf;
305
306   acq_return = gst_omx_port_acquire_buffer (port, &buf);
307   if (acq_return == GST_OMX_ACQUIRE_BUFFER_ERROR) {
308     goto component_error;
309   } else if (acq_return == GST_OMX_ACQUIRE_BUFFER_FLUSHING) {
310     goto flushing;
311   } else if (acq_return == GST_OMX_ACQUIRE_BUFFER_EOS) {
312     goto eos;
313   }
314
315   if (!gst_pad_has_current_caps (GST_AUDIO_DECODER_SRC_PAD (self)) ||
316       acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
317     OMX_PARAM_PORTDEFINITIONTYPE port_def;
318     OMX_AUDIO_PARAM_PCMMODETYPE pcm_param;
319     GstAudioChannelPosition omx_position[OMX_AUDIO_MAXCHANNELS];
320     GstOMXAudioDecClass *klass = GST_OMX_AUDIO_DEC_GET_CLASS (self);
321     gint i;
322
323     GST_DEBUG_OBJECT (self, "Port settings have changed, updating caps");
324
325     /* Reallocate all buffers */
326     if (acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE
327         && gst_omx_port_is_enabled (port)) {
328       err = gst_omx_port_set_enabled (port, FALSE);
329       if (err != OMX_ErrorNone)
330         goto reconfigure_error;
331
332       err = gst_omx_port_wait_buffers_released (port, 5 * GST_SECOND);
333       if (err != OMX_ErrorNone)
334         goto reconfigure_error;
335
336       err = gst_omx_port_deallocate_buffers (port);
337       if (err != OMX_ErrorNone)
338         goto reconfigure_error;
339
340       err = gst_omx_port_wait_enabled (port, 1 * GST_SECOND);
341       if (err != OMX_ErrorNone)
342         goto reconfigure_error;
343     }
344
345     /* Just update caps */
346     GST_AUDIO_DECODER_STREAM_LOCK (self);
347
348     gst_omx_port_get_port_definition (port, &port_def);
349     g_assert (port_def.format.audio.eEncoding == OMX_AUDIO_CodingPCM);
350
351     GST_OMX_INIT_STRUCT (&pcm_param);
352     pcm_param.nPortIndex = self->dec_out_port->index;
353     err =
354         gst_omx_component_get_parameter (self->dec, OMX_IndexParamAudioPcm,
355         &pcm_param);
356     if (err != OMX_ErrorNone) {
357       GST_ERROR_OBJECT (self, "Failed to get PCM parameters: %s (0x%08x)",
358           gst_omx_error_to_string (err), err);
359       goto caps_failed;
360     }
361
362     g_assert (pcm_param.ePCMMode == OMX_AUDIO_PCMModeLinear);
363     g_assert (pcm_param.bInterleaved == OMX_TRUE);
364
365     gst_audio_info_init (&self->info);
366
367     for (i = 0; i < pcm_param.nChannels; i++) {
368       switch (pcm_param.eChannelMapping[i]) {
369         case OMX_AUDIO_ChannelLF:
370           omx_position[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
371           break;
372         case OMX_AUDIO_ChannelRF:
373           omx_position[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
374           break;
375         case OMX_AUDIO_ChannelCF:
376           omx_position[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
377           break;
378         case OMX_AUDIO_ChannelLS:
379           omx_position[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
380           break;
381         case OMX_AUDIO_ChannelRS:
382           omx_position[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
383           break;
384         case OMX_AUDIO_ChannelLFE:
385           omx_position[i] = GST_AUDIO_CHANNEL_POSITION_LFE1;
386           break;
387         case OMX_AUDIO_ChannelCS:
388           omx_position[i] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
389           break;
390         case OMX_AUDIO_ChannelLR:
391           omx_position[i] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
392           break;
393         case OMX_AUDIO_ChannelRR:
394           omx_position[i] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
395           break;
396         case OMX_AUDIO_ChannelNone:
397         default:
398           /* This will break the outer loop too as the
399            * i == pcm_param.nChannels afterwards */
400           for (i = 0; i < pcm_param.nChannels; i++)
401             omx_position[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
402           break;
403       }
404     }
405     if (pcm_param.nChannels == 1
406         && omx_position[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER)
407       omx_position[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
408
409     if (omx_position[0] == GST_AUDIO_CHANNEL_POSITION_NONE
410         && klass->get_channel_positions) {
411       GST_WARNING_OBJECT (self,
412           "Failed to get a valid channel layout, trying fallback");
413       klass->get_channel_positions (self, self->dec_out_port, omx_position);
414     }
415
416     memcpy (self->position, omx_position, sizeof (omx_position));
417     gst_audio_channel_positions_to_valid_order (self->position,
418         pcm_param.nChannels);
419     self->needs_reorder =
420         (memcmp (self->position, omx_position,
421             sizeof (GstAudioChannelPosition) * pcm_param.nChannels) != 0);
422     if (self->needs_reorder)
423       gst_audio_get_channel_reorder_map (pcm_param.nChannels, self->position,
424           omx_position, self->reorder_map);
425
426     gst_audio_info_set_format (&self->info,
427         gst_audio_format_build_integer (pcm_param.eNumData ==
428             OMX_NumericalDataSigned,
429             pcm_param.eEndian ==
430             OMX_EndianLittle ? G_LITTLE_ENDIAN : G_BIG_ENDIAN,
431             pcm_param.nBitPerSample, pcm_param.nBitPerSample),
432         pcm_param.nSamplingRate, pcm_param.nChannels, self->position);
433
434     GST_DEBUG_OBJECT (self,
435         "Setting output state: format %s, rate %u, channels %u",
436         gst_audio_format_to_string (self->info.finfo->format),
437         (guint) pcm_param.nSamplingRate, (guint) pcm_param.nChannels);
438
439     if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (self),
440             &self->info)
441         || !gst_audio_decoder_negotiate (GST_AUDIO_DECODER (self))) {
442       if (buf)
443         gst_omx_port_release_buffer (port, buf);
444       goto caps_failed;
445     }
446
447     GST_AUDIO_DECODER_STREAM_UNLOCK (self);
448
449     if (acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
450       err = gst_omx_port_set_enabled (port, TRUE);
451       if (err != OMX_ErrorNone)
452         goto reconfigure_error;
453
454       err = gst_omx_port_allocate_buffers (port);
455       if (err != OMX_ErrorNone)
456         goto reconfigure_error;
457
458       err = gst_omx_port_wait_enabled (port, 5 * GST_SECOND);
459       if (err != OMX_ErrorNone)
460         goto reconfigure_error;
461
462       err = gst_omx_port_populate (port);
463       if (err != OMX_ErrorNone)
464         goto reconfigure_error;
465
466       err = gst_omx_port_mark_reconfigured (port);
467       if (err != OMX_ErrorNone)
468         goto reconfigure_error;
469     }
470
471     /* Now get a buffer */
472     if (acq_return != GST_OMX_ACQUIRE_BUFFER_OK) {
473       return;
474     }
475   }
476
477   g_assert (acq_return == GST_OMX_ACQUIRE_BUFFER_OK);
478   if (!buf) {
479     g_assert ((klass->cdata.hacks & GST_OMX_HACK_NO_EMPTY_EOS_BUFFER));
480     GST_AUDIO_DECODER_STREAM_LOCK (self);
481     goto eos;
482   }
483
484   /* This prevents a deadlock between the srcpad stream
485    * lock and the audiocodec stream lock, if ::reset()
486    * is called at the wrong time
487    */
488   if (gst_omx_port_is_flushing (port)) {
489     GST_DEBUG_OBJECT (self, "Flushing");
490     gst_omx_port_release_buffer (port, buf);
491     goto flushing;
492   }
493
494   GST_DEBUG_OBJECT (self, "Handling buffer: 0x%08x %" G_GUINT64_FORMAT,
495       (guint) buf->omx_buf->nFlags, (guint64) buf->omx_buf->nTimeStamp);
496
497   GST_AUDIO_DECODER_STREAM_LOCK (self);
498
499   spf = klass->get_samples_per_frame (self, self->dec_out_port);
500
501   if (buf->omx_buf->nFilledLen > 0) {
502     GstBuffer *outbuf;
503     GstMapInfo minfo;
504
505     GST_DEBUG_OBJECT (self, "Handling output data");
506
507     if (buf->omx_buf->nFilledLen % self->info.bpf != 0) {
508       gst_omx_port_release_buffer (port, buf);
509       goto invalid_buffer;
510     }
511
512     outbuf =
513         gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (self),
514         buf->omx_buf->nFilledLen);
515
516     gst_buffer_map (outbuf, &minfo, GST_MAP_WRITE);
517     if (self->needs_reorder) {
518       gint i, n_samples, c, n_channels;
519       gint *reorder_map = self->reorder_map;
520       gint16 *dest, *source;
521
522       dest = (gint16 *) minfo.data;
523       source = (gint16 *) (buf->omx_buf->pBuffer + buf->omx_buf->nOffset);
524       n_samples = buf->omx_buf->nFilledLen / self->info.bpf;
525       n_channels = self->info.channels;
526
527       for (i = 0; i < n_samples; i++) {
528         for (c = 0; c < n_channels; c++) {
529           dest[i * n_channels + reorder_map[c]] = source[i * n_channels + c];
530         }
531       }
532     } else {
533       memcpy (minfo.data, buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
534           buf->omx_buf->nFilledLen);
535     }
536     gst_buffer_unmap (outbuf, &minfo);
537
538     if (spf != -1) {
539       gst_adapter_push (self->output_adapter, outbuf);
540     } else {
541       flow_ret =
542           gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (self), outbuf, 1);
543     }
544   }
545
546   GST_DEBUG_OBJECT (self, "Read frame from component");
547
548   if (spf != -1) {
549     GstBuffer *outbuf;
550     guint avail = gst_adapter_available (self->output_adapter);
551     guint nframes;
552
553     /* We take a multiple of codec frames and push
554      * them downstream
555      */
556     avail /= self->info.bpf;
557     nframes = avail / spf;
558     avail = nframes * spf;
559     avail *= self->info.bpf;
560
561     if (avail > 0) {
562       outbuf = gst_adapter_take_buffer (self->output_adapter, avail);
563       flow_ret =
564           gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (self), outbuf,
565           nframes);
566     }
567   }
568
569   GST_DEBUG_OBJECT (self, "Finished frame: %s", gst_flow_get_name (flow_ret));
570
571   if (buf) {
572     err = gst_omx_port_release_buffer (port, buf);
573     if (err != OMX_ErrorNone)
574       goto release_error;
575   }
576
577   self->downstream_flow_ret = flow_ret;
578
579   if (flow_ret != GST_FLOW_OK)
580     goto flow_error;
581
582   GST_AUDIO_DECODER_STREAM_UNLOCK (self);
583
584   return;
585
586 component_error:
587   {
588     GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
589         ("OpenMAX component in error state %s (0x%08x)",
590             gst_omx_component_get_last_error_string (self->dec),
591             gst_omx_component_get_last_error (self->dec)));
592     gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
593     gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
594     self->downstream_flow_ret = GST_FLOW_ERROR;
595     self->started = FALSE;
596     return;
597   }
598
599 flushing:
600   {
601     GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
602     gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
603     self->downstream_flow_ret = GST_FLOW_FLUSHING;
604     self->started = FALSE;
605     return;
606   }
607
608 eos:
609   {
610     spf = klass->get_samples_per_frame (self, self->dec_out_port);
611     if (spf != -1) {
612       GstBuffer *outbuf;
613       guint avail = gst_adapter_available (self->output_adapter);
614       guint nframes;
615
616       /* On EOS we take the complete adapter content, no matter
617        * if it is a multiple of the codec frame size or not.
618        */
619       avail /= self->info.bpf;
620       nframes = (avail + spf - 1) / spf;
621       avail *= self->info.bpf;
622
623       if (avail > 0) {
624         outbuf = gst_adapter_take_buffer (self->output_adapter, avail);
625         flow_ret =
626             gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (self), outbuf,
627             nframes);
628       }
629     }
630
631     g_mutex_lock (&self->drain_lock);
632     if (self->draining) {
633       GST_DEBUG_OBJECT (self, "Drained");
634       self->draining = FALSE;
635       g_cond_broadcast (&self->drain_cond);
636       flow_ret = GST_FLOW_OK;
637       gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
638     } else {
639       GST_DEBUG_OBJECT (self, "Component signalled EOS");
640       flow_ret = GST_FLOW_EOS;
641     }
642     g_mutex_unlock (&self->drain_lock);
643
644     GST_AUDIO_DECODER_STREAM_LOCK (self);
645     self->downstream_flow_ret = flow_ret;
646
647     /* Here we fallback and pause the task for the EOS case */
648     if (flow_ret != GST_FLOW_OK)
649       goto flow_error;
650
651     GST_AUDIO_DECODER_STREAM_UNLOCK (self);
652
653     return;
654   }
655
656 flow_error:
657   {
658     if (flow_ret == GST_FLOW_EOS) {
659       GST_DEBUG_OBJECT (self, "EOS");
660
661       gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self),
662           gst_event_new_eos ());
663       gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
664       self->started = FALSE;
665     } else if (flow_ret < GST_FLOW_EOS) {
666       GST_ELEMENT_ERROR (self, STREAM, FAILED,
667           ("Internal data stream error."), ("stream stopped, reason %s",
668               gst_flow_get_name (flow_ret)));
669
670       gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self),
671           gst_event_new_eos ());
672       gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
673       self->started = FALSE;
674     } else if (flow_ret == GST_FLOW_FLUSHING) {
675       GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
676       gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
677       self->started = FALSE;
678     }
679     GST_AUDIO_DECODER_STREAM_UNLOCK (self);
680     return;
681   }
682
683 reconfigure_error:
684   {
685     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
686         ("Unable to reconfigure output port"));
687     gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
688     gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
689     self->downstream_flow_ret = GST_FLOW_ERROR;
690     self->started = FALSE;
691     return;
692   }
693
694 invalid_buffer:
695   {
696     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
697         ("Invalid sized input buffer"));
698     gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
699     gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
700     self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
701     self->started = FALSE;
702     GST_AUDIO_DECODER_STREAM_UNLOCK (self);
703     return;
704   }
705
706 caps_failed:
707   {
708     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL), ("Failed to set caps"));
709     gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
710     gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
711     GST_AUDIO_DECODER_STREAM_UNLOCK (self);
712     self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
713     self->started = FALSE;
714     return;
715   }
716 release_error:
717   {
718     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
719         ("Failed to relase output buffer to component: %s (0x%08x)",
720             gst_omx_error_to_string (err), err));
721     gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
722     gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
723     self->downstream_flow_ret = GST_FLOW_ERROR;
724     self->started = FALSE;
725     GST_AUDIO_DECODER_STREAM_UNLOCK (self);
726     return;
727   }
728 }
729
730 static gboolean
731 gst_omx_audio_dec_start (GstAudioDecoder * decoder)
732 {
733   GstOMXAudioDec *self;
734
735   self = GST_OMX_AUDIO_DEC (decoder);
736
737   self->last_upstream_ts = 0;
738   self->downstream_flow_ret = GST_FLOW_OK;
739
740   return TRUE;
741 }
742
743 static gboolean
744 gst_omx_audio_dec_stop (GstAudioDecoder * decoder)
745 {
746   GstOMXAudioDec *self;
747
748   self = GST_OMX_AUDIO_DEC (decoder);
749
750   GST_DEBUG_OBJECT (self, "Stopping decoder");
751
752   gst_omx_port_set_flushing (self->dec_in_port, 5 * GST_SECOND, TRUE);
753   gst_omx_port_set_flushing (self->dec_out_port, 5 * GST_SECOND, TRUE);
754
755   gst_pad_stop_task (GST_AUDIO_DECODER_SRC_PAD (decoder));
756
757   if (gst_omx_component_get_state (self->dec, 0) > OMX_StateIdle)
758     gst_omx_component_set_state (self->dec, OMX_StateIdle);
759
760   self->downstream_flow_ret = GST_FLOW_FLUSHING;
761   self->started = FALSE;
762
763   g_mutex_lock (&self->drain_lock);
764   self->draining = FALSE;
765   g_cond_broadcast (&self->drain_cond);
766   g_mutex_unlock (&self->drain_lock);
767
768   gst_adapter_flush (self->output_adapter,
769       gst_adapter_available (self->output_adapter));
770
771   gst_omx_component_get_state (self->dec, 5 * GST_SECOND);
772
773   gst_buffer_replace (&self->codec_data, NULL);
774
775   GST_DEBUG_OBJECT (self, "Stopped decoder");
776
777   return TRUE;
778 }
779
780 static gboolean
781 gst_omx_audio_dec_set_format (GstAudioDecoder * decoder, GstCaps * caps)
782 {
783   GstOMXAudioDec *self;
784   GstOMXAudioDecClass *klass;
785   GstStructure *s;
786   const GValue *codec_data;
787   gboolean is_format_change = FALSE;
788   gboolean needs_disable = FALSE;
789
790   self = GST_OMX_AUDIO_DEC (decoder);
791   klass = GST_OMX_AUDIO_DEC_GET_CLASS (decoder);
792
793   GST_DEBUG_OBJECT (self, "Setting new caps %" GST_PTR_FORMAT, caps);
794
795   /* Check if the caps change is a real format change or if only irrelevant
796    * parts of the caps have changed or nothing at all.
797    */
798   if (klass->is_format_change)
799     is_format_change = klass->is_format_change (self, self->dec_in_port, caps);
800
801   needs_disable =
802       gst_omx_component_get_state (self->dec,
803       GST_CLOCK_TIME_NONE) != OMX_StateLoaded;
804   /* If the component is not in Loaded state and a real format change happens
805    * we have to disable the port and re-allocate all buffers. If no real
806    * format change happened we can just exit here.
807    */
808   if (needs_disable && !is_format_change) {
809     GST_DEBUG_OBJECT (self,
810         "Already running and caps did not change the format");
811     return TRUE;
812   }
813
814   if (needs_disable && is_format_change) {
815     GstOMXPort *out_port = self->dec_out_port;
816
817     GST_DEBUG_OBJECT (self, "Need to disable and drain decoder");
818
819     gst_omx_audio_dec_drain (self);
820     gst_omx_audio_dec_flush (decoder, FALSE);
821     gst_omx_port_set_flushing (out_port, 5 * GST_SECOND, TRUE);
822
823     if (klass->cdata.hacks & GST_OMX_HACK_NO_COMPONENT_RECONFIGURE) {
824       GST_AUDIO_DECODER_STREAM_UNLOCK (self);
825       gst_omx_audio_dec_stop (GST_AUDIO_DECODER (self));
826       gst_omx_audio_dec_close (GST_AUDIO_DECODER (self));
827       GST_AUDIO_DECODER_STREAM_LOCK (self);
828
829       if (!gst_omx_audio_dec_open (GST_AUDIO_DECODER (self)))
830         return FALSE;
831       needs_disable = FALSE;
832     } else {
833       if (gst_omx_port_set_enabled (self->dec_in_port, FALSE) != OMX_ErrorNone)
834         return FALSE;
835       if (gst_omx_port_set_enabled (out_port, FALSE) != OMX_ErrorNone)
836         return FALSE;
837       if (gst_omx_port_wait_buffers_released (self->dec_in_port,
838               5 * GST_SECOND) != OMX_ErrorNone)
839         return FALSE;
840       if (gst_omx_port_wait_buffers_released (out_port,
841               1 * GST_SECOND) != OMX_ErrorNone)
842         return FALSE;
843       if (gst_omx_port_deallocate_buffers (self->dec_in_port) != OMX_ErrorNone)
844         return FALSE;
845       if (gst_omx_port_deallocate_buffers (self->dec_out_port) != OMX_ErrorNone)
846         return FALSE;
847       if (gst_omx_port_wait_enabled (self->dec_in_port,
848               1 * GST_SECOND) != OMX_ErrorNone)
849         return FALSE;
850       if (gst_omx_port_wait_enabled (out_port, 1 * GST_SECOND) != OMX_ErrorNone)
851         return FALSE;
852     }
853
854     GST_DEBUG_OBJECT (self, "Decoder drained and disabled");
855   }
856
857   if (klass->set_format) {
858     if (!klass->set_format (self, self->dec_in_port, caps)) {
859       GST_ERROR_OBJECT (self, "Subclass failed to set the new format");
860       return FALSE;
861     }
862   }
863
864   GST_DEBUG_OBJECT (self, "Updating outport port definition");
865   if (gst_omx_port_update_port_definition (self->dec_out_port,
866           NULL) != OMX_ErrorNone)
867     return FALSE;
868
869   /* Get codec data from caps */
870   gst_buffer_replace (&self->codec_data, NULL);
871   s = gst_caps_get_structure (caps, 0);
872   codec_data = gst_structure_get_value (s, "codec_data");
873   if (codec_data) {
874     /* Vorbis and some other codecs have multiple buffers in
875      * the stream-header field */
876     self->codec_data = gst_value_get_buffer (codec_data);
877     if (self->codec_data)
878       gst_buffer_ref (self->codec_data);
879   }
880
881   GST_DEBUG_OBJECT (self, "Enabling component");
882
883   if (needs_disable) {
884     if (gst_omx_port_set_enabled (self->dec_in_port, TRUE) != OMX_ErrorNone)
885       return FALSE;
886     if (gst_omx_port_allocate_buffers (self->dec_in_port) != OMX_ErrorNone)
887       return FALSE;
888
889     if ((klass->cdata.hacks & GST_OMX_HACK_NO_DISABLE_OUTPORT)) {
890       if (gst_omx_port_set_enabled (self->dec_out_port, TRUE) != OMX_ErrorNone)
891         return FALSE;
892       if (gst_omx_port_allocate_buffers (self->dec_out_port) != OMX_ErrorNone)
893         return FALSE;
894
895       if (gst_omx_port_wait_enabled (self->dec_out_port,
896               5 * GST_SECOND) != OMX_ErrorNone)
897         return FALSE;
898     }
899
900     if (gst_omx_port_wait_enabled (self->dec_in_port,
901             5 * GST_SECOND) != OMX_ErrorNone)
902       return FALSE;
903     if (gst_omx_port_mark_reconfigured (self->dec_in_port) != OMX_ErrorNone)
904       return FALSE;
905   } else {
906     if (!(klass->cdata.hacks & GST_OMX_HACK_NO_DISABLE_OUTPORT)) {
907       /* Disable output port */
908       if (gst_omx_port_set_enabled (self->dec_out_port, FALSE) != OMX_ErrorNone)
909         return FALSE;
910
911       if (gst_omx_port_wait_enabled (self->dec_out_port,
912               1 * GST_SECOND) != OMX_ErrorNone)
913         return FALSE;
914
915       if (gst_omx_component_set_state (self->dec,
916               OMX_StateIdle) != OMX_ErrorNone)
917         return FALSE;
918
919       /* Need to allocate buffers to reach Idle state */
920       if (gst_omx_port_allocate_buffers (self->dec_in_port) != OMX_ErrorNone)
921         return FALSE;
922     } else {
923       if (gst_omx_component_set_state (self->dec,
924               OMX_StateIdle) != OMX_ErrorNone)
925         return FALSE;
926
927       /* Need to allocate buffers to reach Idle state */
928       if (gst_omx_port_allocate_buffers (self->dec_in_port) != OMX_ErrorNone)
929         return FALSE;
930       if (gst_omx_port_allocate_buffers (self->dec_out_port) != OMX_ErrorNone)
931         return FALSE;
932     }
933
934     if (gst_omx_component_get_state (self->dec,
935             GST_CLOCK_TIME_NONE) != OMX_StateIdle)
936       return FALSE;
937
938     if (gst_omx_component_set_state (self->dec,
939             OMX_StateExecuting) != OMX_ErrorNone)
940       return FALSE;
941
942     if (gst_omx_component_get_state (self->dec,
943             GST_CLOCK_TIME_NONE) != OMX_StateExecuting)
944       return FALSE;
945   }
946
947   /* Unset flushing to allow ports to accept data again */
948   gst_omx_port_set_flushing (self->dec_in_port, 5 * GST_SECOND, FALSE);
949   gst_omx_port_set_flushing (self->dec_out_port, 5 * GST_SECOND, FALSE);
950
951   if (gst_omx_component_get_last_error (self->dec) != OMX_ErrorNone) {
952     GST_ERROR_OBJECT (self, "Component in error state: %s (0x%08x)",
953         gst_omx_component_get_last_error_string (self->dec),
954         gst_omx_component_get_last_error (self->dec));
955     return FALSE;
956   }
957
958   self->downstream_flow_ret = GST_FLOW_OK;
959
960   return TRUE;
961 }
962
963 static void
964 gst_omx_audio_dec_flush (GstAudioDecoder * decoder, gboolean hard)
965 {
966   GstOMXAudioDec *self = GST_OMX_AUDIO_DEC (decoder);
967   OMX_ERRORTYPE err = OMX_ErrorNone;
968
969   GST_DEBUG_OBJECT (self, "Flushing decoder");
970
971   if (gst_omx_component_get_state (self->dec, 0) == OMX_StateLoaded)
972     return;
973
974   /* 0) Pause the components */
975   if (gst_omx_component_get_state (self->dec, 0) == OMX_StateExecuting) {
976     gst_omx_component_set_state (self->dec, OMX_StatePause);
977     gst_omx_component_get_state (self->dec, GST_CLOCK_TIME_NONE);
978   }
979
980   /* 1) Wait until the srcpad loop is stopped,
981    * unlock GST_AUDIO_DECODER_STREAM_LOCK to prevent deadlocks
982    * caused by using this lock from inside the loop function */
983   GST_AUDIO_DECODER_STREAM_UNLOCK (self);
984   gst_pad_stop_task (GST_AUDIO_DECODER_SRC_PAD (decoder));
985   GST_DEBUG_OBJECT (self, "Flushing -- task stopped");
986   GST_AUDIO_DECODER_STREAM_LOCK (self);
987
988   /* 2) Flush the ports */
989   GST_DEBUG_OBJECT (self, "flushing ports");
990   gst_omx_port_set_flushing (self->dec_in_port, 5 * GST_SECOND, TRUE);
991   gst_omx_port_set_flushing (self->dec_out_port, 5 * GST_SECOND, TRUE);
992
993   /* 3) Resume components */
994   gst_omx_component_set_state (self->dec, OMX_StateExecuting);
995   gst_omx_component_get_state (self->dec, GST_CLOCK_TIME_NONE);
996
997   /* 4) Unset flushing to allow ports to accept data again */
998   gst_omx_port_set_flushing (self->dec_in_port, 5 * GST_SECOND, FALSE);
999   gst_omx_port_set_flushing (self->dec_out_port, 5 * GST_SECOND, FALSE);
1000
1001   err = gst_omx_port_populate (self->dec_out_port);
1002
1003   if (err != OMX_ErrorNone) {
1004     GST_WARNING_OBJECT (self, "Failed to populate output port: %s (0x%08x)",
1005         gst_omx_error_to_string (err), err);
1006   }
1007
1008   /* Reset our state */
1009   gst_adapter_flush (self->output_adapter,
1010       gst_adapter_available (self->output_adapter));
1011   self->last_upstream_ts = 0;
1012   self->downstream_flow_ret = GST_FLOW_OK;
1013   self->started = FALSE;
1014   GST_DEBUG_OBJECT (self, "Flush finished");
1015 }
1016
1017 static GstFlowReturn
1018 gst_omx_audio_dec_handle_frame (GstAudioDecoder * decoder, GstBuffer * inbuf)
1019 {
1020   GstOMXAcquireBufferReturn acq_ret = GST_OMX_ACQUIRE_BUFFER_ERROR;
1021   GstOMXAudioDec *self;
1022   GstOMXPort *port;
1023   GstOMXBuffer *buf;
1024   GstBuffer *codec_data = NULL;
1025   guint offset = 0;
1026   GstClockTime timestamp, duration;
1027   OMX_ERRORTYPE err;
1028   GstMapInfo minfo;
1029
1030   self = GST_OMX_AUDIO_DEC (decoder);
1031
1032   GST_DEBUG_OBJECT (self, "Handling frame");
1033
1034   if (self->downstream_flow_ret != GST_FLOW_OK) {
1035     return self->downstream_flow_ret;
1036   }
1037
1038   if (!self->started) {
1039     GST_DEBUG_OBJECT (self, "Starting task");
1040     gst_pad_start_task (GST_AUDIO_DECODER_SRC_PAD (self),
1041         (GstTaskFunction) gst_omx_audio_dec_loop, decoder, NULL);
1042   }
1043
1044   if (inbuf == NULL)
1045     return gst_omx_audio_dec_drain (self);
1046
1047   /* Make sure to keep a reference to the input here,
1048    * it can be unreffed from the other thread if
1049    * finish_frame() is called */
1050   gst_buffer_ref (inbuf);
1051
1052   timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1053   duration = GST_BUFFER_DURATION (inbuf);
1054
1055   port = self->dec_in_port;
1056
1057   gst_buffer_map (inbuf, &minfo, GST_MAP_READ);
1058
1059   while (offset < minfo.size) {
1060     /* Make sure to release the base class stream lock, otherwise
1061      * _loop() can't call _finish_frame() and we might block forever
1062      * because no input buffers are released */
1063     GST_AUDIO_DECODER_STREAM_UNLOCK (self);
1064     acq_ret = gst_omx_port_acquire_buffer (port, &buf);
1065
1066     if (acq_ret == GST_OMX_ACQUIRE_BUFFER_ERROR) {
1067       GST_AUDIO_DECODER_STREAM_LOCK (self);
1068       goto component_error;
1069     } else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_FLUSHING) {
1070       GST_AUDIO_DECODER_STREAM_LOCK (self);
1071       goto flushing;
1072     } else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
1073       /* Reallocate all buffers */
1074       err = gst_omx_port_set_enabled (port, FALSE);
1075       if (err != OMX_ErrorNone) {
1076         GST_AUDIO_DECODER_STREAM_LOCK (self);
1077         goto reconfigure_error;
1078       }
1079
1080       err = gst_omx_port_wait_buffers_released (port, 5 * GST_SECOND);
1081       if (err != OMX_ErrorNone) {
1082         GST_AUDIO_DECODER_STREAM_LOCK (self);
1083         goto reconfigure_error;
1084       }
1085
1086       err = gst_omx_port_deallocate_buffers (port);
1087       if (err != OMX_ErrorNone) {
1088         GST_AUDIO_DECODER_STREAM_LOCK (self);
1089         goto reconfigure_error;
1090       }
1091
1092       err = gst_omx_port_wait_enabled (port, 1 * GST_SECOND);
1093       if (err != OMX_ErrorNone) {
1094         GST_AUDIO_DECODER_STREAM_LOCK (self);
1095         goto reconfigure_error;
1096       }
1097
1098       err = gst_omx_port_set_enabled (port, TRUE);
1099       if (err != OMX_ErrorNone) {
1100         GST_AUDIO_DECODER_STREAM_LOCK (self);
1101         goto reconfigure_error;
1102       }
1103
1104       err = gst_omx_port_allocate_buffers (port);
1105       if (err != OMX_ErrorNone) {
1106         GST_AUDIO_DECODER_STREAM_LOCK (self);
1107         goto reconfigure_error;
1108       }
1109
1110       err = gst_omx_port_wait_enabled (port, 5 * GST_SECOND);
1111       if (err != OMX_ErrorNone) {
1112         GST_AUDIO_DECODER_STREAM_LOCK (self);
1113         goto reconfigure_error;
1114       }
1115
1116       err = gst_omx_port_mark_reconfigured (port);
1117       if (err != OMX_ErrorNone) {
1118         GST_AUDIO_DECODER_STREAM_LOCK (self);
1119         goto reconfigure_error;
1120       }
1121
1122       /* Now get a new buffer and fill it */
1123       GST_AUDIO_DECODER_STREAM_LOCK (self);
1124       continue;
1125     }
1126     GST_AUDIO_DECODER_STREAM_LOCK (self);
1127
1128     g_assert (acq_ret == GST_OMX_ACQUIRE_BUFFER_OK && buf != NULL);
1129
1130     if (buf->omx_buf->nAllocLen - buf->omx_buf->nOffset <= 0) {
1131       gst_omx_port_release_buffer (port, buf);
1132       goto full_buffer;
1133     }
1134
1135     if (self->downstream_flow_ret != GST_FLOW_OK) {
1136       gst_omx_port_release_buffer (port, buf);
1137       goto flow_error;
1138     }
1139
1140     if (self->codec_data) {
1141       GST_DEBUG_OBJECT (self, "Passing codec data to the component");
1142
1143       codec_data = self->codec_data;
1144
1145       if (buf->omx_buf->nAllocLen - buf->omx_buf->nOffset <
1146           gst_buffer_get_size (codec_data)) {
1147         gst_omx_port_release_buffer (port, buf);
1148         goto too_large_codec_data;
1149       }
1150
1151       buf->omx_buf->nFlags |= OMX_BUFFERFLAG_CODECCONFIG;
1152       buf->omx_buf->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1153       buf->omx_buf->nFilledLen = gst_buffer_get_size (codec_data);
1154       gst_buffer_extract (codec_data, 0,
1155           buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
1156           buf->omx_buf->nFilledLen);
1157
1158       if (GST_CLOCK_TIME_IS_VALID (timestamp))
1159         buf->omx_buf->nTimeStamp =
1160             gst_util_uint64_scale (timestamp, OMX_TICKS_PER_SECOND, GST_SECOND);
1161       else
1162         buf->omx_buf->nTimeStamp = 0;
1163       buf->omx_buf->nTickCount = 0;
1164
1165       self->started = TRUE;
1166       err = gst_omx_port_release_buffer (port, buf);
1167       gst_buffer_replace (&self->codec_data, NULL);
1168       if (err != OMX_ErrorNone)
1169         goto release_error;
1170       /* Acquire new buffer for the actual frame */
1171       continue;
1172     }
1173
1174     /* Now handle the frame */
1175     GST_DEBUG_OBJECT (self, "Passing frame offset %d to the component", offset);
1176
1177     /* Copy the buffer content in chunks of size as requested
1178      * by the port */
1179     buf->omx_buf->nFilledLen =
1180         MIN (minfo.size - offset,
1181         buf->omx_buf->nAllocLen - buf->omx_buf->nOffset);
1182     gst_buffer_extract (inbuf, offset,
1183         buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
1184         buf->omx_buf->nFilledLen);
1185
1186     if (timestamp != GST_CLOCK_TIME_NONE) {
1187       buf->omx_buf->nTimeStamp =
1188           gst_util_uint64_scale (timestamp, OMX_TICKS_PER_SECOND, GST_SECOND);
1189       self->last_upstream_ts = timestamp;
1190     } else {
1191       buf->omx_buf->nTimeStamp = 0;
1192     }
1193
1194     if (duration != GST_CLOCK_TIME_NONE && offset == 0) {
1195       buf->omx_buf->nTickCount =
1196           gst_util_uint64_scale (duration, OMX_TICKS_PER_SECOND, GST_SECOND);
1197       self->last_upstream_ts += duration;
1198     } else {
1199       buf->omx_buf->nTickCount = 0;
1200     }
1201
1202     if (offset == 0)
1203       buf->omx_buf->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;
1204
1205     /* TODO: Set flags
1206      *   - OMX_BUFFERFLAG_DECODEONLY for buffers that are outside
1207      *     the segment
1208      */
1209
1210     offset += buf->omx_buf->nFilledLen;
1211
1212     if (offset == minfo.size)
1213       buf->omx_buf->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1214
1215     self->started = TRUE;
1216     err = gst_omx_port_release_buffer (port, buf);
1217     if (err != OMX_ErrorNone)
1218       goto release_error;
1219   }
1220   gst_buffer_unmap (inbuf, &minfo);
1221   gst_buffer_unref (inbuf);
1222
1223   GST_DEBUG_OBJECT (self, "Passed frame to component");
1224
1225   return self->downstream_flow_ret;
1226
1227 full_buffer:
1228   {
1229     gst_buffer_unmap (inbuf, &minfo);
1230     gst_buffer_unref (inbuf);
1231
1232     GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
1233         ("Got OpenMAX buffer with no free space (%p, %u/%u)", buf,
1234             (guint) buf->omx_buf->nOffset, (guint) buf->omx_buf->nAllocLen));
1235     return GST_FLOW_ERROR;
1236   }
1237
1238 flow_error:
1239   {
1240     gst_buffer_unmap (inbuf, &minfo);
1241     gst_buffer_unref (inbuf);
1242
1243     return self->downstream_flow_ret;
1244   }
1245
1246 too_large_codec_data:
1247   {
1248     gst_buffer_unmap (inbuf, &minfo);
1249     gst_buffer_unref (inbuf);
1250
1251     GST_ELEMENT_ERROR (self, STREAM, FORMAT, (NULL),
1252         ("codec_data larger than supported by OpenMAX port "
1253             "(%" G_GSIZE_FORMAT " > %u)", gst_buffer_get_size (codec_data),
1254             (guint) self->dec_in_port->port_def.nBufferSize));
1255     return GST_FLOW_ERROR;
1256   }
1257
1258 component_error:
1259   {
1260     gst_buffer_unmap (inbuf, &minfo);
1261     gst_buffer_unref (inbuf);
1262
1263     GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
1264         ("OpenMAX component in error state %s (0x%08x)",
1265             gst_omx_component_get_last_error_string (self->dec),
1266             gst_omx_component_get_last_error (self->dec)));
1267     return GST_FLOW_ERROR;
1268   }
1269
1270 flushing:
1271   {
1272     gst_buffer_unmap (inbuf, &minfo);
1273     gst_buffer_unref (inbuf);
1274
1275     GST_DEBUG_OBJECT (self, "Flushing -- returning FLUSHING");
1276     return GST_FLOW_FLUSHING;
1277   }
1278 reconfigure_error:
1279   {
1280     gst_buffer_unmap (inbuf, &minfo);
1281     gst_buffer_unref (inbuf);
1282
1283     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1284         ("Unable to reconfigure input port"));
1285     return GST_FLOW_ERROR;
1286   }
1287 release_error:
1288   {
1289     gst_buffer_unmap (inbuf, &minfo);
1290     gst_buffer_unref (inbuf);
1291
1292     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1293         ("Failed to relase input buffer to component: %s (0x%08x)",
1294             gst_omx_error_to_string (err), err));
1295
1296     return GST_FLOW_ERROR;
1297   }
1298 }
1299
1300 static GstFlowReturn
1301 gst_omx_audio_dec_drain (GstOMXAudioDec * self)
1302 {
1303   GstOMXAudioDecClass *klass;
1304   GstOMXBuffer *buf;
1305   GstOMXAcquireBufferReturn acq_ret;
1306   OMX_ERRORTYPE err;
1307
1308   GST_DEBUG_OBJECT (self, "Draining component");
1309
1310   klass = GST_OMX_AUDIO_DEC_GET_CLASS (self);
1311
1312   if (!self->started) {
1313     GST_DEBUG_OBJECT (self, "Component not started yet");
1314     return GST_FLOW_OK;
1315   }
1316   self->started = FALSE;
1317
1318   if ((klass->cdata.hacks & GST_OMX_HACK_NO_EMPTY_EOS_BUFFER)) {
1319     GST_WARNING_OBJECT (self, "Component does not support empty EOS buffers");
1320     return GST_FLOW_OK;
1321   }
1322
1323   /* Make sure to release the base class stream lock, otherwise
1324    * _loop() can't call _finish_frame() and we might block forever
1325    * because no input buffers are released */
1326   GST_AUDIO_DECODER_STREAM_UNLOCK (self);
1327
1328   /* Send an EOS buffer to the component and let the base
1329    * class drop the EOS event. We will send it later when
1330    * the EOS buffer arrives on the output port. */
1331   acq_ret = gst_omx_port_acquire_buffer (self->dec_in_port, &buf);
1332   if (acq_ret != GST_OMX_ACQUIRE_BUFFER_OK) {
1333     GST_AUDIO_DECODER_STREAM_LOCK (self);
1334     GST_ERROR_OBJECT (self, "Failed to acquire buffer for draining: %d",
1335         acq_ret);
1336     return GST_FLOW_ERROR;
1337   }
1338
1339   g_mutex_lock (&self->drain_lock);
1340   self->draining = TRUE;
1341   buf->omx_buf->nFilledLen = 0;
1342   buf->omx_buf->nTimeStamp =
1343       gst_util_uint64_scale (self->last_upstream_ts, OMX_TICKS_PER_SECOND,
1344       GST_SECOND);
1345   buf->omx_buf->nTickCount = 0;
1346   buf->omx_buf->nFlags |= OMX_BUFFERFLAG_EOS;
1347   err = gst_omx_port_release_buffer (self->dec_in_port, buf);
1348   if (err != OMX_ErrorNone) {
1349     GST_ERROR_OBJECT (self, "Failed to drain component: %s (0x%08x)",
1350         gst_omx_error_to_string (err), err);
1351     g_mutex_unlock (&self->drain_lock);
1352     GST_AUDIO_DECODER_STREAM_LOCK (self);
1353     return GST_FLOW_ERROR;
1354   }
1355
1356   GST_DEBUG_OBJECT (self, "Waiting until component is drained");
1357
1358   if (G_UNLIKELY (self->dec->hacks & GST_OMX_HACK_DRAIN_MAY_NOT_RETURN)) {
1359     gint64 wait_until = g_get_monotonic_time () + G_TIME_SPAN_SECOND / 2;
1360
1361     if (!g_cond_wait_until (&self->drain_cond, &self->drain_lock, wait_until))
1362       GST_WARNING_OBJECT (self, "Drain timed out");
1363     else
1364       GST_DEBUG_OBJECT (self, "Drained component");
1365
1366   } else {
1367     g_cond_wait (&self->drain_cond, &self->drain_lock);
1368     GST_DEBUG_OBJECT (self, "Drained component");
1369   }
1370
1371   g_mutex_unlock (&self->drain_lock);
1372   GST_AUDIO_DECODER_STREAM_LOCK (self);
1373
1374   gst_adapter_flush (self->output_adapter,
1375       gst_adapter_available (self->output_adapter));
1376   self->started = FALSE;
1377
1378   return GST_FLOW_OK;
1379 }