1 /*
2  * Copyright 2014 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_ENCODER_OMX_COMPONENT_H_
18 
19 #define SOFT_VIDEO_ENCODER_OMX_COMPONENT_H_
20 
21 #include <media/openmax/OMX_Core.h>
22 #include <media/openmax/OMX_Video.h>
23 #include <media/openmax/OMX_VideoExt.h>
24 
25 #include "SimpleSoftOMXComponent.h"
26 
27 struct hw_module_t;
28 
29 namespace android {
30 
31 struct SoftVideoEncoderOMXComponent : public SimpleSoftOMXComponent {
32     SoftVideoEncoderOMXComponent(
33             const char *name,
34             const char *componentRole,
35             OMX_VIDEO_CODINGTYPE codingType,
36             const CodecProfileLevel *profileLevels,
37             size_t numProfileLevels,
38             int32_t width,
39             int32_t height,
40             const OMX_CALLBACKTYPE *callbacks,
41             OMX_PTR appData,
42             OMX_COMPONENTTYPE **component);
43 
44     virtual OMX_ERRORTYPE internalSetParameter(OMX_INDEXTYPE index, const OMX_PTR param);
45     virtual OMX_ERRORTYPE internalGetParameter(OMX_INDEXTYPE index, OMX_PTR params);
46 
47 protected:
48     void initPorts(
49             OMX_U32 numInputBuffers, OMX_U32 numOutputBuffers, OMX_U32 outputBufferSize,
50             const char *mime, OMX_U32 minCompressionRatio = 1);
51 
52     static void setRawVideoSize(OMX_PARAM_PORTDEFINITIONTYPE *def);
53 
54     static void ConvertFlexYUVToPlanar(
55             uint8_t *dst, size_t dstStride, size_t dstVStride,
56             struct android_ycbcr *ycbcr, int32_t width, int32_t height);
57 
58     static void ConvertYUV420SemiPlanarToYUV420Planar(
59             const uint8_t *inYVU, uint8_t* outYUV, int32_t width, int32_t height);
60 
61     static void ConvertRGB32ToPlanar(
62         uint8_t *dstY, size_t dstStride, size_t dstVStride,
63         const uint8_t *src, size_t width, size_t height, size_t srcStride,
64         bool bgr);
65 
66     const uint8_t *extractGraphicBuffer(
67             uint8_t *dst, size_t dstSize, const uint8_t *src, size_t srcSize,
68             size_t width, size_t height) const;
69 
70     virtual OMX_ERRORTYPE getExtensionIndex(const char *name, OMX_INDEXTYPE *index);
71 
72     OMX_ERRORTYPE validateInputBuffer(const OMX_BUFFERHEADERTYPE *inputBufferHeader);
73 
74     enum {
75         kInputPortIndex = 0,
76         kOutputPortIndex = 1,
77     };
78 
79     bool mInputDataIsMeta;
80     int32_t mWidth;      // width of the input frames
81     int32_t mHeight;     // height of the input frames
82     uint32_t mBitrate;   // target bitrate set for the encoder, in bits per second
83     uint32_t mFramerate; // target framerate set for the encoder, in Q16 format
84     OMX_COLOR_FORMATTYPE mColorFormat;  // Color format for the input port
85 
86 private:
87     void updatePortParams();
88     OMX_ERRORTYPE internalSetPortParams(const OMX_PARAM_PORTDEFINITIONTYPE* port);
89 
90     static const uint32_t kInputBufferAlignment = 1;
91     static const uint32_t kOutputBufferAlignment = 2;
92 
93     mutable const hw_module_t *mGrallocModule;
94 
95     uint32_t mMinOutputBufferSize;
96     uint32_t mMinCompressionRatio;
97 
98     const char *mComponentRole;
99     OMX_VIDEO_CODINGTYPE mCodingType;
100     const CodecProfileLevel *mProfileLevels;
101     size_t mNumProfileLevels;
102 
103     DISALLOW_EVIL_CONSTRUCTORS(SoftVideoEncoderOMXComponent);
104 };
105 
106 }  // namespace android
107 
108 #endif  // SOFT_VIDEO_ENCODER_OMX_COMPONENT_H_
109