summaryrefslogtreecommitdiffstats
path: root/camera/OMXCameraAdapter/OMXReprocess.cpp
blob: 6fdbe7bb58b5e3eb522a1497ef1d94a6325f512c (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
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
* @file OMXReprocess.cpp
*
* This file contains functionality for handling reprocessing operations.
*
*/

#include "CameraHal.h"
#include "OMXCameraAdapter.h"
#include "ErrorUtils.h"


namespace Ti {
namespace Camera {

status_t OMXCameraAdapter::setParametersReprocess(const android::CameraParameters &params,
                                                CameraBuffer* buffers,
                                                BaseCameraAdapter::AdapterState state)
{
    status_t ret = NO_ERROR;
    int w, h, s;
    OMX_COLOR_FORMATTYPE pixFormat;
    OMXCameraPortParameters *portData;
    const char* valstr;

    LOG_FUNCTION_NAME;

    if (!buffers) {
        CAMHAL_LOGE("invalid buffer array");
        return BAD_VALUE;
    }

    portData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mVideoInPortIndex];

    w = buffers[0].width;
    h = buffers[0].height;
    s = buffers[0].stride;

    valstr = buffers[0].format;
    if (valstr != NULL) {
        if(strcmp(valstr, android::CameraParameters::PIXEL_FORMAT_YUV420SP) == 0) {
            CAMHAL_LOGDA("YUV420SP format selected");
            pixFormat = OMX_COLOR_FormatYUV420SemiPlanar;
        } else if (strcmp(valstr, android::CameraParameters::PIXEL_FORMAT_BAYER_RGGB) == 0) {
            CAMHAL_LOGDA("RAW Picture format selected");
            pixFormat = OMX_COLOR_FormatRawBayer10bit;
        } else if (strcmp(valstr, android::CameraParameters::PIXEL_FORMAT_YUV422I) == 0) {
            CAMHAL_LOGDA("YUV422i Picture format selected");
            pixFormat = OMX_COLOR_FormatCbYCrY;
        } else {
            CAMHAL_LOGDA("Format not supported, selecting YUV420SP by default");
            pixFormat = OMX_COLOR_FormatYUV420SemiPlanar;
        }
    } else {
        CAMHAL_LOGDA("Format not supported, selecting YUV420SP by default");
        pixFormat = OMX_COLOR_FormatYUV420SemiPlanar;
    }

    if ( (w != (int)portData->mWidth) || (h != (int)portData->mHeight) ||
         (s != (int) portData->mStride) || (pixFormat != portData->mColorFormat)) {
        portData->mWidth = w;
        portData->mHeight = h;

        if ( ( OMX_COLOR_FormatRawBayer10bit == pixFormat ) ||
             ( OMX_COLOR_FormatCbYCrY == pixFormat ) ) {
            portData->mStride = w * 2;
        } else {
            portData->mStride = s;
        }

        portData->mColorFormat = pixFormat;

        mPendingReprocessSettings |= SetFormat;
    }

    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

status_t OMXCameraAdapter::startReprocess()
{
    status_t ret = NO_ERROR;
    OMX_ERRORTYPE eError = OMX_ErrorNone;
    OMXCameraPortParameters * portData = NULL;

    LOG_FUNCTION_NAME;
    CAMHAL_LOGD ("mReprocConfigured = %d", mReprocConfigured);
    if (!mReprocConfigured) {
        return NO_ERROR;
    }

    portData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mVideoInPortIndex];

    CAMHAL_LOGD ("mReprocConfigured = %d", mBurstFramesQueued);
    if (NO_ERROR == ret) {
        android::AutoMutex lock(mBurstLock);

        for ( int index = 0 ; index < portData->mMaxQueueable ; index++ ) {
            CAMHAL_LOGDB("Queuing buffer on video input port - %p, offset: %d, length: %d",
                         portData->mBufferHeader[index]->pBuffer,
                         portData->mBufferHeader[index]->nOffset,
                         portData->mBufferHeader[index]->nFilledLen);
            portData->mStatus[index] = OMXCameraPortParameters::FILL;
            eError = OMX_EmptyThisBuffer(mCameraAdapterParameters.mHandleComp,
                    (OMX_BUFFERHEADERTYPE*)portData->mBufferHeader[index]);
            GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
        }
    }

#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
            CameraHal::PPM("startReprocess buffers queued on video port: ", &mStartCapture);
#endif

    return (ret | Utils::ErrorUtils::omxToAndroidError(eError));

EXIT:
    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
    performCleanupAfterError();
    LOG_FUNCTION_NAME_EXIT;
    return (ret | Utils::ErrorUtils::omxToAndroidError(eError));
}

status_t OMXCameraAdapter::stopReprocess()
{
    LOG_FUNCTION_NAME;

    status_t ret = NO_ERROR;
    OMX_ERRORTYPE eError = OMX_ErrorNone;
    OMXCameraPortParameters *portData = NULL;

    if (!mReprocConfigured) {
        return NO_ERROR;
    }

    portData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mVideoInPortIndex];

    // Disable port - send command and then free all buffers
    ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
                                OMX_EventCmdComplete,
                                OMX_CommandPortDisable,
                                mCameraAdapterParameters.mVideoInPortIndex,
                                mStopReprocSem);
    eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
                                OMX_CommandPortDisable,
                                mCameraAdapterParameters.mVideoInPortIndex,
                                NULL);
    if (portData) {
        CAMHAL_LOGDB("Freeing buffers on reproc port - num: %d", portData->mNumBufs);
        for (int index = 0 ; index < portData->mNumBufs ; index++) {
            CAMHAL_LOGDB("Freeing buffer on reproc port - 0x%x",
                         ( unsigned int ) portData->mBufferHeader[index]->pBuffer);
            eError = OMX_FreeBuffer(mCameraAdapterParameters.mHandleComp,
                                    mCameraAdapterParameters.mVideoInPortIndex,
                                    (OMX_BUFFERHEADERTYPE*)portData->mBufferHeader[index]);
            GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
        }
    }
    CAMHAL_LOGDA("Waiting for port disable");
    ret = mStopReprocSem.WaitTimeout(OMX_CMD_TIMEOUT);
    if (mComponentState == OMX_StateInvalid) {
        CAMHAL_LOGEA("Invalid State after Disable Image Port Exitting!!!");
        goto EXIT;
    }
    if (NO_ERROR == ret) {
        CAMHAL_LOGDA("Port disabled");
    } else {
        ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
                           OMX_EventCmdComplete,
                           OMX_CommandPortDisable,
                           mCameraAdapterParameters.mVideoInPortIndex,
                           NULL);
        CAMHAL_LOGDA("Timeout expired on port disable");
        goto EXIT;
    }

    deinitInternalBuffers(mCameraAdapterParameters.mVideoInPortIndex);

    mReprocConfigured = false;

EXIT:
    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
    LOG_FUNCTION_NAME_EXIT;
    return (ret | Utils::ErrorUtils::omxToAndroidError(eError));
}

status_t OMXCameraAdapter::disableReprocess(){
    status_t ret = NO_ERROR;
    OMX_ERRORTYPE eError = OMX_ErrorNone;

    // no-op..for now

EXIT:
    return (ret | Utils::ErrorUtils::omxToAndroidError(eError));
}

status_t OMXCameraAdapter::UseBuffersReprocess(CameraBuffer *bufArr, int num)
{
    LOG_FUNCTION_NAME;

    status_t ret = NO_ERROR;
    OMX_ERRORTYPE eError = OMX_ErrorNone;
    OMXCameraPortParameters *portData = NULL;

    portData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mVideoInPortIndex];

    if ( 0 != mUseReprocessSem.Count() ) {
        CAMHAL_LOGEB("Error mUseReprocessSem semaphore count %d", mUseReprocessSem.Count());
        return BAD_VALUE;
    }

    CAMHAL_ASSERT(num > 0);

    if (mAdapterState == REPROCESS_STATE) {
        stopReprocess();
    } else if (mAdapterState == CAPTURE_STATE) {
        stopImageCapture();
        stopReprocess();
    }

#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS

    CameraHal::PPM("Reprocess stopping image capture and disabling image port: ", &bufArr->ppmStamp);

#endif

    portData->mNumBufs = num;

    // Configure
    ret = setParametersReprocess(mParams, bufArr, mAdapterState);

    if (mReprocConfigured) {
        if (mPendingReprocessSettings & ECaptureParamSettings) {
            stopReprocess();
        } else {
            // Tap in port has been already configured.
            return NO_ERROR;
        }
    }

    if (mPendingReprocessSettings & SetFormat) {
        mPendingReprocessSettings &= ~SetFormat;
        ret = setFormat(OMX_CAMERA_PORT_VIDEO_IN_VIDEO, *portData);
        if ( ret != NO_ERROR ) {
            CAMHAL_LOGEB("setFormat() failed %d", ret);
            LOG_FUNCTION_NAME_EXIT;
            return ret;
        }
    }

    // Configure DOMX to use either gralloc handles or vptrs
    OMX_TI_PARAMUSENATIVEBUFFER domxUseGrallocHandles;
    OMX_INIT_STRUCT_PTR (&domxUseGrallocHandles, OMX_TI_PARAMUSENATIVEBUFFER);

    domxUseGrallocHandles.nPortIndex = mCameraAdapterParameters.mVideoInPortIndex;
    if (bufArr[0].type == CAMERA_BUFFER_ANW) {
        CAMHAL_LOGD("Using ANW");
        domxUseGrallocHandles.bEnable = OMX_TRUE;

        // Need to allocate tiler reservation and state we are going to be using
        // pagelist buffers. Assuming this happens when buffers if from anw
        initInternalBuffers(mCameraAdapterParameters.mVideoInPortIndex);
    } else {
        CAMHAL_LOGD("Using ION");
        domxUseGrallocHandles.bEnable = OMX_FALSE;
    }
    eError = OMX_SetParameter(mCameraAdapterParameters.mHandleComp,
                            (OMX_INDEXTYPE)OMX_TI_IndexUseNativeBuffers, &domxUseGrallocHandles);
    if (eError!=OMX_ErrorNone) {
        CAMHAL_LOGEB("OMX_SetParameter - %x", eError);
    }
    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);

#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS

    CameraHal::PPM("Reprocess configuration done: ", &bufArr->ppmStamp);

#endif

    // Enable Port
    ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
                           OMX_EventCmdComplete,
                           OMX_CommandPortEnable,
                           mCameraAdapterParameters.mVideoInPortIndex,
                           mUseReprocessSem);
    eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
                             OMX_CommandPortEnable,
                             mCameraAdapterParameters.mVideoInPortIndex,
                             NULL);
    GOTO_EXIT_IF(( eError != OMX_ErrorNone ), eError);

    for (int index = 0 ; index < portData->mNumBufs ; index++)
    {
        OMX_BUFFERHEADERTYPE *pBufferHdr;
        CAMHAL_LOGDB("OMX_UseBuffer Capture address: 0x%x, size = %d",
                     (unsigned int)bufArr[index].opaque,
                     (int)portData->mBufSize);

        eError = OMX_UseBuffer(mCameraAdapterParameters.mHandleComp,
                               &pBufferHdr,
                               mCameraAdapterParameters.mVideoInPortIndex,
                               0,
                               portData->mBufSize,
                               (OMX_U8*)camera_buffer_get_omx_ptr(&bufArr[index]));

        CAMHAL_LOGDB("OMX_UseBuffer = 0x%x", eError);
        GOTO_EXIT_IF(( eError != OMX_ErrorNone ), eError);

        pBufferHdr->pAppPrivate = (OMX_PTR) &bufArr[index];
        bufArr[index].index = index;
        pBufferHdr->nSize = sizeof(OMX_BUFFERHEADERTYPE);
        pBufferHdr->nVersion.s.nVersionMajor = 1 ;
        pBufferHdr->nVersion.s.nVersionMinor = 1 ;
        pBufferHdr->nVersion.s.nRevision = 0;
        pBufferHdr->nVersion.s.nStep =  0;
        pBufferHdr->nOffset = bufArr[index].offset;
        pBufferHdr->nFilledLen = bufArr[index].actual_size;
        portData->mBufferHeader[index] = pBufferHdr;
    }

    // Wait for port enable event
    CAMHAL_LOGDA("Waiting for port enable");
    ret = mUseReprocessSem.WaitTimeout(OMX_CMD_TIMEOUT);

    // Error out if somethiing bad happened while we wait
    if (mComponentState == OMX_StateInvalid) {
        CAMHAL_LOGEA("Invalid State while trying to enable port for reprocessing");
        goto EXIT;
    }

    if (ret == NO_ERROR) {
        CAMHAL_LOGDA("Port enabled");
    } else {
        ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
                           OMX_EventCmdComplete,
                           OMX_CommandPortEnable,
                           mCameraAdapterParameters.mVideoInPortIndex,
                           NULL);
        CAMHAL_LOGDA("Timeout expired on port enable");
        goto EXIT;
    }

    mReprocConfigured = true;

#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS

    CameraHal::PPM("Reprocess video port enabled and buffers registered: ", &bufArr->ppmStamp);

#endif

    return (ret | Utils::ErrorUtils::omxToAndroidError(eError));

EXIT:
    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
    // Release image buffers
    if ( NULL != mReleaseImageBuffersCallback ) {
        mReleaseImageBuffersCallback(mReleaseData);
    }
    performCleanupAfterError();
    LOG_FUNCTION_NAME_EXIT;
    return (ret | Utils::ErrorUtils::omxToAndroidError(eError));

}

} // namespace Camera
} // namespace Ti