1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HDRCODEC_SAMPLE_H 17 #define HDRCODEC_SAMPLE_H 18 19 #include <iostream> 20 #include <cstdio> 21 #include <unistd.h> 22 #include <atomic> 23 #include <fstream> 24 #include <thread> 25 #include <mutex> 26 #include <queue> 27 #include <string> 28 #include <unordered_map> 29 #include <condition_variable> 30 31 #include "native_avcodec_base.h" 32 #include "native_avdemuxer.h" 33 #include "native_avsource.h" 34 #include "native_avmuxer.h" 35 #include "securec.h" 36 #include "native_avcodec_videodecoder.h" 37 #include "native_avcodec_videoencoder.h" 38 #include "nocopyable.h" 39 #include "native_avmemory.h" 40 #include "native_avformat.h" 41 #include "native_averrors.h" 42 #include "surface/window.h" 43 #include "iconsumer_surface.h" 44 namespace OHOS { 45 namespace Media { 46 class VSignal { 47 public: 48 std::mutex inMutex_; 49 std::mutex outMutex_; 50 std::condition_variable inCond_; 51 std::condition_variable outCond_; 52 std::queue<uint32_t> inIdxQueue_; 53 std::queue<uint32_t> outIdxQueue_; 54 std::queue<OH_AVCodecBufferAttr> attrQueue_; 55 std::queue<OH_AVMemory *> inBufferQueue_; 56 std::queue<OH_AVMemory *> outBufferQueue_; 57 }; 58 59 class HDRCodecNdkSample : public NoCopyable { 60 public: 61 HDRCodecNdkSample() = default; 62 ~HDRCodecNdkSample(); 63 int32_t CreateCodec(); 64 int32_t Configure(); 65 int32_t Start(); 66 int32_t Release(); 67 void WaitForEos(); 68 void ReleaseInFile(); 69 void StopInloop(); 70 void InputFunc(); 71 void FlushBuffer(); 72 void SwitchInputFile(); 73 int32_t ReConfigure(); 74 int32_t RepeatCall(); 75 int32_t CreateVideocoder(std::string codeName, std::string enCodeName); 76 int32_t SendData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data); 77 void PtrStep(uint32_t &bufferSize, unsigned char **pBuffer, uint32_t size); 78 void PtrStepExtraRead(uint32_t &bufferSize, unsigned char **pBuffer); 79 int32_t SendDataHdr(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data); 80 int32_t SendDataH263(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data); 81 int32_t SendDataAvc(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data); 82 int32_t SendDataMpeg2(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data); 83 int32_t SendDataMpeg4(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data); 84 int32_t StartDemuxer(); 85 void GetBufferSize(); 86 void GetMpeg4BufferSize(); 87 void WriteAudioTrack(); 88 int32_t CreateDemuxerVideocoder(const char *file, std::string codeName, std::string enCodeName); 89 OH_AVDemuxer *demuxer = nullptr; 90 OH_AVMuxer *muxer = nullptr; 91 uint32_t videoTrackID = -1; 92 uint32_t audioTrackID = -1; 93 std::unique_ptr<std::thread> audioThread; 94 const char *INP_DIR = "/data/test/media/1920_1080_10_30Mb.h264"; 95 bool needEncode = false; 96 bool needTransCode = false; 97 int32_t DEFAULT_WIDTH = 3840; 98 int32_t DEFAULT_HEIGHT = 2160; 99 int32_t DEFAULT_ROTATION = 0; 100 int32_t DEFAULT_PIXEL_FORMAT = AV_PIXEL_FORMAT_NV12; 101 double DEFAULT_FRAME_RATE = 30.0; 102 const char *MIME_TYPE = OH_AVCODEC_MIMETYPE_VIDEO_AVC; 103 uint32_t REPEAT_START_STOP_BEFORE_EOS = 0; // 1200 测试用例 104 uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例 105 uint32_t REPEAT_START_FLUSH_STOP_BEFORE_EOS = 0; 106 uint32_t frameCount_ = 0; 107 uint32_t repeat_time = 0; 108 uint32_t frameCountDec = 0; 109 uint32_t frameCountEnc = 0; 110 int32_t DEFAULT_PROFILE = HEVC_PROFILE_MAIN_10; 111 bool needOutputFile; 112 VSignal *decSignal; 113 std::unique_ptr<std::thread> inputLoop_; 114 OH_AVCodec *vdec_; 115 OH_AVCodec *venc_; 116 uint32_t errorCount = 0; 117 int32_t typeDec = 0; 118 int32_t inputNum = 0; 119 bool finishLastPush = false; 120 bool DEMUXER_FLAG = false; 121 122 private: 123 OHNativeWindow *window = nullptr; 124 OH_AVCodecAsyncCallback encCb_; 125 OH_AVCodecAsyncCallback decCb_; 126 std::unique_ptr<uint8_t[]> prereadBuffer_; 127 std::unique_ptr<std::vector<uint8_t>> mpegUnit_; 128 uint32_t pPrereadBuffer_; 129 uint32_t prereadBufferSize_; 130 OH_AVSource *source = nullptr; 131 int32_t trackCount = 0; 132 int32_t fd; 133 int32_t outFd; 134 }; 135 } // namespace Media 136 } // namespace OHOS 137 138 #endif 139