SoftMPEG2.cpp revision 9e18c578276311318201e2423c93728cf81f0400
1/*
2 * Copyright 2015 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 "SoftMPEG2"
19#include <utils/Log.h>
20
21#include "iv_datatypedef.h"
22#include "iv.h"
23#include "ivd.h"
24#include "impeg2d.h"
25#include "SoftMPEG2.h"
26
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/MediaDefs.h>
29#include <OMX_VideoExt.h>
30
31namespace android {
32
33#define componentName                   "video_decoder.mpeg2"
34#define codingType                      OMX_VIDEO_CodingMPEG2
35#define CODEC_MIME_TYPE                 MEDIA_MIMETYPE_VIDEO_MPEG2
36
37/** Function and structure definitions to keep code similar for each codec */
38#define ivdec_api_function              impeg2d_api_function
39#define ivdext_init_ip_t                impeg2d_init_ip_t
40#define ivdext_init_op_t                impeg2d_init_op_t
41#define ivdext_fill_mem_rec_ip_t        impeg2d_fill_mem_rec_ip_t
42#define ivdext_fill_mem_rec_op_t        impeg2d_fill_mem_rec_op_t
43#define ivdext_ctl_set_num_cores_ip_t   impeg2d_ctl_set_num_cores_ip_t
44#define ivdext_ctl_set_num_cores_op_t   impeg2d_ctl_set_num_cores_op_t
45
46#define IVDEXT_CMD_CTL_SET_NUM_CORES    \
47        (IVD_CONTROL_API_COMMAND_TYPE_T)IMPEG2D_CMD_CTL_SET_NUM_CORES
48
49static const CodecProfileLevel kProfileLevels[] = {
50    { OMX_VIDEO_MPEG2ProfileSimple, OMX_VIDEO_MPEG2LevelLL  },
51    { OMX_VIDEO_MPEG2ProfileSimple, OMX_VIDEO_MPEG2LevelML  },
52    { OMX_VIDEO_MPEG2ProfileSimple, OMX_VIDEO_MPEG2LevelH14 },
53    { OMX_VIDEO_MPEG2ProfileSimple, OMX_VIDEO_MPEG2LevelHL  },
54
55    { OMX_VIDEO_MPEG2ProfileMain  , OMX_VIDEO_MPEG2LevelLL  },
56    { OMX_VIDEO_MPEG2ProfileMain  , OMX_VIDEO_MPEG2LevelML  },
57    { OMX_VIDEO_MPEG2ProfileMain  , OMX_VIDEO_MPEG2LevelH14 },
58    { OMX_VIDEO_MPEG2ProfileMain  , OMX_VIDEO_MPEG2LevelHL  },
59};
60
61SoftMPEG2::SoftMPEG2(
62        const char *name,
63        const OMX_CALLBACKTYPE *callbacks,
64        OMX_PTR appData,
65        OMX_COMPONENTTYPE **component)
66    : SoftVideoDecoderOMXComponent(
67            name, componentName, codingType,
68            kProfileLevels, ARRAY_SIZE(kProfileLevels),
69            320 /* width */, 240 /* height */, callbacks,
70            appData, component),
71      mMemRecords(NULL),
72      mFlushOutBuffer(NULL),
73      mOmxColorFormat(OMX_COLOR_FormatYUV420Planar),
74      mIvColorFormat(IV_YUV_420P),
75      mNewWidth(mWidth),
76      mNewHeight(mHeight),
77      mChangingResolution(false) {
78    initPorts(kNumBuffers, INPUT_BUF_SIZE, kNumBuffers, CODEC_MIME_TYPE);
79
80    // If input dump is enabled, then open create an empty file
81    GENERATE_FILE_NAMES();
82    CREATE_DUMP_FILE(mInFile);
83
84    CHECK_EQ(initDecoder(), (status_t)OK);
85}
86
87SoftMPEG2::~SoftMPEG2() {
88    CHECK_EQ(deInitDecoder(), (status_t)OK);
89}
90
91
92static size_t getMinTimestampIdx(OMX_S64 *pNTimeStamp, bool *pIsTimeStampValid) {
93    OMX_S64 minTimeStamp = LLONG_MAX;
94    int idx = -1;
95    for (size_t i = 0; i < MAX_TIME_STAMPS; i++) {
96        if (pIsTimeStampValid[i]) {
97            if (pNTimeStamp[i] < minTimeStamp) {
98                minTimeStamp = pNTimeStamp[i];
99                idx = i;
100            }
101        }
102    }
103    return idx;
104}
105
106static size_t GetCPUCoreCount() {
107    long cpuCoreCount = 1;
108#if defined(_SC_NPROCESSORS_ONLN)
109    cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
110#else
111    // _SC_NPROC_ONLN must be defined...
112    cpuCoreCount = sysconf(_SC_NPROC_ONLN);
113#endif
114    CHECK(cpuCoreCount >= 1);
115    ALOGV("Number of CPU cores: %ld", cpuCoreCount);
116    return (size_t)cpuCoreCount;
117}
118
119void SoftMPEG2::logVersion() {
120    ivd_ctl_getversioninfo_ip_t s_ctl_ip;
121    ivd_ctl_getversioninfo_op_t s_ctl_op;
122    UWORD8 au1_buf[512];
123    IV_API_CALL_STATUS_T status;
124
125    s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
126    s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_GETVERSION;
127    s_ctl_ip.u4_size = sizeof(ivd_ctl_getversioninfo_ip_t);
128    s_ctl_op.u4_size = sizeof(ivd_ctl_getversioninfo_op_t);
129    s_ctl_ip.pv_version_buffer = au1_buf;
130    s_ctl_ip.u4_version_buffer_size = sizeof(au1_buf);
131
132    status = ivdec_api_function(mCodecCtx, (void *)&s_ctl_ip, (void *)&s_ctl_op);
133
134    if (status != IV_SUCCESS) {
135        ALOGE("Error in getting version number: 0x%x",
136                s_ctl_op.u4_error_code);
137    } else {
138        ALOGV("Ittiam decoder version number: %s",
139                (char *)s_ctl_ip.pv_version_buffer);
140    }
141    return;
142}
143
144status_t SoftMPEG2::setParams(size_t stride) {
145    ivd_ctl_set_config_ip_t s_ctl_ip;
146    ivd_ctl_set_config_op_t s_ctl_op;
147    IV_API_CALL_STATUS_T status;
148    s_ctl_ip.u4_disp_wd = (UWORD32)stride;
149    s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;
150
151    s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
152    s_ctl_ip.e_vid_dec_mode = IVD_DECODE_FRAME;
153    s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
154    s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
155    s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
156    s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);
157
158    ALOGV("Set the run-time (dynamic) parameters stride = %zu", stride);
159    status = ivdec_api_function(mCodecCtx, (void *)&s_ctl_ip, (void *)&s_ctl_op);
160
161    if (status != IV_SUCCESS) {
162        ALOGE("Error in setting the run-time parameters: 0x%x",
163                s_ctl_op.u4_error_code);
164
165        return UNKNOWN_ERROR;
166    }
167    return OK;
168}
169
170status_t SoftMPEG2::resetPlugin() {
171    mIsInFlush = false;
172    mReceivedEOS = false;
173    memset(mTimeStamps, 0, sizeof(mTimeStamps));
174    memset(mTimeStampsValid, 0, sizeof(mTimeStampsValid));
175
176    /* Initialize both start and end times */
177    gettimeofday(&mTimeStart, NULL);
178    gettimeofday(&mTimeEnd, NULL);
179
180    return OK;
181}
182
183status_t SoftMPEG2::resetDecoder() {
184    ivd_ctl_reset_ip_t s_ctl_ip;
185    ivd_ctl_reset_op_t s_ctl_op;
186    IV_API_CALL_STATUS_T status;
187
188    s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
189    s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_RESET;
190    s_ctl_ip.u4_size = sizeof(ivd_ctl_reset_ip_t);
191    s_ctl_op.u4_size = sizeof(ivd_ctl_reset_op_t);
192
193    status = ivdec_api_function(mCodecCtx, (void *)&s_ctl_ip, (void *)&s_ctl_op);
194    if (IV_SUCCESS != status) {
195        ALOGE("Error in reset: 0x%x", s_ctl_op.u4_error_code);
196        return UNKNOWN_ERROR;
197    }
198
199    /* Set the run-time (dynamic) parameters */
200    setParams(outputBufferWidth());
201
202    /* Set number of cores/threads to be used by the codec */
203    setNumCores();
204
205    return OK;
206}
207
208status_t SoftMPEG2::setNumCores() {
209    ivdext_ctl_set_num_cores_ip_t s_set_cores_ip;
210    ivdext_ctl_set_num_cores_op_t s_set_cores_op;
211    IV_API_CALL_STATUS_T status;
212    s_set_cores_ip.e_cmd = IVD_CMD_VIDEO_CTL;
213    s_set_cores_ip.e_sub_cmd = IVDEXT_CMD_CTL_SET_NUM_CORES;
214    s_set_cores_ip.u4_num_cores = MIN(mNumCores, CODEC_MAX_NUM_CORES);
215    s_set_cores_ip.u4_size = sizeof(ivdext_ctl_set_num_cores_ip_t);
216    s_set_cores_op.u4_size = sizeof(ivdext_ctl_set_num_cores_op_t);
217
218    status = ivdec_api_function(mCodecCtx, (void *)&s_set_cores_ip, (void *)&s_set_cores_op);
219    if (IV_SUCCESS != status) {
220        ALOGE("Error in setting number of cores: 0x%x",
221                s_set_cores_op.u4_error_code);
222        return UNKNOWN_ERROR;
223    }
224    return OK;
225}
226
227status_t SoftMPEG2::setFlushMode() {
228    IV_API_CALL_STATUS_T status;
229    ivd_ctl_flush_ip_t s_video_flush_ip;
230    ivd_ctl_flush_op_t s_video_flush_op;
231
232    s_video_flush_ip.e_cmd = IVD_CMD_VIDEO_CTL;
233    s_video_flush_ip.e_sub_cmd = IVD_CMD_CTL_FLUSH;
234    s_video_flush_ip.u4_size = sizeof(ivd_ctl_flush_ip_t);
235    s_video_flush_op.u4_size = sizeof(ivd_ctl_flush_op_t);
236
237    /* Set the decoder in Flush mode, subsequent decode() calls will flush */
238    status = ivdec_api_function(
239            mCodecCtx, (void *)&s_video_flush_ip, (void *)&s_video_flush_op);
240
241    if (status != IV_SUCCESS) {
242        ALOGE("Error in setting the decoder in flush mode: (%d) 0x%x", status,
243                s_video_flush_op.u4_error_code);
244        return UNKNOWN_ERROR;
245    }
246
247    mWaitForI = true;
248    mIsInFlush = true;
249    return OK;
250}
251
252status_t SoftMPEG2::initDecoder() {
253    IV_API_CALL_STATUS_T status;
254
255    UWORD32 u4_num_reorder_frames;
256    UWORD32 u4_num_ref_frames;
257    UWORD32 u4_share_disp_buf;
258
259    mNumCores = GetCPUCoreCount();
260    mWaitForI = true;
261
262    /* Initialize number of ref and reorder modes (for MPEG2) */
263    u4_num_reorder_frames = 16;
264    u4_num_ref_frames = 16;
265    u4_share_disp_buf = 0;
266
267    uint32_t displayStride = outputBufferWidth();
268    uint32_t displayHeight = outputBufferHeight();
269    uint32_t displaySizeY = displayStride * displayHeight;
270
271    {
272        iv_num_mem_rec_ip_t s_num_mem_rec_ip;
273        iv_num_mem_rec_op_t s_num_mem_rec_op;
274
275        s_num_mem_rec_ip.u4_size = sizeof(s_num_mem_rec_ip);
276        s_num_mem_rec_op.u4_size = sizeof(s_num_mem_rec_op);
277        s_num_mem_rec_ip.e_cmd = IV_CMD_GET_NUM_MEM_REC;
278
279        status = ivdec_api_function(
280                mCodecCtx, (void *)&s_num_mem_rec_ip, (void *)&s_num_mem_rec_op);
281        if (IV_SUCCESS != status) {
282            ALOGE("Error in getting mem records: 0x%x",
283                    s_num_mem_rec_op.u4_error_code);
284            return UNKNOWN_ERROR;
285        }
286
287        mNumMemRecords = s_num_mem_rec_op.u4_num_mem_rec;
288    }
289
290    mMemRecords = (iv_mem_rec_t *)ivd_aligned_malloc(
291            128, mNumMemRecords * sizeof(iv_mem_rec_t));
292    if (mMemRecords == NULL) {
293        ALOGE("Allocation failure");
294        return NO_MEMORY;
295    }
296
297    memset(mMemRecords, 0, mNumMemRecords * sizeof(iv_mem_rec_t));
298
299    {
300        size_t i;
301        ivdext_fill_mem_rec_ip_t s_fill_mem_ip;
302        ivdext_fill_mem_rec_op_t s_fill_mem_op;
303        iv_mem_rec_t *ps_mem_rec;
304
305        s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_size =
306            sizeof(ivdext_fill_mem_rec_ip_t);
307
308        s_fill_mem_ip.u4_share_disp_buf = u4_share_disp_buf;
309        s_fill_mem_ip.e_output_format = mIvColorFormat;
310        s_fill_mem_ip.u4_deinterlace = 1;
311        s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.e_cmd = IV_CMD_FILL_NUM_MEM_REC;
312        s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location = mMemRecords;
313        s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_wd = displayStride;
314        s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_ht = displayHeight;
315        s_fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_size =
316            sizeof(ivdext_fill_mem_rec_op_t);
317
318        ps_mem_rec = mMemRecords;
319        for (i = 0; i < mNumMemRecords; i++) {
320            ps_mem_rec[i].u4_size = sizeof(iv_mem_rec_t);
321        }
322
323        status = ivdec_api_function(
324                mCodecCtx, (void *)&s_fill_mem_ip, (void *)&s_fill_mem_op);
325
326        if (IV_SUCCESS != status) {
327            ALOGE("Error in filling mem records: 0x%x",
328                    s_fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_error_code);
329            return UNKNOWN_ERROR;
330        }
331        mNumMemRecords =
332            s_fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_num_mem_rec_filled;
333
334        ps_mem_rec = mMemRecords;
335
336        for (i = 0; i < mNumMemRecords; i++) {
337            ps_mem_rec->pv_base = ivd_aligned_malloc(
338                    ps_mem_rec->u4_mem_alignment, ps_mem_rec->u4_mem_size);
339            if (ps_mem_rec->pv_base == NULL) {
340                ALOGE("Allocation failure for memory record #%zu of size %u",
341                        i, ps_mem_rec->u4_mem_size);
342                status = IV_FAIL;
343                return NO_MEMORY;
344            }
345
346            ps_mem_rec++;
347        }
348    }
349
350    /* Initialize the decoder */
351    {
352        ivdext_init_ip_t s_init_ip;
353        ivdext_init_op_t s_init_op;
354
355        void *dec_fxns = (void *)ivdec_api_function;
356
357        s_init_ip.s_ivd_init_ip_t.u4_size = sizeof(ivdext_init_ip_t);
358        s_init_ip.s_ivd_init_ip_t.e_cmd = (IVD_API_COMMAND_TYPE_T)IV_CMD_INIT;
359        s_init_ip.s_ivd_init_ip_t.pv_mem_rec_location = mMemRecords;
360        s_init_ip.s_ivd_init_ip_t.u4_frm_max_wd = displayStride;
361        s_init_ip.s_ivd_init_ip_t.u4_frm_max_ht = displayHeight;
362
363        s_init_ip.u4_share_disp_buf = u4_share_disp_buf;
364        s_init_ip.u4_deinterlace = 1;
365
366        s_init_op.s_ivd_init_op_t.u4_size = sizeof(s_init_op);
367
368        s_init_ip.s_ivd_init_ip_t.u4_num_mem_rec = mNumMemRecords;
369        s_init_ip.s_ivd_init_ip_t.e_output_format = mIvColorFormat;
370
371        mCodecCtx = (iv_obj_t *)mMemRecords[0].pv_base;
372        mCodecCtx->pv_fxns = dec_fxns;
373        mCodecCtx->u4_size = sizeof(iv_obj_t);
374
375        status = ivdec_api_function(mCodecCtx, (void *)&s_init_ip, (void *)&s_init_op);
376        if (status != IV_SUCCESS) {
377            ALOGE("Error in init: 0x%x",
378                    s_init_op.s_ivd_init_op_t.u4_error_code);
379            return UNKNOWN_ERROR;
380        }
381    }
382
383    /* Reset the plugin state */
384    resetPlugin();
385
386    /* Set the run time (dynamic) parameters */
387    setParams(displayStride);
388
389    /* Set number of cores/threads to be used by the codec */
390    setNumCores();
391
392    /* Get codec version */
393    logVersion();
394
395    /* Allocate internal picture buffer */
396    uint32_t bufferSize = displaySizeY * 3 / 2;
397    mFlushOutBuffer = (uint8_t *)ivd_aligned_malloc(128, bufferSize);
398    if (NULL == mFlushOutBuffer) {
399        ALOGE("Could not allocate flushOutputBuffer of size %u", bufferSize);
400        return NO_MEMORY;
401    }
402
403    mInitNeeded = false;
404    mFlushNeeded = false;
405    return OK;
406}
407
408status_t SoftMPEG2::deInitDecoder() {
409    size_t i;
410
411    if (mMemRecords) {
412        iv_mem_rec_t *ps_mem_rec;
413
414        ps_mem_rec = mMemRecords;
415        for (i = 0; i < mNumMemRecords; i++) {
416            if (ps_mem_rec->pv_base) {
417                ivd_aligned_free(ps_mem_rec->pv_base);
418            }
419            ps_mem_rec++;
420        }
421        ivd_aligned_free(mMemRecords);
422        mMemRecords = NULL;
423    }
424
425    if (mFlushOutBuffer) {
426        ivd_aligned_free(mFlushOutBuffer);
427        mFlushOutBuffer = NULL;
428    }
429
430    mInitNeeded = true;
431    mChangingResolution = false;
432
433    return OK;
434}
435
436status_t SoftMPEG2::reInitDecoder() {
437    status_t ret;
438
439    deInitDecoder();
440
441    ret = initDecoder();
442    if (OK != ret) {
443        ALOGE("Create failure");
444        deInitDecoder();
445        return NO_MEMORY;
446    }
447    return OK;
448}
449
450void SoftMPEG2::onReset() {
451    SoftVideoDecoderOMXComponent::onReset();
452
453    mWaitForI = true;
454
455    resetDecoder();
456    resetPlugin();
457}
458
459OMX_ERRORTYPE SoftMPEG2::internalSetParameter(OMX_INDEXTYPE index, const OMX_PTR params) {
460    const uint32_t oldWidth = mWidth;
461    const uint32_t oldHeight = mHeight;
462    OMX_ERRORTYPE ret = SoftVideoDecoderOMXComponent::internalSetParameter(index, params);
463    if (mWidth != oldWidth || mHeight != oldHeight) {
464        reInitDecoder();
465    }
466    return ret;
467}
468
469bool SoftMPEG2::setDecodeArgs(
470        ivd_video_decode_ip_t *ps_dec_ip,
471        ivd_video_decode_op_t *ps_dec_op,
472        OMX_BUFFERHEADERTYPE *inHeader,
473        OMX_BUFFERHEADERTYPE *outHeader,
474        size_t timeStampIx) {
475    size_t sizeY = outputBufferWidth() * outputBufferHeight();
476    size_t sizeUV;
477
478    ps_dec_ip->u4_size = sizeof(ivd_video_decode_ip_t);
479    ps_dec_op->u4_size = sizeof(ivd_video_decode_op_t);
480
481    ps_dec_ip->e_cmd = IVD_CMD_VIDEO_DECODE;
482
483    /* When in flush and after EOS with zero byte input,
484     * inHeader is set to zero. Hence check for non-null */
485    if (inHeader) {
486        ps_dec_ip->u4_ts = timeStampIx;
487        ps_dec_ip->pv_stream_buffer = inHeader->pBuffer
488                + inHeader->nOffset;
489        ps_dec_ip->u4_num_Bytes = inHeader->nFilledLen;
490    } else {
491        ps_dec_ip->u4_ts = 0;
492        ps_dec_ip->pv_stream_buffer = NULL;
493        ps_dec_ip->u4_num_Bytes = 0;
494    }
495
496    sizeUV = sizeY / 4;
497    ps_dec_ip->s_out_buffer.u4_min_out_buf_size[0] = sizeY;
498    ps_dec_ip->s_out_buffer.u4_min_out_buf_size[1] = sizeUV;
499    ps_dec_ip->s_out_buffer.u4_min_out_buf_size[2] = sizeUV;
500
501    uint8_t *pBuf;
502    if (outHeader) {
503        if (outHeader->nAllocLen < sizeY + (sizeUV * 2)) {
504            android_errorWriteLog(0x534e4554, "27569635");
505            return false;
506        }
507        pBuf = outHeader->pBuffer;
508    } else {
509        // mFlushOutBuffer always has the right size.
510        pBuf = mFlushOutBuffer;
511    }
512
513    ps_dec_ip->s_out_buffer.pu1_bufs[0] = pBuf;
514    ps_dec_ip->s_out_buffer.pu1_bufs[1] = pBuf + sizeY;
515    ps_dec_ip->s_out_buffer.pu1_bufs[2] = pBuf + sizeY + sizeUV;
516    ps_dec_ip->s_out_buffer.u4_num_bufs = 3;
517    return true;
518}
519void SoftMPEG2::onPortFlushCompleted(OMX_U32 portIndex) {
520    /* Once the output buffers are flushed, ignore any buffers that are held in decoder */
521    if (kOutputPortIndex == portIndex) {
522        setFlushMode();
523
524        while (true) {
525            ivd_video_decode_ip_t s_dec_ip;
526            ivd_video_decode_op_t s_dec_op;
527            IV_API_CALL_STATUS_T status;
528            size_t sizeY, sizeUV;
529
530            setDecodeArgs(&s_dec_ip, &s_dec_op, NULL, NULL, 0);
531
532            status = ivdec_api_function(mCodecCtx, (void *)&s_dec_ip, (void *)&s_dec_op);
533            if (0 == s_dec_op.u4_output_present) {
534                resetPlugin();
535                break;
536            }
537        }
538    }
539}
540
541void SoftMPEG2::onQueueFilled(OMX_U32 portIndex) {
542    UNUSED(portIndex);
543
544    if (mOutputPortSettingsChange != NONE) {
545        return;
546    }
547
548    List<BufferInfo *> &inQueue = getPortQueue(kInputPortIndex);
549    List<BufferInfo *> &outQueue = getPortQueue(kOutputPortIndex);
550
551    /* If input EOS is seen and decoder is not in flush mode,
552     * set the decoder in flush mode.
553     * There can be a case where EOS is sent along with last picture data
554     * In that case, only after decoding that input data, decoder has to be
555     * put in flush. This case is handled here  */
556
557    if (mReceivedEOS && !mIsInFlush) {
558        setFlushMode();
559    }
560
561    while (!outQueue.empty()) {
562        BufferInfo *inInfo;
563        OMX_BUFFERHEADERTYPE *inHeader;
564
565        BufferInfo *outInfo;
566        OMX_BUFFERHEADERTYPE *outHeader;
567        size_t timeStampIx;
568
569        inInfo = NULL;
570        inHeader = NULL;
571
572        if (!mIsInFlush) {
573            if (!inQueue.empty()) {
574                inInfo = *inQueue.begin();
575                inHeader = inInfo->mHeader;
576            } else {
577                break;
578            }
579        }
580
581        outInfo = *outQueue.begin();
582        outHeader = outInfo->mHeader;
583        outHeader->nFlags = 0;
584        outHeader->nTimeStamp = 0;
585        outHeader->nOffset = 0;
586
587        if (inHeader != NULL && (inHeader->nFlags & OMX_BUFFERFLAG_EOS)) {
588            mReceivedEOS = true;
589            if (inHeader->nFilledLen == 0) {
590                inQueue.erase(inQueue.begin());
591                inInfo->mOwnedByUs = false;
592                notifyEmptyBufferDone(inHeader);
593                inHeader = NULL;
594                setFlushMode();
595            }
596        }
597
598        // When there is an init required and the decoder is not in flush mode,
599        // update output port's definition and reinitialize decoder.
600        if (mInitNeeded && !mIsInFlush) {
601            bool portWillReset = false;
602            handlePortSettingsChange(&portWillReset, mNewWidth, mNewHeight);
603
604            CHECK_EQ(reInitDecoder(), (status_t)OK);
605            return;
606        }
607
608        /* Get a free slot in timestamp array to hold input timestamp */
609        {
610            size_t i;
611            timeStampIx = 0;
612            for (i = 0; i < MAX_TIME_STAMPS; i++) {
613                if (!mTimeStampsValid[i]) {
614                    timeStampIx = i;
615                    break;
616                }
617            }
618            if (inHeader != NULL) {
619                mTimeStampsValid[timeStampIx] = true;
620                mTimeStamps[timeStampIx] = inHeader->nTimeStamp;
621            }
622        }
623
624        {
625            ivd_video_decode_ip_t s_dec_ip;
626            ivd_video_decode_op_t s_dec_op;
627            WORD32 timeDelay, timeTaken;
628            size_t sizeY, sizeUV;
629
630            if (!setDecodeArgs(&s_dec_ip, &s_dec_op, inHeader, outHeader, timeStampIx)) {
631                ALOGE("Decoder arg setup failed");
632                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
633                return;
634            }
635            // If input dump is enabled, then write to file
636            DUMP_TO_FILE(mInFile, s_dec_ip.pv_stream_buffer, s_dec_ip.u4_num_Bytes);
637
638            if (s_dec_ip.u4_num_Bytes > 0) {
639                char *ptr = (char *)s_dec_ip.pv_stream_buffer;
640            }
641
642            GETTIME(&mTimeStart, NULL);
643            /* Compute time elapsed between end of previous decode()
644             * to start of current decode() */
645            TIME_DIFF(mTimeEnd, mTimeStart, timeDelay);
646
647            IV_API_CALL_STATUS_T status;
648            status = ivdec_api_function(mCodecCtx, (void *)&s_dec_ip, (void *)&s_dec_op);
649
650            bool unsupportedDimensions = (IMPEG2D_UNSUPPORTED_DIMENSIONS == s_dec_op.u4_error_code);
651            bool resChanged = (IVD_RES_CHANGED == (s_dec_op.u4_error_code & 0xFF));
652
653            GETTIME(&mTimeEnd, NULL);
654            /* Compute time taken for decode() */
655            TIME_DIFF(mTimeStart, mTimeEnd, timeTaken);
656
657            ALOGV("timeTaken=%6d delay=%6d numBytes=%6d", timeTaken, timeDelay,
658                   s_dec_op.u4_num_bytes_consumed);
659            if (s_dec_op.u4_frame_decoded_flag && !mFlushNeeded) {
660                mFlushNeeded = true;
661            }
662
663            if ((inHeader != NULL) && (1 != s_dec_op.u4_frame_decoded_flag)) {
664                /* If the input did not contain picture data, then ignore
665                 * the associated timestamp */
666                mTimeStampsValid[timeStampIx] = false;
667            }
668
669            // This is needed to handle CTS DecoderTest testCodecResetsMPEG2WithoutSurface,
670            // which is not sending SPS/PPS after port reconfiguration and flush to the codec.
671            if (unsupportedDimensions && !mFlushNeeded) {
672                bool portWillReset = false;
673                handlePortSettingsChange(&portWillReset, s_dec_op.u4_pic_wd, s_dec_op.u4_pic_ht);
674
675                CHECK_EQ(reInitDecoder(), (status_t)OK);
676
677                if (setDecodeArgs(&s_dec_ip, &s_dec_op, inHeader, outHeader, timeStampIx)) {
678                    ivdec_api_function(mCodecCtx, (void *)&s_dec_ip, (void *)&s_dec_op);
679                }
680                return;
681            }
682
683            // If the decoder is in the changing resolution mode and there is no output present,
684            // that means the switching is done and it's ready to reset the decoder and the plugin.
685            if (mChangingResolution && !s_dec_op.u4_output_present) {
686                mChangingResolution = false;
687                resetDecoder();
688                resetPlugin();
689                continue;
690            }
691
692            if (unsupportedDimensions || resChanged) {
693                mChangingResolution = true;
694                if (mFlushNeeded) {
695                    setFlushMode();
696                }
697
698                if (unsupportedDimensions) {
699                    mNewWidth = s_dec_op.u4_pic_wd;
700                    mNewHeight = s_dec_op.u4_pic_ht;
701                    mInitNeeded = true;
702                }
703                continue;
704            }
705
706            if ((0 < s_dec_op.u4_pic_wd) && (0 < s_dec_op.u4_pic_ht)) {
707                uint32_t width = s_dec_op.u4_pic_wd;
708                uint32_t height = s_dec_op.u4_pic_ht;
709                bool portWillReset = false;
710                handlePortSettingsChange(&portWillReset, width, height);
711
712                if (portWillReset) {
713                    resetDecoder();
714                    return;
715                }
716            }
717
718            if (s_dec_op.u4_output_present) {
719                size_t timeStampIdx;
720                outHeader->nFilledLen = (mWidth * mHeight * 3) / 2;
721
722                timeStampIdx = getMinTimestampIdx(mTimeStamps, mTimeStampsValid);
723                outHeader->nTimeStamp = mTimeStamps[timeStampIdx];
724                mTimeStampsValid[timeStampIdx] = false;
725
726                /* mWaitForI waits for the first I picture. Once made FALSE, it
727                   has to remain false till explicitly set to TRUE. */
728                mWaitForI = mWaitForI && !(IV_I_FRAME == s_dec_op.e_pic_type);
729
730                if (mWaitForI) {
731                    s_dec_op.u4_output_present = false;
732                } else {
733                    ALOGV("Output timestamp: %lld, res: %ux%u",
734                            (long long)outHeader->nTimeStamp, mWidth, mHeight);
735                    DUMP_TO_FILE(mOutFile, outHeader->pBuffer, outHeader->nFilledLen);
736                    outInfo->mOwnedByUs = false;
737                    outQueue.erase(outQueue.begin());
738                    outInfo = NULL;
739                    notifyFillBufferDone(outHeader);
740                    outHeader = NULL;
741                }
742            } else {
743                /* If in flush mode and no output is returned by the codec,
744                 * then come out of flush mode */
745                mIsInFlush = false;
746
747                /* If EOS was recieved on input port and there is no output
748                 * from the codec, then signal EOS on output port */
749                if (mReceivedEOS) {
750                    outHeader->nFilledLen = 0;
751                    outHeader->nFlags |= OMX_BUFFERFLAG_EOS;
752
753                    outInfo->mOwnedByUs = false;
754                    outQueue.erase(outQueue.begin());
755                    outInfo = NULL;
756                    notifyFillBufferDone(outHeader);
757                    outHeader = NULL;
758                    resetPlugin();
759                }
760            }
761        }
762
763        // TODO: Handle more than one picture data
764        if (inHeader != NULL) {
765            inInfo->mOwnedByUs = false;
766            inQueue.erase(inQueue.begin());
767            inInfo = NULL;
768            notifyEmptyBufferDone(inHeader);
769            inHeader = NULL;
770        }
771    }
772}
773
774}  // namespace android
775
776android::SoftOMXComponent *createSoftOMXComponent(
777        const char *name, const OMX_CALLBACKTYPE *callbacks, OMX_PTR appData,
778        OMX_COMPONENTTYPE **component) {
779    return new android::SoftMPEG2(name, callbacks, appData, component);
780}
781