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