1 /*
2  * Copyright (C) 2022 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 FRAMEWORKS_SERVICES_THUMBNAIL_SERVICE_INCLUDE_THUMBNAIL_CONST_H_
17 #define FRAMEWORKS_SERVICES_THUMBNAIL_SERVICE_INCLUDE_THUMBNAIL_CONST_H_
18 
19 #include "medialibrary_db_const.h"
20 
21 #include <unordered_map>
22 
23 namespace OHOS {
24 namespace Media {
25 constexpr int32_t THUMBNAIL_VERSION = 0;
26 constexpr int32_t DEFAULT_YEAR_SIZE = 64;
27 constexpr int32_t DEFAULT_MTH_SIZE = 128;
28 constexpr int32_t DEFAULT_THUMB_SIZE = 256;
29 constexpr int32_t MAX_DEFAULT_THUMB_SIZE = 768;
30 constexpr int32_t DEFAULT_LCD_SIZE = 1080;
31 constexpr uint32_t INT32_MAX_VALUE_LENGTH = 10;
32 constexpr int32_t VERTICAL_ANGLE = 90;
33 constexpr int32_t STRAIGHT_ANGLE = 180;
34 enum class ThumbnailType : int32_t {
35     LCD,
36     THUMB,
37     MTH,
38     YEAR,
39     THUMB_ASTC,
40     MTH_ASTC,
41     YEAR_ASTC,
42     LCD_EX,
43     THUMB_EX,
44 };
45 
46 enum class GenerateScene : int32_t {
47     LOCAL = 0,
48     CLOUD,
49     BACKGROUND,
50     FOREGROUND,
51     RESTORE,
52     UPGRADE,
53 };
54 
55 enum class LoadSourceType : int32_t {
56     LOCAL_PHOTO = 0,
57     CLOUD_THUMB,
58     CLOUD_LCD,
59     CLOUD_PHOTO,
60 };
61 
62 enum class ThumbnailReady : int32_t {
63     GENERATE_THUMB_LATER,
64     GENERATE_THUMB_NOW,
65     GENERATE_THUMB_RETRY,
66     GENERATE_THUMB_COMPLETED,
67     THUMB_TO_UPLOAD,
68     THUMB_UPLOAD_COMPLETED,
69     THUMB_UPGRADE,
70 };
71 
72 enum class LcdReady : int64_t {
73     GENERATE_LCD_LATER,
74     GENERATE_LCD_FAILED,
75     GENERATE_LCD_COMPLETED,
76 };
77 
78 const std::unordered_map<ThumbnailType, std::string> TYPE_NAME_MAP = {
79     { ThumbnailType::LCD, "LCD" },
80     { ThumbnailType::THUMB, "THUMB" },
81     { ThumbnailType::MTH, "MTH" },
82     { ThumbnailType::YEAR, "YEAR" },
83     { ThumbnailType::THUMB_ASTC, "THUMB_ASTC" },
84     { ThumbnailType::MTH_ASTC, "MTH_ASTC" },
85     { ThumbnailType::YEAR_ASTC, "YEAR_ASTC" },
86 };
87 
88 constexpr uint32_t DEVICE_UDID_LENGTH = 65;
89 
90 constexpr int32_t THUMBNAIL_LCD_GENERATE_THRESHOLD = 5000;
91 constexpr int32_t THUMBNAIL_LCD_AGING_THRESHOLD = 10000;
92 constexpr int32_t WAIT_FOR_MS = 1000;
93 constexpr int32_t WAIT_FOR_SECOND = 3;
94 
95 constexpr float EPSILON = 1e-6;
96 constexpr int32_t SHORT_SIDE_THRESHOLD = 350;
97 constexpr int32_t MAXIMUM_SHORT_SIDE_THRESHOLD = 1050;
98 constexpr int32_t LCD_SHORT_SIDE_THRESHOLD = 512;
99 constexpr int32_t LCD_LONG_SIDE_THRESHOLD = 1920;
100 constexpr int32_t MAXIMUM_LCD_LONG_SIDE = 4096;
101 constexpr int32_t ASPECT_RATIO_THRESHOLD = 3;
102 constexpr int32_t MIN_COMPRESS_BUF_SIZE = 8192;
103 constexpr int32_t DECODE_SCALE_BASE = 2;
104 constexpr int32_t FLAT_ANGLE = 180;
105 constexpr int32_t THUMBNAIL_GENERATE_BATCH_COUNT = 200;
106 constexpr int32_t ASTC_GENERATE_COUNT_AFTER_RESTORE = 500;
107 constexpr int32_t READY_TEMPERATURE_LEVEL = 4;
108 constexpr int32_t EVEN_BASE_NUMBER = 2;
109 const std::string DEFAULT_EXIF_ORIENTATION = "1";
110 
111 const std::string THUMBNAIL_LCD_SUFFIX = "LCD";     // The size fit to screen
112 const std::string THUMBNAIL_THUMB_SUFFIX = "THM";   // The size which height is 256 and width is 256
113 const std::string THUMBNAIL_THUMBASTC_SUFFIX = "THM_ASTC";
114 const std::string THUMBNAIL_MTH_SUFFIX = "MTH";     // The size which height is 128 and width is 128
115 const std::string THUMBNAIL_YEAR_SUFFIX = "YEAR";   // The size which height is 64 and width is 64
116 const std::string THUMBNAIL_LCD_EX_SUFFIX = "THM_EX/LCD";
117 const std::string THUMBNAIL_THUMB_EX_SUFFIX = "THM_EX/THM";
118 
119 const std::string FILE_URI_PREX = "file://";
120 
121 const std::string PHOTO_URI_PREFIX = "file://media/Photo/";
122 
123 const std::string THUMBNAIL_FORMAT = "image/jpeg";
124 const std::string THUMBASTC_FORMAT = "image/astc/4*4";
125 constexpr uint8_t THUMBNAIL_MID = 90;
126 constexpr uint8_t THUMBNAIL_HIGH = 100;
127 constexpr uint8_t ASTC_LOW_QUALITY = 20;
128 
129 constexpr uint32_t THUMBNAIL_QUERY_MAX = 2000;
130 constexpr int64_t AV_FRAME_TIME = 0;
131 
132 constexpr uint8_t NUMBER_HINT_1 = 1;
133 
134 constexpr int32_t DEFAULT_ORIGINAL = -1;
135 
136 const std::string DYNAMIC_RANGE = "decodeDynamicRange";
137 
138 const std::string THUMBNAIL_OPERN_KEYWORD = "operation";
139 const std::string THUMBNAIL_OPER = "oper";
140 const std::string THUMBNAIL_HEIGHT = "height";
141 const std::string THUMBNAIL_WIDTH = "width";
142 const std::string THUMBNAIL_PATH = "path";
143 
144 // create thumbnail in close operation
145 const std::string CLOSE_CREATE_THUMB_STATUS = "create_thumbnail_sync_status";
146 const int32_t CREATE_THUMB_SYNC_STATUS = 1;
147 const int32_t CREATE_THUMB_ASYNC_STATUS = 0;
148 
149 constexpr float FLOAT_EPSILON = 1e-6;
150 
151 // request photo type
152 const std::string REQUEST_PHOTO_TYPE = "requestPhotoType";
153 
154 const int32_t CLOUD_PHOTO_POSITION = 2;
155 const int32_t CLOUD_THUMB_STATUS_DOWNLOAD = 0;
156 const int32_t CLOUD_THUMBNAIL_DOWNLOAD_FINISH_NUMBER = 1000;
157 const std::string RDB_QUERY_COUNT = "count";
158 
159 const int32_t THUMBNAIL_READY_FAILED = 2;
160 
161 // LCD that is over 2MB would not be uploaded
162 const size_t LCD_UPLOAD_LIMIT_SIZE = 2048000;
163 
164 // Only check the latest 3000 data to avoid opreation taking too long time
165 const uint32_t MAXIMUM_LCD_CHECK_NUM = 3000;
166 
GetThumbnailPath(const std::string & path,const std::string & key)167 static inline std::string GetThumbnailPath(const std::string &path, const std::string &key)
168 {
169     if (path.length() < ROOT_MEDIA_DIR.length()) {
170         return "";
171     }
172     std::string suffix = (key == "THM_ASTC") ? ".astc" : ".jpg";
173     return ROOT_MEDIA_DIR + ".thumbs/" + path.substr(ROOT_MEDIA_DIR.length()) + "/" + key + suffix;
174 }
175 
GetThumbSuffix(ThumbnailType type)176 static std::string GetThumbSuffix(ThumbnailType type)
177 {
178     switch (type) {
179         case ThumbnailType::MTH:
180             return THUMBNAIL_MTH_SUFFIX;
181         case ThumbnailType::YEAR:
182             return THUMBNAIL_YEAR_SUFFIX;
183         case ThumbnailType::THUMB:
184             return THUMBNAIL_THUMB_SUFFIX;
185         case ThumbnailType::THUMB_ASTC:
186             return THUMBNAIL_THUMBASTC_SUFFIX;
187         case ThumbnailType::LCD:
188             return THUMBNAIL_LCD_SUFFIX;
189         default:
190             return "";
191     }
192 }
193 
194 static inline ThumbnailType GetThumbType(const int32_t width, const int32_t height, bool isAstc = false)
195 {
196     if (width == DEFAULT_ORIGINAL && height == DEFAULT_ORIGINAL) {
197         return ThumbnailType::LCD;
198     }
199 
200     if (std::min(width, height) <= DEFAULT_THUMB_SIZE &&
201         std::max(width, height) <= MAX_DEFAULT_THUMB_SIZE) {
202         return isAstc ? ThumbnailType::THUMB_ASTC : ThumbnailType::THUMB;
203     }
204 
205     return ThumbnailType::LCD;
206 }
207 
GetSandboxPath(const std::string & path,ThumbnailType type)208 static inline std::string GetSandboxPath(const std::string &path, ThumbnailType type)
209 {
210     if (path.length() < ROOT_MEDIA_DIR.length()) {
211         return "";
212     }
213     std::string suffix = (type == ThumbnailType::THUMB_ASTC) ? ".astc" : ".jpg";
214     std::string suffixStr = path.substr(ROOT_MEDIA_DIR.length()) + "/" + GetThumbSuffix(type) + suffix;
215     return ROOT_SANDBOX_DIR + ".thumbs/" + suffixStr;
216 }
217 
IsThumbnail(const int32_t width,const int32_t height)218 static inline bool IsThumbnail(const int32_t width, const int32_t height)
219 {
220     if (width == DEFAULT_ORIGINAL && height == DEFAULT_ORIGINAL) {
221         return false;
222     }
223     return std::min(width, height) <= DEFAULT_THUMB_SIZE &&
224            std::max(width, height) <= MAX_DEFAULT_THUMB_SIZE;
225 }
226 
227 } // namespace Media
228 } // namespace OHOS
229 
230 #endif  // FRAMEWORKS_SERVICES_THUMBNAIL_SERVICE_INCLUDE_THUMBNAIL_CONST_H_
231