1 /* 2 * Copyright (c) 2025-2025 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 SEI_PARSER_HELPER_H 17 #define SEI_PARSER_HELPER_H 18 19 #include <string> 20 #include <vector> 21 22 #include "surface_type.h" 23 #include "filter/filter.h" 24 #include "buffer/avbuffer_queue.h" 25 #include "media_sync_manager.h" 26 #include "osal/task/autospinlock.h" 27 28 namespace OHOS { 29 namespace Media { 30 class SeiParserHelper; 31 struct SeiPayloadInfo; 32 struct SeiPayloadInfoGroup; 33 34 using HelperConstructFunc = std::function<std::shared_ptr<SeiParserHelper>()>; 35 36 class SeiParserHelper { 37 public: 38 virtual ~SeiParserHelper() = default; 39 40 Status ParseSeiPayload(const std::shared_ptr<AVBuffer> &buffer, std::shared_ptr<SeiPayloadInfoGroup> &group); 41 void SetPayloadTypeVec(const std::vector<int32_t> &vector); 42 43 protected: 44 SeiParserHelper() = default; 45 46 private: 47 48 static int32_t GetSeiTypeOrSize(uint8_t *&bodyPtr, const uint8_t *const maxPtr); 49 static Status FillTargetBuffer(const std::shared_ptr<AVBuffer> buffer, 50 uint8_t *&payloadPtr, const uint8_t *const maxPtr, const int32_t payloadSize); 51 52 virtual bool IsSeiNalu(uint8_t *&headerPtr) = 0; 53 bool FindNextSeiNaluPos(uint8_t *&startPtr, const uint8_t *const maxPtr); 54 Status ParseSeiRbsp( 55 uint8_t *&bodyPtr, const uint8_t *const maxPtr, const std::shared_ptr<SeiPayloadInfoGroup> &group); 56 static uint32_t GetNaluStartSeq(); 57 58 std::vector<int32_t> payloadTypeVec_{}; 59 SpinLock spinLock_; 60 }; 61 62 class AvcSeiParserHelper : public SeiParserHelper { 63 public: 64 AvcSeiParserHelper() = default; 65 66 private: 67 bool IsSeiNalu(uint8_t *&headerPtr) override; 68 }; 69 70 class HevcSeiParserHelper : public SeiParserHelper { 71 public: 72 HevcSeiParserHelper() = default; 73 74 private: 75 bool IsSeiNalu(uint8_t *&headerPtr) override; 76 }; 77 78 class SeiParserHelperFactory { 79 public: 80 static std::shared_ptr<SeiParserHelper> CreateHelper(const std::string &mimeType); 81 82 private: 83 static const std::map<std::string, HelperConstructFunc> HELPER_CONSTRUCTOR_MAP; 84 }; 85 86 struct SeiPayloadInfo { 87 int32_t payloadType; 88 std::shared_ptr<AVBuffer> payload; 89 }; 90 91 struct SeiPayloadInfoGroup { 92 int64_t playbackPosition = 0; 93 std::vector<SeiPayloadInfo> vec; 94 }; 95 96 class SeiParserListener : public IBrokerListener { 97 public: 98 explicit SeiParserListener(const std::string &mimeType, sptr<AVBufferQueueProducer> producer, 99 std::shared_ptr<Pipeline::EventReceiver> eventReceiver, bool isFlowLimited); 100 AsObject()101 sptr<IRemoteObject> AsObject() override 102 { 103 return nullptr; 104 } 105 106 void OnBufferFilled(std::shared_ptr<AVBuffer> &avBuffer) override; 107 108 void SetPayloadTypeVec(const std::vector<int32_t> &vector); 109 110 void OnInterrupted(bool isInterruptNeeded); 111 SetSyncCenter(std::shared_ptr<Pipeline::IMediaSyncCenter> syncCenter)112 void SetSyncCenter(std::shared_ptr<Pipeline::IMediaSyncCenter> syncCenter) 113 { 114 syncCenter_ = syncCenter; 115 } 116 117 Status SetSeiMessageCbStatus(bool status, const std::vector<int32_t> &payloadTypes); 118 119 private: 120 void FlowLimit(const std::shared_ptr<AVBuffer> &avBuffer); 121 122 sptr<AVBufferQueueProducer> producer_{}; 123 std::shared_ptr<SeiParserHelper> seiParserHelper_{}; 124 std::shared_ptr<Pipeline::EventReceiver> eventReceiver_{}; 125 bool isFlowLimited_ { false }; 126 std::atomic<bool> isInterruptNeeded_ { false }; 127 std::mutex mutex_ {}; 128 std::condition_variable cond_ {}; 129 std::shared_ptr<Pipeline::IMediaSyncCenter> syncCenter_; 130 int64_t startPts_ = 0; 131 std::vector<int32_t> payloadTypes_{}; 132 }; 133 } // namespace Media 134 } // namespace OHOS 135 #endif // SEI_PARSER_HELPER_H