summaryrefslogtreecommitdiff
path: root/omx/gstomx_util.c
blob: b1c869ad2950addd1259cf063b35f5378c51a1b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
/*
 * Copyright (C) 2006-2007 Texas Instruments, Incorporated
 * Copyright (C) 2007-2009 Nokia Corporation.
 *
 * Author: Felipe Contreras <felipe.contreras@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "gstomx_util.h"
#include <dlfcn.h>
#include <string.h> /* for memset */

#include "gstomx.h"

GST_DEBUG_CATEGORY (gstomx_util_debug);

/*
 * Forward declarations
 */

static inline void
change_state (GOmxCore *core,
              OMX_STATETYPE state);

static inline void
wait_for_state (GOmxCore *core,
                OMX_STATETYPE state);

static inline void
in_port_cb (GOmxPort *port,
            OMX_BUFFERHEADERTYPE *omx_buffer);

static inline void
out_port_cb (GOmxPort *port,
             OMX_BUFFERHEADERTYPE *omx_buffer);

static inline void
got_buffer (GOmxCore *core,
            GOmxPort *port,
            OMX_BUFFERHEADERTYPE *omx_buffer);

static OMX_ERRORTYPE
EventHandler (OMX_HANDLETYPE omx_handle,
              OMX_PTR app_data,
              OMX_EVENTTYPE event,
              OMX_U32 data_1,
              OMX_U32 data_2,
              OMX_PTR event_data);

static OMX_ERRORTYPE
EmptyBufferDone (OMX_HANDLETYPE omx_handle,
                 OMX_PTR app_data,
                 OMX_BUFFERHEADERTYPE *omx_buffer);

static OMX_ERRORTYPE
FillBufferDone (OMX_HANDLETYPE omx_handle,
                OMX_PTR app_data,
                OMX_BUFFERHEADERTYPE *omx_buffer);

static inline const char *
omx_state_to_str (OMX_STATETYPE omx_state);

static inline const char *
omx_error_to_str (OMX_ERRORTYPE omx_error);

static inline GOmxPort * get_port (GOmxCore *core, guint index);

static inline void
port_free_buffers (GOmxPort *port);

static inline void
port_allocate_buffers (GOmxPort *port);

static inline void
port_start_buffers (GOmxPort *port);

static OMX_CALLBACKTYPE callbacks = { EventHandler, EmptyBufferDone, FillBufferDone };

/* protect implementations hash_table */
static GMutex *imp_mutex;
static GHashTable *implementations;
static gboolean initialized;

/*
 * Util
 */

static void
g_ptr_array_clear (GPtrArray *array)
{
    guint index;
    for (index = 0; index < array->len; index++)
        array->pdata[index] = NULL;
}

static void
g_ptr_array_insert (GPtrArray *array,
                    guint index,
                    gpointer data)
{
    if (index + 1 > array->len)
    {
        g_ptr_array_set_size (array, index + 1);
    }

    array->pdata[index] = data;
}

typedef void (*GOmxPortFunc) (GOmxPort *port);

static inline void
core_for_each_port (GOmxCore *core,
                    GOmxPortFunc func)
{
    guint index;

    for (index = 0; index < core->ports->len; index++)
    {
        GOmxPort *port;

        port = get_port (core, index);

        if (port)
            func (port);
    }
}

/*
 * Main
 */

static GOmxImp *imp_new (const gchar *name);
static void imp_free (GOmxImp *imp);

static GOmxImp *
imp_new (const gchar *name)
{
    GOmxImp *imp;

    imp = g_new0 (GOmxImp, 1);

    /* Load the OpenMAX IL symbols */
    {
        void *handle;

        imp->dl_handle = handle = dlopen (name, RTLD_LAZY);
        if (!handle)
        {
            g_warning ("%s\n", dlerror ());
            g_free (imp);
            return NULL;
        }

        imp->mutex = g_mutex_new ();
        imp->sym_table.init = dlsym (handle, "OMX_Init");
        imp->sym_table.deinit = dlsym (handle, "OMX_Deinit");
        imp->sym_table.get_handle = dlsym (handle, "OMX_GetHandle");
        imp->sym_table.free_handle = dlsym (handle, "OMX_FreeHandle");
    }

    return imp;
}

static void
imp_free (GOmxImp *imp)
{
    if (imp->dl_handle)
    {
        dlclose (imp->dl_handle);
    }
    g_mutex_free (imp->mutex);
    g_free (imp);
}

static inline GOmxImp *
request_imp (const gchar *name)
{
    GOmxImp *imp = NULL;

    g_mutex_lock (imp_mutex);
    imp = g_hash_table_lookup (implementations, name);
    if (!imp)
    {
        imp = imp_new (name);
        if (imp)
            g_hash_table_insert (implementations, g_strdup (name), imp);
    }
    g_mutex_unlock (imp_mutex);

    if (!imp)
        return NULL;

    g_mutex_lock (imp->mutex);
    if (imp->client_count == 0)
    {
        OMX_ERRORTYPE omx_error;
        omx_error = imp->sym_table.init ();
        if (omx_error)
        {
            g_mutex_unlock (imp->mutex);
            return NULL;
        }
    }
    imp->client_count++;
    g_mutex_unlock (imp->mutex);

    return imp;
}

static inline void
release_imp (GOmxImp *imp)
{
    g_mutex_lock (imp->mutex);
    imp->client_count--;
    if (imp->client_count == 0)
    {
        imp->sym_table.deinit ();
    }
    g_mutex_unlock (imp->mutex);
}

void
g_omx_init (void)
{
    if (!initialized)
    {
        /* safe as plugin_init is safe */
        imp_mutex = g_mutex_new ();
        implementations = g_hash_table_new_full (g_str_hash,
                                                 g_str_equal,
                                                 g_free,
                                                 (GDestroyNotify) imp_free);
        initialized = TRUE;
    }
}

void
g_omx_deinit (void)
{
    if (initialized)
    {
        g_hash_table_destroy (implementations);
        g_mutex_free (imp_mutex);
        initialized = FALSE;
    }
}

/*
 * Core
 */

GOmxCore *
g_omx_core_new (void *object)
{
    GOmxCore *core;

    core = g_new0 (GOmxCore, 1);

    core->object = object;
    core->ports = g_ptr_array_new ();

    core->omx_state_condition = g_cond_new ();
    core->omx_state_mutex = g_mutex_new ();

    core->done_sem = g_sem_new ();
    core->flush_sem = g_sem_new ();
    core->port_sem = g_sem_new ();

    core->omx_state = OMX_StateInvalid;

    return core;
}

void
g_omx_core_free (GOmxCore *core)
{
    g_sem_free (core->port_sem);
    g_sem_free (core->flush_sem);
    g_sem_free (core->done_sem);

    g_mutex_free (core->omx_state_mutex);
    g_cond_free (core->omx_state_condition);

    g_ptr_array_free (core->ports, TRUE);

    g_free (core);
}

void
g_omx_core_init (GOmxCore *core)
{
    core->imp = request_imp (core->library_name);

    if (!core->imp)
        return;

    core->omx_error = core->imp->sym_table.get_handle (&core->omx_handle,
                                                       (char *) core->component_name,
                                                       core,
                                                       &callbacks);
    if (!core->omx_error)
        core->omx_state = OMX_StateLoaded;
}

void
g_omx_core_deinit (GOmxCore *core)
{
    if (!core->imp)
        return;

    if (core->omx_state == OMX_StateLoaded ||
        core->omx_state == OMX_StateInvalid)
    {
        if (core->omx_handle)
            core->omx_error = core->imp->sym_table.free_handle (core->omx_handle);
    }

    g_free (core->library_name);
    g_free (core->component_name);

    release_imp (core->imp);
    core->imp = NULL;
}

void
g_omx_core_prepare (GOmxCore *core)
{
    change_state (core, OMX_StateIdle);

    /* Allocate buffers. */
    core_for_each_port (core, port_allocate_buffers);

    wait_for_state (core, OMX_StateIdle);
}

void
g_omx_core_start (GOmxCore *core)
{
    change_state (core, OMX_StateExecuting);
    wait_for_state (core, OMX_StateExecuting);

    if (core->omx_state == OMX_StateExecuting)
        core_for_each_port (core, port_start_buffers);
}

void
g_omx_core_stop (GOmxCore *core)
{
    if (core->omx_state == OMX_StateExecuting ||
        core->omx_state == OMX_StatePause)
    {
        change_state (core, OMX_StateIdle);
        wait_for_state (core, OMX_StateIdle);
    }
}

void
g_omx_core_pause (GOmxCore *core)
{
    change_state (core, OMX_StatePause);
    wait_for_state (core, OMX_StatePause);
}

void
g_omx_core_unload (GOmxCore *core)
{
    if (core->omx_state == OMX_StateIdle ||
        core->omx_state == OMX_StateWaitForResources ||
        core->omx_state == OMX_StateInvalid)
    {
        if (core->omx_state != OMX_StateInvalid)
            change_state (core, OMX_StateLoaded);

        core_for_each_port (core, port_free_buffers);

        if (core->omx_state != OMX_StateInvalid)
            wait_for_state (core, OMX_StateLoaded);
    }

    core_for_each_port (core, g_omx_port_free);
    g_ptr_array_clear (core->ports);
}

static inline GOmxPort *
get_port (GOmxCore *core, guint index)
{
    if (G_LIKELY (index < core->ports->len))
    {
        return g_ptr_array_index (core->ports, index);
    }

    return NULL;
}

GOmxPort *
g_omx_core_new_port (GOmxCore *core,
                     guint index)
{
    GOmxPort *port = get_port (core, index);

    if (port)
    {
        GST_WARNING_OBJECT (core->object, "port %d already exists", index);
        return port;
    }

    port = g_omx_port_new (core, index);
    g_ptr_array_insert (core->ports, index, port);

    return port;
}

void
g_omx_core_set_done (GOmxCore *core)
{
    g_sem_up (core->done_sem);
}

void
g_omx_core_wait_for_done (GOmxCore *core)
{
    g_sem_down (core->done_sem);
}

void
g_omx_core_flush_start (GOmxCore *core)
{
    core_for_each_port (core, g_omx_port_pause);
}

void
g_omx_core_flush_stop (GOmxCore *core)
{
    core_for_each_port (core, g_omx_port_flush);
    core_for_each_port (core, g_omx_port_resume);
}

/*
 * Port
 */

/**
 * note: this is not intended to be called directly by elements (which should
 * instead use g_omx_core_new_port())
 */
GOmxPort *
g_omx_port_new (GOmxCore *core, guint index)
{
    GOmxPort *port;
    port = g_new0 (GOmxPort, 1);

    port->core = core;
    port->port_index = index;
    port->num_buffers = 0;
    port->buffer_size = 0;
    port->buffers = NULL;

    port->enabled = TRUE;
    port->queue = async_queue_new ();
    port->mutex = g_mutex_new ();

    return port;
}

void
g_omx_port_free (GOmxPort *port)
{
    g_mutex_free (port->mutex);
    async_queue_free (port->queue);

    g_free (port->buffers);
    g_free (port);
}

void
g_omx_port_setup (GOmxPort *port)
{
    GOmxPortType type = -1;
    OMX_PARAM_PORTDEFINITIONTYPE param;

    memset (&param, 0, sizeof (param));
    param.nSize = sizeof (OMX_PARAM_PORTDEFINITIONTYPE);
    param.nVersion.s.nVersionMajor = 1;
    param.nVersion.s.nVersionMinor = 1;

    param.nPortIndex = port->port_index;
    OMX_GetParameter (port->core->omx_handle, OMX_IndexParamPortDefinition, &param);

    switch (param.eDir)
    {
        case OMX_DirInput:
            type = GOMX_PORT_INPUT;
            break;
        case OMX_DirOutput:
            type = GOMX_PORT_OUTPUT;
            break;
        default:
            break;
    }

    port->type = type;
    /** @todo should it be nBufferCountMin? */
    port->num_buffers = param.nBufferCountActual;
    port->buffer_size = param.nBufferSize;

    g_free (port->buffers);
    port->buffers = g_new0 (OMX_BUFFERHEADERTYPE *, port->num_buffers);
}

static void
port_allocate_buffers (GOmxPort *port)
{
    guint i;
    gsize size;

    size = port->buffer_size;

    for (i = 0; i < port->num_buffers; i++)
    {
        if (port->omx_allocate)
        {
            OMX_AllocateBuffer (port->core->omx_handle,
                                &port->buffers[i],
                                port->port_index,
                                NULL,
                                size);
        }
        else
        {
            gpointer buffer_data;
            buffer_data = g_malloc (size);
            OMX_UseBuffer (port->core->omx_handle,
                           &port->buffers[i],
                           port->port_index,
                           NULL,
                           size,
                           buffer_data);
        }
    }
}

static void
port_free_buffers (GOmxPort *port)
{
    guint i;

    for (i = 0; i < port->num_buffers; i++)
    {
        OMX_BUFFERHEADERTYPE *omx_buffer;

        omx_buffer = port->buffers[i];

        if (omx_buffer)
        {
#if 0
            /** @todo how shall we free that buffer? */
            if (!port->omx_allocate)
            {
                g_free (omx_buffer->pBuffer);
                omx_buffer->pBuffer = NULL;
            }
#endif

            OMX_FreeBuffer (port->core->omx_handle, port->port_index, omx_buffer);
            port->buffers[i] = NULL;
        }
    }
}

static void
port_start_buffers (GOmxPort *port)
{
    guint i;

    for (i = 0; i < port->num_buffers; i++)
    {
        OMX_BUFFERHEADERTYPE *omx_buffer;

        omx_buffer = port->buffers[i];

        /* If it's an input port we will need to fill the buffer, so put it in
         * the queue, otherwise send to omx for processing (fill it up). */
        if (port->type == GOMX_PORT_INPUT)
            got_buffer (port->core, port, omx_buffer);
        else
            g_omx_port_release_buffer (port, omx_buffer);
    }
}

void
g_omx_port_push_buffer (GOmxPort *port,
                        OMX_BUFFERHEADERTYPE *omx_buffer)
{
    async_queue_push (port->queue, omx_buffer);
}

OMX_BUFFERHEADERTYPE *
g_omx_port_request_buffer (GOmxPort *port)
{
    return async_queue_pop (port->queue);
}

void
g_omx_port_release_buffer (GOmxPort *port,
                           OMX_BUFFERHEADERTYPE *omx_buffer)
{
    switch (port->type)
    {
        case GOMX_PORT_INPUT:
            OMX_EmptyThisBuffer (port->core->omx_handle, omx_buffer);
            break;
        case GOMX_PORT_OUTPUT:
            OMX_FillThisBuffer (port->core->omx_handle, omx_buffer);
            break;
        default:
            break;
    }
}

void
g_omx_port_resume (GOmxPort *port)
{
    async_queue_enable (port->queue);
}

void
g_omx_port_pause (GOmxPort *port)
{
    async_queue_disable (port->queue);
}

void
g_omx_port_flush (GOmxPort *port)
{
    if (port->type == GOMX_PORT_OUTPUT)
    {
        OMX_BUFFERHEADERTYPE *omx_buffer;
        while ((omx_buffer = async_queue_pop_forced (port->queue)))
        {
            omx_buffer->nFilledLen = 0;
            g_omx_port_release_buffer (port, omx_buffer);
        }
    }
    else
    {
        OMX_SendCommand (port->core->omx_handle, OMX_CommandFlush, port->port_index, NULL);
        g_sem_down (port->core->flush_sem);
    }
}

void
g_omx_port_enable (GOmxPort *port)
{
    GOmxCore *core;

    core = port->core;

    OMX_SendCommand (core->omx_handle, OMX_CommandPortEnable, port->port_index, NULL);
    port_allocate_buffers (port);
    if (core->omx_state != OMX_StateLoaded)
        port_start_buffers (port);
    g_omx_port_resume (port);

    g_sem_down (core->port_sem);
}

void
g_omx_port_disable (GOmxPort *port)
{
    GOmxCore *core;

    core = port->core;

    OMX_SendCommand (core->omx_handle, OMX_CommandPortDisable, port->port_index, NULL);
    g_omx_port_pause (port);
    g_omx_port_flush (port);
    port_free_buffers (port);

    g_sem_down (core->port_sem);
}

void
g_omx_port_finish (GOmxPort *port)
{
    port->enabled = FALSE;
    async_queue_disable (port->queue);
}

/*
 * Helper functions.
 */

static inline void
change_state (GOmxCore *core,
              OMX_STATETYPE state)
{
    GST_DEBUG_OBJECT (core->object, "state=%d", state);
    OMX_SendCommand (core->omx_handle, OMX_CommandStateSet, state, NULL);
}

static inline void
complete_change_state (GOmxCore *core,
                       OMX_STATETYPE state)
{
    g_mutex_lock (core->omx_state_mutex);

    core->omx_state = state;
    g_cond_signal (core->omx_state_condition);
    GST_DEBUG_OBJECT (core->object, "state=%d", state);

    g_mutex_unlock (core->omx_state_mutex);
}

static inline void
wait_for_state (GOmxCore *core,
                OMX_STATETYPE state)
{
    GTimeVal tv;
    gboolean signaled;

    g_mutex_lock (core->omx_state_mutex);

    if (core->omx_error != OMX_ErrorNone)
        goto leave;

    g_get_current_time (&tv);
    g_time_val_add (&tv, 15 * G_USEC_PER_SEC);

    /* try once */
    if (core->omx_state != state)
    {
        signaled = g_cond_timed_wait (core->omx_state_condition, core->omx_state_mutex, &tv);

        if (!signaled)
        {
            GST_ERROR_OBJECT (core->object, "timed out switching from '%s' to '%s'",
                              omx_state_to_str(core->omx_state), omx_state_to_str(state));
        }
    }

    if (core->omx_error != OMX_ErrorNone)
        goto leave;

    if (core->omx_state != state)
    {
        GST_ERROR_OBJECT (core->object, "wrong state received: state=%d, expected=%d",
                          core->omx_state, state);
    }

leave:
    g_mutex_unlock (core->omx_state_mutex);
}

/*
 * Callbacks
 */

static inline void
in_port_cb (GOmxPort *port,
            OMX_BUFFERHEADERTYPE *omx_buffer)
{
    /** @todo remove this */

    if (!port->enabled)
        return;
}

static inline void
out_port_cb (GOmxPort *port,
             OMX_BUFFERHEADERTYPE *omx_buffer)
{
    /** @todo remove this */

    if (!port->enabled)
        return;

#if 0
    if (omx_buffer->nFlags & OMX_BUFFERFLAG_EOS)
    {
        g_omx_port_set_done (port);
        return;
    }
#endif
}

static inline void
got_buffer (GOmxCore *core,
            GOmxPort *port,
            OMX_BUFFERHEADERTYPE *omx_buffer)
{
    if (G_UNLIKELY (!omx_buffer))
    {
        return;
    }

    if (G_LIKELY (port))
    {
        g_omx_port_push_buffer (port, omx_buffer);

        switch (port->type)
        {
            case GOMX_PORT_INPUT:
                in_port_cb (port, omx_buffer);
                break;
            case GOMX_PORT_OUTPUT:
                out_port_cb (port, omx_buffer);
                break;
            default:
                break;
        }
    }
}

/*
 * OpenMAX IL callbacks.
 */

static OMX_ERRORTYPE
EventHandler (OMX_HANDLETYPE omx_handle,
              OMX_PTR app_data,
              OMX_EVENTTYPE event,
              OMX_U32 data_1,
              OMX_U32 data_2,
              OMX_PTR event_data)
{
    GOmxCore *core;

    core = (GOmxCore *) app_data;

    switch (event)
    {
        case OMX_EventCmdComplete:
            {
                OMX_COMMANDTYPE cmd;

                cmd = (OMX_COMMANDTYPE) data_1;

                switch (cmd)
                {
                    case OMX_CommandStateSet:
                        complete_change_state (core, data_2);
                        break;
                    case OMX_CommandFlush:
                        g_sem_up (core->flush_sem);
                        break;
                    case OMX_CommandPortDisable:
                    case OMX_CommandPortEnable:
                        g_sem_up (core->port_sem);
                    default:
                        break;
                }
                break;
            }
        case OMX_EventBufferFlag:
            {
                if (data_2 & OMX_BUFFERFLAG_EOS)
                {
                    g_omx_core_set_done (core);
                }
                break;
            }
        case OMX_EventPortSettingsChanged:
            {
                /** @todo only on the relevant port. */
                if (core->settings_changed_cb)
                {
                    core->settings_changed_cb (core);
                }
                break;
            }
        case OMX_EventError:
            {
                core->omx_error = data_1;
                GST_ERROR_OBJECT (core->object, "unrecoverable error: %s (0x%lx)",
                                  omx_error_to_str (data_1), data_1);
                /* component might leave us waiting for buffers, unblock */
                g_omx_core_flush_start (core);
                /* unlock wait_for_state */
                g_mutex_lock (core->omx_state_mutex);
                g_cond_signal (core->omx_state_condition);
                g_mutex_unlock (core->omx_state_mutex);
                break;
            }
        default:
            break;
    }

    return OMX_ErrorNone;
}

static OMX_ERRORTYPE
EmptyBufferDone (OMX_HANDLETYPE omx_handle,
                 OMX_PTR app_data,
                 OMX_BUFFERHEADERTYPE *omx_buffer)
{
    GOmxCore *core;
    GOmxPort *port;

    core = (GOmxCore*) app_data;
    port = get_port (core, omx_buffer->nInputPortIndex);

    GST_CAT_LOG_OBJECT (gstomx_util_debug, core->object, "omx_buffer=%p", omx_buffer);
    got_buffer (core, port, omx_buffer);

    return OMX_ErrorNone;
}

static OMX_ERRORTYPE
FillBufferDone (OMX_HANDLETYPE omx_handle,
                OMX_PTR app_data,
                OMX_BUFFERHEADERTYPE *omx_buffer)
{
    GOmxCore *core;
    GOmxPort *port;

    core = (GOmxCore *) app_data;
    port = get_port (core, omx_buffer->nOutputPortIndex);

    GST_CAT_LOG_OBJECT (gstomx_util_debug, core->object, "omx_buffer=%p", omx_buffer);
    got_buffer (core, port, omx_buffer);

    return OMX_ErrorNone;
}

static inline const char *
omx_state_to_str (OMX_STATETYPE omx_state)
{
    switch (omx_state)
    {
        case OMX_StateInvalid:
            return "invalid";
        case OMX_StateLoaded:
            return "loaded";
        case OMX_StateIdle:
            return "idle";
        case OMX_StateExecuting:
            return "executing";
        case OMX_StatePause:
            return "pause";
        case OMX_StateWaitForResources:
            return "wait for resources";
        default:
            return "unknown";
    }
}

static inline const char *
omx_error_to_str (OMX_ERRORTYPE omx_error)
{
    switch (omx_error)
    {
        case OMX_ErrorNone:
            return "None";

        case OMX_ErrorInsufficientResources:
            return "There were insufficient resources to perform the requested operation";

        case OMX_ErrorUndefined:
            return "The cause of the error could not be determined";

        case OMX_ErrorInvalidComponentName:
            return "The component name string was not valid";

        case OMX_ErrorComponentNotFound:
            return "No component with the specified name string was found";

        case OMX_ErrorInvalidComponent:
            return "The component specified did not have an entry point";

        case OMX_ErrorBadParameter:
            return "One or more parameters were not valid";

        case OMX_ErrorNotImplemented:
            return "The requested function is not implemented";

        case OMX_ErrorUnderflow:
            return "The buffer was emptied before the next buffer was ready";

        case OMX_ErrorOverflow:
            return "The buffer was not available when it was needed";

        case OMX_ErrorHardware:
            return "The hardware failed to respond as expected";

        case OMX_ErrorInvalidState:
            return "The component is in invalid state";

        case OMX_ErrorStreamCorrupt:
            return "Stream is found to be corrupt";

        case OMX_ErrorPortsNotCompatible:
            return "Ports being connected are not compatible";

        case OMX_ErrorResourcesLost:
            return "Resources allocated to an idle component have been lost";

        case OMX_ErrorNoMore:
            return "No more indices can be enumerated";

        case OMX_ErrorVersionMismatch:
            return "The component detected a version mismatch";

        case OMX_ErrorNotReady:
            return "The component is not ready to return data at this time";

        case OMX_ErrorTimeout:
            return "There was a timeout that occurred";

        case OMX_ErrorSameState:
            return "This error occurs when trying to transition into the state you are already in";

        case OMX_ErrorResourcesPreempted:
            return "Resources allocated to an executing or paused component have been preempted";

        case OMX_ErrorPortUnresponsiveDuringAllocation:
            return "Waited an unusually long time for the supplier to allocate buffers";

        case OMX_ErrorPortUnresponsiveDuringDeallocation:
            return "Waited an unusually long time for the supplier to de-allocate buffers";

        case OMX_ErrorPortUnresponsiveDuringStop:
            return "Waited an unusually long time for the non-supplier to return a buffer during stop";

        case OMX_ErrorIncorrectStateTransition:
            return "Attempting a state transition that is not allowed";

        case OMX_ErrorIncorrectStateOperation:
            return "Attempting a command that is not allowed during the present state";

        case OMX_ErrorUnsupportedSetting:
            return "The values encapsulated in the parameter or config structure are not supported";

        case OMX_ErrorUnsupportedIndex:
            return "The parameter or config indicated by the given index is not supported";

        case OMX_ErrorBadPortIndex:
            return "The port index supplied is incorrect";

        case OMX_ErrorPortUnpopulated:
            return "The port has lost one or more of its buffers and it thus unpopulated";

        case OMX_ErrorComponentSuspended:
            return "Component suspended due to temporary loss of resources";

        case OMX_ErrorDynamicResourcesUnavailable:
            return "Component suspended due to an inability to acquire dynamic resources";

        case OMX_ErrorMbErrorsInFrame:
            return "Frame generated macroblock error";

        case OMX_ErrorFormatNotDetected:
            return "Cannot parse or determine the format of an input stream";

        case OMX_ErrorContentPipeOpenFailed:
            return "The content open operation failed";

        case OMX_ErrorContentPipeCreationFailed:
            return "The content creation operation failed";

        case OMX_ErrorSeperateTablesUsed:
            return "Separate table information is being used";

        case OMX_ErrorTunnelingUnsupported:
            return "Tunneling is unsupported by the component";

        default:
            return "Unknown error";
    }
}