You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
10 KiB
1 lines
10 KiB
(()=>{"use strict";var e,t={8103:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.BufferReader=t.CompositeBufferReader=t.CompositeBuffer=t.Uint8ArrayBuffer=void 0;class n{constructor(e){this.buffer=e}get length(){return this.buffer.length}at(e){return this.buffer[e]}subarray(e,t){return new n(this.buffer.subarray(e,t))}}t.Uint8ArrayBuffer=n;class i{constructor(e){this.buffers=[],this.buffers=e}get length(){let e=0;for(const t of this.buffers)e+=t.length;return e}at(e){let t=0;for(const n of this.buffers){const i=e-t;if(i<n.length)return n.at(i);t+=n.length}throw new Error("Out of bounds")}subarray(e,t){const n=[];let r=0;for(const i of this.buffers){const o=Math.max(0,e-r),s=Math.min(i.length,t-r);s>0&&o<s&&n.push(i.subarray(o,s)),r+=i.length}return new i(n)}}t.CompositeBuffer=i,t.CompositeBufferReader=class{constructor(){this.buffers=[]}add(e){this.buffers.push(e)}get length(){let e=0;for(const t of this.buffers)e+=t.length;return e}read(e){if(0===e)return new i([]);const t=[];let r=0;for(;;){if(0===this.buffers.length)throw new Error("Trying to read more bytes than available");const n=this.buffers.shift(),i=e-r;if(n.length===i){t.push(n);break}if(n.length>i){t.push(n.subarray(0,i)),this.buffers.unshift(n.subarray(i,n.length));break}r+=n.length,t.push(n)}return new i(t.map((e=>new n(e))))}},t.BufferReader=class{constructor(e){this._index=0,this.buffer=e}get index(){return this._index}get hasNext(){return this._index<this.buffer.length}readHex(e,t){if(void 0!==t&&this._index+e>t)return 0;let n=0,i=0;const r=this._index;for(let t=this._index+e-1;t>=r;--t)n+=this.buffer.at(t)<<8*i,++i;return this._index+=e,n}readBuffer(e){const t=this.buffer.subarray(this.index,this.index+e);return this._index+=e,t}}},3704:(e,t,n)=>{t.In=void 0;var i=n(5254);Object.defineProperty(t,"In",{enumerable:!0,get:function(){return i.parseDisplaySets}});n(7304)},5254:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.DisplaySet=t.parseDisplaySets=void 0;const i=n(8103),r=n(7304);t.parseDisplaySets=function(){const e=new s,t=new i.CompositeBufferReader;let n=13;return new TransformStream({transform(i,r){for(t.add(i);t.length>=n;)n=e.consume(t.read(n)),e.ready&&r.enqueue(e.next())},flush(t){e.ready&&t.enqueue(e.next())}})};class o{constructor(e,t,n,i,r,o){this.presentationCompositionSegment=e,this.windowDefinitionSegments=t,this.paletteDefinitionSegments=n,this.objectDefinitionSegments=i,this.endDefinitionSegment=r,this.previousDisplaySet=o}get firstOds(){return this.objectDefinitionSegments.find((e=>e.lastInSequenceFlag===r.LastInSequenceFlag.firstInSequence||e.lastInSequenceFlag===r.LastInSequenceFlag.firstAndLastInSequence))}paletteDefinitionSegment(e){const t=this.paletteDefinitionSegments.find((t=>t.paletteId===e));if(void 0===t){if(this.presentationCompositionSegment.compositionState!==r.CompositionState.normal)throw new Error("PCS references invalid PDS and composition state is not 'normal'");if(void 0===this.previousDisplaySet)throw new Error("PCS references invalid PDS and no previous display set to fallback to");return this.previousDisplaySet.paletteDefinitionSegment(e)}return t}imageData(e){const t=this.paletteDefinitionSegment(this.presentationCompositionSegment.paletteId);if(void 0===t)throw new Error("PCS references invalid PDS");const n=this.firstOds;if(void 0===n||void 0===n.width||void 0===n.height)throw new Error("Missing first ODS with defined width and height");const r=null!=e?e:new Uint8ClampedArray(n.width*n.height*4),o=t.paletteEntries.map((e=>this.ycrcbToRgba(e))),s=n.width;return function(e,t){let n=0,i=0,r=0;const o=e.length;for(;n<o;){const o=e.at(n);let s,a,d;if(o>0)a=o,s=1,d=1;else{const t=e.at(n+1);if(0===t)a=0,s=0,d=2,i=0,++r;else if(t<64)a=0,s=t,d=2;else if(t<128)a=0,s=(t-64<<8)+e.at(n+2),d=3;else if(t<192)a=e.at(n+2),s=t-128,d=3;else{const i=e.at(n+2);a=e.at(n+3),s=(t-192<<8)+i,d=4}}if(s>0){for(let e=i;e<i+s;++e)t(e,r,a);i+=s}n+=d}}(new i.CompositeBuffer(this.objectDefinitionSegments.map((e=>e.objectData))),((e,t,n)=>{const i=4*(t*s+e);if(n>=o.length)r[i]=0,r[i+1]=0,r[i+2]=0,r[i+3]=0;else{const e=o[n];r[i]=e.r,r[i+1]=e.g,r[i+2]=e.b,r[i+3]=e.a}})),new ImageData(r.subarray(0,4*n.width*n.height),n.width,n.height)}ycrcbToRgba(e){const t=e.luminance,n=e.colorDifferenceBlue,i=e.colorDifferenceRed;return{r:this.clamp(Math.floor(t+1.4075*(i-128)),0,255),g:this.clamp(Math.floor(t-.3455*(n-128)-.7169*(i-128)),0,255),b:this.clamp(Math.floor(t+1.779*(n-128)),0,255),a:e.transparency}}clamp(e,t,n){return Math.max(t,Math.min(n,e))}}t.DisplaySet=o;class s{constructor(){this.windowDefinitionSegments=[],this.paletteDefinitionSegments=[],this.objectDefinitionSegments=[],this.ready=!1}next(){return this.ready=!1,this.lastDisplaySet}consume(e){const t=new i.BufferReader(e);if(this.header){switch(this.header.segmentType){case r.SegmentType.pcs:if(void 0!==this.presentationCompositionSegment)throw new Error("Unexpected PDS");this.presentationCompositionSegment=function(e,t){const n=e.index+t.segmentSize,i=e.readHex(2,n),o=e.readHex(2,n);e.readHex(1);return{header:t,width:i,height:o,compositionNumber:e.readHex(2,n),compositionState:(0,r.compositionStateFromByte)(e.readHex(1,n)),paletteUpdateFlag:(0,r.paletteUpdateFlagFromByte)(e.readHex(1,n)),paletteId:e.readHex(1,n),compositionObjectCount:e.readHex(1,n),objectId:e.readHex(2,n),windowId:e.readHex(1,n),objectCroppedFlag:(0,r.objectCroppedFlagFromByte)(e.readHex(1,n)),objectHorizontalPosition:e.readHex(2,n),objectVerticalPosition:e.readHex(2,n),objectCroppingHorizontalPosition:e.readHex(2,n),objectCroppingVerticalPosition:e.readHex(2,n),objectCroppingWidth:e.readHex(2,n),objectCroppingHeightPosition:e.readHex(2,n)}}(t,this.header);break;case r.SegmentType.wds:if(void 0===this.presentationCompositionSegment)throw new Error("Unexpected WDS");this.windowDefinitionSegments.push(function(e,t){const n=e.index+t.segmentSize,i=e.readHex(1,n),r=[];for(let t=0;t<i;++t){const t=e.readHex(1,n),i=e.readHex(2,n),o=e.readHex(2,n),s=e.readHex(2,n),a=e.readHex(2,n);r.push({windowId:t,windowHorizontalPosition:i,windowVerticalPosition:o,windowWidth:s,windowHeight:a})}return{header:t,windowCount:i,windowDefinitions:r}}(t,this.header));break;case r.SegmentType.pds:if(void 0===this.presentationCompositionSegment)throw new Error("Unexpected PDS");this.paletteDefinitionSegments.push(function(e,t){const n=e.index+t.segmentSize,i=e.readHex(1,n),r=e.readHex(1,n),o=[];for(;e.index<n;){const t=e.readHex(1,n),i=e.readHex(1,n),r=e.readHex(1,n),s=e.readHex(1,n),a=e.readHex(1,n);o.push({paletteEntryId:t,luminance:i,colorDifferenceRed:r,colorDifferenceBlue:s,transparency:a})}return{header:t,paletteId:i,paletteVersionNumber:r,paletteEntries:o}}(t,this.header));break;case r.SegmentType.ods:if(void 0===this.presentationCompositionSegment)throw new Error("Unexpected ODS");const e=function(e,t){const n=e.readHex(2),i=e.readHex(1),o=(0,r.lastInSequenceFlagFromByte)(e.readHex(1)),s=e.readHex(3);let a,d,h;return o===r.LastInSequenceFlag.firstInSequence||o===r.LastInSequenceFlag.firstAndLastInSequence?(a=e.readHex(2),d=e.readHex(2),h=e.readBuffer(s-4)):h=e.readBuffer(s),{header:t,objectId:n,objectVersionNumber:i,lastInSequenceFlag:o,objectDataLength:s,width:a,height:d,objectData:h}}(t,this.header);this.objectDefinitionSegments.push(e);break;case r.SegmentType.end:if(void 0===this.presentationCompositionSegment)throw new Error("Unexpected end segment");const n={header:this.header};this.lastDisplaySet=new o(this.presentationCompositionSegment,this.windowDefinitionSegments,this.paletteDefinitionSegments,this.objectDefinitionSegments,n,this.lastDisplaySet),this.ready=!0,this.presentationCompositionSegment=void 0,this.windowDefinitionSegments=[],this.paletteDefinitionSegments=[],this.objectDefinitionSegments=[];break;default:throw new Error(`Unknown segment type: ${this.header.segmentType}`)}return this.header=void 0,13}{const e=t.readHex(2);if(20551!==e)throw new Error(`Invalid magic number: ${e}`);const n=t.readHex(4),i=t.readHex(4),o=(0,r.segmentTypeFromByte)(t.readHex(1)),s=t.readHex(2);return this.header={presentationTimestamp:n,decodingTimestamp:i,segmentType:o,segmentSize:s},s}}}},7304:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.lastInSequenceFlagFromByte=t.LastInSequenceFlag=t.objectCroppedFlagFromByte=t.paletteUpdateFlagFromByte=t.compositionStateFromByte=t.CompositionState=t.segmentTypeFromByte=t.SegmentType=void 0,function(e){e[e.pds=20]="pds",e[e.ods=21]="ods",e[e.pcs=22]="pcs",e[e.wds=23]="wds",e[e.end=128]="end"}(n=t.SegmentType||(t.SegmentType={}));const i=Object.values(n);var r;t.segmentTypeFromByte=function(e){for(const t of i)if(e===t)return t;throw new Error(`Invalid segment type byte: ${e}`)},function(e){e[e.normal=0]="normal",e[e.acquisitionState=64]="acquisitionState",e[e.epochStart=128]="epochStart"}(r=t.CompositionState||(t.CompositionState={}));const o=Object.values(r);var s;t.compositionStateFromByte=function(e){for(const t of o)if(e===t)return t;throw new Error(`Invalid composition state byte: ${e}`)},t.paletteUpdateFlagFromByte=function(e){switch(e){case 0:return!1;case 128:return!0;default:throw new Error(`Invalid palette update flag byte: ${e}`)}},t.objectCroppedFlagFromByte=function(e){switch(e){case 0:return!1;case 64:return!0;default:throw new Error(`Invalid object cropped flag byte: ${e}`)}},function(e){e[e.lastInSequence=64]="lastInSequence",e[e.firstInSequence=128]="firstInSequence",e[e.firstAndLastInSequence=192]="firstAndLastInSequence"}(s=t.LastInSequenceFlag||(t.LastInSequenceFlag={}));const a=Object.values(s);t.lastInSequenceFlagFromByte=function(e){for(const t of a)if(e===t)return t;throw new Error(`Invalid last in sequence flag byte: ${e}`)}}},n={};e=function e(i){var r=n[i];if(void 0!==r)return r.exports;var o=n[i]={exports:{}};return t[i](o,o.exports,e),o.exports}(3704),onmessage=async t=>{const{fileStream:n,canvas:i}=t.data;!function(t,n){let i,r;t.pipeThrough((0,e.In)()).pipeTo(new WritableStream({close(){postMessage({command:"finished"})},abort(e){postMessage({command:"error",error:e})},async write(e,t){if(e.objectDefinitionSegments.length>0)void 0===i&&(i=e);else if(void 0!==i){const t=i.presentationCompositionSegment.width,o=i.presentationCompositionSegment.height;r=void 0===r||r.length<o*t*4?new Uint8ClampedArray(t*o*4):r;const s=i.imageData(r);n.width=s.width,n.height=s.height,n.getContext("2d").putImageData(s,0,0),postMessage({command:"subtitle",imageBlob:await n.convertToBlob({type:"image/png"}),subtitle:{start:i.objectDefinitionSegments[0].header.presentationTimestamp/90??0,end:e.endDefinitionSegment.header.presentationTimestamp/90,text:"",textImage:{image:{width:s.width,height:s.height},screen:{width:i.presentationCompositionSegment.width,height:i.presentationCompositionSegment.height}}}}),i=void 0}}}))}(n,i)}})();
|