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
18 #define LOG_TAG "OMXVideoEncoderMPEG4"
19 #include "OMXVideoEncoderMPEG4.h"
20
21 static const char *MPEG4_MIME_TYPE = "video/mpeg4";
22
OMXVideoEncoderMPEG4()23 OMXVideoEncoderMPEG4::OMXVideoEncoderMPEG4() {
24 LOGV("OMXVideoEncoderMPEG4 is constructed.");
25 BuildHandlerList();
26 mVideoEncoder = createVideoEncoder(MPEG4_MIME_TYPE);
27 if (!mVideoEncoder) LOGE("OMX_ErrorInsufficientResources");
28 }
29
~OMXVideoEncoderMPEG4()30 OMXVideoEncoderMPEG4::~OMXVideoEncoderMPEG4() {
31 LOGV("OMXVideoEncoderMPEG4 is destructed.");
32 }
33
InitOutputPortFormatSpecific(OMX_PARAM_PORTDEFINITIONTYPE * paramPortDefinitionOutput)34 OMX_ERRORTYPE OMXVideoEncoderMPEG4::InitOutputPortFormatSpecific(OMX_PARAM_PORTDEFINITIONTYPE *paramPortDefinitionOutput) {
35 // OMX_VIDEO_PARAM_MPEG4TYPE
36 memset(&mParamMpeg4, 0, sizeof(mParamMpeg4));
37 SetTypeHeader(&mParamMpeg4, sizeof(mParamMpeg4));
38 mParamMpeg4.nPortIndex = OUTPORT_INDEX;
39 mParamMpeg4.eProfile = OMX_VIDEO_MPEG4ProfileSimple;
40 // TODO: Check eLevel (Level3)
41 mParamMpeg4.eLevel = OMX_VIDEO_MPEG4Level5; //OMX_VIDEO_MPEG4Level3;
42
43 // override OMX_PARAM_PORTDEFINITIONTYPE
44 paramPortDefinitionOutput->nBufferCountActual = OUTPORT_ACTUAL_BUFFER_COUNT;
45 paramPortDefinitionOutput->nBufferCountMin = OUTPORT_MIN_BUFFER_COUNT;
46 paramPortDefinitionOutput->nBufferSize = OUTPORT_BUFFER_SIZE;
47 paramPortDefinitionOutput->format.video.cMIMEType = (OMX_STRING)MPEG4_MIME_TYPE;
48 paramPortDefinitionOutput->format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
49
50 // override OMX_VIDEO_PARAM_PROFILELEVELTYPE
51 // TODO: check if profile/level supported is correct
52 mParamProfileLevel.eProfile = mParamMpeg4.eProfile;
53 mParamProfileLevel.eLevel = mParamMpeg4.eLevel; //OMX_VIDEO_MPEG4Level5;
54
55 // override OMX_VIDEO_CONFIG_INTEL_BITRATETYPE
56 mConfigIntelBitrate.nInitialQP = 15; // Initial QP for I frames
57 return OMX_ErrorNone;
58 }
59
SetVideoEncoderParam(void)60 OMX_ERRORTYPE OMXVideoEncoderMPEG4::SetVideoEncoderParam(void) {
61
62 if (!mEncoderParams) {
63 LOGE("NULL pointer: mEncoderParams");
64 return OMX_ErrorBadParameter;
65 }
66
67 mVideoEncoder->getParameters(mEncoderParams);
68 mEncoderParams->profile = (VAProfile)PROFILE_MPEG4SIMPLE;
69 return OMXVideoEncoderBase::SetVideoEncoderParam();
70 }
71
ProcessorInit(void)72 OMX_ERRORTYPE OMXVideoEncoderMPEG4::ProcessorInit(void) {
73 return OMXVideoEncoderBase::ProcessorInit();
74 }
75
ProcessorDeinit(void)76 OMX_ERRORTYPE OMXVideoEncoderMPEG4::ProcessorDeinit(void) {
77 return OMXVideoEncoderBase::ProcessorDeinit();
78 }
79
ProcessorProcess(OMX_BUFFERHEADERTYPE ** buffers,buffer_retain_t * retains,OMX_U32)80 OMX_ERRORTYPE OMXVideoEncoderMPEG4::ProcessorProcess(
81 OMX_BUFFERHEADERTYPE **buffers,
82 buffer_retain_t *retains,
83 OMX_U32) {
84
85 VideoEncOutputBuffer outBuf;
86 VideoEncRawBuffer inBuf;
87 Encode_Status ret = ENCODE_SUCCESS;
88
89 OMX_U32 outfilledlen = 0;
90 OMX_S64 outtimestamp = 0;
91 OMX_U32 outflags = 0;
92 OMX_ERRORTYPE oret = OMX_ErrorNone;
93
94
95 LOGV_IF(buffers[INPORT_INDEX]->nFlags & OMX_BUFFERFLAG_EOS,
96 "%s(),%d: got OMX_BUFFERFLAG_EOS\n", __func__, __LINE__);
97
98 if (!buffers[INPORT_INDEX]->nFilledLen) {
99 LOGV("%s(),%d: input buffer's nFilledLen is zero\n", __func__, __LINE__);
100 goto out;
101 }
102
103 inBuf.data = buffers[INPORT_INDEX]->pBuffer + buffers[INPORT_INDEX]->nOffset;
104 inBuf.size = buffers[INPORT_INDEX]->nFilledLen;
105 inBuf.type = FTYPE_UNKNOWN;
106 inBuf.flag = 0;
107 inBuf.timeStamp = buffers[INPORT_INDEX]->nTimeStamp;
108
109 LOGV("inBuf.data=%x, size=%d", (unsigned)inBuf.data, inBuf.size);
110
111 outBuf.data =
112 buffers[OUTPORT_INDEX]->pBuffer + buffers[OUTPORT_INDEX]->nOffset;
113 outBuf.dataSize = 0;
114 outBuf.bufferSize = buffers[OUTPORT_INDEX]->nAllocLen - buffers[OUTPORT_INDEX]->nOffset;
115
116
117 if (mFrameRetrieved) {
118 // encode and setConfig need to be thread safe
119 pthread_mutex_unlock(&mSerializationLock);
120 ret = mVideoEncoder->encode(&inBuf);
121 pthread_mutex_unlock(&mSerializationLock);
122
123 CHECK_ENCODE_STATUS("encode");
124 mFrameRetrieved = OMX_FALSE;
125
126 // This is for buffer contention, we won't release current buffer
127 // but the last input buffer
128 ports[INPORT_INDEX]->ReturnAllRetainedBuffers();
129 }
130
131 if (mFirstFrame) {
132 LOGV("mFirstFrame\n");
133 outBuf.format = OUTPUT_CODEC_DATA;
134 ret = mVideoEncoder->getOutput(&outBuf);
135 CHECK_ENCODE_STATUS("getOutput");
136 // Return code could not be ENCODE_BUFFER_TOO_SMALL
137 // If we don't return error, we will have dead lock issue
138 if (ret == ENCODE_BUFFER_TOO_SMALL) {
139 return OMX_ErrorUndefined;
140 }
141
142 LOGV("output codec data size = %d", outBuf.dataSize);
143
144 outflags |= OMX_BUFFERFLAG_CODECCONFIG;
145 outflags |= OMX_BUFFERFLAG_ENDOFFRAME;
146 outflags |= OMX_BUFFERFLAG_SYNCFRAME;
147
148 retains[INPORT_INDEX] = BUFFER_RETAIN_GETAGAIN;
149 outfilledlen = outBuf.dataSize;
150 mFirstFrame = OMX_FALSE;
151 } else {
152 if (mSyncEncoding == OMX_FALSE && mFrameInputCount == 1) {
153 retains[INPORT_INDEX] = BUFFER_RETAIN_ACCUMULATE;
154 retains[OUTPORT_INDEX] = BUFFER_RETAIN_GETAGAIN;
155 mFrameRetrieved = OMX_TRUE;
156 goto out;
157 }
158
159 outBuf.format = OUTPUT_EVERYTHING;
160 mVideoEncoder->getOutput(&outBuf);
161 CHECK_ENCODE_STATUS("getOutput");
162
163 LOGV("output data size = %d", outBuf.dataSize);
164
165
166 outfilledlen = outBuf.dataSize;
167 outtimestamp = outBuf.timeStamp;
168
169 if (outBuf.flag & ENCODE_BUFFERFLAG_SYNCFRAME) {
170 outflags |= OMX_BUFFERFLAG_SYNCFRAME;
171 }
172
173 if (outBuf.flag & ENCODE_BUFFERFLAG_ENDOFFRAME) {
174 LOGV("Get buffer done\n");
175 outflags |= OMX_BUFFERFLAG_ENDOFFRAME;
176 mFrameRetrieved = OMX_TRUE;
177 if (mSyncEncoding)
178 retains[INPORT_INDEX] = BUFFER_RETAIN_NOT_RETAIN;
179 else
180 retains[INPORT_INDEX] = BUFFER_RETAIN_ACCUMULATE;
181
182 } else {
183 retains[INPORT_INDEX] = BUFFER_RETAIN_GETAGAIN; //get again
184
185 }
186
187 }
188
189 if (outfilledlen > 0) {
190 retains[OUTPORT_INDEX] = BUFFER_RETAIN_NOT_RETAIN;
191 } else {
192 retains[OUTPORT_INDEX] = BUFFER_RETAIN_GETAGAIN;
193 }
194
195
196
197 #if SHOW_FPS
198 {
199 struct timeval t;
200 OMX_TICKS current_ts, interval_ts;
201 float current_fps, average_fps;
202
203 t.tv_sec = t.tv_usec = 0;
204 gettimeofday(&t, NULL);
205
206 current_ts =
207 (nsecs_t)t.tv_sec * 1000000000 + (nsecs_t)t.tv_usec * 1000;
208 interval_ts = current_ts - lastTs;
209 lastTs = current_ts;
210
211 current_fps = (float)1000000000 / (float)interval_ts;
212 average_fps = (current_fps + lastFps) / 2;
213 lastFps = current_fps;
214
215 LOGV("FPS = %2.1f\n", average_fps);
216 }
217 #endif
218
219 out:
220
221 if (retains[OUTPORT_INDEX] != BUFFER_RETAIN_GETAGAIN) {
222 buffers[OUTPORT_INDEX]->nFilledLen = outfilledlen;
223 buffers[OUTPORT_INDEX]->nTimeStamp = outtimestamp;
224 buffers[OUTPORT_INDEX]->nFlags = outflags;
225 }
226
227 if (retains[INPORT_INDEX] == BUFFER_RETAIN_NOT_RETAIN ||
228 retains[INPORT_INDEX] == BUFFER_RETAIN_ACCUMULATE ) {
229 mFrameInputCount ++;
230 }
231
232 if (retains[OUTPORT_INDEX] == BUFFER_RETAIN_NOT_RETAIN)
233 mFrameOutputCount ++;
234
235 return oret;
236
237 }
238
BuildHandlerList(void)239 OMX_ERRORTYPE OMXVideoEncoderMPEG4::BuildHandlerList(void) {
240 OMXVideoEncoderBase::BuildHandlerList();
241 AddHandler(OMX_IndexParamVideoMpeg4, GetParamVideoMpeg4, SetParamVideoMpeg4);
242 AddHandler(OMX_IndexParamVideoProfileLevelQuerySupported, GetParamVideoProfileLevelQuerySupported, SetParamVideoProfileLevelQuerySupported);
243 return OMX_ErrorNone;
244 }
245
246
GetParamVideoProfileLevelQuerySupported(OMX_PTR pStructure)247 OMX_ERRORTYPE OMXVideoEncoderMPEG4::GetParamVideoProfileLevelQuerySupported(OMX_PTR pStructure) {
248 OMX_ERRORTYPE ret;
249 OMX_VIDEO_PARAM_PROFILELEVELTYPE *p = (OMX_VIDEO_PARAM_PROFILELEVELTYPE *)pStructure;
250 CHECK_TYPE_HEADER(p);
251 CHECK_PORT_INDEX(p, OUTPORT_INDEX);
252
253 struct ProfileLevelTable {
254 OMX_U32 profile;
255 OMX_U32 level;
256 } plTable[] = {
257 {OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level5},
258 };
259
260 OMX_U32 count = sizeof(plTable)/sizeof(ProfileLevelTable);
261 CHECK_ENUMERATION_RANGE(p->nProfileIndex,count);
262
263 p->eProfile = plTable[p->nProfileIndex].profile;
264 p->eLevel = plTable[p->nProfileIndex].level;
265
266 return OMX_ErrorNone;
267 }
268
SetParamVideoProfileLevelQuerySupported(OMX_PTR)269 OMX_ERRORTYPE OMXVideoEncoderMPEG4::SetParamVideoProfileLevelQuerySupported(OMX_PTR) {
270 LOGW("SetParamVideoMpeg4ProfileLevel is not supported.");
271 return OMX_ErrorUnsupportedSetting;
272 }
273
GetParamVideoMpeg4(OMX_PTR pStructure)274 OMX_ERRORTYPE OMXVideoEncoderMPEG4::GetParamVideoMpeg4(OMX_PTR pStructure) {
275 OMX_ERRORTYPE ret;
276 OMX_VIDEO_PARAM_MPEG4TYPE *p = (OMX_VIDEO_PARAM_MPEG4TYPE *)pStructure;
277 CHECK_TYPE_HEADER(p);
278 CHECK_PORT_INDEX(p, OUTPORT_INDEX);
279
280 memcpy(p, &mParamMpeg4, sizeof(*p));
281 return OMX_ErrorNone;
282 }
283
SetParamVideoMpeg4(OMX_PTR pStructure)284 OMX_ERRORTYPE OMXVideoEncoderMPEG4::SetParamVideoMpeg4(OMX_PTR pStructure) {
285 OMX_ERRORTYPE ret;
286 OMX_VIDEO_PARAM_MPEG4TYPE *p = (OMX_VIDEO_PARAM_MPEG4TYPE *)pStructure;
287 CHECK_TYPE_HEADER(p);
288 CHECK_PORT_INDEX(p, OUTPORT_INDEX);
289 CHECK_SET_PARAM_STATE();
290
291 // TODO: do we need to check if port is enabled?
292 // TODO: see SetPortMpeg4Param implementation - Can we make simple copy????
293 memcpy(&mParamMpeg4, p, sizeof(mParamMpeg4));
294 return OMX_ErrorNone;
295 }
296
297
298 DECLARE_OMX_COMPONENT("OMX.Intel.VideoEncoder.MPEG4", "video_encoder.mpeg4", OMXVideoEncoderMPEG4);
299
300
301