1/*
2 * Copyright (C) 2012 Intel Corporation.  All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18#ifndef __ISV_PROCESSOR_H
19#define __ISV_PROCESSOR_H
20
21#include <media/stagefright/MetaData.h>
22#include "isv_worker.h"
23
24#include <utils/Mutex.h>
25#include <utils/threads.h>
26#include <utils/Errors.h>
27#include "isv_bufmanager.h"
28#define ISV_COMPONENT_LOCK_DEBUG 0
29#define ISV_THREAD_DEBUG 0
30
31using namespace android;
32
33typedef enum {
34    kPortIndexInput  = 0,
35    kPortIndexOutput = 1
36}PORT_INDEX;
37
38class ISVBufferManager;
39class ISVProcessorObserver: public RefBase
40{
41public:
42    virtual OMX_ERRORTYPE releaseBuffer(PORT_INDEX index, OMX_BUFFERHEADERTYPE* pBuffer, bool bFlush) = 0;
43    virtual OMX_ERRORTYPE reportOutputCrop() = 0;
44};
45
46class ISVProcessor : public Thread
47{
48public:
49    ISVProcessor(bool canCallJava, sp<ISVBufferManager> bufferManager, sp<ISVProcessorObserver> observer, uint32_t width, uint32_t height);
50    virtual ~ISVProcessor();
51
52    virtual status_t readyToRun();
53
54    // Derived class must implement threadLoop(). The thread starts its life
55    // here. There are two ways of using the Thread object:
56                                                // 1) loop: if threadLoop() returns true, it will be called again if
57                                                //          requestExit() wasn't called.
58                                                // 2) once: if threadLoop() returns false, the thread will exit upon return.
59    virtual bool threadLoop();
60    bool isCurrentThread() const;
61
62    void start();
63    void stop();
64    bool isReadytoRun();
65
66    //configure FRC factor
67    status_t configFRC(uint32_t fps);
68    //add output buffer into mOutputBuffers
69    void addOutput(OMX_BUFFERHEADERTYPE* output);
70    //add intput buffer into mInputBuffers
71    void addInput(OMX_BUFFERHEADERTYPE* input);
72    //notify flush and wait flush finish
73    void notifyFlush();
74    void waitFlushFinished();
75
76private:
77    bool getBufForFirmwareOutput(Vector<ISVBuffer*> *fillBufList,
78            uint32_t *fillBufNum);
79    status_t updateFirmwareOutputBufStatus(uint32_t fillBufNum);
80    bool getBufForFirmwareInput(Vector<ISVBuffer*> *procBufList,
81            ISVBuffer **inputBuf,
82            uint32_t *procBufNum );
83    status_t updateFirmwareInputBufStatus(uint32_t procBufNum);
84    //flush input&ouput buffer queue
85    void flush();
86    //return whether this fps is valid
87    inline bool isFrameRateValid(uint32_t fps);
88    //auto calculate fps if the framework doesn't set the correct fps
89    status_t calculateFps(int64_t timeStamp, uint32_t* fps);
90    //config vpp filters
91    status_t configFilters(OMX_BUFFERHEADERTYPE* buffer);
92
93private:
94    sp<ISVProcessorObserver> mpOwner;
95    android_thread_id_t mThreadId;
96    bool mThreadRunning;
97
98    sp<ISVWorker> mISVWorker;
99    sp<ISVProfile> mISVProfile;
100    // buffer manager
101    sp<ISVBufferManager> mBufferManager;
102
103    Vector<OMX_BUFFERHEADERTYPE*> mOutputBuffers;
104    Mutex mOutputLock; // to protect access to mOutputBuffers
105    uint32_t mOutputProcIdx;
106
107    Vector<OMX_BUFFERHEADERTYPE*> mInputBuffers;
108    Mutex mInputLock; // to protect access to mFillBuffers
109    uint32_t mInputProcIdx;
110
111    const static uint32_t WINDOW_SIZE = 4;  // must >= 2
112    Vector<int64_t>mTimeWindow;
113    // conditon for thread running
114    Mutex mLock;
115    Condition mRunCond;
116
117    // condition for seek finish
118    Mutex mEndLock;
119    Condition mEndCond;
120
121    uint32_t mNumTaskInProcesing;
122    uint32_t mNumRetry;
123    bool mError;
124    bool mbFlush;
125    bool mbBypass;
126    bool mFlagEnd;
127    bool mFrcOn;
128
129    // ISV filter configuration
130    uint32_t mFilters;
131    FilterParam mFilterParam;
132};
133
134#endif /* __ISV_THREAD_H*/
135