SoftAVCEncoder.cpp revision 3fd96683850cf27648e036180acb149fac362242
1/*
2 * Copyright (C) 2012 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//#define LOG_NDEBUG 0
18#define LOG_TAG "SoftAVCEncoder"
19#include <utils/Log.h>
20
21#include "avcenc_api.h"
22#include "avcenc_int.h"
23#include "OMX_Video.h"
24
25#include <HardwareAPI.h>
26#include <MetadataBufferType.h>
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/MediaDefs.h>
29#include <media/stagefright/MediaErrors.h>
30#include <media/stagefright/MetaData.h>
31#include <media/stagefright/Utils.h>
32#include <ui/Rect.h>
33#include <ui/GraphicBufferMapper.h>
34
35#include "SoftAVCEncoder.h"
36
37#ifndef INT32_MAX
38#define INT32_MAX   2147483647
39#endif
40
41namespace android {
42
43template<class T>
44static void InitOMXParams(T *params) {
45    params->nSize = sizeof(T);
46    params->nVersion.s.nVersionMajor = 1;
47    params->nVersion.s.nVersionMinor = 0;
48    params->nVersion.s.nRevision = 0;
49    params->nVersion.s.nStep = 0;
50}
51
52typedef struct LevelConversion {
53    OMX_U32 omxLevel;
54    AVCLevel avcLevel;
55} LevelConcersion;
56
57static LevelConversion ConversionTable[] = {
58    { OMX_VIDEO_AVCLevel1,  AVC_LEVEL1_B },
59    { OMX_VIDEO_AVCLevel1b, AVC_LEVEL1   },
60    { OMX_VIDEO_AVCLevel11, AVC_LEVEL1_1 },
61    { OMX_VIDEO_AVCLevel12, AVC_LEVEL1_2 },
62    { OMX_VIDEO_AVCLevel13, AVC_LEVEL1_3 },
63    { OMX_VIDEO_AVCLevel2,  AVC_LEVEL2 },
64#if 0
65    // encoding speed is very poor if video
66    // resolution is higher than CIF
67    { OMX_VIDEO_AVCLevel21, AVC_LEVEL2_1 },
68    { OMX_VIDEO_AVCLevel22, AVC_LEVEL2_2 },
69    { OMX_VIDEO_AVCLevel3,  AVC_LEVEL3   },
70    { OMX_VIDEO_AVCLevel31, AVC_LEVEL3_1 },
71    { OMX_VIDEO_AVCLevel32, AVC_LEVEL3_2 },
72    { OMX_VIDEO_AVCLevel4,  AVC_LEVEL4   },
73    { OMX_VIDEO_AVCLevel41, AVC_LEVEL4_1 },
74    { OMX_VIDEO_AVCLevel42, AVC_LEVEL4_2 },
75    { OMX_VIDEO_AVCLevel5,  AVC_LEVEL5   },
76    { OMX_VIDEO_AVCLevel51, AVC_LEVEL5_1 },
77#endif
78};
79
80static status_t ConvertOmxAvcLevelToAvcSpecLevel(
81        OMX_U32 omxLevel, AVCLevel *avcLevel) {
82    for (size_t i = 0, n = sizeof(ConversionTable)/sizeof(ConversionTable[0]);
83        i < n; ++i) {
84        if (omxLevel == ConversionTable[i].omxLevel) {
85            *avcLevel = ConversionTable[i].avcLevel;
86            return OK;
87        }
88    }
89
90    ALOGE("ConvertOmxAvcLevelToAvcSpecLevel: %d level not supported",
91            (int32_t)omxLevel);
92
93    return BAD_VALUE;
94}
95
96static status_t ConvertAvcSpecLevelToOmxAvcLevel(
97    AVCLevel avcLevel, OMX_U32 *omxLevel) {
98    for (size_t i = 0, n = sizeof(ConversionTable)/sizeof(ConversionTable[0]);
99        i < n; ++i) {
100        if (avcLevel == ConversionTable[i].avcLevel) {
101            *omxLevel = ConversionTable[i].omxLevel;
102            return OK;
103        }
104    }
105
106    ALOGE("ConvertAvcSpecLevelToOmxAvcLevel: %d level not supported",
107            (int32_t) avcLevel);
108
109    return BAD_VALUE;
110}
111
112inline static void ConvertYUV420SemiPlanarToYUV420Planar(
113        uint8_t *inyuv, uint8_t* outyuv,
114        int32_t width, int32_t height) {
115
116    int32_t outYsize = width * height;
117    uint32_t *outy =  (uint32_t *) outyuv;
118    uint16_t *outcb = (uint16_t *) (outyuv + outYsize);
119    uint16_t *outcr = (uint16_t *) (outyuv + outYsize + (outYsize >> 2));
120
121    /* Y copying */
122    memcpy(outy, inyuv, outYsize);
123
124    /* U & V copying */
125    uint32_t *inyuv_4 = (uint32_t *) (inyuv + outYsize);
126    for (int32_t i = height >> 1; i > 0; --i) {
127        for (int32_t j = width >> 2; j > 0; --j) {
128            uint32_t temp = *inyuv_4++;
129            uint32_t tempU = temp & 0xFF;
130            tempU = tempU | ((temp >> 8) & 0xFF00);
131
132            uint32_t tempV = (temp >> 8) & 0xFF;
133            tempV = tempV | ((temp >> 16) & 0xFF00);
134
135            // Flip U and V
136            *outcb++ = tempV;
137            *outcr++ = tempU;
138        }
139    }
140}
141
142static void* MallocWrapper(
143        void *userData, int32_t size, int32_t attrs) {
144    void *ptr = malloc(size);
145    if (ptr)
146        memset(ptr, 0, size);
147    return ptr;
148}
149
150static void FreeWrapper(void *userData, void* ptr) {
151    free(ptr);
152}
153
154static int32_t DpbAllocWrapper(void *userData,
155        unsigned int sizeInMbs, unsigned int numBuffers) {
156    SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData);
157    CHECK(encoder != NULL);
158    return encoder->allocOutputBuffers(sizeInMbs, numBuffers);
159}
160
161static int32_t BindFrameWrapper(
162        void *userData, int32_t index, uint8_t **yuv) {
163    SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData);
164    CHECK(encoder != NULL);
165    return encoder->bindOutputBuffer(index, yuv);
166}
167
168static void UnbindFrameWrapper(void *userData, int32_t index) {
169    SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData);
170    CHECK(encoder != NULL);
171    return encoder->unbindOutputBuffer(index);
172}
173
174SoftAVCEncoder::SoftAVCEncoder(
175            const char *name,
176            const OMX_CALLBACKTYPE *callbacks,
177            OMX_PTR appData,
178            OMX_COMPONENTTYPE **component)
179    : SimpleSoftOMXComponent(name, callbacks, appData, component),
180      mVideoWidth(176),
181      mVideoHeight(144),
182      mVideoFrameRate(30),
183      mVideoBitRate(192000),
184      mVideoColorFormat(OMX_COLOR_FormatYUV420Planar),
185      mStoreMetaDataInBuffers(false),
186      mIDRFrameRefreshIntervalInSec(1),
187      mAVCEncProfile(AVC_BASELINE),
188      mAVCEncLevel(AVC_LEVEL2),
189      mNumInputFrames(-1),
190      mPrevTimestampUs(-1),
191      mStarted(false),
192      mSawInputEOS(false),
193      mSignalledError(false),
194      mHandle(new tagAVCHandle),
195      mEncParams(new tagAVCEncParam),
196      mInputFrameData(NULL),
197      mSliceGroup(NULL) {
198
199    initPorts();
200    ALOGI("Construct SoftAVCEncoder");
201}
202
203SoftAVCEncoder::~SoftAVCEncoder() {
204    ALOGV("Destruct SoftAVCEncoder");
205    releaseEncoder();
206    List<BufferInfo *> &outQueue = getPortQueue(1);
207    List<BufferInfo *> &inQueue = getPortQueue(0);
208    CHECK(outQueue.empty());
209    CHECK(inQueue.empty());
210}
211
212OMX_ERRORTYPE SoftAVCEncoder::initEncParams() {
213    CHECK(mHandle != NULL);
214    memset(mHandle, 0, sizeof(tagAVCHandle));
215    mHandle->AVCObject = NULL;
216    mHandle->userData = this;
217    mHandle->CBAVC_DPBAlloc = DpbAllocWrapper;
218    mHandle->CBAVC_FrameBind = BindFrameWrapper;
219    mHandle->CBAVC_FrameUnbind = UnbindFrameWrapper;
220    mHandle->CBAVC_Malloc = MallocWrapper;
221    mHandle->CBAVC_Free = FreeWrapper;
222
223    CHECK(mEncParams != NULL);
224    memset(mEncParams, 0, sizeof(mEncParams));
225    mEncParams->rate_control = AVC_ON;
226    mEncParams->initQP = 0;
227    mEncParams->init_CBP_removal_delay = 1600;
228
229    mEncParams->intramb_refresh = 0;
230    mEncParams->auto_scd = AVC_ON;
231    mEncParams->out_of_band_param_set = AVC_ON;
232    mEncParams->poc_type = 2;
233    mEncParams->log2_max_poc_lsb_minus_4 = 12;
234    mEncParams->delta_poc_zero_flag = 0;
235    mEncParams->offset_poc_non_ref = 0;
236    mEncParams->offset_top_bottom = 0;
237    mEncParams->num_ref_in_cycle = 0;
238    mEncParams->offset_poc_ref = NULL;
239
240    mEncParams->num_ref_frame = 1;
241    mEncParams->num_slice_group = 1;
242    mEncParams->fmo_type = 0;
243
244    mEncParams->db_filter = AVC_ON;
245    mEncParams->disable_db_idc = 0;
246
247    mEncParams->alpha_offset = 0;
248    mEncParams->beta_offset = 0;
249    mEncParams->constrained_intra_pred = AVC_OFF;
250
251    mEncParams->data_par = AVC_OFF;
252    mEncParams->fullsearch = AVC_OFF;
253    mEncParams->search_range = 16;
254    mEncParams->sub_pel = AVC_OFF;
255    mEncParams->submb_pred = AVC_OFF;
256    mEncParams->rdopt_mode = AVC_OFF;
257    mEncParams->bidir_pred = AVC_OFF;
258
259    mEncParams->use_overrun_buffer = AVC_OFF;
260
261    if (mVideoColorFormat == OMX_COLOR_FormatYUV420SemiPlanar) {
262        // Color conversion is needed.
263        CHECK(mInputFrameData == NULL);
264        if (((uint64_t)mVideoWidth * mVideoHeight) > ((uint64_t)INT32_MAX / 3)) {
265            ALOGE("Buffer size is too big.");
266            return OMX_ErrorUndefined;
267        }
268        mInputFrameData =
269            (uint8_t *) malloc((mVideoWidth * mVideoHeight * 3 ) >> 1);
270        CHECK(mInputFrameData != NULL);
271    }
272
273    // PV's AVC encoder requires the video dimension of multiple
274    if (mVideoWidth % 16 != 0 || mVideoHeight % 16 != 0) {
275        ALOGE("Video frame size %dx%d must be a multiple of 16",
276            mVideoWidth, mVideoHeight);
277        return OMX_ErrorBadParameter;
278    }
279
280    mEncParams->width = mVideoWidth;
281    mEncParams->height = mVideoHeight;
282    mEncParams->bitrate = mVideoBitRate;
283    mEncParams->frame_rate = 1000 * mVideoFrameRate;  // In frames/ms!
284    mEncParams->CPB_size = (uint32_t) (mVideoBitRate >> 1);
285
286    int32_t nMacroBlocks = ((((mVideoWidth + 15) >> 4) << 4) *
287            (((mVideoHeight + 15) >> 4) << 4)) >> 8;
288    CHECK(mSliceGroup == NULL);
289    if ((size_t)nMacroBlocks > SIZE_MAX / sizeof(uint32_t)) {
290        ALOGE("requested memory size is too big.");
291        return OMX_ErrorUndefined;
292    }
293    mSliceGroup = (uint32_t *) malloc(sizeof(uint32_t) * nMacroBlocks);
294    CHECK(mSliceGroup != NULL);
295    for (int ii = 0, idx = 0; ii < nMacroBlocks; ++ii) {
296        mSliceGroup[ii] = idx++;
297        if (idx >= mEncParams->num_slice_group) {
298            idx = 0;
299        }
300    }
301    mEncParams->slice_group = mSliceGroup;
302
303    // Set IDR frame refresh interval
304    if (mIDRFrameRefreshIntervalInSec < 0) {
305        mEncParams->idr_period = -1;
306    } else if (mIDRFrameRefreshIntervalInSec == 0) {
307        mEncParams->idr_period = 1;  // All I frames
308    } else {
309        mEncParams->idr_period =
310            (mIDRFrameRefreshIntervalInSec * mVideoFrameRate);
311    }
312
313    // Set profile and level
314    mEncParams->profile = mAVCEncProfile;
315    mEncParams->level = mAVCEncLevel;
316
317    return OMX_ErrorNone;
318}
319
320OMX_ERRORTYPE SoftAVCEncoder::initEncoder() {
321    CHECK(!mStarted);
322
323    OMX_ERRORTYPE errType = OMX_ErrorNone;
324    if (OMX_ErrorNone != (errType = initEncParams())) {
325        ALOGE("Failed to initialized encoder params");
326        mSignalledError = true;
327        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
328        return errType;
329    }
330
331    AVCEnc_Status err;
332    err = PVAVCEncInitialize(mHandle, mEncParams, NULL, NULL);
333    if (err != AVCENC_SUCCESS) {
334        ALOGE("Failed to initialize the encoder: %d", err);
335        mSignalledError = true;
336        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
337        return OMX_ErrorUndefined;
338    }
339
340    mNumInputFrames = -2;  // 1st two buffers contain SPS and PPS
341    mSpsPpsHeaderReceived = false;
342    mReadyForNextFrame = true;
343    mIsIDRFrame = false;
344    mStarted = true;
345
346    return OMX_ErrorNone;
347}
348
349OMX_ERRORTYPE SoftAVCEncoder::releaseEncoder() {
350    if (!mStarted) {
351        return OMX_ErrorNone;
352    }
353
354    PVAVCCleanUpEncoder(mHandle);
355    releaseOutputBuffers();
356
357    delete mInputFrameData;
358    mInputFrameData = NULL;
359
360    delete mSliceGroup;
361    mSliceGroup = NULL;
362
363    delete mEncParams;
364    mEncParams = NULL;
365
366    delete mHandle;
367    mHandle = NULL;
368
369    mStarted = false;
370
371    return OMX_ErrorNone;
372}
373
374void SoftAVCEncoder::releaseOutputBuffers() {
375    for (size_t i = 0; i < mOutputBuffers.size(); ++i) {
376        MediaBuffer *buffer = mOutputBuffers.editItemAt(i);
377        buffer->setObserver(NULL);
378        buffer->release();
379    }
380    mOutputBuffers.clear();
381}
382
383void SoftAVCEncoder::initPorts() {
384    OMX_PARAM_PORTDEFINITIONTYPE def;
385    InitOMXParams(&def);
386
387    const size_t kInputBufferSize = (mVideoWidth * mVideoHeight * 3) >> 1;
388
389    // 31584 is PV's magic number.  Not sure why.
390    const size_t kOutputBufferSize =
391            (kInputBufferSize > 31584) ? kInputBufferSize: 31584;
392
393    def.nPortIndex = 0;
394    def.eDir = OMX_DirInput;
395    def.nBufferCountMin = kNumBuffers;
396    def.nBufferCountActual = def.nBufferCountMin;
397    def.nBufferSize = kInputBufferSize;
398    def.bEnabled = OMX_TRUE;
399    def.bPopulated = OMX_FALSE;
400    def.eDomain = OMX_PortDomainVideo;
401    def.bBuffersContiguous = OMX_FALSE;
402    def.nBufferAlignment = 1;
403
404    def.format.video.cMIMEType = const_cast<char *>("video/raw");
405    def.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
406    def.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
407    def.format.video.xFramerate = (mVideoFrameRate << 16);  // Q16 format
408    def.format.video.nBitrate = mVideoBitRate;
409    def.format.video.nFrameWidth = mVideoWidth;
410    def.format.video.nFrameHeight = mVideoHeight;
411    def.format.video.nStride = mVideoWidth;
412    def.format.video.nSliceHeight = mVideoHeight;
413
414    addPort(def);
415
416    def.nPortIndex = 1;
417    def.eDir = OMX_DirOutput;
418    def.nBufferCountMin = kNumBuffers;
419    def.nBufferCountActual = def.nBufferCountMin;
420    def.nBufferSize = kOutputBufferSize;
421    def.bEnabled = OMX_TRUE;
422    def.bPopulated = OMX_FALSE;
423    def.eDomain = OMX_PortDomainVideo;
424    def.bBuffersContiguous = OMX_FALSE;
425    def.nBufferAlignment = 2;
426
427    def.format.video.cMIMEType = const_cast<char *>("video/avc");
428    def.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
429    def.format.video.eColorFormat = OMX_COLOR_FormatUnused;
430    def.format.video.xFramerate = (0 << 16);  // Q16 format
431    def.format.video.nBitrate = mVideoBitRate;
432    def.format.video.nFrameWidth = mVideoWidth;
433    def.format.video.nFrameHeight = mVideoHeight;
434    def.format.video.nStride = mVideoWidth;
435    def.format.video.nSliceHeight = mVideoHeight;
436
437    addPort(def);
438}
439
440OMX_ERRORTYPE SoftAVCEncoder::internalGetParameter(
441        OMX_INDEXTYPE index, OMX_PTR params) {
442    switch (index) {
443        case OMX_IndexParamVideoErrorCorrection:
444        {
445            return OMX_ErrorNotImplemented;
446        }
447
448        case OMX_IndexParamVideoBitrate:
449        {
450            OMX_VIDEO_PARAM_BITRATETYPE *bitRate =
451                (OMX_VIDEO_PARAM_BITRATETYPE *) params;
452
453            if (bitRate->nPortIndex != 1) {
454                return OMX_ErrorUndefined;
455            }
456
457            bitRate->eControlRate = OMX_Video_ControlRateVariable;
458            bitRate->nTargetBitrate = mVideoBitRate;
459            return OMX_ErrorNone;
460        }
461
462        case OMX_IndexParamVideoPortFormat:
463        {
464            OMX_VIDEO_PARAM_PORTFORMATTYPE *formatParams =
465                (OMX_VIDEO_PARAM_PORTFORMATTYPE *)params;
466
467            if (formatParams->nPortIndex > 1) {
468                return OMX_ErrorUndefined;
469            }
470
471            if (formatParams->nIndex > 2) {
472                return OMX_ErrorNoMore;
473            }
474
475            if (formatParams->nPortIndex == 0) {
476                formatParams->eCompressionFormat = OMX_VIDEO_CodingUnused;
477                if (formatParams->nIndex == 0) {
478                    formatParams->eColorFormat = OMX_COLOR_FormatYUV420Planar;
479                } else if (formatParams->nIndex == 1) {
480                    formatParams->eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
481                } else {
482                    formatParams->eColorFormat = OMX_COLOR_FormatAndroidOpaque;
483                }
484            } else {
485                formatParams->eCompressionFormat = OMX_VIDEO_CodingAVC;
486                formatParams->eColorFormat = OMX_COLOR_FormatUnused;
487            }
488
489            return OMX_ErrorNone;
490        }
491
492        case OMX_IndexParamVideoAvc:
493        {
494            OMX_VIDEO_PARAM_AVCTYPE *avcParams =
495                (OMX_VIDEO_PARAM_AVCTYPE *)params;
496
497            if (avcParams->nPortIndex != 1) {
498                return OMX_ErrorUndefined;
499            }
500
501            avcParams->eProfile = OMX_VIDEO_AVCProfileBaseline;
502            OMX_U32 omxLevel = AVC_LEVEL2;
503            if (OMX_ErrorNone !=
504                ConvertAvcSpecLevelToOmxAvcLevel(mAVCEncLevel, &omxLevel)) {
505                return OMX_ErrorUndefined;
506            }
507
508            avcParams->eLevel = (OMX_VIDEO_AVCLEVELTYPE) omxLevel;
509            avcParams->nRefFrames = 1;
510            avcParams->nBFrames = 0;
511            avcParams->bUseHadamard = OMX_TRUE;
512            avcParams->nAllowedPictureTypes =
513                    (OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP);
514            avcParams->nRefIdx10ActiveMinus1 = 0;
515            avcParams->nRefIdx11ActiveMinus1 = 0;
516            avcParams->bWeightedPPrediction = OMX_FALSE;
517            avcParams->bEntropyCodingCABAC = OMX_FALSE;
518            avcParams->bconstIpred = OMX_FALSE;
519            avcParams->bDirect8x8Inference = OMX_FALSE;
520            avcParams->bDirectSpatialTemporal = OMX_FALSE;
521            avcParams->nCabacInitIdc = 0;
522            return OMX_ErrorNone;
523        }
524
525        case OMX_IndexParamVideoProfileLevelQuerySupported:
526        {
527            OMX_VIDEO_PARAM_PROFILELEVELTYPE *profileLevel =
528                (OMX_VIDEO_PARAM_PROFILELEVELTYPE *)params;
529
530            if (profileLevel->nPortIndex != 1) {
531                return OMX_ErrorUndefined;
532            }
533
534            const size_t size =
535                    sizeof(ConversionTable) / sizeof(ConversionTable[0]);
536
537            if (profileLevel->nProfileIndex >= size) {
538                return OMX_ErrorNoMore;
539            }
540
541            profileLevel->eProfile = OMX_VIDEO_AVCProfileBaseline;
542            profileLevel->eLevel = ConversionTable[profileLevel->nProfileIndex].omxLevel;
543
544            return OMX_ErrorNone;
545        }
546
547        default:
548            return SimpleSoftOMXComponent::internalGetParameter(index, params);
549    }
550}
551
552OMX_ERRORTYPE SoftAVCEncoder::internalSetParameter(
553        OMX_INDEXTYPE index, const OMX_PTR params) {
554    int32_t indexFull = index;
555
556    switch (indexFull) {
557        case OMX_IndexParamVideoErrorCorrection:
558        {
559            return OMX_ErrorNotImplemented;
560        }
561
562        case OMX_IndexParamVideoBitrate:
563        {
564            OMX_VIDEO_PARAM_BITRATETYPE *bitRate =
565                (OMX_VIDEO_PARAM_BITRATETYPE *) params;
566
567            if (bitRate->nPortIndex != 1 ||
568                bitRate->eControlRate != OMX_Video_ControlRateVariable) {
569                return OMX_ErrorUndefined;
570            }
571
572            mVideoBitRate = bitRate->nTargetBitrate;
573            return OMX_ErrorNone;
574        }
575
576        case OMX_IndexParamPortDefinition:
577        {
578            OMX_PARAM_PORTDEFINITIONTYPE *def =
579                (OMX_PARAM_PORTDEFINITIONTYPE *)params;
580            if (def->nPortIndex > 1) {
581                return OMX_ErrorUndefined;
582            }
583
584            if (def->nPortIndex == 0) {
585                if (def->format.video.eCompressionFormat != OMX_VIDEO_CodingUnused ||
586                    (def->format.video.eColorFormat != OMX_COLOR_FormatYUV420Planar &&
587                     def->format.video.eColorFormat != OMX_COLOR_FormatYUV420SemiPlanar &&
588                     def->format.video.eColorFormat != OMX_COLOR_FormatAndroidOpaque)) {
589                    return OMX_ErrorUndefined;
590                }
591            } else {
592                if (def->format.video.eCompressionFormat != OMX_VIDEO_CodingAVC ||
593                    (def->format.video.eColorFormat != OMX_COLOR_FormatUnused)) {
594                    return OMX_ErrorUndefined;
595                }
596            }
597
598            OMX_ERRORTYPE err = SimpleSoftOMXComponent::internalSetParameter(index, params);
599            if (OMX_ErrorNone != err) {
600                return err;
601            }
602
603            if (def->nPortIndex == 0) {
604                mVideoWidth = def->format.video.nFrameWidth;
605                mVideoHeight = def->format.video.nFrameHeight;
606                mVideoFrameRate = def->format.video.xFramerate >> 16;
607                mVideoColorFormat = def->format.video.eColorFormat;
608            } else {
609                mVideoBitRate = def->format.video.nBitrate;
610            }
611
612            return OMX_ErrorNone;
613        }
614
615        case OMX_IndexParamStandardComponentRole:
616        {
617            const OMX_PARAM_COMPONENTROLETYPE *roleParams =
618                (const OMX_PARAM_COMPONENTROLETYPE *)params;
619
620            if (strncmp((const char *)roleParams->cRole,
621                        "video_encoder.avc",
622                        OMX_MAX_STRINGNAME_SIZE - 1)) {
623                return OMX_ErrorUndefined;
624            }
625
626            return OMX_ErrorNone;
627        }
628
629        case OMX_IndexParamVideoPortFormat:
630        {
631            const OMX_VIDEO_PARAM_PORTFORMATTYPE *formatParams =
632                (const OMX_VIDEO_PARAM_PORTFORMATTYPE *)params;
633
634            if (formatParams->nPortIndex > 1) {
635                return OMX_ErrorUndefined;
636            }
637
638            if (formatParams->nIndex > 2) {
639                return OMX_ErrorNoMore;
640            }
641
642            if (formatParams->nPortIndex == 0) {
643                if (formatParams->eCompressionFormat != OMX_VIDEO_CodingUnused ||
644                    ((formatParams->nIndex == 0 &&
645                      formatParams->eColorFormat != OMX_COLOR_FormatYUV420Planar) ||
646                    (formatParams->nIndex == 1 &&
647                     formatParams->eColorFormat != OMX_COLOR_FormatYUV420SemiPlanar) ||
648                    (formatParams->nIndex == 2 &&
649                     formatParams->eColorFormat != OMX_COLOR_FormatAndroidOpaque) )) {
650                    return OMX_ErrorUndefined;
651                }
652                mVideoColorFormat = formatParams->eColorFormat;
653            } else {
654                if (formatParams->eCompressionFormat != OMX_VIDEO_CodingAVC ||
655                    formatParams->eColorFormat != OMX_COLOR_FormatUnused) {
656                    return OMX_ErrorUndefined;
657                }
658            }
659
660            return OMX_ErrorNone;
661        }
662
663        case OMX_IndexParamVideoAvc:
664        {
665            OMX_VIDEO_PARAM_AVCTYPE *avcType =
666                (OMX_VIDEO_PARAM_AVCTYPE *)params;
667
668            if (avcType->nPortIndex != 1) {
669                return OMX_ErrorUndefined;
670            }
671
672            // PV's AVC encoder only supports baseline profile
673            if (avcType->eProfile != OMX_VIDEO_AVCProfileBaseline ||
674                avcType->nRefFrames != 1 ||
675                avcType->nBFrames != 0 ||
676                avcType->bUseHadamard != OMX_TRUE ||
677                (avcType->nAllowedPictureTypes & OMX_VIDEO_PictureTypeB) != 0 ||
678                avcType->nRefIdx10ActiveMinus1 != 0 ||
679                avcType->nRefIdx11ActiveMinus1 != 0 ||
680                avcType->bWeightedPPrediction != OMX_FALSE ||
681                avcType->bEntropyCodingCABAC != OMX_FALSE ||
682                avcType->bconstIpred != OMX_FALSE ||
683                avcType->bDirect8x8Inference != OMX_FALSE ||
684                avcType->bDirectSpatialTemporal != OMX_FALSE ||
685                avcType->nCabacInitIdc != 0) {
686                return OMX_ErrorUndefined;
687            }
688
689            if (OK != ConvertOmxAvcLevelToAvcSpecLevel(avcType->eLevel, &mAVCEncLevel)) {
690                return OMX_ErrorUndefined;
691            }
692
693            return OMX_ErrorNone;
694        }
695
696        case kStoreMetaDataExtensionIndex:
697        {
698            StoreMetaDataInBuffersParams *storeParams =
699                    (StoreMetaDataInBuffersParams*)params;
700            if (storeParams->nPortIndex != 0) {
701                ALOGE("%s: StoreMetadataInBuffersParams.nPortIndex not zero!",
702                        __FUNCTION__);
703                return OMX_ErrorUndefined;
704            }
705
706            mStoreMetaDataInBuffers = storeParams->bStoreMetaData;
707            ALOGV("StoreMetaDataInBuffers set to: %s",
708                    mStoreMetaDataInBuffers ? " true" : "false");
709
710            if (mStoreMetaDataInBuffers) {
711                mVideoColorFormat == OMX_COLOR_FormatYUV420SemiPlanar;
712                if (mInputFrameData == NULL) {
713                    if (((uint64_t)mVideoWidth * mVideoHeight) > ((uint64_t)INT32_MAX / 3)) {
714                        ALOGE("Buffer size is too big.");
715                        return OMX_ErrorUndefined;
716                    }
717                    mInputFrameData =
718                            (uint8_t *) malloc((mVideoWidth * mVideoHeight * 3 ) >> 1);
719                }
720            }
721
722            return OMX_ErrorNone;
723        }
724
725        default:
726            return SimpleSoftOMXComponent::internalSetParameter(index, params);
727    }
728}
729
730void SoftAVCEncoder::onQueueFilled(OMX_U32 portIndex) {
731    if (mSignalledError || mSawInputEOS) {
732        return;
733    }
734
735    if (!mStarted) {
736        if (OMX_ErrorNone != initEncoder()) {
737            return;
738        }
739    }
740
741    List<BufferInfo *> &inQueue = getPortQueue(0);
742    List<BufferInfo *> &outQueue = getPortQueue(1);
743
744    while (!mSawInputEOS && !inQueue.empty() && !outQueue.empty()) {
745        BufferInfo *inInfo = *inQueue.begin();
746        OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
747        BufferInfo *outInfo = *outQueue.begin();
748        OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
749
750        outHeader->nTimeStamp = 0;
751        outHeader->nFlags = 0;
752        outHeader->nOffset = 0;
753        outHeader->nFilledLen = 0;
754        outHeader->nOffset = 0;
755
756        uint8_t *outPtr = (uint8_t *) outHeader->pBuffer;
757        uint32_t dataLength = outHeader->nAllocLen;
758
759        if (!mSpsPpsHeaderReceived && mNumInputFrames < 0) {
760            // 4 bytes are reserved for holding the start code 0x00000001
761            // of the sequence parameter set at the beginning.
762            outPtr += 4;
763            dataLength -= 4;
764        }
765
766        int32_t type;
767        AVCEnc_Status encoderStatus = AVCENC_SUCCESS;
768
769        // Combine SPS and PPS and place them in the very first output buffer
770        // SPS and PPS are separated by start code 0x00000001
771        // Assume that we have exactly one SPS and exactly one PPS.
772        while (!mSpsPpsHeaderReceived && mNumInputFrames <= 0) {
773            encoderStatus = PVAVCEncodeNAL(mHandle, outPtr, &dataLength, &type);
774            if (encoderStatus == AVCENC_WRONG_STATE) {
775                mSpsPpsHeaderReceived = true;
776                CHECK_EQ(0, mNumInputFrames);  // 1st video frame is 0
777                outHeader->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
778                outQueue.erase(outQueue.begin());
779                outInfo->mOwnedByUs = false;
780                notifyFillBufferDone(outHeader);
781                return;
782            } else {
783                switch (type) {
784                    case AVC_NALTYPE_SPS:
785                        ++mNumInputFrames;
786                        memcpy((uint8_t *)outHeader->pBuffer, "\x00\x00\x00\x01", 4);
787                        outHeader->nFilledLen = 4 + dataLength;
788                        outPtr += (dataLength + 4);  // 4 bytes for next start code
789                        dataLength = outHeader->nAllocLen - outHeader->nFilledLen;
790                        break;
791                    default:
792                        CHECK_EQ(AVC_NALTYPE_PPS, type);
793                        ++mNumInputFrames;
794                        memcpy((uint8_t *) outHeader->pBuffer + outHeader->nFilledLen,
795                                "\x00\x00\x00\x01", 4);
796                        outHeader->nFilledLen += (dataLength + 4);
797                        outPtr += (dataLength + 4);
798                        break;
799                }
800            }
801        }
802
803        buffer_handle_t srcBuffer; // for MetaDataMode only
804
805        // Get next input video frame
806        if (mReadyForNextFrame) {
807            // Save the input buffer info so that it can be
808            // passed to an output buffer
809            InputBufferInfo info;
810            info.mTimeUs = inHeader->nTimeStamp;
811            info.mFlags = inHeader->nFlags;
812            mInputBufferInfoVec.push(info);
813            mPrevTimestampUs = inHeader->nTimeStamp;
814
815            if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
816                mSawInputEOS = true;
817            }
818
819            if (inHeader->nFilledLen > 0) {
820                AVCFrameIO videoInput;
821                memset(&videoInput, 0, sizeof(videoInput));
822                videoInput.height = ((mVideoHeight  + 15) >> 4) << 4;
823                videoInput.pitch = ((mVideoWidth + 15) >> 4) << 4;
824                videoInput.coding_timestamp = (inHeader->nTimeStamp + 500) / 1000;  // in ms
825                uint8_t *inputData = NULL;
826                if (mStoreMetaDataInBuffers) {
827                    if (inHeader->nFilledLen != 8) {
828                        ALOGE("MetaData buffer is wrong size! "
829                                "(got %lu bytes, expected 8)", inHeader->nFilledLen);
830                        mSignalledError = true;
831                        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
832                        return;
833                    }
834                    inputData =
835                            extractGrallocData(inHeader->pBuffer + inHeader->nOffset,
836                                    &srcBuffer);
837                    if (inputData == NULL) {
838                        ALOGE("Unable to extract gralloc buffer in metadata mode");
839                        mSignalledError = true;
840                        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
841                        return;
842                    }
843                    // TODO: Verify/convert pixel format enum
844                } else {
845                    inputData = (uint8_t *)inHeader->pBuffer + inHeader->nOffset;
846                }
847
848                if (mVideoColorFormat != OMX_COLOR_FormatYUV420Planar) {
849                    ConvertYUV420SemiPlanarToYUV420Planar(
850                        inputData, mInputFrameData, mVideoWidth, mVideoHeight);
851                    inputData = mInputFrameData;
852                }
853                CHECK(inputData != NULL);
854                videoInput.YCbCr[0] = inputData;
855                videoInput.YCbCr[1] = videoInput.YCbCr[0] + videoInput.height * videoInput.pitch;
856                videoInput.YCbCr[2] = videoInput.YCbCr[1] +
857                    ((videoInput.height * videoInput.pitch) >> 2);
858                videoInput.disp_order = mNumInputFrames;
859
860                encoderStatus = PVAVCEncSetInput(mHandle, &videoInput);
861                if (encoderStatus == AVCENC_SUCCESS || encoderStatus == AVCENC_NEW_IDR) {
862                    mReadyForNextFrame = false;
863                    ++mNumInputFrames;
864                    if (encoderStatus == AVCENC_NEW_IDR) {
865                        mIsIDRFrame = 1;
866                    }
867                } else {
868                    if (encoderStatus < AVCENC_SUCCESS) {
869                        ALOGE("encoderStatus = %d at line %d", encoderStatus, __LINE__);
870                        mSignalledError = true;
871                        releaseGrallocData(srcBuffer);
872                        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
873                        return;
874                    } else {
875                        ALOGV("encoderStatus = %d at line %d", encoderStatus, __LINE__);
876                        inQueue.erase(inQueue.begin());
877                        inInfo->mOwnedByUs = false;
878                        releaseGrallocData(srcBuffer);
879                        notifyEmptyBufferDone(inHeader);
880                        return;
881                    }
882                }
883            }
884        }
885
886        // Encode an input video frame
887        CHECK(encoderStatus == AVCENC_SUCCESS || encoderStatus == AVCENC_NEW_IDR);
888        dataLength = outHeader->nAllocLen;  // Reset the output buffer length
889        if (inHeader->nFilledLen > 0) {
890            encoderStatus = PVAVCEncodeNAL(mHandle, outPtr, &dataLength, &type);
891            if (encoderStatus == AVCENC_SUCCESS) {
892                CHECK(NULL == PVAVCEncGetOverrunBuffer(mHandle));
893            } else if (encoderStatus == AVCENC_PICTURE_READY) {
894                CHECK(NULL == PVAVCEncGetOverrunBuffer(mHandle));
895                if (mIsIDRFrame) {
896                    outHeader->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;
897                    mIsIDRFrame = false;
898                }
899                mReadyForNextFrame = true;
900                AVCFrameIO recon;
901                if (PVAVCEncGetRecon(mHandle, &recon) == AVCENC_SUCCESS) {
902                    PVAVCEncReleaseRecon(mHandle, &recon);
903                }
904            } else {
905                dataLength = 0;
906                mReadyForNextFrame = true;
907            }
908
909            if (encoderStatus < AVCENC_SUCCESS) {
910                ALOGE("encoderStatus = %d at line %d", encoderStatus, __LINE__);
911                mSignalledError = true;
912                releaseGrallocData(srcBuffer);
913                notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
914                return;
915            }
916        } else {
917            dataLength = 0;
918        }
919
920        inQueue.erase(inQueue.begin());
921        inInfo->mOwnedByUs = false;
922        releaseGrallocData(srcBuffer);
923        notifyEmptyBufferDone(inHeader);
924
925        outQueue.erase(outQueue.begin());
926        CHECK(!mInputBufferInfoVec.empty());
927        InputBufferInfo *inputBufInfo = mInputBufferInfoVec.begin();
928        outHeader->nTimeStamp = inputBufInfo->mTimeUs;
929        outHeader->nFlags |= (inputBufInfo->mFlags | OMX_BUFFERFLAG_ENDOFFRAME);
930        if (mSawInputEOS) {
931            outHeader->nFlags |= OMX_BUFFERFLAG_EOS;
932        }
933        outHeader->nFilledLen = dataLength;
934        outInfo->mOwnedByUs = false;
935        notifyFillBufferDone(outHeader);
936        mInputBufferInfoVec.erase(mInputBufferInfoVec.begin());
937    }
938}
939
940int32_t SoftAVCEncoder::allocOutputBuffers(
941        unsigned int sizeInMbs, unsigned int numBuffers) {
942    CHECK(mOutputBuffers.isEmpty());
943    size_t frameSize = (sizeInMbs << 7) * 3;
944    for (unsigned int i = 0; i <  numBuffers; ++i) {
945        MediaBuffer *buffer = new MediaBuffer(frameSize);
946        buffer->setObserver(this);
947        mOutputBuffers.push(buffer);
948    }
949
950    return 1;
951}
952
953void SoftAVCEncoder::unbindOutputBuffer(int32_t index) {
954    CHECK(index >= 0);
955}
956
957int32_t SoftAVCEncoder::bindOutputBuffer(int32_t index, uint8_t **yuv) {
958    CHECK(index >= 0);
959    CHECK(index < (int32_t) mOutputBuffers.size());
960    *yuv = (uint8_t *) mOutputBuffers[index]->data();
961
962    return 1;
963}
964
965void SoftAVCEncoder::signalBufferReturned(MediaBuffer *buffer) {
966    ALOGV("signalBufferReturned: %p", buffer);
967}
968
969OMX_ERRORTYPE SoftAVCEncoder::getExtensionIndex(
970        const char *name, OMX_INDEXTYPE *index) {
971    if (!strcmp(name, "OMX.google.android.index.storeMetaDataInBuffers")) {
972        *(int32_t*)index = kStoreMetaDataExtensionIndex;
973        return OMX_ErrorNone;
974    }
975    return OMX_ErrorUndefined;
976}
977
978uint8_t *SoftAVCEncoder::extractGrallocData(void *data, buffer_handle_t *buffer) {
979    OMX_U32 type = *(OMX_U32*)data;
980    status_t res;
981    if (type != kMetadataBufferTypeGrallocSource) {
982        ALOGE("Data passed in with metadata mode does not have type "
983                "kMetadataBufferTypeGrallocSource (%d), has type %ld instead",
984                kMetadataBufferTypeGrallocSource, type);
985        return NULL;
986    }
987    buffer_handle_t imgBuffer = *(buffer_handle_t*)((uint8_t*)data + 4);
988
989    const Rect rect(mVideoWidth, mVideoHeight);
990    uint8_t *img;
991    res = GraphicBufferMapper::get().lock(imgBuffer,
992            GRALLOC_USAGE_HW_VIDEO_ENCODER,
993            rect, (void**)&img);
994    if (res != OK) {
995        ALOGE("%s: Unable to lock image buffer %p for access", __FUNCTION__,
996                imgBuffer);
997        return NULL;
998    }
999
1000    *buffer = imgBuffer;
1001    return img;
1002}
1003
1004void SoftAVCEncoder::releaseGrallocData(buffer_handle_t buffer) {
1005    if (mStoreMetaDataInBuffers) {
1006        GraphicBufferMapper::get().unlock(buffer);
1007    }
1008}
1009
1010}  // namespace android
1011
1012android::SoftOMXComponent *createSoftOMXComponent(
1013        const char *name, const OMX_CALLBACKTYPE *callbacks,
1014        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
1015    return new android::SoftAVCEncoder(name, callbacks, appData, component);
1016}
1017