Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
358 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Utils used to parse miaf-based files (avif/heic/heif) | ||
// | ||
// We look for last `ispe` box (first box is presumably a thumbnail). Images with metadata encoded | ||
// after image data are not supported. Image sequences sometimes can't be recognized if they don't have `ispe` box. | ||
// | ||
// ISO media file spec: | ||
// https://web.archive.org/web/20180219054429/http://l.web.umkc.edu/lizhu/teaching/2016sp.video-communication/ref/mp4.pdf | ||
// | ||
// ISO image file format spec: | ||
// https://standards.iso.org/ittf/PubliclyAvailableStandards/c066067_ISO_IEC_23008-12_2017.zip | ||
// | ||
|
||
'use strict'; | ||
|
||
/* eslint-disable consistent-return */ | ||
|
||
var readUInt32BE = require('./common').readUInt32BE; | ||
|
||
/* | ||
* interface Box { | ||
* size: uint32; // if size == 0, box lasts until EOF | ||
* boxtype: char[4]; | ||
* largesize?: uint64; // only if size == 1 | ||
* usertype?: char[16]; // only if boxtype == 'uuid' | ||
* } | ||
*/ | ||
function readBox(data, offset) { | ||
if (data.length < 4 + offset) return null; | ||
|
||
var size = readUInt32BE(data, offset); | ||
|
||
// size includes first 4 bytes (length) | ||
if (data.length < size + offset || size < 8) return null; | ||
|
||
// if size === 1, real size is following uint64 (only for big boxes, not needed) | ||
// if size === 0, real size is until the end of the file (only for big boxes, not needed) | ||
|
||
return { | ||
type: String.fromCharCode.apply(null, data.slice(offset + 4, offset + 8)), | ||
data: data.slice(offset + 8, offset + size), | ||
end: offset + size | ||
}; | ||
} | ||
|
||
|
||
module.exports.readBox = readBox; | ||
|
||
|
||
module.exports.readSizeFromMeta = function (data) { | ||
var newData, box, offset; | ||
|
||
for (offset = 4 /* version+flags */, newData = null; (box = readBox(data, offset)); offset = box.end) { | ||
if (box.type === 'iprp') { | ||
newData = box.data; | ||
break; | ||
} | ||
} | ||
|
||
if (!newData) return; | ||
data = newData; | ||
|
||
for (offset = 0, newData = null; (box = readBox(data, offset)); offset = box.end) { | ||
if (box.type === 'ipco') { | ||
newData = box.data; | ||
break; | ||
} | ||
} | ||
|
||
if (!newData) return; | ||
data = newData; | ||
|
||
for (offset = 0, newData = null; (box = readBox(data, offset)); offset = box.end) { | ||
if (box.type === 'ispe') { | ||
newData = box.data; | ||
// no break here (!), second ispe may override the first, see | ||
// https://github.com/tigranbs/test-heic-images | ||
} | ||
} | ||
|
||
if (!newData) return; | ||
data = newData; | ||
|
||
return { | ||
width: readUInt32BE(data, 4), | ||
height: readUInt32BE(data, 8) | ||
}; | ||
}; | ||
|
||
|
||
module.exports.getMimeType = function (data) { | ||
var str = String.fromCharCode.apply(null, data); | ||
|
||
var brand = str.slice(0, 4); | ||
var compat = {}; | ||
|
||
compat[brand] = true; | ||
(str.slice(8).match(/..../g) || []).forEach(function (b) { | ||
compat[b] = true; | ||
}); | ||
|
||
// heic and avif are superset of miaf, so they should all list mif1 as compatible | ||
if (!compat.mif1 && !compat.msf1 && !compat.miaf) return; | ||
|
||
if (brand === 'avif' || brand === 'avis' || brand === 'avio') { | ||
// `.avifs` and `image/avif-sequence` are removed from spec, all files have single type | ||
return { type: 'avif', mime: 'image/avif' }; | ||
} | ||
|
||
// https://nokiatech.github.io/heif/technical.html | ||
if (brand === 'heic' || brand === 'heix') { | ||
return { type: 'heic', mime: 'image/heic' }; | ||
} | ||
|
||
if (brand === 'hevc' || brand === 'hevx') { | ||
return { type: 'heic', mime: 'image/heic-sequence' }; | ||
} | ||
|
||
if (compat.avif || compat.avis) { | ||
return { type: 'avif', mime: 'image/avif' }; | ||
} | ||
|
||
if (compat.heic || compat.heix || compat.hevc || compat.hevx || compat.heis) { | ||
if (compat.msf1) { | ||
return { type: 'heif', mime: 'image/heif-sequence' }; | ||
} | ||
return { type: 'heif', mime: 'image/heif' }; | ||
} | ||
|
||
return { type: 'avif', mime: 'image/avif' }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.