1/*
2 * Copyright (C) 2013 The Android Open Source Project
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 SOFT_VIDEO_DECODER_OMX_COMPONENT_H_
18
19#define SOFT_VIDEO_DECODER_OMX_COMPONENT_H_
20
21#include "SimpleSoftOMXComponent.h"
22
23#include <media/stagefright/foundation/AHandlerReflector.h>
24#include <media/stagefright/foundation/ColorUtils.h>
25#include <media/IOMX.h>
26#include <media/hardware/HardwareAPI.h>
27
28#include <utils/RefBase.h>
29#include <utils/threads.h>
30#include <utils/Vector.h>
31
32namespace android {
33
34struct SoftVideoDecoderOMXComponent : public SimpleSoftOMXComponent {
35    SoftVideoDecoderOMXComponent(
36            const char *name,
37            const char *componentRole,
38            OMX_VIDEO_CODINGTYPE codingType,
39            const CodecProfileLevel *profileLevels,
40            size_t numProfileLevels,
41            int32_t width,
42            int32_t height,
43            const OMX_CALLBACKTYPE *callbacks,
44            OMX_PTR appData,
45            OMX_COMPONENTTYPE **component);
46
47protected:
48    enum {
49        kDescribeColorAspectsIndex = kPrepareForAdaptivePlaybackIndex + 1,
50        kDescribeHdrStaticInfoIndex = kPrepareForAdaptivePlaybackIndex + 2,
51    };
52
53    enum {
54        kNotSupported,
55        kPreferBitstream,
56        kPreferContainer,
57    };
58
59    virtual void onPortEnableCompleted(OMX_U32 portIndex, bool enabled);
60    virtual void onReset();
61
62    virtual OMX_ERRORTYPE internalGetParameter(
63            OMX_INDEXTYPE index, OMX_PTR params);
64
65    virtual OMX_ERRORTYPE internalSetParameter(
66            OMX_INDEXTYPE index, const OMX_PTR params);
67
68    virtual OMX_ERRORTYPE getConfig(
69            OMX_INDEXTYPE index, OMX_PTR params);
70
71    virtual OMX_ERRORTYPE setConfig(
72            OMX_INDEXTYPE index, const OMX_PTR params);
73
74    virtual OMX_ERRORTYPE getExtensionIndex(
75            const char *name, OMX_INDEXTYPE *index);
76
77    virtual bool supportsDescribeColorAspects();
78
79    virtual int getColorAspectPreference();
80
81    virtual bool supportDescribeHdrStaticInfo();
82
83    // This function sets both minimum buffer count and actual buffer count of
84    // input port to be |numInputBuffers|. It will also set both minimum buffer
85    // count and actual buffer count of output port to be |numOutputBuffers|.
86    void initPorts(OMX_U32 numInputBuffers,
87            OMX_U32 inputBufferSize,
88            OMX_U32 numOutputBuffers,
89            const char *mimeType,
90            OMX_U32 minCompressionRatio = 1u);
91
92    // This function sets input port's minimum buffer count to |numMinInputBuffers|,
93    // sets input port's actual buffer count to |numInputBuffers|, sets output port's
94    // minimum buffer count to |numMinOutputBuffers| and sets output port's actual buffer
95    // count to be |numOutputBuffers|.
96    void initPorts(OMX_U32 numMinInputBuffers,
97            OMX_U32 numInputBuffers,
98            OMX_U32 inputBufferSize,
99            OMX_U32 numMinOutputBuffers,
100            OMX_U32 numOutputBuffers,
101            const char *mimeType,
102            OMX_U32 minCompressionRatio = 1u);
103
104    virtual void updatePortDefinitions(bool updateCrop = true, bool updateInputSize = false);
105
106    uint32_t outputBufferWidth();
107    uint32_t outputBufferHeight();
108
109    enum CropSettingsMode {
110        kCropUnSet = 0,
111        kCropSet,
112        kCropChanged,
113    };
114
115    // This function will handle several port change events which include
116    // size changed, crop changed, stride changed and coloraspects changed.
117    // It will trigger OMX_EventPortSettingsChanged event if necessary.
118    void handlePortSettingsChange(
119            bool *portWillReset, uint32_t width, uint32_t height,
120            OMX_COLOR_FORMATTYPE outputFormat = OMX_COLOR_FormatYUV420Planar,
121            CropSettingsMode cropSettingsMode = kCropUnSet,
122            bool fakeStride = false);
123
124    void copyYV12FrameToOutputBuffer(
125            uint8_t *dst, const uint8_t *srcY, const uint8_t *srcU, const uint8_t *srcV,
126            size_t srcYStride, size_t srcUStride, size_t srcVStride);
127
128    enum {
129        kInputPortIndex  = 0,
130        kOutputPortIndex = 1,
131        kMaxPortIndex = 1,
132    };
133
134    bool mIsAdaptive;
135    uint32_t mAdaptiveMaxWidth, mAdaptiveMaxHeight;
136    uint32_t mWidth, mHeight;
137    uint32_t mCropLeft, mCropTop, mCropWidth, mCropHeight;
138    OMX_COLOR_FORMATTYPE mOutputFormat;
139    HDRStaticInfo mHdrStaticInfo;
140    enum {
141        NONE,
142        AWAITING_DISABLED,
143        AWAITING_ENABLED
144    } mOutputPortSettingsChange;
145
146    bool mUpdateColorAspects;
147
148    Mutex mColorAspectsLock;
149    // color aspects passed from the framework.
150    ColorAspects mDefaultColorAspects;
151    // color aspects parsed from the bitstream.
152    ColorAspects mBitstreamColorAspects;
153    // final color aspects after combining the above two aspects.
154    ColorAspects mFinalColorAspects;
155
156    bool colorAspectsDiffer(const ColorAspects &a, const ColorAspects &b);
157
158    // This functions takes two color aspects and updates the mFinalColorAspects
159    // based on |preferredAspects|.
160    void updateFinalColorAspects(
161            const ColorAspects &otherAspects, const ColorAspects &preferredAspects);
162
163    // This function will update the mFinalColorAspects based on codec preference.
164    status_t handleColorAspectsChange();
165
166    // Helper function to dump the ColorAspects.
167    void dumpColorAspects(const ColorAspects &colorAspects);
168
169private:
170    uint32_t mMinInputBufferSize;
171    uint32_t mMinCompressionRatio;
172
173    const char *mComponentRole;
174    OMX_VIDEO_CODINGTYPE mCodingType;
175    const CodecProfileLevel *mProfileLevels;
176    size_t mNumProfileLevels;
177
178    DISALLOW_EVIL_CONSTRUCTORS(SoftVideoDecoderOMXComponent);
179};
180
181}  // namespace android
182
183#endif  // SOFT_VIDEO_DECODER_OMX_COMPONENT_H_
184