1 /*
2 * Copyright (c) 2009-2011 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 #ifndef __VIDEO_ENCODER_BASE_H__
18 #define __VIDEO_ENCODER_BASE_H__
19 
20 #include <va/va.h>
21 #include <va/va_tpi.h>
22 #include "VideoEncoderDef.h"
23 #include "VideoEncoderInterface.h"
24 #include "IntelMetadataBuffer.h"
25 #include <utils/List.h>
26 #include <utils/threads.h>
27 #include "VideoEncoderUtils.h"
28 
29 struct SurfaceMap {
30     VASurfaceID surface;
31     VASurfaceID surface_backup;
32     IntelMetadataBufferType type;
33     int32_t value;
34     ValueInfo vinfo;
35     bool added;
36 };
37 
38 struct EncodeTask {
39     VASurfaceID enc_surface;
40     VASurfaceID ref_surface;
41     VASurfaceID rec_surface;
42     VABufferID coded_buffer;
43 
44     FrameType type;
45     int flag;
46     int64_t timestamp;  //corresponding input frame timestamp
47     void *priv;  //input buffer data
48 
49     bool completed;   //if encode task is done complet by HW
50 };
51 
52 class VideoEncoderBase : IVideoEncoder {
53 
54 public:
55     VideoEncoderBase();
56     virtual ~VideoEncoderBase();
57 
58     virtual Encode_Status start(void);
59     virtual void flush(void);
60     virtual Encode_Status stop(void);
61     virtual Encode_Status encode(VideoEncRawBuffer *inBuffer, uint32_t timeout);
62 
63     /*
64     * getOutput can be called several time for a frame (such as first time  codec data, and second time others)
65     * encoder will provide encoded data according to the format (whole frame, codec_data, sigle NAL etc)
66     * If the buffer passed to encoded is not big enough, this API call will return ENCODE_BUFFER_TOO_SMALL
67     * and caller should provide a big enough buffer and call again
68     */
69     virtual Encode_Status getOutput(VideoEncOutputBuffer *outBuffer, uint32_t timeout);
70 
71     virtual Encode_Status getParameters(VideoParamConfigSet *videoEncParams);
72     virtual Encode_Status setParameters(VideoParamConfigSet *videoEncParams);
73     virtual Encode_Status setConfig(VideoParamConfigSet *videoEncConfig);
74     virtual Encode_Status getConfig(VideoParamConfigSet *videoEncConfig);
75     virtual Encode_Status getMaxOutSize(uint32_t *maxSize);
76 
77 protected:
78     virtual Encode_Status sendEncodeCommand(EncodeTask* task) = 0;
79     virtual Encode_Status derivedSetParams(VideoParamConfigSet *videoEncParams) = 0;
80     virtual Encode_Status derivedGetParams(VideoParamConfigSet *videoEncParams) = 0;
81     virtual Encode_Status derivedGetConfig(VideoParamConfigSet *videoEncConfig) = 0;
82     virtual Encode_Status derivedSetConfig(VideoParamConfigSet *videoEncConfig) = 0;
83     virtual Encode_Status getExtFormatOutput(VideoEncOutputBuffer *outBuffer) = 0;
84     virtual Encode_Status updateFrameInfo(EncodeTask* task) ;
85 
86     Encode_Status renderDynamicFrameRate();
87     Encode_Status renderDynamicBitrate(EncodeTask* task);
88     Encode_Status renderHrd();
89     Encode_Status queryProfileLevelConfig(VADisplay dpy, VAProfile profile);
90 
91 private:
92     void setDefaultParams(void);
93     Encode_Status setUpstreamBuffer(VideoParamsUpstreamBuffer *upStreamBuffer);
94     Encode_Status getNewUsrptrFromSurface(uint32_t width, uint32_t height, uint32_t format,
95             uint32_t expectedSize, uint32_t *outsize, uint32_t *stride, uint8_t **usrptr);
96     VASurfaceMap* findSurfaceMapByValue(intptr_t value);
97     Encode_Status manageSrcSurface(VideoEncRawBuffer *inBuffer, VASurfaceID *sid);
98     void PrepareFrameInfo(EncodeTask* task);
99 
100     Encode_Status prepareForOutput(VideoEncOutputBuffer *outBuffer, bool *useLocalBuffer);
101     Encode_Status cleanupForOutput();
102     Encode_Status outputAllData(VideoEncOutputBuffer *outBuffer);
103     Encode_Status queryAutoReferenceConfig(VAProfile profile);
104     Encode_Status querySupportedSurfaceMemTypes();
105     Encode_Status copySurfaces(VASurfaceID srcId, VASurfaceID destId);
106     VASurfaceID CreateSurfaceFromExternalBuf(int32_t value, ValueInfo& vinfo);
107 
108 protected:
109 
110     bool mInitialized;
111     bool mStarted;
112     VADisplay mVADisplay;
113     VAContextID mVAContext;
114     VAConfigID mVAConfig;
115     VAEntrypoint mVAEntrypoint;
116 
117 
118     VideoParamsCommon mComParams;
119     VideoParamsHRD mHrdParam;
120     VideoParamsStoreMetaDataInBuffers mStoreMetaDataInBuffers;
121 
122     bool mNewHeader;
123 
124     bool mRenderMaxSliceSize; //Max Slice Size
125     bool mRenderQP;
126     bool mRenderAIR;
127     bool mRenderCIR;
128     bool mRenderFrameRate;
129     bool mRenderBitRate;
130     bool mRenderHrd;
131     bool mRenderMaxFrameSize;
132     bool mRenderMultiTemporal;
133     bool mForceKFrame;
134 
135     VABufferID mSeqParamBuf;
136     VABufferID mRcParamBuf;
137     VABufferID mFrameRateParamBuf;
138     VABufferID mPicParamBuf;
139     VABufferID mSliceParamBuf;
140     VASurfaceID* mAutoRefSurfaces;
141 
142     android::List <VASurfaceMap *> mSrcSurfaceMapList;  //all mapped surface info list from input buffer
143     android::List <EncodeTask *> mEncodeTaskList;  //all encode tasks list
144     android::List <VABufferID> mVACodedBufferList;  //all available codedbuffer list
145 
146     VASurfaceID mRefSurface;        //reference surface, only used in base
147     VASurfaceID mRecSurface;        //reconstructed surface, only used in base
148     uint32_t mFrameNum;
149     uint32_t mCodedBufSize;
150     bool mAutoReference;
151     uint32_t mAutoReferenceSurfaceNum;
152     uint32_t mEncPackedHeaders;
153     uint32_t mEncMaxRefFrames;
154 
155     bool mSliceSizeOverflow;
156 
157     //Current Outputting task
158     EncodeTask *mCurOutputTask;
159 
160     //Current outputting CodedBuffer status
161     VABufferID mOutCodedBuffer;
162     bool mCodedBufferMapped;
163     uint8_t *mOutCodedBufferPtr;
164     VACodedBufferSegment *mCurSegment;
165     uint32_t mOffsetInSeg;
166     uint32_t mTotalSize;
167     uint32_t mTotalSizeCopied;
168     android::Mutex               mCodedBuffer_Lock, mEncodeTask_Lock;
169     android::Condition           mCodedBuffer_Cond, mEncodeTask_Cond;
170 
171     bool mFrameSkipped;
172 
173     //supported surface memory types
174     int mSupportedSurfaceMemType;
175 
176     //VASurface mapping extra action
177     int mVASurfaceMappingAction;
178 
179     // For Temporal Layer Bitrate FrameRate settings
180     VideoConfigTemperalLayerBitrateFramerate mTemporalLayerBitrateFramerate[3];
181 
182 #ifdef INTEL_VIDEO_XPROC_SHARING
183     uint32_t mSessionFlag;
184 #endif
185 };
186 #endif /* __VIDEO_ENCODER_BASE_H__ */
187