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 SOURCE_H 17 #define SOURCE_H 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 #include "libavformat/avformat.h" 23 #include "libavutil/opt.h" 24 #ifdef __cplusplus 25 } 26 #endif 27 28 #include <thread> 29 #include "avcodec_errors.h" 30 #include "hevc_parser_manager.h" 31 #include "source_plugin.h" 32 #include "plugin_types.h" 33 #include "plugin_buffer.h" 34 #include "plugin_event.h" 35 #include "plugin_definition.h" 36 #include "sourcebase.h" 37 38 namespace OHOS { 39 namespace MediaAVCodec { 40 namespace Plugin { 41 struct SourceRegister : PackageRegister { 42 public: 43 std::string name = "custom register"; 44 Status AddPlugin(const PluginDefBase& def) override; 45 Status AddPackage(const PackageDef& def) override; 46 std::shared_ptr<SourcePlugin> sourcePlugin {nullptr}; 47 private: 48 std::shared_ptr<PackageDef> packageDef; 49 }; 50 51 class Source : public SourceBase { 52 public: 53 Source(); 54 ~Source(); 55 56 int32_t Init(std::string& uri) override; 57 int32_t GetTrackCount(uint32_t &trackCount) override; 58 int32_t GetSourceFormat(Media::Format &format) override; 59 int32_t GetTrackFormat(Media::Format &format, uint32_t trackIndex) override; 60 uintptr_t GetSourceAddr() override; 61 62 private: 63 struct CustomIOContext { 64 SourcePlugin* sourcePlugin = nullptr; 65 size_t offset = 0; 66 size_t position = 0; 67 bool eof = false; 68 uint64_t fileSize = 0; 69 AVIOContext* avioContext = nullptr; 70 }; 71 72 std::shared_ptr<AVFormatContext> formatContext_; 73 std::shared_ptr<AVInputFormat> inputFormat_; 74 std::shared_ptr<SourcePlugin> sourcePlugin_; 75 std::shared_ptr<SourceRegister> register_; 76 CustomIOContext customIOContext_; 77 AVIOContext* avioContext_ = nullptr; 78 void* handler_ = nullptr; 79 std::shared_ptr<HevcParserManager> hevcParser_ {nullptr}; 80 int32_t LoadDynamicPlugin(const std::string& path); 81 int32_t SniffInputFormat(); 82 static int AVReadPacket(void* opaque, uint8_t* buf, int bufSize); 83 static int64_t AVSeek(void* opaque, int64_t offset, int whence); 84 void InitAVIOContext(int flags); 85 int32_t InitAVFormatContext(); 86 void GetVideoFirstKeyFrame(); 87 void ParseHEVCMetadataInfo(const AVStream& avStream, Media::Format &format); 88 void ParseHDRVividCUVVInfo(Media::Format &format); 89 bool IsAVTrack(const AVStream &avStream); 90 AVPacket *firstFrame_ = nullptr; 91 bool hasHevc_ = false; 92 }; 93 } // namespace Plugin 94 } // namespace MediaAVCodec 95 } // namespace OHOS 96 #endif // AVSOURCE_H