Repository navigation
Expand file tree
/
Copy pathImageDecoder.ts
More file actions
228 lines (195 loc) · 5.86 KB
/
Copy pathImageDecoder.ts
File metadata and controls
228 lines (195 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* ImageDecoder - Decodes still images to VideoFrame
* Implements the W3C WebCodecs ImageDecoder interface
*/
import { VideoFrame } from './VideoFrame';
import { DOMException, BufferSource } from './types';
// Load native addon
import { native } from './native';
export interface ImageDecodeResult {
image: VideoFrame;
complete: boolean;
}
export interface ImageDecodeOptions {
frameIndex?: number;
completeFramesOnly?: boolean;
}
export interface ImageDecoderInit {
data: BufferSource | ReadableStream<BufferSource>;
type: string;
colorSpaceConversion?: 'default' | 'none';
desiredWidth?: number;
desiredHeight?: number;
preferAnimation?: boolean;
}
/**
* Check if a value is a ReadableStream
*/
function isReadableStream(value: unknown): value is ReadableStream {
return (
typeof value === 'object' &&
value !== null &&
typeof (value as any).getReader === 'function'
);
}
export class ImageDecoder {
private _native: any;
private _type: string;
private _complete: boolean = false;
private _closed: boolean = false;
private _completedPromise: Promise<void>;
private _completedResolve!: () => void;
private _completedReject!: (error: Error) => void;
private _streamReader?: ReadableStreamDefaultReader<BufferSource>;
/**
* Check if a MIME type is supported for decoding
*/
static isTypeSupported(type: string): Promise<boolean> {
if (!native || !native.ImageDecoderNative) {
return Promise.resolve(false);
}
return Promise.resolve(native.ImageDecoderNative.isTypeSupported(type));
}
constructor(init: ImageDecoderInit) {
if (!init.data) {
throw new TypeError('data is required');
}
if (!init.type) {
throw new TypeError('type is required');
}
if (!native || !native.ImageDecoderNative) {
throw new DOMException('Native addon not available', 'NotSupportedError');
}
this._type = init.type;
this._completedPromise = new Promise((resolve, reject) => {
this._completedResolve = resolve;
this._completedReject = reject;
});
// Handle ReadableStream
if (isReadableStream(init.data)) {
this._handleReadableStream(init.data as ReadableStream<BufferSource>);
return;
}
// Handle BufferSource
let dataBuffer: Buffer;
if (init.data instanceof ArrayBuffer) {
dataBuffer = Buffer.from(init.data);
} else if (ArrayBuffer.isView(init.data)) {
dataBuffer = Buffer.from(init.data.buffer, init.data.byteOffset, init.data.byteLength);
} else {
throw new TypeError('Invalid data type: expected BufferSource or ReadableStream');
}
this._initNative(dataBuffer);
}
/**
* Handle ReadableStream input by reading all chunks and concatenating them
*/
private async _handleReadableStream(stream: ReadableStream<BufferSource>): Promise<void> {
this._streamReader = stream.getReader();
const chunks: Uint8Array[] = [];
try {
while (true) {
const { done, value } = await this._streamReader.read();
if (done) break;
// Convert BufferSource to Uint8Array
let chunk: Uint8Array;
if (value instanceof ArrayBuffer) {
chunk = new Uint8Array(value);
} else if (ArrayBuffer.isView(value)) {
chunk = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
} else {
throw new TypeError('ReadableStream yielded invalid chunk type');
}
chunks.push(chunk);
}
// Concatenate all chunks
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
const fullData = new Uint8Array(totalLength);
let offset = 0;
for (const chunk of chunks) {
fullData.set(chunk, offset);
offset += chunk.length;
}
this._initNative(Buffer.from(fullData));
} catch (error) {
this._completedReject(error instanceof Error ? error : new Error(String(error)));
}
}
/**
* Initialize the native decoder with buffer data
*/
private _initNative(dataBuffer: Buffer): void {
try {
this._native = new native.ImageDecoderNative({
data: dataBuffer,
type: this._type,
});
this._complete = this._native.complete;
if (this._complete) {
this._completedResolve();
}
} catch (e: any) {
const error = new DOMException(e.message || 'Failed to create ImageDecoder', 'NotSupportedError');
this._completedReject(error);
throw error;
}
}
/**
* Whether all image data has been received
*/
get complete(): boolean {
return this._complete;
}
/**
* Promise that resolves when complete becomes true
*/
get completed(): Promise<void> {
return this._completedPromise;
}
/**
* The MIME type of the image
*/
get type(): string {
return this._type;
}
/**
* Decode an image frame
*/
async decode(options?: ImageDecodeOptions): Promise<ImageDecodeResult> {
if (this._closed) {
throw new DOMException('ImageDecoder is closed', 'InvalidStateError');
}
if (!this._complete) {
throw new DOMException('Image data is not complete', 'InvalidStateError');
}
try {
const result = this._native.decode(options?.frameIndex ?? 0);
// The native decode returns an object with image (native VideoFrameNative) and complete
// We need to wrap the native frame in a VideoFrame
const videoFrame = new VideoFrame(result.image);
return {
image: videoFrame,
complete: result.complete,
};
} catch (e: any) {
throw new DOMException(e.message || 'Failed to decode image', 'EncodingError');
}
}
/**
* Reset the decoder state
*/
reset(): void {
if (!this._closed) {
this._native.reset();
}
}
/**
* Close the decoder and release resources
*/
close(): void {
if (!this._closed) {
this._closed = true;
this._native.close();
}
}
}