comments: containers IsoBmff Mux / Meta, Iso9660
This commit is contained in:
1 parent
3cda652143
commit
24cbddaafa
3 files changed
+8
-44
No files matched your search
@@ -1,18 +1,8 @@
|
|||||||
--!strict
|
--!strict
|
||||||
-- containers/Iso9660: ISO 9660 (ECMA-119) CD / DVD images, read. The volume descriptors from sector 16 (primary,
|
-- containers/Iso9660: ISO 9660 (ECMA-119) images, read; from ECMA-119, SUSP / RRIP 1.12, Joliet and El Torito 1.0, no
|
||||||
-- supplementary, boot record, terminator); the directory tree walked through directory records (records never cross
|
-- code ported. Name sets: Rock Ridge, else Joliet (%/@, %/C, %/E), else plain (";1" and a trailing "." dropped);
|
||||||
-- a sector; multi-extent files are joined; associated files skipped; interleaved files listed, their read is
|
-- multi-extent files joined, CL / RE / PL relocations resolved, El Torito catalog in info.boot. Not supported: zisofs
|
||||||
-- Err.UNSUPPORTED). Three name sets: plain ISO 9660 (";1" versions and a lone trailing "." dropped), Joliet (UCS-2 names
|
-- (read returns the stored bytes), interleaved files (Err.UNSUPPORTED), UDF, writing.
|
||||||
-- of a supplementary descriptor with the %/@, %/C or %/E escape) and Rock Ridge (SUSP 1.12 / RRIP 1.12 in the
|
|
||||||
-- primary tree: SP, CE continuation areas, NM names, PX modes / links / ids, PN devices, TF times, SL symlinks, CL / RE
|
|
||||||
-- / PL relocated deep directories, with the rr_moved directory hidden when it ends up empty). The tree used is Rock
|
|
||||||
-- Ridge when present, else Joliet, else plain (opts.prefer overrides). El Torito: the boot record's catalog
|
|
||||||
-- (validation entry, initial / default entry) in info.boot. Written from ECMA-119, the SUSP / RRIP 1.12 and Joliet
|
|
||||||
-- specifications and El Torito 1.0; no code ported. Not supported: zisofs (ZF) compressed files (listed; read returns
|
|
||||||
-- the stored bytes), UDF, writing.
|
|
||||||
--
|
|
||||||
-- local entries, info = Iso9660.read(buf, offset?, length?, opts?) -- { Entry }, Info | nil, Err
|
|
||||||
-- local data, err = Iso9660.extract(buf, base, entry) -- the entry's bytes (a copy of its extents)
|
|
||||||
local Err = require("@rosetta/core/Err")
|
local Err = require("@rosetta/core/Err")
|
||||||
local Types = require("@rosetta/Types")
|
local Types = require("@rosetta/Types")
|
||||||
local Text = require("@rosetta/codecs/text/Text")
|
local Text = require("@rosetta/codecs/text/Text")
|
||||||
@@ -45,7 +35,6 @@ export type ReadOptions = {
|
|||||||
local SECTOR = 2048
|
local SECTOR = 2048
|
||||||
local S_TYPES: { [number]: string } = { [0x8000] = "0", [0x4000] = "5", [0xA000] = "2", [0x2000] = "3", [0x6000] = "4", [0x1000] = "6", [0xC000] = "s" }
|
local S_TYPES: { [number]: string } = { [0x8000] = "0", [0x4000] = "5", [0xA000] = "2", [0x2000] = "3", [0x6000] = "4", [0x1000] = "6", [0xC000] = "s" }
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ fields
|
|
||||||
local function u16(b: buffer, o: number): number
|
local function u16(b: buffer, o: number): number
|
||||||
return buffer.readu16(b, o)
|
return buffer.readu16(b, o)
|
||||||
end
|
end
|
||||||
@@ -78,10 +67,8 @@ local function longDate(b: buffer, o: number): number?
|
|||||||
return Types.Date.toUnix({ kind = "datetime", year = tonumber(y), month = tonumber(mo), day = tonumber(d), hour = tonumber(h), minute = tonumber(mi), second = tonumber(se), offset = off * 15 })
|
return Types.Date.toUnix({ kind = "datetime", year = tonumber(y), month = tonumber(mo), day = tonumber(d), hour = tonumber(h), minute = tonumber(mi), second = tonumber(se), offset = off * 15 })
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ SUSP / Rock Ridge
|
|
||||||
type Susp = { [string]: any }
|
type Susp = { [string]: any }
|
||||||
|
|
||||||
-- the SUSP entries of a system use area (following CE continuation areas) -> { { sig, at, len } }
|
|
||||||
local function suspEntries(b: buffer, base: number, limit: number, o: number, e: number, bs: number, budget: { number }): { { any } }
|
local function suspEntries(b: buffer, base: number, limit: number, o: number, e: number, bs: number, budget: { number }): { { any } }
|
||||||
local out = {}
|
local out = {}
|
||||||
while o + 4 <= e do
|
while o + 4 <= e do
|
||||||
@@ -116,7 +103,6 @@ local function suspEntries(b: buffer, base: number, limit: number, o: number, e:
|
|||||||
return out
|
return out
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Rock Ridge fields of a record: { name, mode, uid, gid, nlink, mtime, link, rdev, child, relocated, zisofs }
|
|
||||||
local function rockRidge(b: buffer, base: number, limit: number, o: number, e: number, bs: number): Susp
|
local function rockRidge(b: buffer, base: number, limit: number, o: number, e: number, bs: number): Susp
|
||||||
local rr: Susp = {}
|
local rr: Susp = {}
|
||||||
local name: { string }? = nil
|
local name: { string }? = nil
|
||||||
@@ -189,7 +175,6 @@ local function rockRidge(b: buffer, base: number, limit: number, o: number, e: n
|
|||||||
return rr
|
return rr
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ directories
|
|
||||||
type Record = {
|
type Record = {
|
||||||
name: string,
|
name: string,
|
||||||
dir: boolean,
|
dir: boolean,
|
||||||
@@ -202,7 +187,6 @@ type Record = {
|
|||||||
rr: Susp?,
|
rr: Susp?,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- the records of a directory extent (without "." and "..") | nil, Err
|
|
||||||
local function records(b: buffer, base: number, limit: number, at: number, size: number, bs: number, joliet: boolean, rrSkip: number?): ({ Record }?, Err.Err?)
|
local function records(b: buffer, base: number, limit: number, at: number, size: number, bs: number, joliet: boolean, rrSkip: number?): ({ Record }?, Err.Err?)
|
||||||
if at + size > limit then
|
if at + size > limit then
|
||||||
return nil, Err.new(Err.TRUNCATED, "directory past the end", at)
|
return nil, Err.new(Err.TRUNCATED, "directory past the end", at)
|
||||||
@@ -258,7 +242,6 @@ local function records(b: buffer, base: number, limit: number, at: number, size:
|
|||||||
return out, nil
|
return out, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- the root record of a volume descriptor at vd -> lba, size
|
|
||||||
local function root(b: buffer, vd: number): (number, number)
|
local function root(b: buffer, vd: number): (number, number)
|
||||||
return u32(b, vd + 156 + 2), u32(b, vd + 156 + 10)
|
return u32(b, vd + 156 + 2), u32(b, vd + 156 + 10)
|
||||||
end
|
end
|
||||||
@@ -281,7 +264,6 @@ local function rockRidgeSkip(b: buffer, base: number, limit: number, lba: number
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ El Torito
|
|
||||||
local function boot(b: buffer, base: number, limit: number, vd: number, bs: number): { [string]: any }?
|
local function boot(b: buffer, base: number, limit: number, vd: number, bs: number): { [string]: any }?
|
||||||
if str(b, vd + 7, 32) ~= "EL TORITO SPECIFICATION" then
|
if str(b, vd + 7, 32) ~= "EL TORITO SPECIFICATION" then
|
||||||
return nil
|
return nil
|
||||||
@@ -309,8 +291,6 @@ local function boot(b: buffer, base: number, limit: number, vd: number, bs: numb
|
|||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ read
|
|
||||||
-- read(buf, offset?, length?, opts?) -> { Entry }, Info | nil, Err
|
|
||||||
function Iso9660.read(b: buffer, offset: number?, length: number?, opts: ReadOptions?): ({ Entry }?, any)
|
function Iso9660.read(b: buffer, offset: number?, length: number?, opts: ReadOptions?): ({ Entry }?, any)
|
||||||
local base = offset or 0
|
local base = offset or 0
|
||||||
local limit = math.min(buffer.len(b), base + (length or (buffer.len(b) - base)))
|
local limit = math.min(buffer.len(b), base + (length or (buffer.len(b) - base)))
|
||||||
@@ -498,7 +478,6 @@ function Iso9660.read(b: buffer, offset: number?, length: number?, opts: ReadOpt
|
|||||||
return out, info
|
return out, info
|
||||||
end
|
end
|
||||||
|
|
||||||
-- extract(buf, entry, limit?) -> a copy of the entry's data | nil, Err
|
|
||||||
function Iso9660.extract(b: buffer, e: Entry, limit: number?): (buffer?, Err.Err?)
|
function Iso9660.extract(b: buffer, e: Entry, limit: number?): (buffer?, Err.Err?)
|
||||||
if e.interleaved then
|
if e.interleaved then
|
||||||
return nil, Err.new(Err.UNSUPPORTED, "interleaved file " .. e.name)
|
return nil, Err.new(Err.UNSUPPORTED, "interleaved file " .. e.name)
|
||||||
@@ -516,7 +495,6 @@ function Iso9660.extract(b: buffer, e: Entry, limit: number?): (buffer?, Err.Err
|
|||||||
return out, nil
|
return out, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- valid(buf, offset?) -> true when a volume descriptor "CD001" sits at sector 16
|
|
||||||
function Iso9660.valid(b: buffer, offset: number?): boolean
|
function Iso9660.valid(b: buffer, offset: number?): boolean
|
||||||
local p = (offset or 0) + 16 * SECTOR
|
local p = (offset or 0) + 16 * SECTOR
|
||||||
return p + 6 <= buffer.len(b) and buffer.readstring(b, p + 1, 5) == "CD001"
|
return p + 6 <= buffer.len(b) and buffer.readstring(b, p + 1, 5) == "CD001"
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ local function uint(r: Bytes.Reader, size: number): number
|
|||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ HEIF items
|
|
||||||
export type Property = { type: string, box: Box.Box, [string]: any }
|
export type Property = { type: string, box: Box.Box, [string]: any }
|
||||||
export type Item = {
|
export type Item = {
|
||||||
id: number,
|
id: number,
|
||||||
@@ -103,7 +102,6 @@ local function property(b: buffer, box: Box.Box): Property
|
|||||||
return p
|
return p
|
||||||
end
|
end
|
||||||
|
|
||||||
-- items(buf, meta) -> Items | nil, Err (meta: a top-level 'meta' box)
|
|
||||||
function Meta.items(b: buffer, meta: Box.Box): (Items?, Err.Err?)
|
function Meta.items(b: buffer, meta: Box.Box): (Items?, Err.Err?)
|
||||||
local out: Items = { items = {}, order = {}, properties = {} }
|
local out: Items = { items = {}, order = {}, properties = {} }
|
||||||
local pitm = Box.child(meta, "pitm")
|
local pitm = Box.child(meta, "pitm")
|
||||||
@@ -253,8 +251,6 @@ function Meta.items(b: buffer, meta: Box.Box): (Items?, Err.Err?)
|
|||||||
return out, nil
|
return out, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- itemData(buf, base, limit, items, id) -> buffer | nil, Err: the item's bytes (extents concatenated); base / limit
|
|
||||||
-- bound the file in the buffer
|
|
||||||
function Meta.itemData(b: buffer, base: number, limit: number, items: Items, id: number): (buffer?, Err.Err?)
|
function Meta.itemData(b: buffer, base: number, limit: number, items: Items, id: number): (buffer?, Err.Err?)
|
||||||
local it = items.items[id]
|
local it = items.items[id]
|
||||||
if not it then
|
if not it then
|
||||||
@@ -296,7 +292,6 @@ function Meta.itemData(b: buffer, base: number, limit: number, items: Items, id:
|
|||||||
return out, nil
|
return out, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ iTunes tags
|
|
||||||
-- ilst item -> standard tag name
|
-- ilst item -> standard tag name
|
||||||
local TAG: { [string]: string } = {
|
local TAG: { [string]: string } = {
|
||||||
["\169nam"] = "title", ["\169ART"] = "artist", aART = "albumartist", ["\169alb"] = "album", ["\169day"] = "date",
|
["\169nam"] = "title", ["\169ART"] = "artist", aART = "albumartist", ["\169alb"] = "album", ["\169day"] = "date",
|
||||||
@@ -330,7 +325,6 @@ local GENRES = {
|
|||||||
|
|
||||||
export type Picture = { mime: string, data: buffer }
|
export type Picture = { mime: string, data: buffer }
|
||||||
|
|
||||||
-- tags(buf, ilst) -> Tags, { Picture }
|
|
||||||
function Meta.tags(b: buffer, ilst: Box.Box?): ({ [string]: string }, { Picture })
|
function Meta.tags(b: buffer, ilst: Box.Box?): ({ [string]: string }, { Picture })
|
||||||
local tags: { [string]: string } = {}
|
local tags: { [string]: string } = {}
|
||||||
local pics: { Picture } = {}
|
local pics: { Picture } = {}
|
||||||
@@ -396,7 +390,6 @@ function Meta.tags(b: buffer, ilst: Box.Box?): ({ [string]: string }, { Picture
|
|||||||
return tags, pics
|
return tags, pics
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ ilst writer
|
|
||||||
local function box(w: Bytes.Writer, typ: string, body: (Bytes.Writer) -> ())
|
local function box(w: Bytes.Writer, typ: string, body: (Bytes.Writer) -> ())
|
||||||
local start = w:tell()
|
local start = w:tell()
|
||||||
w:u32be(0)
|
w:u32be(0)
|
||||||
@@ -426,8 +419,6 @@ local function num(s: string?): number
|
|||||||
return math.clamp(math.floor(v), 0, 65535)
|
return math.clamp(math.floor(v), 0, 65535)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- writeIlst(w, tags, pictures?): an ilst box with the tags (standard names -> iTunes atoms; unknown names -> '----'
|
|
||||||
-- freeform com.apple.iTunes items) and cover pictures
|
|
||||||
function Meta.writeIlst(w: Bytes.Writer, tags: { [string]: string }, pictures: { Picture }?)
|
function Meta.writeIlst(w: Bytes.Writer, tags: { [string]: string }, pictures: { Picture }?)
|
||||||
box(w, "ilst", function()
|
box(w, "ilst", function()
|
||||||
local keys = {}
|
local keys = {}
|
||||||
@@ -506,7 +497,7 @@ function Meta.writeIlst(w: Bytes.Writer, tags: { [string]: string }, pictures: {
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- writeUdta(w, tags, pictures?): udta / meta (hdlr mdir) / ilst
|
-- udta / meta (hdlr mdir) / ilst
|
||||||
function Meta.writeUdta(w: Bytes.Writer, tags: { [string]: string }, pictures: { Picture }?)
|
function Meta.writeUdta(w: Bytes.Writer, tags: { [string]: string }, pictures: { Picture }?)
|
||||||
box(w, "udta", function()
|
box(w, "udta", function()
|
||||||
box(w, "meta", function()
|
box(w, "meta", function()
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
--!strict
|
--!strict
|
||||||
-- containers/IsoBmff/Mux: writes a progressive (non-fragmented) ISO BMFF / MP4 / M4A file: ftyp, moov (mvhd, one trak
|
-- containers/IsoBmff/Mux: writes a progressive ISO BMFF / MP4 / M4A file (ISO/IEC 14496-12): ftyp, moov first ("fast
|
||||||
-- per track with tkhd / edts / mdia / mdhd / hdlr / minf / [sv]mhd / dinf / stbl), the samples interleaved in chunks
|
-- start"), the samples interleaved in chunks in one mdat, udta / meta / ilst tags; co64 past 4 GB. Audio and video
|
||||||
-- in one mdat, and udta / meta / ilst tags. moov goes first ("fast start"); co64 replaces stco past 4 GB. Sample
|
-- sample entries with their configuration box, plus raw child boxes. Never throws: bad specs return nil, Err.ARG.
|
||||||
-- entries: audio (mp4a + esds, alac, Opus + dOps, fLaC + dfLa, ...) and video (avc1 / hvc1 / av01 / vp09 / ... +
|
|
||||||
-- their configuration box), plus any raw child boxes. Never throws: bad specs return nil, Err.ARG.
|
|
||||||
local Err = require("@rosetta/core/Err")
|
local Err = require("@rosetta/core/Err")
|
||||||
local Bytes = require("@rosetta/core/Bytes")
|
local Bytes = require("@rosetta/core/Bytes")
|
||||||
local Meta = require("@rosetta/containers/IsoBmff/Meta")
|
local Meta = require("@rosetta/containers/IsoBmff/Meta")
|
||||||
@@ -154,7 +152,6 @@ end
|
|||||||
|
|
||||||
type Chunk = { track: number, first: number, count: number, time: number, offset: number }
|
type Chunk = { track: number, first: number, count: number, time: number, offset: number }
|
||||||
|
|
||||||
-- runs of equal values: { { count, value } }
|
|
||||||
local function runs(n: number, value: (number) -> number): { { number } }
|
local function runs(n: number, value: (number) -> number): { { number } }
|
||||||
local out = {}
|
local out = {}
|
||||||
for i = 1, n do
|
for i = 1, n do
|
||||||
@@ -381,7 +378,6 @@ local function moov(w: Bytes.Writer, spec: MuxSpec, prep: { Prepared }, co64: bo
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- mux(spec) -> buffer | nil, Err
|
|
||||||
function Mux.mux(spec: MuxSpec): (buffer?, Err.Err?)
|
function Mux.mux(spec: MuxSpec): (buffer?, Err.Err?)
|
||||||
if type(spec) ~= "table" or type(spec.tracks) ~= "table" or #spec.tracks == 0 then
|
if type(spec) ~= "table" or type(spec.tracks) ~= "table" or #spec.tracks == 0 then
|
||||||
return nil, Err.new(Err.ARG, "mux: expected { tracks = { ... } }")
|
return nil, Err.new(Err.ARG, "mux: expected { tracks = { ... } }")
|
||||||
@@ -476,7 +472,6 @@ function Mux.mux(spec: MuxSpec): (buffer?, Err.Err?)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
local co64 = mdatSize > 4294967295 - 2 ^ 24
|
local co64 = mdatSize > 4294967295 - 2 ^ 24
|
||||||
-- ftyp
|
|
||||||
local w = Bytes.writer(mdatSize + 4096)
|
local w = Bytes.writer(mdatSize + 4096)
|
||||||
box(w, "ftyp", function()
|
box(w, "ftyp", function()
|
||||||
w:string(spec.brand or "isom")
|
w:string(spec.brand or "isom")
|
||||||
|
|||||||
Reference in new issue
Block a user