Skip to content

Commit 94fd2f1

Browse files
authored
Merge pull request mozilla#258 from ChunMinChang/avif-alpha-capi
Add an API exposing alpha_item and premultiplied_alpha
2 parents 1220fd4 + 77d5d46 commit 94fd2f1

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

mp4parse_capi/fuzz/fuzz_targets/avif.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ fuzz_target!(|data: &[u8]| {
2929
return;
3030
}
3131

32-
let mut primary_item = Default::default();
33-
mp4parse_avif_get_primary_item(context, &mut primary_item);
32+
let mut avif_image = Default::default();
33+
mp4parse_avif_get_image(context, &mut avif_image);
3434

3535
mp4parse_avif_free(context);
3636
}

mp4parse_capi/src/lib.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,14 @@ pub struct Mp4parseParser {
436436
video_track_sample_descriptions: TryHashMap<u32, TryVec<Mp4parseTrackVideoSampleInfo>>,
437437
}
438438

439+
#[repr(C)]
440+
#[derive(Default)]
441+
pub struct AvifImage {
442+
pub primary_item: Mp4parseByteData,
443+
pub alpha_item: Mp4parseByteData,
444+
pub premultiplied_alpha: bool,
445+
}
446+
439447
/// A unified interface for the parsers which have different contexts, but
440448
/// share the same pattern of construction. This allows unification of
441449
/// argument validation from C and minimizes the surface of unsafe code.
@@ -1177,26 +1185,31 @@ fn mp4parse_get_track_video_info_safe(
11771185
/// # Safety
11781186
///
11791187
/// This function is unsafe because it dereferences both the parser and
1180-
/// primary_item raw pointers passed into it. Callers should ensure the parser
1181-
/// pointer points to a valid `Mp4parseAvifParser`, and that the primary_item
1182-
/// pointer points to a valid `Mp4parseByteData`. If there was not a previous
1188+
/// avif_image raw pointers passed into it. Callers should ensure the parser
1189+
/// pointer points to a valid `Mp4parseAvifParser`, and that the avif_image
1190+
/// pointer points to a valid `AvifImage`. If there was not a previous
11831191
/// successful call to `mp4parse_avif_read()`, no guarantees are made as to
1184-
/// the state of `primary_item`.
1192+
/// the state of `avif_image`. If `avif_image.alpha_item` is set to a
1193+
/// positive `length` and non-null `data`, then the `avif_image` contains an
1194+
/// valid alpha channel data. Otherwise, the image is opaque.
11851195
#[no_mangle]
1186-
pub unsafe extern "C" fn mp4parse_avif_get_primary_item(
1196+
pub unsafe extern "C" fn mp4parse_avif_get_image(
11871197
parser: *mut Mp4parseAvifParser,
1188-
primary_item: *mut Mp4parseByteData,
1198+
avif_image: *mut AvifImage,
11891199
) -> Mp4parseStatus {
1190-
if parser.is_null() {
1200+
if parser.is_null() || avif_image.is_null() {
11911201
return Mp4parseStatus::BadArg;
11921202
}
11931203

11941204
// Initialize fields to default values to ensure all fields are always valid.
1195-
*primary_item = Default::default();
1196-
1205+
*avif_image = Default::default();
11971206
let context = (*parser).context();
11981207

1199-
(*primary_item).set_data(context.primary_item());
1208+
(*avif_image).primary_item.set_data(context.primary_item());
1209+
if let Some(context_alpha_item) = context.alpha_item() {
1210+
(*avif_image).alpha_item.set_data(context_alpha_item);
1211+
(*avif_image).premultiplied_alpha = context.premultiplied_alpha;
1212+
}
12001213

12011214
Mp4parseStatus::Ok
12021215
}

0 commit comments

Comments
 (0)