1 /* 2 * Copyright (C) 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 CLONE_RESTORE_CV_ANALYSIS_H 17 #define CLONE_RESTORE_CV_ANALYSIS_H 18 19 #include <sstream> 20 #include <string> 21 22 #include "backup_const.h" 23 #include "clone_restore_highlight.h" 24 #include "media_log.h" 25 #include "nlohmann/json.hpp" 26 #include "rdb_store.h" 27 #include "safe_map.h" 28 29 namespace OHOS::Media { 30 class CloneRestoreCVAnalysis { 31 public: 32 void Init(int32_t sceneCode, const std::string &taskId, std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, 33 std::shared_ptr<NativeRdb::RdbStore> mediaRdb, const std::string &backupRestoreDir); 34 void RestoreAlbums(CloneRestoreHighlight &cloneHighlight); 35 36 private: 37 struct AnalysisLabelInfo { 38 std::optional<int32_t> id; 39 std::optional<int32_t> fileId; 40 std::optional<int32_t> fileIdNew; 41 std::optional<int32_t> categoryId; 42 std::optional<std::string> subLabel; 43 std::optional<double> prob; 44 std::optional<std::string> feature; 45 std::optional<std::string> simResult; 46 std::optional<std::string> labelVersion; 47 std::optional<std::string> saliencySubprob; 48 std::optional<std::string> analysisVersion; 49 std::optional<int32_t> duplicateChecking; 50 ToStringAnalysisLabelInfo51 std::string ToString() const 52 { 53 std::stringstream outputStr; 54 outputStr << "AnalysisLabelInfo[" << "id: "; 55 if (id.has_value()) { outputStr << id.value(); } 56 outputStr << ", fileId: "; 57 if (fileId.has_value()) { outputStr << fileId.value(); } 58 outputStr << "]"; 59 return outputStr.str(); 60 } 61 }; 62 63 struct AnalysisSaliencyInfo { 64 std::optional<int32_t> id; 65 std::optional<int32_t> fileId; 66 std::optional<int32_t> fileIdNew; 67 std::optional<double> saliencyX; 68 std::optional<double> saliencyY; 69 std::optional<std::string> saliencyVersion; 70 std::optional<std::string> analysisVersion; 71 ToStringAnalysisSaliencyInfo72 std::string ToString() const 73 { 74 std::stringstream outputStr; 75 outputStr << "AnalysisSaliencyInfo[" << "id: "; 76 if (id.has_value()) { outputStr << id.value(); } 77 outputStr << ", fileId: "; 78 if (fileId.has_value()) { outputStr << fileId.value(); } 79 outputStr << "]"; 80 return outputStr.str(); 81 } 82 }; 83 84 struct AnalysisRecommendationInfo { 85 std::optional<int32_t> id; 86 std::optional<int32_t> fileId; 87 std::optional<int32_t> fileIdNew; 88 std::optional<int32_t> rcmdId; 89 std::optional<std::string> rcmdResolution; 90 std::optional<int32_t> rcmdScaleX; 91 std::optional<int32_t> rcmdScaleY; 92 std::optional<int32_t> rcmdScaleWidth; 93 std::optional<int32_t> rcmdScaleHeight; 94 std::optional<std::string> rcmdVersion; 95 std::optional<double> scaleX; 96 std::optional<double> scaleY; 97 std::optional<double> scaleWidth; 98 std::optional<double> scaleHeight; 99 std::optional<std::string> analysisVersion; 100 std::optional<std::string> movementCrop; 101 std::optional<std::string> movementVersion; 102 ToStringAnalysisRecommendationInfo103 std::string ToString() const 104 { 105 std::stringstream outputStr; 106 outputStr << "AnalysisRecommendationInfo[" << "id: "; 107 if (id.has_value()) { outputStr << id.value(); } 108 outputStr << ", fileId: "; 109 if (fileId.has_value()) { outputStr << fileId.value(); } 110 outputStr << "]"; 111 return outputStr.str(); 112 } 113 }; 114 115 void GetAssetMapInfos(CloneRestoreHighlight &cloneHighlight); 116 void GetAssetAlbumInfos(CloneRestoreHighlight &cloneHighlight); 117 void MoveAnalysisAssets(const std::string &srcPath, const std::string &dstPath); 118 void InsertIntoAssetMap(); 119 void InsertIntoSdMap(); 120 int32_t BatchInsertWithRetry(const std::string &tableName, 121 const std::vector<NativeRdb::ValuesBucket> &values, int64_t &rowNum); 122 void GetAnalysisLabelInfos(CloneRestoreHighlight &cloneHighlight); 123 void GetLabelRowInfo(AnalysisLabelInfo &info, std::shared_ptr<NativeRdb::ResultSet> resultSet); 124 void InsertIntoAnalysisLabel(); 125 void GetLabelInsertValue(NativeRdb::ValuesBucket &value, const AnalysisLabelInfo &info); 126 std::unordered_set<std::string> GetCommonColumns(const std::string &tableName); 127 void GetAnalysisSaliencyInfos(CloneRestoreHighlight &cloneHighlight); 128 void GetSaliencyRowInfo(AnalysisSaliencyInfo &info, std::shared_ptr<NativeRdb::ResultSet> resultSet); 129 void InsertIntoAnalysisSaliency(); 130 void GetSaliencyInsertValue(NativeRdb::ValuesBucket &value, const AnalysisSaliencyInfo &info); 131 void GetAnalysisRecommendationInfos(CloneRestoreHighlight &cloneHighlight); 132 void GetRecommendationRowInfo(AnalysisRecommendationInfo &info, std::shared_ptr<NativeRdb::ResultSet> resultSet); 133 void InsertIntoAnalysisRecommendation(); 134 void GetRecommendationInsertValue(NativeRdb::ValuesBucket &value, const AnalysisRecommendationInfo &info); 135 std::string ParsePlayInfo(const std::string &oldPlayInfo, CloneRestoreHighlight &cloneHighlight); 136 void ParseEffectline(nlohmann::json &newPlayInfo, size_t effectlineIndex, CloneRestoreHighlight &cloneHighlight); 137 void UpdateHighlightPlayInfos(CloneRestoreHighlight &cloneHighlight); 138 void ReportCloneRestoreCVAnalysisTask(); 139 int32_t GetNewAssetId(int32_t assetId); 140 std::string GetNewEffectVideoUri(const std::string &oldVideoUri); 141 std::string GetNewTransitionVideoUri(const std::string &oldVideoUri, CloneRestoreHighlight &cloneHighlight); 142 std::string GetNewPhotoUriByUri(const std::string &oldUri, CloneRestoreHighlight &cloneHighlight); 143 144 int32_t sceneCode_{-1}; 145 std::string taskId_; 146 // old media_liabrary.db 147 std::shared_ptr<NativeRdb::RdbStore> mediaRdb_; 148 // new media_liabrary.db 149 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_; 150 std::string assetPath_; 151 std::string garblePath_; 152 std::vector<std::pair<int32_t, int32_t>> assetMapDatas_; 153 std::vector<std::pair<int32_t, int32_t>> sdMapDatas_; 154 std::unordered_map<int32_t, int32_t> fileIdMap_; 155 std::unordered_map<int32_t, int32_t> albumIdMap_; 156 std::unordered_map<int32_t, int32_t> assetIdMap_; 157 std::unordered_map<std::string, std::string> assetUriMap_; 158 std::unordered_map<std::string, std::unordered_set<std::string>> intersectionMap_; 159 std::vector<AnalysisLabelInfo> labelInfos_; 160 std::vector<AnalysisSaliencyInfo> saliencyInfos_; 161 std::vector<AnalysisRecommendationInfo> recommendInfos_; 162 int64_t failCnt_{0}; 163 }; 164 } // namespace OHOS::Media 165 #endif // CLONE_RESTORE_CV_ANALYSIS_H