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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// @trace REQ-BRW-051
//! WebCodecs `ImageDecoder` (REQ-BRW-051, Bao fork self-build; contract =
//! <https://wicg.github.io/image-output/> + the WPT image-decoder oracle).
//! The decode plane is the component image-crate backend (png/gif/jpeg/webp/
//! bmp/ico real decode; avif honestly unsupported).
//!
//! Shape mandated by the WPT oracle: the constructor NEVER throws — an
//! unsupported type (`junk/type` with a decodable body) or a failed decode
//! constructs successfully and marks the decoder failed, so `decode()` and
//! `tracks.ready` reject NotSupportedError afterwards. `decode()` decodes
//! lazily (a truncated body still constructs; its decode rejects — the WPT
//! truncated case expects an EncodingError face, our mapping lands on
//! NotSupportedError: registered divergence, follow-up candidate).
//!
//! Registered boundaries (webidl header + this slice): the data source is
//! `BufferSource` only (no ReadableStream incremental delivery);
//! `colorSpaceConversion`/`desiredWidth`/`desiredHeight` are accepted but not
//! applied; `reset()` is a no-op (all decodes complete synchronously).
use std::cell::Cell;
use dom_struct::dom_struct;
use js::context::JSContext;
use js::rust::HandleObject;
use script_bindings::cell::DomRefCell;
use script_bindings::reflector::{DomObject, Reflector, reflect_dom_object_with_proto};
use script_bindings::root::Dom;
use crate::dom::bindings::buffer_source::get_buffer_source_slice;
use crate::dom::bindings::codegen::Bindings::ImageDecoderBinding::{
ImageDecodeOptions, ImageDecodeResult, ImageDecoderInit, ImageDecoderMethods,
};
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::globalscope::GlobalScope;
use crate::dom::imagetrack::ImageTrack;
use crate::dom::imagetracklist::ImageTrackList;
use crate::dom::promise::{Promise, RootedPromise};
use crate::dom::videoframe::VideoFrame;
use webcodecs::image_decoder::{decode_image, is_type_supported};
use webcodecs::video_frame::{DecodedVideoFrame, FrameColorSpace, FrameFormat};
/// Snapshot of one decoded RGBA frame (plain data; the component
/// `ImageFrame` is not traceable so the DOM layer owns its copy).
#[derive(Clone, JSTraceable, MallocSizeOf)]
struct ImageFrameSnapshot {
width: u32,
height: u32,
rgba: Vec<u8>,
/// Frame delay in milliseconds (0 for stills).
duration_ms: u32,
}
/// Snapshot of a successful decode: the frames plus the track metadata.
#[derive(Clone, JSTraceable, MallocSizeOf)]
struct DecodedImage {
frames: Vec<ImageFrameSnapshot>,
animated: bool,
frame_count: u32,
/// `None` = infinite repetition.
repetition_count: Option<u32>,
}
/// <https://wicg.github.io/image-output/#dom-imagedecoder>
#[dom_struct]
pub(crate) struct ImageDecoder {
reflector_: Reflector,
/// The `type` the decoder was constructed with.
type_: DomRefCell<String>,
/// Whether construction failed (unsupported type or failed decode): the
/// deferred NotSupportedError face for `decode()` and `tracks.ready`.
failed: Cell<bool>,
/// Whether `close()` was called (subsequent `decode()` rejects
/// InvalidStateError).
closed: Cell<bool>,
/// The decoded image when construction succeeded.
decoded: DomRefCell<Option<DecodedImage>>,
tracks: MutNullableDom<ImageTrackList>,
completed: MutNullableDom<Promise>,
}
impl ImageDecoder {
fn new_inherited(type_: String, failed: bool, decoded: Option<DecodedImage>) -> ImageDecoder {
ImageDecoder {
reflector_: Reflector::new(),
type_: DomRefCell::new(type_),
failed: Cell::new(failed),
closed: Cell::new(false),
decoded: DomRefCell::new(decoded),
tracks: Default::default(),
completed: Default::default(),
}
}
#[allow(unsafe_code)]
fn thread_cx() -> JSContext {
unsafe { JSContext::get_from_thread() }.expect("no active JS context")
}
}
impl ImageDecoderMethods<crate::DomTypeHolder> for ImageDecoder {
/// <https://wicg.github.io/image-output/#dom-imagedecoder-imagedecoder>
fn Constructor(
cx: &mut JSContext,
global: &GlobalScope,
proto: Option<HandleObject>,
init: &ImageDecoderInit,
) -> Fallible<DomRoot<ImageDecoder>> {
// Copy the bytes at construction (the BufferSource is consumed
// synchronously; a detached buffer yields zero bytes and fails the
// decode honestly).
let no_gc = cx.no_gc();
let bytes = get_buffer_source_slice(&init.data, no_gc).to_vec();
let mime = init.type_.to_string();
// The constructor never throws (WPT junk/type oracle): unsupported
// types and failed decodes construct into the failed state.
let outcome = if !is_type_supported(&mime) {
None
} else {
decode_image(
&webcodecs::config::ImageDecoderConfig {
mime_type: mime.clone(),
prefer_animation: init.preferAnimation.unwrap_or(false),
},
&bytes,
)
.ok()
};
let decoded = outcome.map(|result| DecodedImage {
frames: result
.frames
.iter()
.map(|frame| ImageFrameSnapshot {
width: frame.width,
height: frame.height,
rgba: frame.rgba.clone(),
duration_ms: frame.duration_ms,
})
.collect(),
animated: result.track.animated,
frame_count: result.track.frame_count,
repetition_count: result.track.repetition_count,
});
let failed = decoded.is_none();
Ok(reflect_dom_object_with_proto(
cx,
Box::new(ImageDecoder::new_inherited(mime, failed, decoded)),
global,
proto,
))
}
/// <https://wicg.github.io/image-output/#dom-imagedecoder-type>
fn Type(&self) -> crate::dom::bindings::str::DOMString {
self.type_.borrow().clone().into()
}
/// <https://wicg.github.io/image-output/#dom-imagedecoder-complete>
/// The data source is fully buffered (BufferSource-only slice), so the
/// decoder is always complete — false only for a failed construction.
fn Complete(&self) -> bool {
!self.failed.get()
}
/// <https://wicg.github.io/image-output/#dom-imagedecoder-completed>
fn Completed(&self) -> RootedPromise {
let global = self.global();
let global: &GlobalScope = &global;
let mut cx = Self::thread_cx();
let promise = self.completed.or_init(|| {
// All data is buffered: `completed` resolves immediately (the
// empty-data failure surface surfaces through decode() instead).
let resolved = Promise::new_resolved(&mut cx, global, ());
DomRoot::from_ref(&*resolved)
});
// Re-wrap the cached promise so every access observes the same JS
// promise object.
Promise::new_with_js_promise(&cx, promise.reflector().get_jsobject())
}
/// <https://wicg.github.io/image-output/#dom-imagedecoder-tracks>
fn Tracks(&self) -> DomRoot<ImageTrackList> {
let global = self.global();
let global: &GlobalScope = &global;
self.tracks.or_init(|| {
let mut cx = Self::thread_cx();
// The ready promise settles with the constructor outcome:
// resolved once tracks exist, rejected NotSupportedError for an
// unsupported type or failed decode (WPT junk/type oracle). The
// rejection goes through `reject_error` — `Error` converts to a
// JS value only through the DOM error path, not as a plain value.
let ready: Dom<Promise> = if self.failed.get() {
let promise = Promise::new(&mut cx, global);
promise.reject_error(&mut cx, Error::NotSupported(None));
Dom::from_ref(&*promise)
} else {
let promise = Promise::new_resolved(&mut cx, global, ());
Dom::from_ref(&*promise)
};
let track = if self.failed.get() {
None
} else {
self.decoded.borrow().as_ref().map(|decoded| {
ImageTrack::new_selected(
&mut cx,
global,
decoded.animated,
decoded.frame_count,
decoded.repetition_count,
)
})
};
ImageTrackList::new(&mut cx, global, None, track, ready)
})
}
/// <https://wicg.github.io/image-output/#dom-imagedecoder-decode>
fn Decode(&self, options: &ImageDecodeOptions) -> RootedPromise {
let global = self.global();
let global: &GlobalScope = &global;
let mut cx = Self::thread_cx();
let promise = Promise::new(&mut cx, &global);
if self.closed.get() {
promise.reject_error(&mut cx, Error::InvalidState(None));
return promise;
}
if self.failed.get() {
promise.reject_error(&mut cx, Error::NotSupported(None));
return promise;
}
let decoded = self.decoded.borrow();
let Some(decoded) = decoded.as_ref() else {
promise.reject_error(&mut cx, Error::NotSupported(None));
return promise;
};
// Out-of-range frameIndex rejects RangeError (WPT oracle).
let Some(frame) = decoded.frames.get(options.frameIndex as usize) else {
promise.reject_error(
&mut cx,
Error::Range(c"frameIndex is out of range".to_owned()),
);
return promise;
};
// The image backend normalizes to RGBA8; surface it as a VideoFrame
// through the decoded-frame delivery path.
let decoded_video = DecodedVideoFrame {
format: FrameFormat::Rgba,
coded_width: frame.width,
coded_height: frame.height,
display_width: frame.width,
display_height: frame.height,
timestamp: 0,
duration: Some(i64::from(frame.duration_ms) * 1000),
color_space: FrameColorSpace {
primaries: "bt709",
transfer: "bt709",
matrix: "rgb",
full_range: true,
},
planes: vec![frame.rgba.clone()],
strides: vec![frame.width * 4],
};
match VideoFrame::from_decoded_frame(&mut cx, global, &decoded_video) {
Ok(video_frame) => promise.resolve_native(
&mut cx,
&ImageDecodeResult {
image: DomRoot::from_ref(&*video_frame),
complete: true,
},
),
Err(error) => promise.reject_error(&mut cx, error),
}
promise
}
/// <https://wicg.github.io/image-output/#dom-imagedecoder-reset>
/// All decodes complete synchronously in this slice, so there is no
/// in-flight work to abort.
fn Reset(&self) -> Fallible<()> {
Ok(())
}
/// <https://wicg.github.io/image-output/#dom-imagedecoder-close>
fn Close(&self) -> Fallible<()> {
self.closed.set(true);
Ok(())
}
/// <https://wicg.github.io/image-output/#dom-imagedecoder-istypesupported>
fn IsTypeSupported(
global: &GlobalScope,
type_: crate::dom::bindings::str::DOMString,
) -> RootedPromise {
let mut cx = Self::thread_cx();
let promise = Promise::new(&mut cx, global);
promise.resolve_native(&mut cx, &is_type_supported(&type_.str()));
promise
}
}