1 /* 2 * Copyright (c) 2022-2024 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 OHOS_DECODE_DATA_PROCESS_H 17 #define OHOS_DECODE_DATA_PROCESS_H 18 19 #include <chrono> 20 #include <cstdint> 21 #include <queue> 22 #include <deque> 23 #include <thread> 24 #include <vector> 25 26 #include "avcodec_common.h" 27 #include "avcodec_video_decoder.h" 28 #include "buffer/avsharedmemory.h" 29 #include "event_handler.h" 30 #include "meta/format.h" 31 #include "ibuffer_consumer_listener.h" 32 #include "iconsumer_surface.h" 33 #include "avcodec_errors.h" 34 #include "securec.h" 35 #include "surface.h" 36 37 #include "abstract_data_process.h" 38 #include "data_buffer.h" 39 #include "dcamera_codec_event.h" 40 #include "dcamera_pipeline_source.h" 41 #include "distributed_camera_errno.h" 42 #include "image_common_type.h" 43 #include "dcamera_utils_tools.h" 44 45 namespace OHOS { 46 namespace DistributedHardware { 47 class DCameraPipelineSource; 48 class DecodeVideoCallback; 49 50 class DecodeDataProcess : public AbstractDataProcess, public std::enable_shared_from_this<DecodeDataProcess> { 51 public: DecodeDataProcess(const std::shared_ptr<AppExecFwk::EventHandler> & pipeEventHandler,const std::weak_ptr<DCameraPipelineSource> & callbackPipSource)52 DecodeDataProcess(const std::shared_ptr<AppExecFwk::EventHandler>& pipeEventHandler, 53 const std::weak_ptr<DCameraPipelineSource>& callbackPipSource) 54 : pipeSrcEventHandler_(pipeEventHandler), callbackPipelineSource_(callbackPipSource) {} 55 ~DecodeDataProcess() override; 56 57 int32_t InitNode(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig, 58 VideoConfigParams& processedConfig) override; 59 int32_t ProcessData(std::vector<std::shared_ptr<DataBuffer>>& inputBuffers) override; 60 void ReleaseProcessNode() override; 61 62 void OnError(); 63 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<Media::AVSharedMemory> buffer); 64 void OnOutputFormatChanged(const Media::Format &format); 65 void OnOutputBufferAvailable(uint32_t index, const MediaAVCodec::AVCodecBufferInfo& info, 66 const MediaAVCodec::AVCodecBufferFlag& flag, std::shared_ptr<Media::AVSharedMemory> buffer); 67 void GetDecoderOutputBuffer(const sptr<IConsumerSurface>& surface); 68 VideoConfigParams GetSourceConfig() const; 69 VideoConfigParams GetTargetConfig() const; 70 void OnSurfaceOutputBufferAvailable(const sptr<IConsumerSurface>& surface); 71 void AlignFirstFrameTime(); 72 73 int32_t GetProperty(const std::string& propertyName, PropertyCarrier& propertyCarrier) override; 74 75 private: 76 bool IsInDecoderRange(const VideoConfigParams& curConfig); 77 bool IsConvertible(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig); 78 void InitCodecEvent(); 79 int32_t InitDecoder(); 80 int32_t ConfigureVideoDecoder(); 81 int32_t InitDecoderMetadataFormat(); 82 int32_t SetDecoderOutputSurface(); 83 int32_t StartVideoDecoder(); 84 int32_t StopVideoDecoder(); 85 void ReleaseVideoDecoder(); 86 void ReleaseDecoderSurface(); 87 void ReleaseCodecEvent(); 88 void BeforeDecodeDump(uint8_t *buffer, size_t bufSize); 89 int32_t FeedDecoderInputBuffer(); 90 int64_t GetDecoderTimeStamp(); 91 void IncreaseWaitDecodeCnt(); 92 void ReduceWaitDecodeCnt(); 93 void CopyDecodedImage(const sptr<SurfaceBuffer>& surBuf, int32_t alignedWidth, int32_t alignedHeight); 94 bool IsCorrectSurfaceBuffer(const sptr<SurfaceBuffer>& surBuf, int32_t alignedWidth, int32_t alignedHeight); 95 void PostOutputDataBuffers(std::shared_ptr<DataBuffer>& outputBuffer); 96 int32_t DecodeDone(std::vector<std::shared_ptr<DataBuffer>>& outputBuffers); 97 void StartEventHandler(); 98 99 private: 100 constexpr static int32_t VIDEO_DECODER_QUEUE_MAX = 1000; 101 constexpr static int32_t MAX_YUV420_BUFFER_SIZE = 1920 * 1080 * 3 / 2 * 2; 102 constexpr static int32_t MAX_RGB32_BUFFER_SIZE = 1920 * 1080 * 4 * 2; 103 constexpr static int32_t MAX_BUFFER_SIZE = 1920 * 1080 * 4 * 2; 104 constexpr static int32_t MIN_FRAME_RATE = 0; 105 constexpr static double MAX_FRAME_RATE = 30; 106 constexpr static int32_t MIN_VIDEO_WIDTH = 320; 107 constexpr static int32_t MIN_VIDEO_HEIGHT = 240; 108 constexpr static int32_t MAX_VIDEO_WIDTH = 1920; 109 constexpr static int32_t MAX_VIDEO_HEIGHT = 1080; 110 constexpr static int32_t FIRST_FRAME_INPUT_NUM = 2; 111 constexpr static int32_t RGB32_MEMORY_COEFFICIENT = 4; 112 constexpr static int32_t YUV_BYTES_PER_PIXEL = 3; 113 constexpr static int32_t Y2UV_RATIO = 2; 114 constexpr static int32_t BUFFER_MAX_SIZE = 50 * 1024 * 1024; 115 constexpr static int32_t ALIGNED_WIDTH_MAX_SIZE = 10000; 116 constexpr static uint32_t MEMORY_RATIO_UV = 1; 117 118 std::shared_ptr<AppExecFwk::EventHandler> pipeSrcEventHandler_; 119 std::weak_ptr<DCameraPipelineSource> callbackPipelineSource_; 120 std::mutex mtxDecoderLock_; 121 std::mutex mtxDecoderState_; 122 std::mutex mtxHoldCount_; 123 std::mutex mtxDequeLock_; 124 VideoConfigParams sourceConfig_; 125 VideoConfigParams targetConfig_; 126 VideoConfigParams processedConfig_; 127 std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> videoDecoder_ = nullptr; 128 std::shared_ptr<MediaAVCodec::AVCodecCallback> decodeVideoCallback_ = nullptr; 129 sptr<IConsumerSurface> decodeConsumerSurface_ = nullptr; 130 sptr<Surface> decodeProducerSurface_ = nullptr; 131 sptr<IBufferConsumerListener> decodeSurfaceListener_ = nullptr; 132 133 std::atomic<bool> isDecoderProcess_ = false; 134 int32_t waitDecoderOutputCount_ = 0; 135 int32_t alignedHeight_ = 0; 136 int64_t lastFeedDecoderInputBufferTimeUs_ = 0; 137 int64_t outputTimeStampUs_ = 0; 138 std::string processType_; 139 Media::Format metadataFormat_; 140 Media::Format decodeOutputFormat_; 141 MediaAVCodec::AVCodecBufferInfo outputInfo_; 142 std::queue<std::shared_ptr<DataBuffer>> inputBuffersQueue_; 143 std::queue<std::shared_ptr<Media::AVSharedMemory>> availableInputBufferQueue_; 144 std::queue<uint32_t> availableInputIndexsQueue_; 145 std::deque<DCameraFrameInfo> frameInfoDeque_; 146 FILE *dumpDecBeforeFile_ = nullptr; 147 FILE *dumpDecAfterFile_ = nullptr; 148 149 std::mutex eventMutex_; 150 std::thread eventThread_; 151 std::condition_variable eventCon_; 152 std::shared_ptr<AppExecFwk::EventHandler> decEventHandler_ = nullptr; 153 }; 154 } // namespace DistributedHardware 155 } // namespace OHOS 156 #endif // OHOS_DECODE_DATA_PROCESS_H 157