Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
Chong Zhang | ee079fe | 2017-08-23 13:51:17 -0700 | [diff] [blame] | 18 | #define LOG_TAG "ItemTable" |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 19 | |
Marco Nelissen | 7522617 | 2016-11-16 14:10:52 -0800 | [diff] [blame] | 20 | #include <ItemTable.h> |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 21 | #include <media/DataSource.h> |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 22 | #include <media/stagefright/MetaData.h> |
| 23 | #include <media/stagefright/MediaErrors.h> |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 24 | #include <media/stagefright/foundation/ABuffer.h> |
Dongwon Kang | 6076128 | 2017-10-09 11:16:48 -0700 | [diff] [blame] | 25 | #include <media/stagefright/foundation/ByteUtils.h> |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 26 | #include <media/stagefright/foundation/hexdump.h> |
Dongwon Kang | e7a8a63 | 2017-10-09 14:52:58 -0700 | [diff] [blame] | 27 | #include <media/stagefright/foundation/MediaDefs.h> |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 28 | #include <utils/Log.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | namespace heif { |
| 33 | |
| 34 | ///////////////////////////////////////////////////////////////////// |
| 35 | // |
| 36 | // struct to keep track of one image item |
| 37 | // |
| 38 | |
| 39 | struct ImageItem { |
| 40 | friend struct ItemReference; |
| 41 | friend struct ItemProperty; |
| 42 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 43 | ImageItem() : ImageItem(0, 0, false) {} |
| 44 | ImageItem(uint32_t _type, uint32_t _id, bool _hidden) : |
| 45 | type(_type), itemId(_id), hidden(_hidden), |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 46 | rows(0), columns(0), width(0), height(0), rotation(0), |
| 47 | offset(0), size(0), nextTileIndex(0) {} |
| 48 | |
| 49 | bool isGrid() const { |
| 50 | return type == FOURCC('g', 'r', 'i', 'd'); |
| 51 | } |
| 52 | |
| 53 | status_t getNextTileItemId(uint32_t *nextTileItemId, bool reset) { |
| 54 | if (reset) { |
| 55 | nextTileIndex = 0; |
| 56 | } |
| 57 | if (nextTileIndex >= dimgRefs.size()) { |
| 58 | return ERROR_END_OF_STREAM; |
| 59 | } |
| 60 | *nextTileItemId = dimgRefs[nextTileIndex++]; |
| 61 | return OK; |
| 62 | } |
| 63 | |
| 64 | uint32_t type; |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 65 | uint32_t itemId; |
| 66 | bool hidden; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 67 | int32_t rows; |
| 68 | int32_t columns; |
| 69 | int32_t width; |
| 70 | int32_t height; |
| 71 | int32_t rotation; |
| 72 | off64_t offset; |
| 73 | size_t size; |
| 74 | sp<ABuffer> hvcc; |
| 75 | sp<ABuffer> icc; |
| 76 | |
| 77 | Vector<uint32_t> thumbnails; |
| 78 | Vector<uint32_t> dimgRefs; |
| 79 | size_t nextTileIndex; |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | ///////////////////////////////////////////////////////////////////// |
| 84 | // |
| 85 | // ISO boxes |
| 86 | // |
| 87 | |
| 88 | struct Box { |
| 89 | protected: |
| 90 | Box(const sp<DataSource> source, uint32_t type) : |
| 91 | mDataSource(source), mType(type) {} |
| 92 | |
| 93 | virtual ~Box() {} |
| 94 | |
| 95 | virtual status_t onChunkData( |
| 96 | uint32_t /*type*/, off64_t /*offset*/, size_t /*size*/) { |
| 97 | return OK; |
| 98 | } |
| 99 | |
| 100 | inline uint32_t type() const { return mType; } |
| 101 | |
| 102 | inline sp<DataSource> source() const { return mDataSource; } |
| 103 | |
| 104 | status_t parseChunk(off64_t *offset); |
| 105 | |
| 106 | status_t parseChunks(off64_t offset, size_t size); |
| 107 | |
| 108 | private: |
| 109 | sp<DataSource> mDataSource; |
| 110 | uint32_t mType; |
| 111 | }; |
| 112 | |
| 113 | status_t Box::parseChunk(off64_t *offset) { |
| 114 | if (*offset < 0) { |
| 115 | ALOGE("b/23540914"); |
| 116 | return ERROR_MALFORMED; |
| 117 | } |
| 118 | uint32_t hdr[2]; |
| 119 | if (mDataSource->readAt(*offset, hdr, 8) < 8) { |
| 120 | return ERROR_IO; |
| 121 | } |
| 122 | uint64_t chunk_size = ntohl(hdr[0]); |
| 123 | int32_t chunk_type = ntohl(hdr[1]); |
| 124 | off64_t data_offset = *offset + 8; |
| 125 | |
| 126 | if (chunk_size == 1) { |
| 127 | if (mDataSource->readAt(*offset + 8, &chunk_size, 8) < 8) { |
| 128 | return ERROR_IO; |
| 129 | } |
| 130 | chunk_size = ntoh64(chunk_size); |
| 131 | data_offset += 8; |
| 132 | |
| 133 | if (chunk_size < 16) { |
| 134 | // The smallest valid chunk is 16 bytes long in this case. |
| 135 | return ERROR_MALFORMED; |
| 136 | } |
| 137 | } else if (chunk_size == 0) { |
| 138 | // This shouldn't happen since we should never be top level |
| 139 | ALOGE("invalid chunk size 0 for non-top level box"); |
| 140 | return ERROR_MALFORMED; |
| 141 | } else if (chunk_size < 8) { |
| 142 | // The smallest valid chunk is 8 bytes long. |
| 143 | ALOGE("invalid chunk size: %lld", (long long)chunk_size); |
| 144 | return ERROR_MALFORMED; |
| 145 | } |
| 146 | |
| 147 | char chunk[5]; |
| 148 | MakeFourCCString(chunk_type, chunk); |
| 149 | ALOGV("chunk: %s @ %lld", chunk, (long long)*offset); |
| 150 | |
| 151 | off64_t chunk_data_size = chunk_size - (data_offset - *offset); |
| 152 | if (chunk_data_size < 0) { |
| 153 | ALOGE("b/23540914"); |
| 154 | return ERROR_MALFORMED; |
| 155 | } |
| 156 | |
| 157 | status_t err = onChunkData(chunk_type, data_offset, chunk_data_size); |
| 158 | |
| 159 | if (err != OK) { |
| 160 | return err; |
| 161 | } |
| 162 | *offset += chunk_size; |
| 163 | return OK; |
| 164 | } |
| 165 | |
| 166 | status_t Box::parseChunks(off64_t offset, size_t size) { |
| 167 | off64_t stopOffset = offset + size; |
| 168 | while (offset < stopOffset) { |
| 169 | status_t err = parseChunk(&offset); |
| 170 | if (err != OK) { |
| 171 | return err; |
| 172 | } |
| 173 | } |
| 174 | if (offset != stopOffset) { |
| 175 | return ERROR_MALFORMED; |
| 176 | } |
| 177 | return OK; |
| 178 | } |
| 179 | |
| 180 | /////////////////////////////////////////////////////////////////////// |
| 181 | |
| 182 | struct FullBox : public Box { |
| 183 | protected: |
| 184 | FullBox(const sp<DataSource> source, uint32_t type) : |
| 185 | Box(source, type), mVersion(0), mFlags(0) {} |
| 186 | |
| 187 | inline uint8_t version() const { return mVersion; } |
| 188 | |
| 189 | inline uint32_t flags() const { return mFlags; } |
| 190 | |
| 191 | status_t parseFullBoxHeader(off64_t *offset, size_t *size); |
| 192 | |
| 193 | private: |
| 194 | uint8_t mVersion; |
| 195 | uint32_t mFlags; |
| 196 | }; |
| 197 | |
| 198 | status_t FullBox::parseFullBoxHeader(off64_t *offset, size_t *size) { |
| 199 | if (*size < 4) { |
| 200 | return ERROR_MALFORMED; |
| 201 | } |
| 202 | if (!source()->readAt(*offset, &mVersion, 1)) { |
| 203 | return ERROR_IO; |
| 204 | } |
| 205 | if (!source()->getUInt24(*offset + 1, &mFlags)) { |
| 206 | return ERROR_IO; |
| 207 | } |
| 208 | *offset += 4; |
| 209 | *size -= 4; |
| 210 | return OK; |
| 211 | } |
| 212 | |
| 213 | ///////////////////////////////////////////////////////////////////// |
| 214 | // |
| 215 | // PrimaryImage box |
| 216 | // |
| 217 | |
| 218 | struct PitmBox : public FullBox { |
| 219 | PitmBox(const sp<DataSource> source) : |
| 220 | FullBox(source, FOURCC('p', 'i', 't', 'm')) {} |
| 221 | |
| 222 | status_t parse(off64_t offset, size_t size, uint32_t *primaryItemId); |
| 223 | }; |
| 224 | |
| 225 | status_t PitmBox::parse(off64_t offset, size_t size, uint32_t *primaryItemId) { |
| 226 | status_t err = parseFullBoxHeader(&offset, &size); |
| 227 | if (err != OK) { |
| 228 | return err; |
| 229 | } |
| 230 | |
| 231 | size_t itemIdSize = (version() == 0) ? 2 : 4; |
| 232 | if (size < itemIdSize) { |
| 233 | return ERROR_MALFORMED; |
| 234 | } |
| 235 | uint32_t itemId; |
| 236 | if (!source()->getUInt32Var(offset, &itemId, itemIdSize)) { |
| 237 | return ERROR_IO; |
| 238 | } |
| 239 | |
| 240 | ALOGV("primary id %d", itemId); |
| 241 | *primaryItemId = itemId; |
| 242 | |
| 243 | return OK; |
| 244 | } |
| 245 | |
| 246 | ///////////////////////////////////////////////////////////////////// |
| 247 | // |
| 248 | // ItemLocation related boxes |
| 249 | // |
| 250 | |
| 251 | struct ExtentEntry { |
| 252 | uint64_t extentIndex; |
| 253 | uint64_t extentOffset; |
| 254 | uint64_t extentLength; |
| 255 | }; |
| 256 | |
| 257 | struct ItemLoc { |
| 258 | ItemLoc() : ItemLoc(0, 0, 0, 0) {} |
| 259 | ItemLoc(uint32_t item_id, uint16_t construction_method, |
| 260 | uint16_t data_reference_index, uint64_t base_offset) : |
| 261 | itemId(item_id), |
| 262 | constructionMethod(construction_method), |
| 263 | dataReferenceIndex(data_reference_index), |
| 264 | baseOffset(base_offset) {} |
| 265 | |
| 266 | void addExtent(const ExtentEntry& extent) { |
| 267 | extents.push_back(extent); |
| 268 | } |
| 269 | |
| 270 | status_t getLoc(off64_t *offset, size_t *size, |
| 271 | off64_t idatOffset, size_t idatSize) const { |
| 272 | // TODO: fix extent handling, fix constructionMethod = 2 |
| 273 | CHECK(extents.size() == 1); |
| 274 | if (constructionMethod == 0) { |
| 275 | *offset = baseOffset + extents[0].extentOffset; |
| 276 | *size = extents[0].extentLength; |
| 277 | return OK; |
| 278 | } else if (constructionMethod == 1) { |
| 279 | if (baseOffset + extents[0].extentOffset + extents[0].extentLength |
| 280 | > idatSize) { |
| 281 | return ERROR_MALFORMED; |
| 282 | } |
| 283 | *offset = baseOffset + extents[0].extentOffset + idatOffset; |
| 284 | *size = extents[0].extentLength; |
| 285 | return OK; |
| 286 | } |
| 287 | return ERROR_UNSUPPORTED; |
| 288 | } |
| 289 | |
| 290 | // parsed info |
| 291 | uint32_t itemId; |
| 292 | uint16_t constructionMethod; |
| 293 | uint16_t dataReferenceIndex; |
| 294 | off64_t baseOffset; |
| 295 | Vector<ExtentEntry> extents; |
| 296 | }; |
| 297 | |
| 298 | struct IlocBox : public FullBox { |
| 299 | IlocBox(const sp<DataSource> source, KeyedVector<uint32_t, ItemLoc> *itemLocs) : |
| 300 | FullBox(source, FOURCC('i', 'l', 'o', 'c')), |
| 301 | mItemLocs(itemLocs), mHasConstructMethod1(false) {} |
| 302 | |
| 303 | status_t parse(off64_t offset, size_t size); |
| 304 | |
| 305 | bool hasConstructMethod1() { return mHasConstructMethod1; } |
| 306 | |
| 307 | private: |
| 308 | static bool isSizeFieldValid(uint32_t offset_size) { |
| 309 | return offset_size == 0 || offset_size == 4 || offset_size == 8; |
| 310 | } |
| 311 | KeyedVector<uint32_t, ItemLoc> *mItemLocs; |
| 312 | bool mHasConstructMethod1; |
| 313 | }; |
| 314 | |
| 315 | status_t IlocBox::parse(off64_t offset, size_t size) { |
| 316 | status_t err = parseFullBoxHeader(&offset, &size); |
| 317 | if (err != OK) { |
| 318 | return err; |
| 319 | } |
| 320 | if (version() > 2) { |
| 321 | ALOGE("%s: invalid version %d", __FUNCTION__, version()); |
| 322 | return ERROR_MALFORMED; |
| 323 | } |
| 324 | |
| 325 | if (size < 2) { |
| 326 | return ERROR_MALFORMED; |
| 327 | } |
| 328 | uint8_t offset_size; |
| 329 | if (!source()->readAt(offset++, &offset_size, 1)) { |
| 330 | return ERROR_IO; |
| 331 | } |
| 332 | uint8_t length_size = (offset_size & 0xF); |
| 333 | offset_size >>= 4; |
| 334 | |
| 335 | uint8_t base_offset_size; |
| 336 | if (!source()->readAt(offset++, &base_offset_size, 1)) { |
| 337 | return ERROR_IO; |
| 338 | } |
| 339 | uint8_t index_size = 0; |
| 340 | if (version() == 1 || version() == 2) { |
| 341 | index_size = (base_offset_size & 0xF); |
| 342 | } |
| 343 | base_offset_size >>= 4; |
| 344 | size -= 2; |
| 345 | |
| 346 | if (!isSizeFieldValid(offset_size) |
| 347 | || !isSizeFieldValid(length_size) |
| 348 | || !isSizeFieldValid(base_offset_size) |
| 349 | || !isSizeFieldValid((index_size))) { |
| 350 | ALOGE("%s: offset size not valid: %d, %d, %d, %d", __FUNCTION__, |
| 351 | offset_size, length_size, base_offset_size, index_size); |
| 352 | return ERROR_MALFORMED; |
| 353 | } |
| 354 | |
| 355 | uint32_t item_count; |
| 356 | size_t itemFieldSize = version() < 2 ? 2 : 4; |
| 357 | if (size < itemFieldSize) { |
| 358 | return ERROR_MALFORMED; |
| 359 | } |
| 360 | if (!source()->getUInt32Var(offset, &item_count, itemFieldSize)) { |
| 361 | return ERROR_IO; |
| 362 | } |
| 363 | |
| 364 | ALOGV("item_count %lld", (long long) item_count); |
| 365 | offset += itemFieldSize; |
| 366 | size -= itemFieldSize; |
| 367 | |
| 368 | for (size_t i = 0; i < item_count; i++) { |
| 369 | uint32_t item_id; |
| 370 | if (!source()->getUInt32Var(offset, &item_id, itemFieldSize)) { |
| 371 | return ERROR_IO; |
| 372 | } |
| 373 | ALOGV("item[%zu]: id %lld", i, (long long)item_id); |
| 374 | offset += itemFieldSize; |
| 375 | |
| 376 | uint8_t construction_method = 0; |
| 377 | if (version() == 1 || version() == 2) { |
| 378 | uint8_t buf[2]; |
| 379 | if (!source()->readAt(offset, buf, 2)) { |
| 380 | return ERROR_IO; |
| 381 | } |
| 382 | construction_method = (buf[1] & 0xF); |
| 383 | ALOGV("construction_method %d", construction_method); |
| 384 | if (construction_method == 1) { |
| 385 | mHasConstructMethod1 = true; |
| 386 | } |
| 387 | |
| 388 | offset += 2; |
| 389 | } |
| 390 | |
| 391 | uint16_t data_reference_index; |
| 392 | if (!source()->getUInt16(offset, &data_reference_index)) { |
| 393 | return ERROR_IO; |
| 394 | } |
| 395 | ALOGV("data_reference_index %d", data_reference_index); |
| 396 | if (data_reference_index != 0) { |
| 397 | // we don't support reference to other files |
| 398 | return ERROR_UNSUPPORTED; |
| 399 | } |
| 400 | offset += 2; |
| 401 | |
| 402 | uint64_t base_offset = 0; |
| 403 | if (base_offset_size != 0) { |
| 404 | if (!source()->getUInt64Var(offset, &base_offset, base_offset_size)) { |
| 405 | return ERROR_IO; |
| 406 | } |
| 407 | offset += base_offset_size; |
| 408 | } |
| 409 | ALOGV("base_offset %lld", (long long) base_offset); |
| 410 | |
| 411 | ssize_t index = mItemLocs->add(item_id, ItemLoc( |
| 412 | item_id, construction_method, data_reference_index, base_offset)); |
| 413 | ItemLoc &item = mItemLocs->editValueAt(index); |
| 414 | |
| 415 | uint16_t extent_count; |
| 416 | if (!source()->getUInt16(offset, &extent_count)) { |
| 417 | return ERROR_IO; |
| 418 | } |
| 419 | ALOGV("extent_count %d", extent_count); |
| 420 | |
| 421 | if (extent_count > 1 && (offset_size == 0 || length_size == 0)) { |
| 422 | // if the item is dividec into more than one extents, offset and |
| 423 | // length must be present. |
| 424 | return ERROR_MALFORMED; |
| 425 | } |
| 426 | offset += 2; |
| 427 | |
| 428 | for (size_t j = 0; j < extent_count; j++) { |
| 429 | uint64_t extent_index = 1; // default=1 |
| 430 | if ((version() == 1 || version() == 2) && (index_size > 0)) { |
| 431 | if (!source()->getUInt64Var(offset, &extent_index, index_size)) { |
| 432 | return ERROR_IO; |
| 433 | } |
| 434 | // TODO: add support for this mode |
| 435 | offset += index_size; |
| 436 | ALOGV("extent_index %lld", (long long)extent_index); |
| 437 | } |
| 438 | |
| 439 | uint64_t extent_offset = 0; // default=0 |
| 440 | if (offset_size > 0) { |
| 441 | if (!source()->getUInt64Var(offset, &extent_offset, offset_size)) { |
| 442 | return ERROR_IO; |
| 443 | } |
| 444 | offset += offset_size; |
| 445 | } |
| 446 | ALOGV("extent_offset %lld", (long long)extent_offset); |
| 447 | |
| 448 | uint64_t extent_length = 0; // this indicates full length of file |
| 449 | if (length_size > 0) { |
| 450 | if (!source()->getUInt64Var(offset, &extent_length, length_size)) { |
| 451 | return ERROR_IO; |
| 452 | } |
| 453 | offset += length_size; |
| 454 | } |
| 455 | ALOGV("extent_length %lld", (long long)extent_length); |
| 456 | |
| 457 | item.addExtent({ extent_index, extent_offset, extent_length }); |
| 458 | } |
| 459 | } |
| 460 | return OK; |
| 461 | } |
| 462 | |
| 463 | ///////////////////////////////////////////////////////////////////// |
| 464 | // |
| 465 | // ItemReference related boxes |
| 466 | // |
| 467 | |
| 468 | struct ItemReference : public Box, public RefBase { |
| 469 | ItemReference(const sp<DataSource> source, uint32_t type, uint32_t itemIdSize) : |
| 470 | Box(source, type), mItemId(0), mRefIdSize(itemIdSize) {} |
| 471 | |
| 472 | status_t parse(off64_t offset, size_t size); |
| 473 | |
| 474 | uint32_t itemId() { return mItemId; } |
| 475 | |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 476 | void apply(KeyedVector<uint32_t, ImageItem> &itemIdToItemMap) const; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 477 | |
| 478 | private: |
| 479 | uint32_t mItemId; |
| 480 | uint32_t mRefIdSize; |
| 481 | Vector<uint32_t> mRefs; |
| 482 | |
| 483 | DISALLOW_EVIL_CONSTRUCTORS(ItemReference); |
| 484 | }; |
| 485 | |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 486 | void ItemReference::apply(KeyedVector<uint32_t, ImageItem> &itemIdToItemMap) const { |
| 487 | ssize_t itemIndex = itemIdToItemMap.indexOfKey(mItemId); |
| 488 | |
| 489 | // ignore non-image items |
| 490 | if (itemIndex < 0) { |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | ALOGV("attach reference type 0x%x to item id %d)", type(), mItemId); |
| 495 | |
| 496 | if (type() == FOURCC('d', 'i', 'm', 'g')) { |
| 497 | ImageItem &derivedImage = itemIdToItemMap.editValueAt(itemIndex); |
| 498 | if (!derivedImage.dimgRefs.empty()) { |
| 499 | ALOGW("dimgRefs if not clean!"); |
| 500 | } |
| 501 | derivedImage.dimgRefs.appendVector(mRefs); |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 502 | |
| 503 | for (size_t i = 0; i < mRefs.size(); i++) { |
| 504 | itemIndex = itemIdToItemMap.indexOfKey(mRefs[i]); |
| 505 | |
| 506 | // ignore non-image items |
| 507 | if (itemIndex < 0) { |
| 508 | continue; |
| 509 | } |
| 510 | ImageItem &sourceImage = itemIdToItemMap.editValueAt(itemIndex); |
| 511 | |
| 512 | // mark the source image of the derivation as hidden |
| 513 | sourceImage.hidden = true; |
| 514 | } |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 515 | } else if (type() == FOURCC('t', 'h', 'm', 'b')) { |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 516 | // mark thumbnail image as hidden, these can be retrieved if the client |
| 517 | // request thumbnail explicitly, but won't be exposed as displayables. |
| 518 | ImageItem &thumbImage = itemIdToItemMap.editValueAt(itemIndex); |
| 519 | thumbImage.hidden = true; |
| 520 | |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 521 | for (size_t i = 0; i < mRefs.size(); i++) { |
| 522 | itemIndex = itemIdToItemMap.indexOfKey(mRefs[i]); |
| 523 | |
| 524 | // ignore non-image items |
| 525 | if (itemIndex < 0) { |
| 526 | continue; |
| 527 | } |
| 528 | ALOGV("Image item id %d uses thumbnail item id %d", mRefs[i], mItemId); |
| 529 | ImageItem &masterImage = itemIdToItemMap.editValueAt(itemIndex); |
| 530 | if (!masterImage.thumbnails.empty()) { |
| 531 | ALOGW("already has thumbnails!"); |
| 532 | } |
| 533 | masterImage.thumbnails.push_back(mItemId); |
| 534 | } |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 535 | } else if (type() == FOURCC('a', 'u', 'x', 'l')) { |
| 536 | // mark auxiliary image as hidden |
| 537 | ImageItem &auxImage = itemIdToItemMap.editValueAt(itemIndex); |
| 538 | auxImage.hidden = true; |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 539 | } else { |
| 540 | ALOGW("ignoring unsupported ref type 0x%x", type()); |
| 541 | } |
| 542 | } |
| 543 | |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 544 | status_t ItemReference::parse(off64_t offset, size_t size) { |
| 545 | if (size < mRefIdSize + 2) { |
| 546 | return ERROR_MALFORMED; |
| 547 | } |
| 548 | if (!source()->getUInt32Var(offset, &mItemId, mRefIdSize)) { |
| 549 | return ERROR_IO; |
| 550 | } |
| 551 | offset += mRefIdSize; |
| 552 | |
| 553 | uint16_t count; |
| 554 | if (!source()->getUInt16(offset, &count)) { |
| 555 | return ERROR_IO; |
| 556 | } |
| 557 | offset += 2; |
| 558 | size -= (mRefIdSize + 2); |
| 559 | |
| 560 | if (size < count * mRefIdSize) { |
| 561 | return ERROR_MALFORMED; |
| 562 | } |
| 563 | |
| 564 | for (size_t i = 0; i < count; i++) { |
| 565 | uint32_t refItemId; |
| 566 | if (!source()->getUInt32Var(offset, &refItemId, mRefIdSize)) { |
| 567 | return ERROR_IO; |
| 568 | } |
| 569 | offset += mRefIdSize; |
| 570 | mRefs.push_back(refItemId); |
| 571 | ALOGV("item id %d: referencing item id %d", mItemId, refItemId); |
| 572 | } |
| 573 | |
| 574 | return OK; |
| 575 | } |
| 576 | |
| 577 | struct IrefBox : public FullBox { |
| 578 | IrefBox(const sp<DataSource> source, Vector<sp<ItemReference> > *itemRefs) : |
| 579 | FullBox(source, FOURCC('i', 'r', 'e', 'f')), mRefIdSize(0), mItemRefs(itemRefs) {} |
| 580 | |
| 581 | status_t parse(off64_t offset, size_t size); |
| 582 | |
| 583 | protected: |
| 584 | status_t onChunkData(uint32_t type, off64_t offset, size_t size) override; |
| 585 | |
| 586 | private: |
| 587 | uint32_t mRefIdSize; |
| 588 | Vector<sp<ItemReference> > *mItemRefs; |
| 589 | }; |
| 590 | |
| 591 | status_t IrefBox::parse(off64_t offset, size_t size) { |
| 592 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 593 | status_t err = parseFullBoxHeader(&offset, &size); |
| 594 | if (err != OK) { |
| 595 | return err; |
| 596 | } |
| 597 | |
| 598 | mRefIdSize = (version() == 0) ? 2 : 4; |
| 599 | return parseChunks(offset, size); |
| 600 | } |
| 601 | |
| 602 | status_t IrefBox::onChunkData(uint32_t type, off64_t offset, size_t size) { |
| 603 | sp<ItemReference> itemRef = new ItemReference(source(), type, mRefIdSize); |
| 604 | |
| 605 | status_t err = itemRef->parse(offset, size); |
| 606 | if (err != OK) { |
| 607 | return err; |
| 608 | } |
| 609 | mItemRefs->push_back(itemRef); |
| 610 | return OK; |
| 611 | } |
| 612 | |
| 613 | ///////////////////////////////////////////////////////////////////// |
| 614 | // |
| 615 | // ItemProperty related boxes |
| 616 | // |
| 617 | |
| 618 | struct AssociationEntry { |
| 619 | uint32_t itemId; |
| 620 | bool essential; |
| 621 | uint16_t index; |
| 622 | }; |
| 623 | |
| 624 | struct ItemProperty : public RefBase { |
| 625 | ItemProperty() {} |
| 626 | |
| 627 | virtual void attachTo(ImageItem &/*image*/) const { |
| 628 | ALOGW("Unrecognized property"); |
| 629 | } |
| 630 | virtual status_t parse(off64_t /*offset*/, size_t /*size*/) { |
| 631 | ALOGW("Unrecognized property"); |
| 632 | return OK; |
| 633 | } |
| 634 | |
| 635 | private: |
| 636 | DISALLOW_EVIL_CONSTRUCTORS(ItemProperty); |
| 637 | }; |
| 638 | |
| 639 | struct IspeBox : public FullBox, public ItemProperty { |
| 640 | IspeBox(const sp<DataSource> source) : |
| 641 | FullBox(source, FOURCC('i', 's', 'p', 'e')), mWidth(0), mHeight(0) {} |
| 642 | |
| 643 | status_t parse(off64_t offset, size_t size) override; |
| 644 | |
| 645 | void attachTo(ImageItem &image) const override { |
| 646 | image.width = mWidth; |
| 647 | image.height = mHeight; |
| 648 | } |
| 649 | |
| 650 | private: |
| 651 | uint32_t mWidth; |
| 652 | uint32_t mHeight; |
| 653 | }; |
| 654 | |
| 655 | status_t IspeBox::parse(off64_t offset, size_t size) { |
| 656 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 657 | |
| 658 | status_t err = parseFullBoxHeader(&offset, &size); |
| 659 | if (err != OK) { |
| 660 | return err; |
| 661 | } |
| 662 | |
| 663 | if (size < 8) { |
| 664 | return ERROR_MALFORMED; |
| 665 | } |
| 666 | if (!source()->getUInt32(offset, &mWidth) |
| 667 | || !source()->getUInt32(offset + 4, &mHeight)) { |
| 668 | return ERROR_IO; |
| 669 | } |
| 670 | ALOGV("property ispe: %dx%d", mWidth, mHeight); |
| 671 | |
| 672 | return OK; |
| 673 | } |
| 674 | |
| 675 | struct HvccBox : public Box, public ItemProperty { |
| 676 | HvccBox(const sp<DataSource> source) : |
| 677 | Box(source, FOURCC('h', 'v', 'c', 'C')) {} |
| 678 | |
| 679 | status_t parse(off64_t offset, size_t size) override; |
| 680 | |
| 681 | void attachTo(ImageItem &image) const override { |
| 682 | image.hvcc = mHVCC; |
| 683 | } |
| 684 | |
| 685 | private: |
| 686 | sp<ABuffer> mHVCC; |
| 687 | }; |
| 688 | |
| 689 | status_t HvccBox::parse(off64_t offset, size_t size) { |
| 690 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 691 | |
| 692 | mHVCC = new ABuffer(size); |
| 693 | |
| 694 | if (mHVCC->data() == NULL) { |
| 695 | ALOGE("b/28471206"); |
| 696 | return NO_MEMORY; |
| 697 | } |
| 698 | |
| 699 | if (source()->readAt(offset, mHVCC->data(), size) < (ssize_t)size) { |
| 700 | return ERROR_IO; |
| 701 | } |
| 702 | |
| 703 | ALOGV("property hvcC"); |
| 704 | |
| 705 | return OK; |
| 706 | } |
| 707 | |
| 708 | struct IrotBox : public Box, public ItemProperty { |
| 709 | IrotBox(const sp<DataSource> source) : |
| 710 | Box(source, FOURCC('i', 'r', 'o', 't')), mAngle(0) {} |
| 711 | |
| 712 | status_t parse(off64_t offset, size_t size) override; |
| 713 | |
| 714 | void attachTo(ImageItem &image) const override { |
| 715 | image.rotation = mAngle * 90; |
| 716 | } |
| 717 | |
| 718 | private: |
| 719 | uint8_t mAngle; |
| 720 | }; |
| 721 | |
| 722 | status_t IrotBox::parse(off64_t offset, size_t size) { |
| 723 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 724 | |
| 725 | if (size < 1) { |
| 726 | return ERROR_MALFORMED; |
| 727 | } |
| 728 | if (source()->readAt(offset, &mAngle, 1) != 1) { |
| 729 | return ERROR_IO; |
| 730 | } |
| 731 | mAngle &= 0x3; |
| 732 | ALOGV("property irot: %d", mAngle); |
| 733 | |
| 734 | return OK; |
| 735 | } |
| 736 | |
| 737 | struct ColrBox : public Box, public ItemProperty { |
| 738 | ColrBox(const sp<DataSource> source) : |
| 739 | Box(source, FOURCC('c', 'o', 'l', 'r')) {} |
| 740 | |
| 741 | status_t parse(off64_t offset, size_t size) override; |
| 742 | |
| 743 | void attachTo(ImageItem &image) const override { |
| 744 | image.icc = mICCData; |
| 745 | } |
| 746 | |
| 747 | private: |
| 748 | sp<ABuffer> mICCData; |
| 749 | }; |
| 750 | |
| 751 | status_t ColrBox::parse(off64_t offset, size_t size) { |
| 752 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 753 | |
| 754 | if (size < 4) { |
| 755 | return ERROR_MALFORMED; |
| 756 | } |
| 757 | uint32_t colour_type; |
| 758 | if (!source()->getUInt32(offset, &colour_type)) { |
| 759 | return ERROR_IO; |
| 760 | } |
| 761 | offset += 4; |
| 762 | size -= 4; |
| 763 | if (colour_type == FOURCC('n', 'c', 'l', 'x')) { |
| 764 | return OK; |
| 765 | } |
| 766 | if ((colour_type != FOURCC('r', 'I', 'C', 'C')) && |
| 767 | (colour_type != FOURCC('p', 'r', 'o', 'f'))) { |
| 768 | return ERROR_MALFORMED; |
| 769 | } |
| 770 | |
| 771 | mICCData = new ABuffer(size); |
| 772 | if (mICCData->data() == NULL) { |
| 773 | ALOGE("b/28471206"); |
| 774 | return NO_MEMORY; |
| 775 | } |
| 776 | |
| 777 | if (source()->readAt(offset, mICCData->data(), size) != (ssize_t)size) { |
| 778 | return ERROR_IO; |
| 779 | } |
| 780 | |
| 781 | ALOGV("property Colr: size %zd", size); |
| 782 | return OK; |
| 783 | } |
| 784 | |
| 785 | struct IpmaBox : public FullBox { |
| 786 | IpmaBox(const sp<DataSource> source, Vector<AssociationEntry> *associations) : |
| 787 | FullBox(source, FOURCC('i', 'p', 'm', 'a')), mAssociations(associations) {} |
| 788 | |
| 789 | status_t parse(off64_t offset, size_t size); |
| 790 | private: |
| 791 | Vector<AssociationEntry> *mAssociations; |
| 792 | }; |
| 793 | |
| 794 | status_t IpmaBox::parse(off64_t offset, size_t size) { |
| 795 | status_t err = parseFullBoxHeader(&offset, &size); |
| 796 | if (err != OK) { |
| 797 | return err; |
| 798 | } |
| 799 | |
| 800 | if (size < 4) { |
| 801 | return ERROR_MALFORMED; |
| 802 | } |
| 803 | uint32_t entryCount; |
| 804 | if (!source()->getUInt32(offset, &entryCount)) { |
| 805 | return ERROR_IO; |
| 806 | } |
| 807 | offset += 4; |
| 808 | size -= 4; |
| 809 | |
| 810 | for (size_t k = 0; k < entryCount; ++k) { |
| 811 | uint32_t itemId = 0; |
| 812 | size_t itemIdSize = (version() < 1) ? 2 : 4; |
| 813 | |
| 814 | if (size < itemIdSize + 1) { |
| 815 | return ERROR_MALFORMED; |
| 816 | } |
| 817 | |
| 818 | if (!source()->getUInt32Var(offset, &itemId, itemIdSize)) { |
| 819 | return ERROR_IO; |
| 820 | } |
| 821 | offset += itemIdSize; |
| 822 | size -= itemIdSize; |
| 823 | |
| 824 | uint8_t associationCount; |
| 825 | if (!source()->readAt(offset, &associationCount, 1)) { |
| 826 | return ERROR_IO; |
| 827 | } |
| 828 | offset++; |
| 829 | size--; |
| 830 | |
| 831 | for (size_t i = 0; i < associationCount; ++i) { |
| 832 | size_t propIndexSize = (flags() & 1) ? 2 : 1; |
| 833 | if (size < propIndexSize) { |
| 834 | return ERROR_MALFORMED; |
| 835 | } |
| 836 | uint16_t propIndex; |
| 837 | if (!source()->getUInt16Var(offset, &propIndex, propIndexSize)) { |
| 838 | return ERROR_IO; |
| 839 | } |
| 840 | offset += propIndexSize; |
| 841 | size -= propIndexSize; |
| 842 | uint16_t bitmask = (1 << (8 * propIndexSize - 1)); |
| 843 | AssociationEntry entry = { |
| 844 | .itemId = itemId, |
| 845 | .essential = !!(propIndex & bitmask), |
| 846 | .index = (uint16_t) (propIndex & ~bitmask) |
| 847 | }; |
| 848 | |
| 849 | ALOGV("item id %d associated to property %d (essential %d)", |
| 850 | itemId, entry.index, entry.essential); |
| 851 | |
| 852 | mAssociations->push_back(entry); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | return OK; |
| 857 | } |
| 858 | |
| 859 | struct IpcoBox : public Box { |
| 860 | IpcoBox(const sp<DataSource> source, Vector<sp<ItemProperty> > *properties) : |
| 861 | Box(source, FOURCC('i', 'p', 'c', 'o')), mItemProperties(properties) {} |
| 862 | |
| 863 | status_t parse(off64_t offset, size_t size); |
| 864 | protected: |
| 865 | status_t onChunkData(uint32_t type, off64_t offset, size_t size) override; |
| 866 | |
| 867 | private: |
| 868 | Vector<sp<ItemProperty> > *mItemProperties; |
| 869 | }; |
| 870 | |
| 871 | status_t IpcoBox::parse(off64_t offset, size_t size) { |
| 872 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 873 | // push dummy as the index is 1-based |
| 874 | mItemProperties->push_back(new ItemProperty()); |
| 875 | return parseChunks(offset, size); |
| 876 | } |
| 877 | |
| 878 | status_t IpcoBox::onChunkData(uint32_t type, off64_t offset, size_t size) { |
| 879 | sp<ItemProperty> itemProperty; |
| 880 | switch(type) { |
| 881 | case FOURCC('h', 'v', 'c', 'C'): |
| 882 | { |
| 883 | itemProperty = new HvccBox(source()); |
| 884 | break; |
| 885 | } |
| 886 | case FOURCC('i', 's', 'p', 'e'): |
| 887 | { |
| 888 | itemProperty = new IspeBox(source()); |
| 889 | break; |
| 890 | } |
| 891 | case FOURCC('i', 'r', 'o', 't'): |
| 892 | { |
| 893 | itemProperty = new IrotBox(source()); |
| 894 | break; |
| 895 | } |
| 896 | case FOURCC('c', 'o', 'l', 'r'): |
| 897 | { |
| 898 | itemProperty = new ColrBox(source()); |
| 899 | break; |
| 900 | } |
| 901 | default: |
| 902 | { |
| 903 | // push dummy to maintain correct item property index |
| 904 | itemProperty = new ItemProperty(); |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | status_t err = itemProperty->parse(offset, size); |
| 909 | if (err != OK) { |
| 910 | return err; |
| 911 | } |
| 912 | mItemProperties->push_back(itemProperty); |
| 913 | return OK; |
| 914 | } |
| 915 | |
| 916 | struct IprpBox : public Box { |
| 917 | IprpBox(const sp<DataSource> source, |
| 918 | Vector<sp<ItemProperty> > *properties, |
| 919 | Vector<AssociationEntry> *associations) : |
| 920 | Box(source, FOURCC('i', 'p', 'r', 'p')), |
| 921 | mProperties(properties), mAssociations(associations) {} |
| 922 | |
| 923 | status_t parse(off64_t offset, size_t size); |
| 924 | protected: |
| 925 | status_t onChunkData(uint32_t type, off64_t offset, size_t size) override; |
| 926 | |
| 927 | private: |
| 928 | Vector<sp<ItemProperty> > *mProperties; |
| 929 | Vector<AssociationEntry> *mAssociations; |
| 930 | }; |
| 931 | |
| 932 | status_t IprpBox::parse(off64_t offset, size_t size) { |
| 933 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 934 | |
| 935 | status_t err = parseChunks(offset, size); |
| 936 | if (err != OK) { |
| 937 | return err; |
| 938 | } |
| 939 | return OK; |
| 940 | } |
| 941 | |
| 942 | status_t IprpBox::onChunkData(uint32_t type, off64_t offset, size_t size) { |
| 943 | switch(type) { |
| 944 | case FOURCC('i', 'p', 'c', 'o'): |
| 945 | { |
| 946 | IpcoBox ipcoBox(source(), mProperties); |
| 947 | return ipcoBox.parse(offset, size); |
| 948 | } |
| 949 | case FOURCC('i', 'p', 'm', 'a'): |
| 950 | { |
| 951 | IpmaBox ipmaBox(source(), mAssociations); |
| 952 | return ipmaBox.parse(offset, size); |
| 953 | } |
| 954 | default: |
| 955 | { |
| 956 | ALOGW("Unrecognized box."); |
| 957 | break; |
| 958 | } |
| 959 | } |
| 960 | return OK; |
| 961 | } |
| 962 | |
| 963 | ///////////////////////////////////////////////////////////////////// |
| 964 | // |
| 965 | // ItemInfo related boxes |
| 966 | // |
| 967 | struct ItemInfo { |
| 968 | uint32_t itemId; |
| 969 | uint32_t itemType; |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 970 | bool hidden; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 971 | }; |
| 972 | |
| 973 | struct InfeBox : public FullBox { |
| 974 | InfeBox(const sp<DataSource> source) : |
| 975 | FullBox(source, FOURCC('i', 'n', 'f', 'e')) {} |
| 976 | |
| 977 | status_t parse(off64_t offset, size_t size, ItemInfo *itemInfo); |
| 978 | |
| 979 | private: |
| 980 | bool parseNullTerminatedString(off64_t *offset, size_t *size, String8 *out); |
| 981 | }; |
| 982 | |
| 983 | bool InfeBox::parseNullTerminatedString( |
| 984 | off64_t *offset, size_t *size, String8 *out) { |
| 985 | char tmp[256]; |
| 986 | size_t len = 0; |
| 987 | off64_t newOffset = *offset; |
| 988 | off64_t stopOffset = *offset + *size; |
| 989 | while (newOffset < stopOffset) { |
| 990 | if (!source()->readAt(newOffset++, &tmp[len], 1)) { |
| 991 | return false; |
| 992 | } |
| 993 | if (tmp[len] == 0) { |
| 994 | out->append(tmp, len); |
| 995 | |
| 996 | *offset = newOffset; |
| 997 | *size = stopOffset - newOffset; |
| 998 | |
| 999 | return true; |
| 1000 | } |
| 1001 | if (++len >= sizeof(tmp)) { |
| 1002 | out->append(tmp, len); |
| 1003 | len = 0; |
| 1004 | } |
| 1005 | } |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
| 1009 | status_t InfeBox::parse(off64_t offset, size_t size, ItemInfo *itemInfo) { |
| 1010 | status_t err = parseFullBoxHeader(&offset, &size); |
| 1011 | if (err != OK) { |
| 1012 | return err; |
| 1013 | } |
| 1014 | |
| 1015 | if (version() == 0 || version() == 1) { |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1016 | return ERROR_UNSUPPORTED; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1017 | } else { // version >= 2 |
| 1018 | uint32_t item_id; |
| 1019 | size_t itemIdSize = (version() == 2) ? 2 : 4; |
| 1020 | if (size < itemIdSize + 6) { |
| 1021 | return ERROR_MALFORMED; |
| 1022 | } |
| 1023 | if (!source()->getUInt32Var(offset, &item_id, itemIdSize)) { |
| 1024 | return ERROR_IO; |
| 1025 | } |
| 1026 | ALOGV("item_id %d", item_id); |
| 1027 | offset += itemIdSize; |
| 1028 | uint16_t item_protection_index; |
| 1029 | if (!source()->getUInt16(offset, &item_protection_index)) { |
| 1030 | return ERROR_IO; |
| 1031 | } |
| 1032 | ALOGV("item_protection_index %d", item_protection_index); |
| 1033 | offset += 2; |
| 1034 | uint32_t item_type; |
| 1035 | if (!source()->getUInt32(offset, &item_type)) { |
| 1036 | return ERROR_IO; |
| 1037 | } |
| 1038 | |
| 1039 | itemInfo->itemId = item_id; |
| 1040 | itemInfo->itemType = item_type; |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1041 | // According to HEIF spec, (flags & 1) indicates the image is hidden |
| 1042 | // and not supposed to be displayed. |
| 1043 | itemInfo->hidden = (flags() & 1); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1044 | |
| 1045 | char itemTypeString[5]; |
| 1046 | MakeFourCCString(item_type, itemTypeString); |
| 1047 | ALOGV("item_type %s", itemTypeString); |
| 1048 | offset += 4; |
| 1049 | size -= itemIdSize + 6; |
| 1050 | |
| 1051 | String8 item_name; |
| 1052 | if (!parseNullTerminatedString(&offset, &size, &item_name)) { |
| 1053 | return ERROR_MALFORMED; |
| 1054 | } |
| 1055 | ALOGV("item_name %s", item_name.c_str()); |
| 1056 | |
| 1057 | if (item_type == FOURCC('m', 'i', 'm', 'e')) { |
| 1058 | String8 content_type; |
| 1059 | if (!parseNullTerminatedString(&offset, &size, &content_type)) { |
| 1060 | return ERROR_MALFORMED; |
| 1061 | } |
| 1062 | |
| 1063 | String8 content_encoding; |
| 1064 | if (!parseNullTerminatedString(&offset, &size, &content_encoding)) { |
| 1065 | return ERROR_MALFORMED; |
| 1066 | } |
| 1067 | } else if (item_type == FOURCC('u', 'r', 'i', ' ')) { |
| 1068 | String8 item_uri_type; |
| 1069 | if (!parseNullTerminatedString(&offset, &size, &item_uri_type)) { |
| 1070 | return ERROR_MALFORMED; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | return OK; |
| 1075 | } |
| 1076 | |
| 1077 | struct IinfBox : public FullBox { |
| 1078 | IinfBox(const sp<DataSource> source, Vector<ItemInfo> *itemInfos) : |
| 1079 | FullBox(source, FOURCC('i', 'i', 'n', 'f')), |
| 1080 | mItemInfos(itemInfos), mHasGrids(false) {} |
| 1081 | |
| 1082 | status_t parse(off64_t offset, size_t size); |
| 1083 | |
| 1084 | bool hasGrids() { return mHasGrids; } |
| 1085 | |
| 1086 | protected: |
| 1087 | status_t onChunkData(uint32_t type, off64_t offset, size_t size) override; |
| 1088 | |
| 1089 | private: |
| 1090 | Vector<ItemInfo> *mItemInfos; |
| 1091 | bool mHasGrids; |
| 1092 | }; |
| 1093 | |
| 1094 | status_t IinfBox::parse(off64_t offset, size_t size) { |
| 1095 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 1096 | |
| 1097 | status_t err = parseFullBoxHeader(&offset, &size); |
| 1098 | if (err != OK) { |
| 1099 | return err; |
| 1100 | } |
| 1101 | |
| 1102 | size_t entryCountSize = version() == 0 ? 2 : 4; |
| 1103 | if (size < entryCountSize) { |
| 1104 | return ERROR_MALFORMED; |
| 1105 | } |
| 1106 | uint32_t entry_count; |
| 1107 | if (!source()->getUInt32Var(offset, &entry_count, entryCountSize)) { |
| 1108 | return ERROR_IO; |
| 1109 | } |
| 1110 | ALOGV("entry_count %d", entry_count); |
| 1111 | |
| 1112 | off64_t stopOffset = offset + size; |
| 1113 | offset += entryCountSize; |
| 1114 | for (size_t i = 0; i < entry_count && offset < stopOffset; i++) { |
| 1115 | ALOGV("entry %zu", i); |
| 1116 | status_t err = parseChunk(&offset); |
| 1117 | if (err != OK) { |
| 1118 | return err; |
| 1119 | } |
| 1120 | } |
| 1121 | if (offset != stopOffset) { |
| 1122 | return ERROR_MALFORMED; |
| 1123 | } |
| 1124 | |
| 1125 | return OK; |
| 1126 | } |
| 1127 | |
| 1128 | status_t IinfBox::onChunkData(uint32_t type, off64_t offset, size_t size) { |
| 1129 | if (type != FOURCC('i', 'n', 'f', 'e')) { |
| 1130 | return OK; |
| 1131 | } |
| 1132 | |
| 1133 | InfeBox infeBox(source()); |
| 1134 | ItemInfo itemInfo; |
| 1135 | status_t err = infeBox.parse(offset, size, &itemInfo); |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1136 | if (err == OK) { |
| 1137 | mItemInfos->push_back(itemInfo); |
| 1138 | mHasGrids |= (itemInfo.itemType == FOURCC('g', 'r', 'i', 'd')); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1139 | } |
Chong Zhang | 5518a2c | 2017-10-13 18:31:25 -0700 | [diff] [blame] | 1140 | // InfeBox parse returns ERROR_UNSUPPORTED if the box if an unsupported |
| 1141 | // version. Ignore this error as it's not fatal. |
| 1142 | return (err == ERROR_UNSUPPORTED) ? OK : err; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | ////////////////////////////////////////////////////////////////// |
| 1146 | |
| 1147 | ItemTable::ItemTable(const sp<DataSource> &source) |
| 1148 | : mDataSource(source), |
| 1149 | mPrimaryItemId(0), |
| 1150 | mIdatOffset(0), |
| 1151 | mIdatSize(0), |
| 1152 | mImageItemsValid(false), |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1153 | mCurrentItemIndex(0) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1154 | mRequiredBoxes.insert('iprp'); |
| 1155 | mRequiredBoxes.insert('iloc'); |
| 1156 | mRequiredBoxes.insert('pitm'); |
| 1157 | mRequiredBoxes.insert('iinf'); |
| 1158 | } |
| 1159 | |
| 1160 | ItemTable::~ItemTable() {} |
| 1161 | |
| 1162 | status_t ItemTable::parse(uint32_t type, off64_t data_offset, size_t chunk_data_size) { |
| 1163 | switch(type) { |
| 1164 | case FOURCC('i', 'l', 'o', 'c'): |
| 1165 | { |
| 1166 | return parseIlocBox(data_offset, chunk_data_size); |
| 1167 | } |
| 1168 | case FOURCC('i', 'i', 'n', 'f'): |
| 1169 | { |
| 1170 | return parseIinfBox(data_offset, chunk_data_size); |
| 1171 | } |
| 1172 | case FOURCC('i', 'p', 'r', 'p'): |
| 1173 | { |
| 1174 | return parseIprpBox(data_offset, chunk_data_size); |
| 1175 | } |
| 1176 | case FOURCC('p', 'i', 't', 'm'): |
| 1177 | { |
| 1178 | return parsePitmBox(data_offset, chunk_data_size); |
| 1179 | } |
| 1180 | case FOURCC('i', 'd', 'a', 't'): |
| 1181 | { |
| 1182 | return parseIdatBox(data_offset, chunk_data_size); |
| 1183 | } |
| 1184 | case FOURCC('i', 'r', 'e', 'f'): |
| 1185 | { |
| 1186 | return parseIrefBox(data_offset, chunk_data_size); |
| 1187 | } |
| 1188 | case FOURCC('i', 'p', 'r', 'o'): |
| 1189 | { |
| 1190 | ALOGW("ipro box not supported!"); |
| 1191 | break; |
| 1192 | } |
| 1193 | default: |
| 1194 | { |
| 1195 | ALOGW("unrecognized box type: 0x%x", type); |
| 1196 | break; |
| 1197 | } |
| 1198 | } |
| 1199 | return ERROR_UNSUPPORTED; |
| 1200 | } |
| 1201 | |
| 1202 | status_t ItemTable::parseIlocBox(off64_t offset, size_t size) { |
| 1203 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 1204 | |
| 1205 | IlocBox ilocBox(mDataSource, &mItemLocs); |
| 1206 | status_t err = ilocBox.parse(offset, size); |
| 1207 | if (err != OK) { |
| 1208 | return err; |
| 1209 | } |
| 1210 | |
| 1211 | if (ilocBox.hasConstructMethod1()) { |
| 1212 | mRequiredBoxes.insert('idat'); |
| 1213 | } |
| 1214 | |
| 1215 | return buildImageItemsIfPossible('iloc'); |
| 1216 | } |
| 1217 | |
| 1218 | status_t ItemTable::parseIinfBox(off64_t offset, size_t size) { |
| 1219 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 1220 | |
| 1221 | IinfBox iinfBox(mDataSource, &mItemInfos); |
| 1222 | status_t err = iinfBox.parse(offset, size); |
| 1223 | if (err != OK) { |
| 1224 | return err; |
| 1225 | } |
| 1226 | |
| 1227 | if (iinfBox.hasGrids()) { |
| 1228 | mRequiredBoxes.insert('iref'); |
| 1229 | } |
| 1230 | |
| 1231 | return buildImageItemsIfPossible('iinf'); |
| 1232 | } |
| 1233 | |
| 1234 | status_t ItemTable::parsePitmBox(off64_t offset, size_t size) { |
| 1235 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 1236 | |
| 1237 | PitmBox pitmBox(mDataSource); |
| 1238 | status_t err = pitmBox.parse(offset, size, &mPrimaryItemId); |
| 1239 | if (err != OK) { |
| 1240 | return err; |
| 1241 | } |
| 1242 | |
| 1243 | return buildImageItemsIfPossible('pitm'); |
| 1244 | } |
| 1245 | |
| 1246 | status_t ItemTable::parseIprpBox(off64_t offset, size_t size) { |
| 1247 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 1248 | |
| 1249 | IprpBox iprpBox(mDataSource, &mItemProperties, &mAssociations); |
| 1250 | status_t err = iprpBox.parse(offset, size); |
| 1251 | if (err != OK) { |
| 1252 | return err; |
| 1253 | } |
| 1254 | |
| 1255 | return buildImageItemsIfPossible('iprp'); |
| 1256 | } |
| 1257 | |
| 1258 | status_t ItemTable::parseIdatBox(off64_t offset, size_t size) { |
| 1259 | ALOGV("%s: idat offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 1260 | |
| 1261 | // only remember the offset and size of idat box for later use |
| 1262 | mIdatOffset = offset; |
| 1263 | mIdatSize = size; |
| 1264 | |
| 1265 | return buildImageItemsIfPossible('idat'); |
| 1266 | } |
| 1267 | |
| 1268 | status_t ItemTable::parseIrefBox(off64_t offset, size_t size) { |
| 1269 | ALOGV("%s: offset %lld, size %zu", __FUNCTION__, (long long)offset, size); |
| 1270 | |
| 1271 | IrefBox irefBox(mDataSource, &mItemReferences); |
| 1272 | status_t err = irefBox.parse(offset, size); |
| 1273 | if (err != OK) { |
| 1274 | return err; |
| 1275 | } |
| 1276 | |
| 1277 | return buildImageItemsIfPossible('iref'); |
| 1278 | } |
| 1279 | |
| 1280 | status_t ItemTable::buildImageItemsIfPossible(uint32_t type) { |
| 1281 | if (mImageItemsValid) { |
| 1282 | return OK; |
| 1283 | } |
| 1284 | |
| 1285 | mBoxesSeen.insert(type); |
| 1286 | |
| 1287 | // need at least 'iprp', 'iloc', 'pitm', 'iinf'; |
| 1288 | // need 'idat' if any items used construction_method of 2; |
| 1289 | // need 'iref' if there are grids. |
| 1290 | if (!std::includes( |
| 1291 | mBoxesSeen.begin(), mBoxesSeen.end(), |
| 1292 | mRequiredBoxes.begin(), mRequiredBoxes.end())) { |
| 1293 | return OK; |
| 1294 | } |
| 1295 | |
| 1296 | ALOGV("building image table..."); |
| 1297 | |
| 1298 | for (size_t i = 0; i < mItemInfos.size(); i++) { |
| 1299 | const ItemInfo &info = mItemInfos[i]; |
| 1300 | |
| 1301 | |
| 1302 | // ignore non-image items |
| 1303 | if (info.itemType != FOURCC('g', 'r', 'i', 'd') && |
| 1304 | info.itemType != FOURCC('h', 'v', 'c', '1')) { |
| 1305 | continue; |
| 1306 | } |
| 1307 | |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1308 | ssize_t itemIndex = mItemIdToItemMap.indexOfKey(info.itemId); |
| 1309 | if (itemIndex >= 0) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1310 | ALOGW("ignoring duplicate image item id %d", info.itemId); |
| 1311 | continue; |
| 1312 | } |
| 1313 | |
| 1314 | ssize_t ilocIndex = mItemLocs.indexOfKey(info.itemId); |
| 1315 | if (ilocIndex < 0) { |
| 1316 | ALOGE("iloc missing for image item id %d", info.itemId); |
| 1317 | continue; |
| 1318 | } |
| 1319 | const ItemLoc &iloc = mItemLocs[ilocIndex]; |
| 1320 | |
| 1321 | off64_t offset; |
| 1322 | size_t size; |
| 1323 | if (iloc.getLoc(&offset, &size, mIdatOffset, mIdatSize) != OK) { |
| 1324 | return ERROR_MALFORMED; |
| 1325 | } |
| 1326 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1327 | ImageItem image(info.itemType, info.itemId, info.hidden); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1328 | |
| 1329 | ALOGV("adding %s: itemId %d", image.isGrid() ? "grid" : "image", info.itemId); |
| 1330 | |
| 1331 | if (image.isGrid()) { |
| 1332 | if (size > 12) { |
| 1333 | return ERROR_MALFORMED; |
| 1334 | } |
| 1335 | uint8_t buf[12]; |
| 1336 | if (!mDataSource->readAt(offset, buf, size)) { |
| 1337 | return ERROR_IO; |
| 1338 | } |
| 1339 | |
| 1340 | image.rows = buf[2] + 1; |
| 1341 | image.columns = buf[3] + 1; |
| 1342 | |
| 1343 | ALOGV("rows %d, columans %d", image.rows, image.columns); |
| 1344 | } else { |
| 1345 | image.offset = offset; |
| 1346 | image.size = size; |
| 1347 | } |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1348 | mItemIdToItemMap.add(info.itemId, image); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | for (size_t i = 0; i < mAssociations.size(); i++) { |
| 1352 | attachProperty(mAssociations[i]); |
| 1353 | } |
| 1354 | |
| 1355 | for (size_t i = 0; i < mItemReferences.size(); i++) { |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1356 | mItemReferences[i]->apply(mItemIdToItemMap); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1359 | bool foundPrimary = false; |
| 1360 | for (size_t i = 0; i < mItemIdToItemMap.size(); i++) { |
| 1361 | // add all non-hidden images, also add the primary even if it's marked |
| 1362 | // hidden, in case the primary is set to a thumbnail |
| 1363 | bool isPrimary = (mItemIdToItemMap[i].itemId == mPrimaryItemId); |
| 1364 | if (!mItemIdToItemMap[i].hidden || isPrimary) { |
| 1365 | mDisplayables.push_back(i); |
| 1366 | } |
| 1367 | foundPrimary |= isPrimary; |
| 1368 | } |
| 1369 | |
| 1370 | ALOGV("found %zu displayables", mDisplayables.size()); |
| 1371 | |
| 1372 | // fail if no displayables are found |
| 1373 | if (mDisplayables.empty()) { |
| 1374 | return ERROR_MALFORMED; |
| 1375 | } |
| 1376 | |
| 1377 | // if the primary item id is invalid, set primary to the first displayable |
| 1378 | if (!foundPrimary) { |
| 1379 | mPrimaryItemId = mItemIdToItemMap[mDisplayables[0]].itemId; |
| 1380 | } |
| 1381 | |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1382 | mImageItemsValid = true; |
| 1383 | return OK; |
| 1384 | } |
| 1385 | |
| 1386 | void ItemTable::attachProperty(const AssociationEntry &association) { |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1387 | ssize_t itemIndex = mItemIdToItemMap.indexOfKey(association.itemId); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1388 | |
| 1389 | // ignore non-image items |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1390 | if (itemIndex < 0) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1391 | return; |
| 1392 | } |
| 1393 | |
| 1394 | uint16_t propertyIndex = association.index; |
| 1395 | if (propertyIndex >= mItemProperties.size()) { |
| 1396 | ALOGW("Ignoring invalid property index %d", propertyIndex); |
| 1397 | return; |
| 1398 | } |
| 1399 | |
| 1400 | ALOGV("attach property %d to item id %d)", |
| 1401 | propertyIndex, association.itemId); |
| 1402 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1403 | mItemProperties[propertyIndex]->attachTo(mItemIdToItemMap.editValueAt(itemIndex)); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1406 | uint32_t ItemTable::countImages() const { |
| 1407 | return mImageItemsValid ? mDisplayables.size() : 0; |
| 1408 | } |
| 1409 | |
| 1410 | sp<MetaData> ItemTable::getImageMeta(const uint32_t imageIndex) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1411 | if (!mImageItemsValid) { |
| 1412 | return NULL; |
| 1413 | } |
| 1414 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1415 | if (imageIndex >= mDisplayables.size()) { |
| 1416 | ALOGE("%s: invalid image index %u", __FUNCTION__, imageIndex); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1417 | return NULL; |
| 1418 | } |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1419 | const uint32_t itemIndex = mDisplayables[imageIndex]; |
| 1420 | ALOGV("image[%u]: item index %u", imageIndex, itemIndex); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1421 | |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1422 | const ImageItem *image = &mItemIdToItemMap[itemIndex]; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1423 | |
| 1424 | sp<MetaData> meta = new MetaData; |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1425 | meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1426 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1427 | if (image->itemId == mPrimaryItemId) { |
| 1428 | meta->setInt32(kKeyIsPrimaryImage, 1); |
| 1429 | } |
| 1430 | |
| 1431 | ALOGV("image[%u]: size %dx%d", imageIndex, image->width, image->height); |
| 1432 | |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1433 | meta->setInt32(kKeyWidth, image->width); |
| 1434 | meta->setInt32(kKeyHeight, image->height); |
| 1435 | if (image->rotation != 0) { |
Chong Zhang | 686c23b | 2017-10-05 15:16:59 -0700 | [diff] [blame] | 1436 | // Rotation angle in HEIF is CCW, convert to CW here to be |
| 1437 | // consistent with the other media formats. |
| 1438 | switch(image->rotation) { |
| 1439 | case 90: meta->setInt32(kKeyRotation, 270); break; |
| 1440 | case 180: meta->setInt32(kKeyRotation, 180); break; |
| 1441 | case 270: meta->setInt32(kKeyRotation, 90); break; |
| 1442 | default: break; // don't set if invalid |
| 1443 | } |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1444 | } |
| 1445 | meta->setInt32(kKeyMaxInputSize, image->width * image->height * 1.5); |
| 1446 | |
| 1447 | if (!image->thumbnails.empty()) { |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1448 | ssize_t thumbItemIndex = mItemIdToItemMap.indexOfKey(image->thumbnails[0]); |
| 1449 | if (thumbItemIndex >= 0) { |
| 1450 | const ImageItem &thumbnail = mItemIdToItemMap[thumbItemIndex]; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1451 | |
| 1452 | meta->setInt32(kKeyThumbnailWidth, thumbnail.width); |
| 1453 | meta->setInt32(kKeyThumbnailHeight, thumbnail.height); |
| 1454 | meta->setData(kKeyThumbnailHVCC, kTypeHVCC, |
| 1455 | thumbnail.hvcc->data(), thumbnail.hvcc->size()); |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1456 | ALOGV("image[%u]: thumbnail: size %dx%d, item index %zd", |
| 1457 | imageIndex, thumbnail.width, thumbnail.height, thumbItemIndex); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1458 | } else { |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1459 | ALOGW("%s: Referenced thumbnail does not exist!", __FUNCTION__); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | if (image->isGrid()) { |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1464 | ssize_t tileItemIndex = mItemIdToItemMap.indexOfKey(image->dimgRefs[0]); |
| 1465 | if (tileItemIndex < 0) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1466 | return NULL; |
| 1467 | } |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1468 | meta->setInt32(kKeyGridRows, image->rows); |
| 1469 | meta->setInt32(kKeyGridCols, image->columns); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1470 | |
Chong Zhang | ee079fe | 2017-08-23 13:51:17 -0700 | [diff] [blame] | 1471 | // point image to the first tile for grid size and HVCC |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1472 | image = &mItemIdToItemMap.editValueAt(tileItemIndex); |
Chong Zhang | ee079fe | 2017-08-23 13:51:17 -0700 | [diff] [blame] | 1473 | meta->setInt32(kKeyGridWidth, image->width); |
| 1474 | meta->setInt32(kKeyGridHeight, image->height); |
| 1475 | meta->setInt32(kKeyMaxInputSize, image->width * image->height * 1.5); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | if (image->hvcc == NULL) { |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1479 | ALOGE("%s: hvcc is missing for image[%u]!", __FUNCTION__, imageIndex); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1480 | return NULL; |
| 1481 | } |
| 1482 | meta->setData(kKeyHVCC, kTypeHVCC, image->hvcc->data(), image->hvcc->size()); |
| 1483 | |
| 1484 | if (image->icc != NULL) { |
| 1485 | meta->setData(kKeyIccProfile, 0, image->icc->data(), image->icc->size()); |
| 1486 | } |
| 1487 | return meta; |
| 1488 | } |
| 1489 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1490 | status_t ItemTable::findImageItem(const uint32_t imageIndex, uint32_t *itemIndex) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1491 | if (!mImageItemsValid) { |
| 1492 | return INVALID_OPERATION; |
| 1493 | } |
| 1494 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1495 | if (imageIndex >= mDisplayables.size()) { |
| 1496 | ALOGE("%s: invalid image index %d", __FUNCTION__, imageIndex); |
| 1497 | return BAD_VALUE; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1500 | *itemIndex = mDisplayables[imageIndex]; |
| 1501 | |
| 1502 | ALOGV("image[%u]: item index %u", imageIndex, *itemIndex); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1503 | return OK; |
| 1504 | } |
| 1505 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1506 | status_t ItemTable::findThumbnailItem(const uint32_t imageIndex, uint32_t *itemIndex) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1507 | if (!mImageItemsValid) { |
| 1508 | return INVALID_OPERATION; |
| 1509 | } |
| 1510 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1511 | if (imageIndex >= mDisplayables.size()) { |
| 1512 | ALOGE("%s: invalid image index %d", __FUNCTION__, imageIndex); |
| 1513 | return BAD_VALUE; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1516 | uint32_t masterItemIndex = mDisplayables[imageIndex]; |
| 1517 | |
| 1518 | const ImageItem &masterImage = mItemIdToItemMap[masterItemIndex]; |
| 1519 | if (masterImage.thumbnails.empty()) { |
| 1520 | *itemIndex = masterItemIndex; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1521 | return OK; |
| 1522 | } |
| 1523 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1524 | ssize_t thumbItemIndex = mItemIdToItemMap.indexOfKey(masterImage.thumbnails[0]); |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1525 | if (thumbItemIndex < 0) { |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame^] | 1526 | ALOGW("%s: Thumbnail item id %d not found, use master instead", |
| 1527 | __FUNCTION__, masterImage.thumbnails[0]); |
| 1528 | *itemIndex = masterItemIndex; |
| 1529 | return OK; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1532 | *itemIndex = thumbItemIndex; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1533 | return OK; |
| 1534 | } |
| 1535 | |
| 1536 | status_t ItemTable::getImageOffsetAndSize( |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1537 | uint32_t *itemIndex, off64_t *offset, size_t *size) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1538 | if (!mImageItemsValid) { |
| 1539 | return INVALID_OPERATION; |
| 1540 | } |
| 1541 | |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1542 | if (itemIndex != NULL) { |
| 1543 | if (*itemIndex >= mItemIdToItemMap.size()) { |
| 1544 | ALOGE("%s: Bad item index!", __FUNCTION__); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1545 | return BAD_VALUE; |
| 1546 | } |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1547 | mCurrentItemIndex = *itemIndex; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1550 | ImageItem &image = mItemIdToItemMap.editValueAt(mCurrentItemIndex); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1551 | if (image.isGrid()) { |
| 1552 | uint32_t tileItemId; |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1553 | status_t err = image.getNextTileItemId(&tileItemId, itemIndex != NULL); |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1554 | if (err != OK) { |
| 1555 | return err; |
| 1556 | } |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1557 | ssize_t tileItemIndex = mItemIdToItemMap.indexOfKey(tileItemId); |
| 1558 | if (tileItemIndex < 0) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1559 | return ERROR_END_OF_STREAM; |
| 1560 | } |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1561 | *offset = mItemIdToItemMap[tileItemIndex].offset; |
| 1562 | *size = mItemIdToItemMap[tileItemIndex].size; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1563 | } else { |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1564 | if (itemIndex == NULL) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1565 | // For single images, we only allow it to be read once, after that |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1566 | // it's EOS. New item index must be requested each time. |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1567 | return ERROR_END_OF_STREAM; |
| 1568 | } |
Chong Zhang | ecd0813 | 2017-10-05 16:09:29 -0700 | [diff] [blame] | 1569 | *offset = mItemIdToItemMap[mCurrentItemIndex].offset; |
| 1570 | *size = mItemIdToItemMap[mCurrentItemIndex].size; |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | return OK; |
| 1574 | } |
| 1575 | |
| 1576 | } // namespace heif |
| 1577 | |
| 1578 | } // namespace android |