import{IsobmffDemuxer as R}from"./isobmff/isobmff-demuxer.js";import{IsobmffReader as x}from"./isobmff/isobmff-reader.js";import{EBMLId as m,EBMLReader as I,MIN_HEADER_SIZE as w}from"./matroska/ebml.js";import{MatroskaDemuxer as S}from"./matroska/matroska-demuxer.js";import{Mp3Demuxer as M}from"./mp3/mp3-demuxer.js";import{FRAME_HEADER_SIZE as p}from"../shared/mp3-misc.js";import{Mp3Reader as _}from"./mp3/mp3-reader.js";import{OggDemuxer as y}from"./ogg/ogg-demuxer.js";import{OggReader as T}from"./ogg/ogg-reader.js";import{RiffReader as z}from"./wave/riff-reader.js";import{WaveDemuxer as E}from"./wave/wave-demuxer.js";export class InputFormat{}export class IsobmffInputFormat extends InputFormat{async _getMajorBrand(e){if(await e._mainReader.source.getSize()<12)return null;const r=new x(e._mainReader);return r.pos=4,r.readAscii(4)!=="ftyp"?null:r.readAscii(4)}_createDemuxer(e){return new R(e)}}export class Mp4InputFormat extends IsobmffInputFormat{async _canReadInput(e){const a=await this._getMajorBrand(e);return!!a&&a!=="qt  "}get name(){return"MP4"}get mimeType(){return"video/mp4"}}export class QuickTimeInputFormat extends IsobmffInputFormat{async _canReadInput(e){return await this._getMajorBrand(e)==="qt  "}get name(){return"QuickTime File Format"}get mimeType(){return"video/quicktime"}}function F(){return 5}export class MatroskaInputFormat extends InputFormat{async isSupportedEBMLOfDocType(e,a){if(await e._mainReader.source.getSize()<8)return!1;const t=new I(e._mainReader),o=t.readVarIntSize();if(o===null||(F(),o<1||o>8)||t.readUnsignedInt(o)!==m.EBML)return!1;const i=t.readElementSize();if(i===null)return!1;const f=t.pos;for(;t.pos<=f+i-w;){const d=t.readElementHeader();if(!d)break;const{id:l,size:c}=d,g=t.pos;if(c===null)return!1;switch(l){case m.EBMLVersion:if(t.readUnsignedInt(c)!==1)return!1;break;case m.EBMLReadVersion:if(t.readUnsignedInt(c)!==1)return!1;break;case m.DocType:if(t.readAsciiString(c)!==a)return!1;break;case m.DocTypeVersion:if(t.readUnsignedInt(c)>4)return!1;break}t.pos=g+c}return!0}_canReadInput(e){return this.isSupportedEBMLOfDocType(e,"matroska")}_createDemuxer(e){return new S(e)}get name(){return"Matroska"}get mimeType(){return"video/x-matroska"}}export class WebMInputFormat extends MatroskaInputFormat{_canReadInput(e){return this.isSupportedEBMLOfDocType(e,"webm")}get name(){return"WebM"}get mimeType(){return"video/webm"}}export class Mp3InputFormat extends InputFormat{async _canReadInput(e){const a=await e._mainReader.source.getSize();if(a<4)return!1;const r=new _(e._mainReader);r.fileSize=a;const t=r.readId3();t&&(r.pos+=t.size);const o=r.pos;await r.reader.loadRange(r.pos,r.pos+4096);const s=r.readNextFrameHeader(Math.min(o+4096,a));if(!s)return!1;if(t)return!0;r.pos=s.startPos+s.totalSize,await r.reader.loadRange(r.pos,r.pos+p);const i=r.readNextFrameHeader(r.pos+p);return!(!i||s.channel!==i.channel||s.sampleRate!==i.sampleRate)}_createDemuxer(e){return new M(e)}get name(){return"MP3"}get mimeType(){return"audio/mpeg"}}export class WaveInputFormat extends InputFormat{async _canReadInput(e){if(await e._mainReader.source.getSize()<12)return!1;const r=new z(e._mainReader),t=r.readAscii(4);return t!=="RIFF"&&t!=="RIFX"&&t!=="RF64"?!1:(r.pos=8,r.readAscii(4)==="WAVE")}_createDemuxer(e){return new E(e)}get name(){return"WAVE"}get mimeType(){return"audio/wav"}}export class OggInputFormat extends InputFormat{async _canReadInput(e){return await e._mainReader.source.getSize()<4?!1:new T(e._mainReader).readAscii(4)==="OggS"}_createDemuxer(e){return new y(e)}get name(){return"Ogg"}get mimeType(){return"application/ogg"}}export const MP4=new Mp4InputFormat,QTFF=new QuickTimeInputFormat,MATROSKA=new MatroskaInputFormat,WEBM=new WebMInputFormat,MP3=new Mp3InputFormat,WAVE=new WaveInputFormat,OGG=new OggInputFormat,ALL_FORMATS=[MP4,QTFF,MATROSKA,WEBM,WAVE,OGG,MP3];
