10704ebd2Sopenharmony_ci/*
20704ebd2Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
30704ebd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40704ebd2Sopenharmony_ci * you may not use this file except in compliance with the License.
50704ebd2Sopenharmony_ci * You may obtain a copy of the License at
60704ebd2Sopenharmony_ci *
70704ebd2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80704ebd2Sopenharmony_ci *
90704ebd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100704ebd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110704ebd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120704ebd2Sopenharmony_ci * See the License for the specific language governing permissions and
130704ebd2Sopenharmony_ci * limitations under the License.
140704ebd2Sopenharmony_ci */
150704ebd2Sopenharmony_ci
160704ebd2Sopenharmony_ci#include "decoder_demo.h"
170704ebd2Sopenharmony_ci
180704ebd2Sopenharmony_ci#include <iostream>
190704ebd2Sopenharmony_ci#include <unistd.h>
200704ebd2Sopenharmony_ci
210704ebd2Sopenharmony_ci#include "avcodec_list.h"
220704ebd2Sopenharmony_ci#include "securec.h"
230704ebd2Sopenharmony_ci#include "wm_common.h"
240704ebd2Sopenharmony_ci#include "window.h"
250704ebd2Sopenharmony_ci#include "window_scene.h"
260704ebd2Sopenharmony_ci#include "window_option.h"
270704ebd2Sopenharmony_ci
280704ebd2Sopenharmony_ciusing namespace OHOS::DistributedHardware;
290704ebd2Sopenharmony_cistatic const int32_t ES_R[325] = {
300704ebd2Sopenharmony_ci    11895, 8109, 1578, 1616, 1313, 572, 805, 837, 755, 706, 952, 879, 13193, 422, 389, 509, 725, 465, 479, 959, 677,
310704ebd2Sopenharmony_ci    364, 541, 696, 9306, 322, 318, 767, 590, 422, 530, 403, 505, 566, 445, 508, 7783, 460, 405, 343, 451, 608, 431,
320704ebd2Sopenharmony_ci    411, 543, 487, 527, 400, 6287, 385, 418, 391, 592, 434, 412, 398, 504, 492, 479, 561, 5413, 317, 355, 422, 467,
330704ebd2Sopenharmony_ci    452, 476, 460, 490, 492, 485, 451, 5036, 312, 408, 460, 432, 502, 388, 475, 407, 544, 401, 487, 4404, 362, 378,
340704ebd2Sopenharmony_ci    427, 416, 426, 456, 414, 438, 424, 442, 444, 4310, 362, 388, 393, 390, 441, 398, 423, 369, 443, 406, 392, 4231,
350704ebd2Sopenharmony_ci    343, 363, 355, 390, 459, 371, 378, 381, 405, 392, 426, 3975, 387, 337, 393, 439, 378, 355, 374, 484, 381, 373,
360704ebd2Sopenharmony_ci    423, 3869, 312, 350, 400, 345, 356, 320, 473, 431, 386, 338, 431, 3426, 268, 315, 416, 383, 373, 381, 354, 383,
370704ebd2Sopenharmony_ci    328, 348, 418, 3715, 324, 361, 331, 350, 302, 409, 377, 359, 384, 334, 326, 3439, 266, 324, 329, 353, 405, 303,
380704ebd2Sopenharmony_ci    357, 332, 292, 361, 333, 3542, 294, 284, 247, 331, 306, 322, 287, 367, 341, 276, 258, 3980, 246, 245, 259, 309,
390704ebd2Sopenharmony_ci    333, 250, 275, 334, 281, 253, 371, 3640, 213, 231, 301, 302, 228, 289, 290, 281, 201, 284, 277, 4242, 205, 328,
400704ebd2Sopenharmony_ci    237, 283, 295, 266, 230, 321, 348, 212, 308, 4103, 259, 238, 245, 298, 330, 265, 271, 287, 267, 286, 290, 3856,
410704ebd2Sopenharmony_ci    269, 242, 209, 314, 267, 278, 280, 314, 250, 433, 238, 3654, 195, 246, 301, 298, 250, 270, 320, 269, 305, 258,
420704ebd2Sopenharmony_ci    368, 3810, 231, 212, 279, 289, 252, 303, 287, 295, 206, 264, 349, 4071, 242, 296, 271, 231, 307, 265, 254, 267,
430704ebd2Sopenharmony_ci    317, 232, 348, 4077, 259, 222, 268, 235, 324, 266, 256, 312, 246, 248, 325, 4000, 266, 201, 230, 293, 264, 265,
440704ebd2Sopenharmony_ci    273, 301, 304, 253, 266, 3978, 228, 232, 250, 248, 281, 219, 243, 293, 287, 253, 328, 3719
450704ebd2Sopenharmony_ci};
460704ebd2Sopenharmony_ci
470704ebd2Sopenharmony_cistatic const int32_t ES_W[183] = {
480704ebd2Sopenharmony_ci    2111, 109091, 9316, 969, 13656, 1349, 959, 10484, 1219, 14839, 1092, 23378, 1653, 1725, 1526, 8500, 15407,
490704ebd2Sopenharmony_ci    2058, 1346, 21066, 3758, 1734, 1950, 19955, 3997, 1732, 1784, 22687, 4392, 2448, 2180, 17456, 3930, 1851,
500704ebd2Sopenharmony_ci    1802, 24227, 4393, 2639, 2778, 18314, 4023, 2392, 2283, 20566, 4118, 2664, 2013, 18964, 2624, 45258, 5860,
510704ebd2Sopenharmony_ci    4124, 3473, 27772, 4687, 3140, 2939, 26288, 3808, 2967, 2823, 27219, 3943, 3242, 2667, 27372, 3522, 2899,
520704ebd2Sopenharmony_ci    2316, 26608, 3284, 2853, 2285, 19581, 2894, 2436, 24898, 4002, 2876, 2807, 25730, 3903, 2874, 2975, 26309,
530704ebd2Sopenharmony_ci    3771, 2763, 2666, 23404, 3826, 2410, 2644, 24629, 4145, 3121, 2878, 50773, 7040, 3945, 3292, 30828, 5210,
540704ebd2Sopenharmony_ci    2883, 3277, 31501, 4809, 3068, 3220, 30746, 4715, 3461, 3583, 32278, 4798, 3398, 3389, 31404, 4921, 3382,
550704ebd2Sopenharmony_ci    3766, 31821, 5848, 3860, 4047, 37642, 5793, 4271, 4094, 29853, 6163, 4399, 4063, 32151, 6038, 4332, 4041,
560704ebd2Sopenharmony_ci    30390, 5679, 4098, 3921, 29401, 5307, 3996, 3945, 45997, 7060, 3716, 4183, 26357, 6190, 3714, 4250, 29086,
570704ebd2Sopenharmony_ci    5929, 3491, 4489, 27772, 6656, 4219, 4348, 25851, 6088, 3617, 4477, 25722, 6303, 3856, 4208, 25348, 5896,
580704ebd2Sopenharmony_ci    3816, 4521, 22462, 5914, 3673, 4594, 18091, 6474, 3878, 4492, 10890, 4823, 4148
590704ebd2Sopenharmony_ci};
600704ebd2Sopenharmony_ci
610704ebd2Sopenharmony_ciusing namespace OHOS;
620704ebd2Sopenharmony_ciusing namespace OHOS::MediaAVCodec;
630704ebd2Sopenharmony_ciusing namespace std;
640704ebd2Sopenharmony_cinamespace {
650704ebd2Sopenharmony_ci    constexpr double DEFAULT_FRAME_RATE = 30.0;
660704ebd2Sopenharmony_ci    constexpr uint32_t MAX_INPUT_BUFFER_SIZE = 30000;
670704ebd2Sopenharmony_ci    constexpr uint32_t FRAME_DURATION_US = 33000;
680704ebd2Sopenharmony_ci    constexpr uint32_t VIDEO_DATA_FORMAT_NV12 = 2;
690704ebd2Sopenharmony_ci    constexpr uint32_t VIDEO_DATA_FORMAT_RGBA = 5;
700704ebd2Sopenharmony_ci    constexpr uint32_t SLEEP_THREE_SECOND = 3;
710704ebd2Sopenharmony_ci    constexpr uint32_t INDEX_CONSTANT = 10000;
720704ebd2Sopenharmony_ci    const string CODEC_NAME_H264 = "OMX_hisi_video_encoder_avc";
730704ebd2Sopenharmony_ci    const string CODEC_NAME_MPEG4 = "avenc_mpeg4";
740704ebd2Sopenharmony_ci}
750704ebd2Sopenharmony_ci
760704ebd2Sopenharmony_civoid VDecDemo::RunCase()
770704ebd2Sopenharmony_ci{
780704ebd2Sopenharmony_ci    CheckCodecType();
790704ebd2Sopenharmony_ci    CreateVdec();
800704ebd2Sopenharmony_ci    Media::Format format;
810704ebd2Sopenharmony_ci    format.PutIntValue("width", width_);
820704ebd2Sopenharmony_ci    format.PutIntValue("height", height_);
830704ebd2Sopenharmony_ci    if (isW) {
840704ebd2Sopenharmony_ci        format.PutIntValue("pixel_format", VIDEO_DATA_FORMAT_NV12);
850704ebd2Sopenharmony_ci    } else {
860704ebd2Sopenharmony_ci        format.PutIntValue("pixel_format", VIDEO_DATA_FORMAT_RGBA);
870704ebd2Sopenharmony_ci    }
880704ebd2Sopenharmony_ci    format.PutDoubleValue("frame_rate", DEFAULT_FRAME_RATE);
890704ebd2Sopenharmony_ci    format.PutIntValue("max_input_size", MAX_INPUT_BUFFER_SIZE);
900704ebd2Sopenharmony_ci    Configure(format);
910704ebd2Sopenharmony_ci    SetSurface();
920704ebd2Sopenharmony_ci    Prepare();
930704ebd2Sopenharmony_ci    Start();
940704ebd2Sopenharmony_ci    sleep(SLEEP_THREE_SECOND);
950704ebd2Sopenharmony_ci    Stop();
960704ebd2Sopenharmony_ci    Release();
970704ebd2Sopenharmony_ci}
980704ebd2Sopenharmony_ci
990704ebd2Sopenharmony_ciint32_t VDecDemo::CreateVdec()
1000704ebd2Sopenharmony_ci{
1010704ebd2Sopenharmony_ci    if (isW) {
1020704ebd2Sopenharmony_ci        vdec_ = VideoDecoderFactory::CreateByMime("video/avc");
1030704ebd2Sopenharmony_ci    } else {
1040704ebd2Sopenharmony_ci        vdec_ = VideoDecoderFactory::CreateByMime("video/mp4v-es");
1050704ebd2Sopenharmony_ci    }
1060704ebd2Sopenharmony_ci
1070704ebd2Sopenharmony_ci    signal_ = make_shared<VDecSignal>();
1080704ebd2Sopenharmony_ci    cb_ = make_unique<VDecDemoCallback>(signal_);
1090704ebd2Sopenharmony_ci    vdec_->SetCallback(cb_);
1100704ebd2Sopenharmony_ci    return 0;
1110704ebd2Sopenharmony_ci}
1120704ebd2Sopenharmony_ci
1130704ebd2Sopenharmony_ciint32_t VDecDemo::Configure(const Media::Format &format)
1140704ebd2Sopenharmony_ci{
1150704ebd2Sopenharmony_ci    return vdec_->Configure(format);
1160704ebd2Sopenharmony_ci}
1170704ebd2Sopenharmony_ci
1180704ebd2Sopenharmony_ciint32_t VDecDemo::Prepare()
1190704ebd2Sopenharmony_ci{
1200704ebd2Sopenharmony_ci    return vdec_->Prepare();
1210704ebd2Sopenharmony_ci}
1220704ebd2Sopenharmony_ci
1230704ebd2Sopenharmony_ciint32_t VDecDemo::Start()
1240704ebd2Sopenharmony_ci{
1250704ebd2Sopenharmony_ci    isRunning_.store(true);
1260704ebd2Sopenharmony_ci
1270704ebd2Sopenharmony_ci    testFile_ = std::make_unique<std::ifstream>();
1280704ebd2Sopenharmony_ci    testFile_->open("/data/media/video.es", std::ios::in | std::ios::binary);
1290704ebd2Sopenharmony_ci
1300704ebd2Sopenharmony_ci    inputLoop_ = make_unique<thread>(&VDecDemo::InputFunc, this);
1310704ebd2Sopenharmony_ci    outputLoop_ = make_unique<thread>(&VDecDemo::OutputFunc, this);
1320704ebd2Sopenharmony_ci    return vdec_->Start();
1330704ebd2Sopenharmony_ci}
1340704ebd2Sopenharmony_ci
1350704ebd2Sopenharmony_ciint32_t VDecDemo::Stop()
1360704ebd2Sopenharmony_ci{
1370704ebd2Sopenharmony_ci    isRunning_.store(false);
1380704ebd2Sopenharmony_ci
1390704ebd2Sopenharmony_ci    if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1400704ebd2Sopenharmony_ci        {
1410704ebd2Sopenharmony_ci            unique_lock<mutex> inLock(signal_->inMutex_);
1420704ebd2Sopenharmony_ci            signal_->inQueue_.push(INDEX_CONSTANT);
1430704ebd2Sopenharmony_ci            signal_->inCond_.notify_all();
1440704ebd2Sopenharmony_ci        }
1450704ebd2Sopenharmony_ci        inputLoop_->join();
1460704ebd2Sopenharmony_ci        inputLoop_.reset();
1470704ebd2Sopenharmony_ci    }
1480704ebd2Sopenharmony_ci
1490704ebd2Sopenharmony_ci    if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1500704ebd2Sopenharmony_ci        {
1510704ebd2Sopenharmony_ci            unique_lock<mutex> outLock(signal_->outMutex_);
1520704ebd2Sopenharmony_ci            signal_->outQueue_.push(INDEX_CONSTANT);
1530704ebd2Sopenharmony_ci            signal_->outCond_.notify_all();
1540704ebd2Sopenharmony_ci        }
1550704ebd2Sopenharmony_ci        outputLoop_->join();
1560704ebd2Sopenharmony_ci        outputLoop_.reset();
1570704ebd2Sopenharmony_ci    }
1580704ebd2Sopenharmony_ci
1590704ebd2Sopenharmony_ci    return vdec_->Stop();
1600704ebd2Sopenharmony_ci}
1610704ebd2Sopenharmony_ci
1620704ebd2Sopenharmony_ciint32_t VDecDemo::Flush()
1630704ebd2Sopenharmony_ci{
1640704ebd2Sopenharmony_ci    return vdec_->Flush();
1650704ebd2Sopenharmony_ci}
1660704ebd2Sopenharmony_ci
1670704ebd2Sopenharmony_ciint32_t VDecDemo::Reset()
1680704ebd2Sopenharmony_ci{
1690704ebd2Sopenharmony_ci    return vdec_->Reset();
1700704ebd2Sopenharmony_ci}
1710704ebd2Sopenharmony_ci
1720704ebd2Sopenharmony_ciint32_t VDecDemo::Release()
1730704ebd2Sopenharmony_ci{
1740704ebd2Sopenharmony_ci    return vdec_->Release();
1750704ebd2Sopenharmony_ci}
1760704ebd2Sopenharmony_ci
1770704ebd2Sopenharmony_civoid VDecDemo::SetOutputSurface(sptr<Surface> surface)
1780704ebd2Sopenharmony_ci{
1790704ebd2Sopenharmony_ci    surface_ = surface;
1800704ebd2Sopenharmony_ci}
1810704ebd2Sopenharmony_ci
1820704ebd2Sopenharmony_civoid VDecDemo::SetWindowSize(uint32_t width, uint32_t height)
1830704ebd2Sopenharmony_ci{
1840704ebd2Sopenharmony_ci    width_ = width;
1850704ebd2Sopenharmony_ci    height_ = height;
1860704ebd2Sopenharmony_ci}
1870704ebd2Sopenharmony_ci
1880704ebd2Sopenharmony_ciint32_t VDecDemo::SetSurface()
1890704ebd2Sopenharmony_ci{
1900704ebd2Sopenharmony_ci    return vdec_->SetOutputSurface(surface_);
1910704ebd2Sopenharmony_ci}
1920704ebd2Sopenharmony_ci
1930704ebd2Sopenharmony_civoid VDecDemo::CheckCodecType()
1940704ebd2Sopenharmony_ci{
1950704ebd2Sopenharmony_ci    std::vector<std::string> localCodecArray;
1960704ebd2Sopenharmony_ci    std::shared_ptr<MediaAVCodec::AVCodecList> codecList = MediaAVCodec::AVCodecListFactory::CreateAVCodecList();
1970704ebd2Sopenharmony_ci    const std::vector<std::string> encoderName = {std::string(MediaAVCodec::CodecMimeType::VIDEO_AVC),
1980704ebd2Sopenharmony_ci                                                  std::string(MediaAVCodec::CodecMimeType::VIDEO_HEVC)};
1990704ebd2Sopenharmony_ci    for (const auto &coder : encoderName) {
2000704ebd2Sopenharmony_ci        MediaAVCodec::CapabilityData *capData = codecList->GetCapability(coder, true,
2010704ebd2Sopenharmony_ci            MediaAVCodec::AVCodecCategory::AVCODEC_HARDWARE);
2020704ebd2Sopenharmony_ci        std::string mimeType = capData->mimeType;
2030704ebd2Sopenharmony_ci        localCodecArray.push_back(mimeType);
2040704ebd2Sopenharmony_ci    }
2050704ebd2Sopenharmony_ci
2060704ebd2Sopenharmony_ci    if (std::find(localCodecArray.begin(), localCodecArray.end(),
2070704ebd2Sopenharmony_ci        CODEC_NAME_H264) != localCodecArray.end()) {
2080704ebd2Sopenharmony_ci        DHLOGI("device is W");
2090704ebd2Sopenharmony_ci        isW = true;
2100704ebd2Sopenharmony_ci    } else if (std::find(localCodecArray.begin(), localCodecArray.end(),
2110704ebd2Sopenharmony_ci        CODEC_NAME_MPEG4) != localCodecArray.end()) {
2120704ebd2Sopenharmony_ci        DHLOGI("device is R");
2130704ebd2Sopenharmony_ci        isW = false;
2140704ebd2Sopenharmony_ci    }
2150704ebd2Sopenharmony_ci}
2160704ebd2Sopenharmony_ci
2170704ebd2Sopenharmony_ciconst int32_t* VDecDemo::GetFrameLen()
2180704ebd2Sopenharmony_ci{
2190704ebd2Sopenharmony_ci    const int32_t* frameLen = nullptr;
2200704ebd2Sopenharmony_ci    if (isW) {
2210704ebd2Sopenharmony_ci        frameLen = ES_W;
2220704ebd2Sopenharmony_ci        defaultFrameCount_ = sizeof(ES_W) / sizeof(ES_W[0]);
2230704ebd2Sopenharmony_ci    } else {
2240704ebd2Sopenharmony_ci        frameLen = ES_R;
2250704ebd2Sopenharmony_ci        defaultFrameCount_ = sizeof(ES_R) / sizeof(ES_R[0]);
2260704ebd2Sopenharmony_ci    }
2270704ebd2Sopenharmony_ci    return frameLen;
2280704ebd2Sopenharmony_ci}
2290704ebd2Sopenharmony_ci
2300704ebd2Sopenharmony_civoid VDecDemo::InputFunc()
2310704ebd2Sopenharmony_ci{
2320704ebd2Sopenharmony_ci    const int32_t *frameLen = GetFrameLen();
2330704ebd2Sopenharmony_ci
2340704ebd2Sopenharmony_ci    while (isRunning_.load()) {
2350704ebd2Sopenharmony_ci        unique_lock<mutex> lock(signal_->inMutex_);
2360704ebd2Sopenharmony_ci        signal_->inCond_.wait(
2370704ebd2Sopenharmony_ci            lock, [this]() { return signal_->inQueue_.size() > 0 && signal_->availableInputBufferQueue_.size() > 0; });
2380704ebd2Sopenharmony_ci
2390704ebd2Sopenharmony_ci        if (!isRunning_.load()) {
2400704ebd2Sopenharmony_ci            break;
2410704ebd2Sopenharmony_ci        }
2420704ebd2Sopenharmony_ci
2430704ebd2Sopenharmony_ci        uint32_t index = signal_->inQueue_.front();
2440704ebd2Sopenharmony_ci        std::shared_ptr<Media::AVSharedMemory> buffer = signal_->availableInputBufferQueue_.front();
2450704ebd2Sopenharmony_ci        if(buffer == nullptr) {
2460704ebd2Sopenharmony_ci            break;
2470704ebd2Sopenharmony_ci        }
2480704ebd2Sopenharmony_ci
2490704ebd2Sopenharmony_ci        char *fileBuffer = static_cast<char *>(malloc(sizeof(char) * (*frameLen) + 1));
2500704ebd2Sopenharmony_ci        if (fileBuffer == nullptr) {
2510704ebd2Sopenharmony_ci            break;
2520704ebd2Sopenharmony_ci        }
2530704ebd2Sopenharmony_ci
2540704ebd2Sopenharmony_ci        (void)testFile_->read(fileBuffer, *frameLen);
2550704ebd2Sopenharmony_ci        if (memcpy_s(buffer->GetBase(), buffer->GetSize(), fileBuffer, *frameLen) != EOK) {
2560704ebd2Sopenharmony_ci            free(fileBuffer);
2570704ebd2Sopenharmony_ci            DHLOGI("Fatal: memcpy fail");
2580704ebd2Sopenharmony_ci            break;
2590704ebd2Sopenharmony_ci        }
2600704ebd2Sopenharmony_ci
2610704ebd2Sopenharmony_ci        AVCodecBufferInfo info;
2620704ebd2Sopenharmony_ci        info.size = *frameLen;
2630704ebd2Sopenharmony_ci        info.offset = 0;
2640704ebd2Sopenharmony_ci        info.presentationTimeUs = timeStamp_;
2650704ebd2Sopenharmony_ci
2660704ebd2Sopenharmony_ci        int32_t ret = 0;
2670704ebd2Sopenharmony_ci        if (isFirstFrame_) {
2680704ebd2Sopenharmony_ci            ret = vdec_->QueueInputBuffer(index, info, AVCODEC_BUFFER_FLAG_CODEC_DATA);
2690704ebd2Sopenharmony_ci            isFirstFrame_ = false;
2700704ebd2Sopenharmony_ci        } else {
2710704ebd2Sopenharmony_ci            ret = vdec_->QueueInputBuffer(index, info, AVCODEC_BUFFER_FLAG_NONE);
2720704ebd2Sopenharmony_ci        }
2730704ebd2Sopenharmony_ci
2740704ebd2Sopenharmony_ci        free(fileBuffer);
2750704ebd2Sopenharmony_ci        frameLen++;
2760704ebd2Sopenharmony_ci        timeStamp_ += FRAME_DURATION_US;
2770704ebd2Sopenharmony_ci        signal_->inQueue_.pop();
2780704ebd2Sopenharmony_ci        signal_->availableInputBufferQueue_.pop();
2790704ebd2Sopenharmony_ci
2800704ebd2Sopenharmony_ci        frameCount_++;
2810704ebd2Sopenharmony_ci        if (frameCount_ == defaultFrameCount_) {
2820704ebd2Sopenharmony_ci            DHLOGI("Finish decode, exit");
2830704ebd2Sopenharmony_ci            break;
2840704ebd2Sopenharmony_ci        }
2850704ebd2Sopenharmony_ci
2860704ebd2Sopenharmony_ci        if (ret != 0) {
2870704ebd2Sopenharmony_ci            DHLOGI("Fatal error, exit");
2880704ebd2Sopenharmony_ci            break;
2890704ebd2Sopenharmony_ci        }
2900704ebd2Sopenharmony_ci    }
2910704ebd2Sopenharmony_ci}
2920704ebd2Sopenharmony_ci
2930704ebd2Sopenharmony_civoid VDecDemo::OutputFunc()
2940704ebd2Sopenharmony_ci{
2950704ebd2Sopenharmony_ci    while (isRunning_.load()) {
2960704ebd2Sopenharmony_ci        unique_lock<mutex> lock(signal_->outMutex_);
2970704ebd2Sopenharmony_ci        signal_->outCond_.wait(lock, [this]() { return signal_->outQueue_.size() > 0; });
2980704ebd2Sopenharmony_ci
2990704ebd2Sopenharmony_ci        if (!isRunning_.load()) {
3000704ebd2Sopenharmony_ci            break;
3010704ebd2Sopenharmony_ci        }
3020704ebd2Sopenharmony_ci
3030704ebd2Sopenharmony_ci        uint32_t index = signal_->outQueue_.front();
3040704ebd2Sopenharmony_ci        if (vdec_->ReleaseOutputBuffer(index, true) != 0) {
3050704ebd2Sopenharmony_ci            DHLOGI("Fatal: ReleaseOutputBuffer fail");
3060704ebd2Sopenharmony_ci            break;
3070704ebd2Sopenharmony_ci        }
3080704ebd2Sopenharmony_ci
3090704ebd2Sopenharmony_ci        signal_->outQueue_.pop();
3100704ebd2Sopenharmony_ci    }
3110704ebd2Sopenharmony_ci}
3120704ebd2Sopenharmony_ci
3130704ebd2Sopenharmony_civoid VDecDemoCallback::OnError(AVCodecErrorType errorType, int32_t errorCode)
3140704ebd2Sopenharmony_ci{
3150704ebd2Sopenharmony_ci    DHLOGI("Error received, errorType: %{public}d, errorCode: %{public}d", errorType, errorCode);
3160704ebd2Sopenharmony_ci}
3170704ebd2Sopenharmony_ci
3180704ebd2Sopenharmony_civoid VDecDemoCallback::OnOutputFormatChanged(const Media::Format &format)
3190704ebd2Sopenharmony_ci{
3200704ebd2Sopenharmony_ci    DHLOGI("OnOutputFormatChanged received");
3210704ebd2Sopenharmony_ci}
3220704ebd2Sopenharmony_ci
3230704ebd2Sopenharmony_civoid VDecDemoCallback::OnInputBufferAvailable(uint32_t index, std::shared_ptr<Media::AVSharedMemory> buffer)
3240704ebd2Sopenharmony_ci{
3250704ebd2Sopenharmony_ci    DHLOGI("OnInputBufferAvailable received, index: %{public}d", index);
3260704ebd2Sopenharmony_ci    unique_lock<mutex> lock(signal_->inMutex_);
3270704ebd2Sopenharmony_ci    signal_->inQueue_.push(index);
3280704ebd2Sopenharmony_ci    signal_->availableInputBufferQueue_.push(buffer);
3290704ebd2Sopenharmony_ci    signal_->inCond_.notify_all();
3300704ebd2Sopenharmony_ci}
3310704ebd2Sopenharmony_ci
3320704ebd2Sopenharmony_civoid VDecDemoCallback::OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
3330704ebd2Sopenharmony_ci                                               std::shared_ptr<Media::AVSharedMemory> buffer)
3340704ebd2Sopenharmony_ci{
3350704ebd2Sopenharmony_ci    DHLOGI("OnOutputBufferAvailable received, index: %{public}d", index);
3360704ebd2Sopenharmony_ci    unique_lock<mutex> lock(signal_->outMutex_);
3370704ebd2Sopenharmony_ci    signal_->outQueue_.push(index);
3380704ebd2Sopenharmony_ci    signal_->outCond_.notify_all();
3390704ebd2Sopenharmony_ci}
340