• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DECODER_BASE_H_
18 #define VIDEO_DECODER_BASE_H_
19 
20 #include <va/va.h>
21 #include <va/va_tpi.h>
22 #include "VideoDecoderDefs.h"
23 #include "VideoDecoderInterface.h"
24 #include <pthread.h>
25 #include <dlfcn.h>
26 
27 extern "C" {
28 #include "vbp_loader.h"
29 }
30 
31 #ifndef Display
32 #ifdef USE_GEN_HW
33 typedef char Display;
34 #else
35 typedef unsigned int Display;
36 #endif
37 #endif
38 
39 // TODO: check what is the best number. Must be at least 2 to support one backward reference frame.
40 // Currently set to 8 to support 7 backward reference frames. This value is used for AVC frame reordering only.
41 // e.g:
42 // POC: 4P,  8P,  10P,  6B and mNextOutputPOC = 5
43 #define OUTPUT_WINDOW_SIZE 8
44 
45 /*
46  * ITU-R BT.601, BT.709  transfer matrices from VA 2.0
47  * Video Color Field definitions Design Spec(Version 0.03).
48  * [R', G', B'] values are in the range [0, 1], Y' is in the range [0,1]
49  * and [Pb, Pr] components are in the range [-0.5, 0.5].
50 */
51 static float s601[9] = {
52     1, -0.000001, 1.402,
53     1, -0.344136, -0.714136,
54     1, 1.772, 0
55 };
56 
57 static float s709[9] = {
58     1, 0, 1.5748,
59     1, -0.187324, -0.468124,
60     1, 1.8556, 0
61 };
62 
63 class VideoDecoderBase : public IVideoDecoder {
64 public:
65     VideoDecoderBase(const char *mimeType, _vbp_parser_type type);
66     virtual ~VideoDecoderBase();
67 
68     virtual Decode_Status start(VideoConfigBuffer *buffer);
69     virtual Decode_Status reset(VideoConfigBuffer *buffer) ;
70     virtual void stop(void);
71     //virtual Decode_Status decode(VideoDecodeBuffer *buffer);
72     virtual void flush(void);
73     virtual void freeSurfaceBuffers(void);
74     virtual const VideoRenderBuffer* getOutput(bool draining = false, VideoErrorBuffer *output_buf = NULL);
75     virtual Decode_Status signalRenderDone(void * graphichandler, bool isNew = false);
76     virtual const VideoFormatInfo* getFormatInfo(void);
77     virtual bool checkBufferAvail();
78     virtual void enableErrorReport(bool enabled = false) {mErrReportEnabled = enabled; };
79     virtual int getOutputQueueLength(void);
80 
81 protected:
82     // each acquireSurfaceBuffer must be followed by a corresponding outputSurfaceBuffer or releaseSurfaceBuffer.
83     // Only one surface buffer can be acquired at any given time
84     virtual Decode_Status acquireSurfaceBuffer(void);
85     // frame is successfully decoded to the acquired surface buffer and surface is ready for output
86     virtual Decode_Status outputSurfaceBuffer(void);
87     // acquired surface  buffer is not used
88     virtual Decode_Status releaseSurfaceBuffer(void);
89     // flush all decoded but not rendered buffers
90     virtual void flushSurfaceBuffers(void);
91     virtual Decode_Status endDecodingFrame(bool dropFrame);
92     virtual VideoSurfaceBuffer* findOutputByPoc(bool draining = false);
93     virtual VideoSurfaceBuffer* findOutputByPct(bool draining = false);
94     virtual VideoSurfaceBuffer* findOutputByPts();
95     virtual Decode_Status setupVA(uint32_t numSurface, VAProfile profile, uint32_t numExtraSurface = 0);
96     virtual Decode_Status terminateVA(void);
97     virtual Decode_Status parseBuffer(uint8_t *buffer, int32_t size, bool config, void** vbpData);
98 
alignMB(uint32_t a)99     static inline uint32_t alignMB(uint32_t a) {
100          return ((a + 15) & (~15));
101     }
102 
103     virtual Decode_Status getRawDataFromSurface(VideoRenderBuffer *renderBuffer = NULL, uint8_t *pRawData = NULL, uint32_t *pSize = NULL, bool internal = true);
104 
105 #if (defined USE_AVC_SHORT_FORMAT) || (defined USE_SLICE_HEADER_PARSING)
106     Decode_Status updateBuffer(uint8_t *buffer, int32_t size, void** vbpData);
107     Decode_Status queryBuffer(void **vbpData);
108     Decode_Status setParserType(_vbp_parser_type type);
109     virtual Decode_Status getCodecSpecificConfigs(VAProfile profile, VAConfigID *config);
110 #endif
111     virtual Decode_Status checkHardwareCapability();
112     Decode_Status createSurfaceFromHandle(int32_t index);
113 private:
114     Decode_Status mapSurface(void);
115     void initSurfaceBuffer(bool reset);
116     void drainDecodingErrors(VideoErrorBuffer *outErrBuf, VideoRenderBuffer *currentSurface);
117     void fillDecodingErrors(VideoRenderBuffer *currentSurface);
118 
119     bool mInitialized;
120     pthread_mutex_t mLock;
121 
122 protected:
123     bool mLowDelay; // when true, decoded frame is immediately output for rendering
124     bool mStoreMetaData; // when true, meta data mode is enabled for adaptive playback
125     VideoFormatInfo mVideoFormatInfo;
126     Display *mDisplay;
127     VADisplay mVADisplay;
128     VAContextID mVAContext;
129     VAConfigID mVAConfig;
130     VASurfaceID *mExtraSurfaces; // extra surfaces array
131     int32_t mNumExtraSurfaces;
132     bool mVAStarted;
133     uint64_t mCurrentPTS; // current presentation time stamp (unit is unknown, depend on the framework: GStreamer 100-nanosec, Android: microsecond)
134     // the following three member variables should be set using
135     // acquireSurfaceBuffer/outputSurfaceBuffer/releaseSurfaceBuffer
136     VideoSurfaceBuffer *mAcquiredBuffer;
137     VideoSurfaceBuffer *mLastReference;
138     VideoSurfaceBuffer *mForwardReference;
139     VideoConfigBuffer  mConfigBuffer; // only store configure meta data.
140     bool mDecodingFrame; // indicate whether a frame is being decoded
141     bool mSizeChanged; // indicate whether video size is changed.
142     bool mShowFrame; // indicate whether the decoded frame is for display
143 
144     int32_t mOutputWindowSize; // indicate limit of number of outstanding frames for output
145     int32_t mRotationDegrees;
146     pthread_mutex_t mFormatLock;
147 
148     bool mErrReportEnabled;
149     bool mWiDiOn;
150     typedef uint32_t (*OpenFunc)(uint32_t, void **);
151     typedef uint32_t (*CloseFunc)(void *);
152     typedef uint32_t (*ParseFunc)(void *, uint8_t *, uint32_t, uint8_t);
153     typedef uint32_t (*QueryFunc)(void *, void **);
154     typedef uint32_t (*FlushFunc)(void *);
155     typedef uint32_t (*UpdateFunc)(void *, void *, uint32_t, void **);
156     void *mLibHandle;
157     OpenFunc mParserOpen;
158     CloseFunc mParserClose;
159     ParseFunc mParserParse;
160     QueryFunc mParserQuery;
161     FlushFunc mParserFlush;
162     UpdateFunc mParserUpdate;
163     enum {
164         // TODO: move this to vbp_loader.h
165         VBP_INVALID = 0xFF,
166         // TODO: move this to va.h
167         VAProfileSoftwareDecoding = 0xFF,
168     };
169 
170     enum OUTPUT_METHOD {
171         // output by Picture Coding Type (I, P, B)
172          OUTPUT_BY_PCT,
173         // output by Picture Order Count (for AVC only)
174          OUTPUT_BY_POC,
175          //OUTPUT_BY_POS,
176          //OUTPUT_BY_PTS,
177      };
178 
179 private:
180     bool mRawOutput; // whether to output NV12 raw data
181     bool mManageReference;  // this should stay true for VC1/MP4 decoder, and stay false for AVC decoder. AVC  handles reference frame using DPB
182     OUTPUT_METHOD mOutputMethod;
183 
184     int32_t mNumSurfaces;
185     VideoSurfaceBuffer *mSurfaceBuffers;
186     VideoSurfaceBuffer *mOutputHead; // head of output buffer list
187     VideoSurfaceBuffer *mOutputTail;  // tail of output buffer list
188     VASurfaceID *mSurfaces; // surfaces array
189     VASurfaceAttribExternalBuffers *mVASurfaceAttrib;
190     uint8_t **mSurfaceUserPtr; // mapped user space pointer
191     int32_t mSurfaceAcquirePos; // position of surface to start acquiring
192     int32_t mNextOutputPOC; // Picture order count of next output
193     _vbp_parser_type mParserType;
194     void *mParserHandle;
195     void *mSignalBufferPre[MAX_GRAPHIC_BUFFER_NUM];
196     uint32 mSignalBufferSize;
197     bool mUseGEN;
198     uint32_t mMetaDataBuffersNum;
199 protected:
ManageReference(bool enable)200     void ManageReference(bool enable) {mManageReference = enable;}
setOutputMethod(OUTPUT_METHOD method)201     void setOutputMethod(OUTPUT_METHOD method) {mOutputMethod = method;}
setOutputWindowSize(int32_t size)202     void setOutputWindowSize(int32_t size) {mOutputWindowSize = (size < OUTPUT_WINDOW_SIZE) ? size : OUTPUT_WINDOW_SIZE;}
203     void querySurfaceRenderStatus(VideoSurfaceBuffer* surface);
enableLowDelayMode(bool enable)204     void enableLowDelayMode(bool enable) {mLowDelay = enable;}
205     void setRotationDegrees(int32_t rotationDegrees);
206     void setRenderRect(void);
207     void setColorSpaceInfo(int32_t colorMatrix, int32_t videoRange);
208 };
209 
210 
211 #endif  // VIDEO_DECODER_BASE_H_
212