--!strict --!optimize 2 -- audio/aac: raw AAC in ADTS framing (ISO/IEC 13818-7 / 14496-3 1.A.3). Supports: AAC-LC via codecs/audio/Aac (1-8 -- channels, every rate), HE-AAC / HE-AACv2 (SBR / PS found in the stream: ADTS cannot signal them; opts.sbr = false -- keeps the AAC-LC core), CRC-protected headers, a leading ID3v2 tag, resync, seeking by frame. Not supported: writing, -- ADIF, LATM. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") local Types = require("@rosetta/Types") local Pcm = require("@rosetta/codecs/audio/Pcm") local Stream = require("@rosetta/codecs/audio/Stream") local Aac = require("@rosetta/codecs/audio/Aac") -- ------------------------------------------------------------------------------------------------ contract local AacFormat = { name = "aac", category = "audio", extensions = { "aac", "adts" }, mime = { "audio/aac", "audio/aacp", "audio/x-aac" }, deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Aac", "codecs/audio/Pcm", "codecs/audio/Stream" }, caps = { decode = true, encode = false, codecs = { "aac" }, streaming = true }, } -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { maxFrames: number?, -- default 2^22 sbr: boolean?, -- false: decode only the AAC-LC core of HE-AAC (half rate, faster); default true } -- ------------------------------------------------------------------------------------------------ constants local readu8 = buffer.readu8 local function id3Size(b: buffer, p: number, limit: number): number if p + 10 > limit or buffer.readstring(b, p, 3) ~= "ID3" then return 0 end local s = 0 for i = 6, 9 do s = s * 128 + bit32.band(readu8(b, p + i), 127) end local footer = if bit32.band(readu8(b, p + 5), 0x10) ~= 0 then 10 else 0 return 10 + s + footer end local ID3_TAGS: { [string]: string } = { TIT2 = "title", TPE1 = "artist", TALB = "album", TYER = "date", TDRC = "date", TCON = "genre", TRCK = "track", COMM = "comment", TCOM = "composer", TPE2 = "albumartist", } local function id3Tags(b: buffer, p: number, size: number): Types.Tags local tags: Types.Tags = {} local ver = readu8(b, p + 3) if ver < 3 then return tags end local q, stop = p + 10, p + size while q + 10 <= stop do local id = buffer.readstring(b, q, 4) if not string.match(id, "^[A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]$") then break end local n = 0 for i = 4, 7 do n = if ver == 4 then n * 128 + bit32.band(readu8(b, q + i), 127) else n * 256 + readu8(b, q + i) end if n < 1 or q + 10 + n > stop then break end local key = ID3_TAGS[id] local enc = readu8(b, q + 10) if key and (enc == 0 or enc == 3) then local s = buffer.readstring(b, q + 11, n - 1) if id == "COMM" and #s > 3 then s = string.sub(s, 4) local z = string.find(s, "\0", 1, true) if z then s = string.sub(s, z + 1) end end s = (string.gsub(s, "%z+$", "")) tags[key] = s end q += 10 + n end return tags end -- ------------------------------------------------------------------------------------------------ sniff function AacFormat.sniff(r: Bytes.Reader): number local b, p, limit = r.buf, r.pos, r.limit p += id3Size(b, p, limit) local h = Aac.adts(b, p, limit) if not h then return 0 end local h2 = Aac.adts(b, p + h.frameSize, limit) if h2 and h2.sfIndex == h.sfIndex and h2.channelConfig == h.channelConfig then return 0.9 end return if p + h.frameSize >= limit then 0.7 else 0.4 end -- ------------------------------------------------------------------------------------------------ decode local function index(r: Bytes.Reader, maxFrames: number): ({ number }, { number }, Aac.Adts?, Err.Err?) local b, limit = r.buf, r.limit local p = r.base + id3Size(b, r.base, limit) local offs, sizes = {}, {} local first: Aac.Adts? = nil while p + 7 <= limit do local h = Aac.adts(b, p, limit) if h and first and (h.sfIndex ~= first.sfIndex or h.channelConfig ~= first.channelConfig) then h = nil end if not h then p += 1 continue end if p + h.frameSize > limit then break end if not first then first = h end if h.blocks ~= 1 then return offs, sizes, first, Err.new(Err.UNSUPPORTED, "ADTS frames with " .. h.blocks .. " raw data blocks", p) end table.insert(offs, p + h.headerSize) table.insert(sizes, h.frameSize - h.headerSize) if #offs > maxFrames then return offs, sizes, first, Err.new(Err.LIMIT, "more than " .. maxFrames .. " frames") end p += h.frameSize end return offs, sizes, first, nil end function AacFormat.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.Err?) local o: DecodeOptions = opts or {} local offs, sizes, first, ierr = index(r, o.maxFrames or 2 ^ 22) if ierr then return nil, ierr end if not first then return nil, Err.new(Err.CORRUPT, "no ADTS frame", r.base) end local h = first :: Aac.Adts if h.objectType ~= 2 then return nil, Err.new(Err.UNSUPPORTED, "ADTS profile " .. h.objectType .. " (only AAC-LC)") end if h.channelConfig == 0 then return nil, Err.new(Err.UNSUPPORTED, "ADTS with a program config element (channel configuration 0)") end local channels = ({ 1, 2, 3, 4, 5, 6, 8 })[h.channelConfig] :: number local dec, derr = Aac.decoder({ sfIndex = h.sfIndex, channels = channels, channelConfig = h.channelConfig }, { sbr = o.sbr }) if not dec then return nil, derr end local d = dec :: Aac.Decoder if Aac.probeFrames(d, r.buf, offs, sizes) then d:reset() end local sr, fl = d.sampleRate, d.frameLength channels = d.channels local nframes = #offs local total = nframes * fl local bytes = 0 for _, s in sizes do bytes += s end local id3 = id3Size(r.buf, r.base, r.limit) local tags = if id3 > 0 then id3Tags(r.buf, r.base, id3) else {} local buf = r.buf local out: any = { sampleRate = sr, channels = channels, frames = total, duration = total / sr, bitrate = if total > 0 then math.floor(bytes * 8 * sr / total) else 0, tags = tags, codec = "aac", } -- 1024 (2048 with SBR) samples per ADTS frame; pre-roll for the overlap (and the SBR filterbanks / time deltas) return Stream.packets(out, { count = nframes, start = function(i: number): number return (i - 1) * fl end, decode = function(i: number): (number?, Err.Err?) return d:decode(buf, offs[i], sizes[i]) end, pcm = function(): { number } return d.pcm end, reset = function() d:reset() end, preroll = if d.sbrOutput then 3 else 1, }) end function AacFormat.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.Err?) local dec, err = AacFormat.open(r, opts) if not dec then return nil, err end local d: any = dec return Pcm.drain(d, { format = "aac", codec = "aac", bitrate = d.bitrate }) end return AacFormat