// An FLV tag's body as RTMP carries it (FLV 10.1, Annex E.4.2 and E.4.3): video as AVC (codec 7, its packet type 0 the // AVCDecoderConfigurationRecord, 1 length-prefixed NAL units with a composition time offset), audio as AAC (format 10, its // packet type 0 the AudioSpecificConfig, 1 a raw frame). Anything else is named as the codec it is and not read: other // codecs, and Enhanced RTMP's ex-header (its first byte's high bit), which carries HEVC, AV1 and multitrack video. export type AvcConfig = { /** the AVCDecoderConfigurationRecord as sent, which an `avcC` box holds as is (ISO/IEC 14496-15 §5.3.3.1) */ record: Uint8Array; /** the RFC 6381 codec string: `avc1.` and the profile, constraint flags and level, in hex */ codec: string; width: number; height: number; }; export type AacConfig = { /** the AudioSpecificConfig as sent, which an `esds` box's DecoderSpecificInfo holds as is */ asc: Uint8Array; codec: string; objectType: number; sampleRate: number; channels: number; }; export type VideoTag = | { kind: 'config'; config: AvcConfig } | { kind: 'frame'; key: boolean; cts: number; data: Uint8Array } | { kind: 'other' } | { kind: 'unsupported'; codec: string }; export type AudioTag = | { kind: 'config'; config: AacConfig } | { kind: 'frame'; data: Uint8Array } | { kind: 'unsupported'; codec: string }; const VIDEO_CODECS: Record = { 2: 'Sorenson H.263', 3: 'Screen video', 4: 'On2 VP6', 5: 'On2 VP6 with alpha', 6: 'Screen video 2', 12: 'HEVC', 13: 'AV1' }; const AUDIO_FORMATS: Record = { 0: 'linear PCM', 1: 'ADPCM', 2: 'MP3', 3: 'linear PCM (little-endian)', 4: 'Nellymoser 16 kHz', 5: 'Nellymoser 8 kHz', 6: 'Nellymoser', 7: 'G.711 A-law', 8: 'G.711 mu-law', 11: 'Speex', 14: 'MP3 8 kHz', 15: 'device-specific' }; const hex2 = (n: number): string => n.toString(16).padStart(2, '0'); export function readVideoTag(p: Uint8Array): VideoTag { if (p.length < 1) return { kind: 'other' }; if (p[0]! & 0x80) { const fourcc = p.length >= 5 ? String.fromCharCode(p[1]!, p[2]!, p[3]!, p[4]!) : '?'; return { kind: 'unsupported', codec: `Enhanced RTMP ${fourcc}` }; } const frameType = p[0]! >> 4; const codec = p[0]! & 0x0f; if (codec !== 7) return { kind: 'unsupported', codec: VIDEO_CODECS[codec] ?? `video codec ${codec}` }; // a video info/command frame (frame type 5) carries no picture if (frameType === 5 || p.length < 5) return { kind: 'other' }; const packetType = p[1]!; const cts = ((p[2]! << 16) | (p[3]! << 8) | p[4]!) << 8 >> 8; const data = p.subarray(5); if (packetType === 0) return { kind: 'config', config: avcConfig(data) }; if (packetType === 1) return { kind: 'frame', key: frameType === 1, cts, data }; return { kind: 'other' }; } export function readAudioTag(p: Uint8Array): AudioTag { if (p.length < 2) return { kind: 'frame', data: new Uint8Array(0) }; const format = p[0]! >> 4; if (format !== 10) return { kind: 'unsupported', codec: AUDIO_FORMATS[format] ?? `audio format ${format}` }; if (p[1] === 0) return { kind: 'config', config: aacConfig(p.subarray(2)) }; return { kind: 'frame', data: p.subarray(2) }; } function avcConfig(record: Uint8Array): AvcConfig { const copy = record.slice(); const codec = copy.length >= 4 ? `avc1.${hex2(copy[1]!)}${hex2(copy[2]!)}${hex2(copy[3]!)}` : 'avc1.42e01e'; let width = 0; let height = 0; // the first SPS: after the version, profile, compatibility, level, length size and SPS count if (copy.length > 8 && (copy[5]! & 0x1f) > 0) { const length = (copy[6]! << 8) | copy[7]!; try { ({ width, height } = spsSize(copy.subarray(8, 8 + length))); } catch { /* the metadata's size stands in */ } } return { record: copy, codec, width, height }; } function aacConfig(asc: Uint8Array): AacConfig { const copy = asc.slice(); const RATES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350]; const bits = new Bits(copy); let objectType = bits.u(5); if (objectType === 31) objectType = 32 + bits.u(6); const index = bits.u(4); const sampleRate = index === 15 ? bits.u(24) : RATES[index] ?? 44100; const channels = bits.u(4); return { asc: copy, codec: `mp4a.40.${objectType}`, objectType, sampleRate, channels }; } /** A bit reader over bytes, MSB first; a read past the end reads zeros. */ class Bits { private at = 0; constructor(private readonly bytes: Uint8Array) {} bit(): number { const byte = this.bytes[this.at >> 3] ?? 0; const v = (byte >> (7 - (this.at & 7))) & 1; this.at++; return v; } u(n: number): number { let v = 0; for (let i = 0; i < n; i++) v = v * 2 + this.bit(); return v; } ue(): number { let zeros = 0; while (this.bit() === 0) { if (++zeros > 31) throw new Error('exp-Golomb code too long'); } return 2 ** zeros - 1 + this.u(zeros); } se(): number { const k = this.ue(); return k % 2 ? (k + 1) / 2 : -(k / 2); } } /** A sequence parameter set's picture size (H.264 §7.3.2.1.1), its cropping applied. */ function spsSize(nal: Uint8Array): { width: number; height: number } { // the NAL unit's RBSP: its header byte off, each emulation-prevention byte (00 00 03) removed const rbsp: number[] = []; for (let i = 1; i < nal.length; i++) { if (i >= 3 && nal[i] === 3 && nal[i - 1] === 0 && nal[i - 2] === 0) continue; rbsp.push(nal[i]!); } const b = new Bits(Uint8Array.from(rbsp)); const profile = b.u(8); b.u(16); b.ue(); let chroma = 1; if ([100, 110, 122, 244, 44, 83, 86, 118, 128, 138, 139, 134, 135].includes(profile)) { chroma = b.ue(); if (chroma === 3) b.bit(); b.ue(); b.ue(); b.bit(); if (b.bit()) { for (let i = 0; i < (chroma === 3 ? 12 : 8); i++) { if (!b.bit()) continue; let last = 8; let next = 8; for (let j = 0; j < (i < 6 ? 16 : 64); j++) { if (next !== 0) next = (last + b.se() + 256) % 256; last = next === 0 ? last : next; } } } } b.ue(); const poc = b.ue(); if (poc === 0) b.ue(); else if (poc === 1) { b.bit(); b.se(); b.se(); const n = b.ue(); for (let i = 0; i < n; i++) b.se(); } b.ue(); b.bit(); const widthMbs = b.ue() + 1; const heightUnits = b.ue() + 1; const frameMbsOnly = b.bit(); if (!frameMbsOnly) b.bit(); b.bit(); let crop = [0, 0, 0, 0]; if (b.bit()) crop = [b.ue(), b.ue(), b.ue(), b.ue()]; const unitX = chroma === 1 || chroma === 2 ? 2 : 1; const unitY = (chroma === 1 ? 2 : 1) * (2 - frameMbsOnly); return { width: widthMbs * 16 - (crop[0]! + crop[1]!) * unitX, height: (2 - frameMbsOnly) * heightUnits * 16 - (crop[2]! + crop[3]!) * unitY, }; }