summaryrefslogtreecommitdiff
path: root/omx/video/src/openmax_il/video_encode/src/OMX_VideoEnc_Thread.c
blob: d2689c02c9b21e42416405cb6486cb7734a52f03 (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

/*
 * Copyright (C) Texas Instruments - http://www.ti.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; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 *
 * 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
 */
/* =============================================================================
*             Texas Instruments OMAP(TM) Platform Software
*  (c) Copyright Texas Instruments, Incorporated.  All Rights Reserved.
*
*  Use of this software is controlled by the terms and conditions found
*  in the license agreement under which this software has been supplied.
* =========================================================================== */
/**
* @file OMX_VideoEnc_Thread.c
*
* This file implements OMX Component for MPEG-4 encoder that
* is fully compliant with the OMX specification 1.5.
*
* @path  $(CSLPATH)\src
*
* @rev  0.1
*/
/* -------------------------------------------------------------------------- */
/* =============================================================================
*!
*! Revision History
*! ================================================================
*!
*! 02-Feb-2006 mf: Revisions appear in reverse chronological order;
*! that is, newest first.  The date format is dd-Mon-yyyy.
* =========================================================================== */


/* ------compilation control switches ----------------------------------------*/
/******************************************************************************
*  INCLUDE FILES
*******************************************************************************/
/* ----- system and platform files -------------------------------------------*/
#ifdef UNDER_CE
    #include <windows.h>
    #include <oaf_osal.h>
    #include <omx_core.h>
#else
    #define _XOPEN_SOURCE 600
    #include <wchar.h>
    #include <unistd.h>
    #include <sys/select.h>
    #include <errno.h>
    #include <fcntl.h>
#endif

#include <dbapi.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>

/*------- Program Header Files -----------------------------------------------*/
#include "OMX_VideoEnc_Utils.h"

/******************************************************************************
*  EXTERNAL REFERENCES NOTE : only use if not found in header file
*******************************************************************************/
/*--------data declarations --------------------------------------------------*/

/*--------function prototypes ------------------------------------------------*/

/******************************************************************************
*  PUBLIC DECLARATIONS Defined here, used elsewhere
*******************************************************************************/
/*--------data declarations --------------------------------------------------*/

/*--------function prototypes ------------------------------------------------*/

/******************************************************************************
*  PRIVATE DECLARATIONS Defined here, used only here
*******************************************************************************/
/*--------data declarations --------------------------------------------------*/

/*--------macro definitions --------------------------------------------------*/

/*--------function prototypes ------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/**
  * OMX_VideoEnc_Thread()
  *
  * Called by VIDENC_Start_ComponentThread function.
  *
  * @param pThreadData
  *
  * @retval OMX_ErrorNone                  success, ready to roll
  *         OMX_ErrorInsufficientResources if the malloc fails
  **/
/*----------------------------------------------------------------------------*/
void* OMX_VIDENC_Thread (void* pThreadData)
{
    int status = -1;
    int fdmax = -1;
    fd_set rfds;
    OMX_ERRORTYPE eError = OMX_ErrorNone;
    OMX_COMMANDTYPE eCmd = -1;
    OMX_U32 nParam1;
    int nRet = -1;
    OMX_PTR pCmdData = NULL;
    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
    LCML_DSP_INTERFACE* pLcmlHandle = NULL;
    sigset_t set;

    if (!pThreadData)
    {
        eError = OMX_ErrorBadParameter;
        goto OMX_CONF_CMD_BAIL;
    }

    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)pThreadData;
    pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;

#ifdef __PERF_INSTRUMENTATION__
    pComponentPrivate->pPERFcomp = PERF_Create(PERF_FOURCC('V', 'E', ' ', 'T'),
                                               PERF_ModuleComponent |
                                               PERF_ModuleVideoEncode);
#endif

    /** Looking for highest number of file descriptor
        for pipes inorder to put in select loop */

    fdmax = pComponentPrivate->nCmdPipe[0];

    if (pComponentPrivate->nFree_oPipe[0] > fdmax)
    {
        fdmax = pComponentPrivate->nFree_oPipe[0];
    }

    if (pComponentPrivate->nFilled_iPipe[0] > fdmax)
    {
        fdmax = pComponentPrivate->nFilled_iPipe[0];
    }

    while (1)
    {
        FD_ZERO (&rfds);
        FD_SET (pComponentPrivate->nCmdPipe[0], &rfds);
        FD_SET (pComponentPrivate->nFree_oPipe[0], &rfds);
        FD_SET (pComponentPrivate->nFilled_iPipe[0], &rfds);

        sigemptyset(&set);
        sigaddset(&set,SIGALRM);
        status = pselect(fdmax+1, &rfds, NULL, NULL, NULL,&set);

        if (0 == status)
        {
            OMX_TRACE2(pComponentPrivate->dbg, "pselect() = 0\n");
#ifndef UNDER_CE
            sched_yield();
#else
            sched_yield();
#endif
        }
        else if (-1 == status)
        {
            if (pComponentPrivate->eState != OMX_StateLoaded)
            {
                OMX_TRACE3(pComponentPrivate->dbg, "select() error.\n");
                OMX_VIDENC_EVENT_HANDLER(pComponentPrivate, OMX_EventError, OMX_ErrorHardware, 0, NULL);
            }
            /*OMX_VIDENC_SET_ERROR_BAIL(eError, OMX_ErrorHardware, pComponentPrivate);*/
            eError = OMX_ErrorHardware;
            OMX_ERROR5(pComponentPrivate->dbg, "*Fatal Error : %x\n", eError);
            OMX_VIDENC_HandleError(pComponentPrivate, eError);
        }
        else
        {
            if (FD_ISSET(pComponentPrivate->nCmdPipe[0], &rfds))
            {
                nRet = read(pComponentPrivate->nCmdPipe[0],
                            &eCmd,
                            sizeof(eCmd));
                if (nRet == -1)
                {
                    OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from cmdPipe\n");
                    OMX_VIDENC_SET_ERROR_BAIL(eError,
                                              OMX_ErrorHardware,
                                              pComponentPrivate);
                }

#ifdef __PERF_INSTRUMENTATION__
                        PERF_ReceivedCommand(pComponentPrivate->pPERFcomp,
                                             eCmd, 0, PERF_ModuleLLMM);
#endif
                if (eCmd == (OMX_COMMANDTYPE)-1)
                {
                    OMX_PRCOMM2(pComponentPrivate->dbg, "Received thread close command.\n");
                    OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorNone);
                }

                if (eCmd == OMX_CommandMarkBuffer)
                {
                    nRet = read(pComponentPrivate->nCmdDataPipe[0],
                                &pCmdData,
                                sizeof(pCmdData));
                    if (nRet == -1)
                    {
                        OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from cmdDataPipe\n");
                        OMX_VIDENC_SET_ERROR_BAIL(eError,
                                                  OMX_ErrorHardware,
                                                  pComponentPrivate);
                    }
                }
                else
                {
                    nRet = read(pComponentPrivate->nCmdDataPipe[0],
                                &nParam1,
                                sizeof(nParam1));
                    if (nRet == -1)
                    {
                        OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from cmdDataPipe\n");
                        OMX_VIDENC_SET_ERROR_BAIL(eError,
                                                  OMX_ErrorHardware,
                                                  pComponentPrivate);
                    }
                }

#ifdef __PERF_INSTRUMENTATION__
                    PERF_ReceivedCommand(pComponentPrivate->pPERFcomp,
                                         eCmd,
                                         (eCmd == OMX_CommandMarkBuffer) ? ((OMX_U32) pCmdData) : nParam1,
                                         PERF_ModuleLLMM);
#endif

                switch (eCmd)
                {
                    case OMX_CommandStateSet :
                    OMX_PRSTATE2(pComponentPrivate->dbg, "Enters OMX_VIDENC_HandleCommandStateSet\n");
                        eError = OMX_VIDENC_HandleCommandStateSet(pComponentPrivate,
                                                                  nParam1);
                        OMX_VIDENC_BAIL_IF_ERROR(eError, pComponentPrivate);
                    OMX_PRSTATE2(pComponentPrivate->dbg, "Exits OMX_VIDENC_HandleCommandStateSet\n");
                        break;
                    case OMX_CommandFlush :
                    OMX_PRSTATE2(pComponentPrivate->dbg, "Enters OMX_VIDENC_HandleCommandFlush\n");
                        eError = OMX_VIDENC_HandleCommandFlush(pComponentPrivate,
                                                               nParam1,
                                                               OMX_FALSE);
                        OMX_VIDENC_BAIL_IF_ERROR(eError, pComponentPrivate);
                    OMX_PRSTATE2(pComponentPrivate->dbg, "Exits OMX_VIDENC_HandleCommandFlush\n");
                        break;
                    case OMX_CommandPortDisable :
                    OMX_PRSTATE2(pComponentPrivate->dbg, "Enters OMX_VIDENC_HandleCommandDisablePort\n");
                        eError = OMX_VIDENC_HandleCommandDisablePort(pComponentPrivate,
                                                                     nParam1);
                        OMX_VIDENC_BAIL_IF_ERROR(eError, pComponentPrivate);
                    OMX_PRSTATE2(pComponentPrivate->dbg, "Exits OMX_VIDENC_HandleCommandDisablePort\n");
                        break;
                    case OMX_CommandPortEnable :
                    OMX_PRSTATE2(pComponentPrivate->dbg, "Enters OMX_VIDENC_HandleCommandDisablePort\n");
                        eError = OMX_VIDENC_HandleCommandEnablePort(pComponentPrivate,
                                                                    nParam1);
                        OMX_VIDENC_BAIL_IF_ERROR(eError, pComponentPrivate);
                    OMX_PRSTATE2(pComponentPrivate->dbg, "Exits OMX_VIDENC_HandleCommandDisablePort\n");
                        break;
                    case OMX_CommandMarkBuffer :
                        if (!pComponentPrivate->pMarkBuf)
                        {
                            pComponentPrivate->pMarkBuf = (OMX_MARKTYPE*)(pCmdData);
                        }
                        break;
                    default:
                        OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
                                                 OMX_EventError,
                                                 OMX_ErrorUndefined,
                                                 0,
                                                 NULL);
                }
            }

            if ((FD_ISSET(pComponentPrivate->nFilled_iPipe[0], &rfds)) &&
                (pComponentPrivate->eState != OMX_StatePause &&
                pComponentPrivate->eState != OMX_StateIdle))
            {
                OMX_PRBUFFER1(pComponentPrivate->dbg, "Enters OMX_VIDENC_Process_FilledInBuf\n");
                eError = OMX_VIDENC_Process_FilledInBuf(pComponentPrivate);
                if (eError != OMX_ErrorNone)
                {
                    OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
                                             OMX_EventError,
                                             OMX_ErrorUndefined,
                                             0,
                                             NULL);
                    OMX_VIDENC_BAIL_IF_ERROR(eError, pComponentPrivate);
                }
                OMX_PRBUFFER1(pComponentPrivate->dbg, "Exits OMX_VIDENC_Process_FilledInBuf\n");
            }

            if (FD_ISSET(pComponentPrivate->nFree_oPipe[0], &rfds) &&
                (pComponentPrivate->eState!= OMX_StatePause &&
                pComponentPrivate->eState != OMX_StateIdle &&
                pComponentPrivate->eState != OMX_StateLoaded))
            {
                OMX_PRBUFFER1(pComponentPrivate->dbg, "Enters OMX_VIDENC_Process_FreeOutBuf\n");
                eError = OMX_VIDENC_Process_FreeOutBuf(pComponentPrivate);
                if (eError != OMX_ErrorNone)
                {
                    OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
                                             OMX_EventError,
                                             OMX_ErrorUndefined,
                                             0,
                                             NULL);
                    OMX_VIDENC_BAIL_IF_ERROR(eError, pComponentPrivate);
                }
                OMX_PRBUFFER1(pComponentPrivate->dbg, "Exits OMX_VIDENC_Process_FreeOutBuf\n");
            }
        }
    }

OMX_CONF_CMD_BAIL:

#ifdef __PERF_INSTRUMENTATION__
    if (pComponentPrivate)
        PERF_Done(pComponentPrivate->pPERFcomp);
#endif
    if (pComponentPrivate)
        OMX_PRINT2(pComponentPrivate->dbg, "Component Thread Exits\n");
    return (void*)eError;
}