Skip to content

Commit fda1b81

Browse files
committed
Merge mp4parse_avif_get_primary_item and mp4parse_avif_get_alpha_item
1 parent 90ca3df commit fda1b81

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

mp4parse_capi/fuzz/fuzz_targets/avif.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +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);
34-
35-
let mut alpha_item = Default::default();
36-
let mut premultiplied_alpha = Default::default();
37-
mp4parse_avif_get_alpha_item(context, &mut alpha_item, &mut premultiplied_alpha);
32+
let mut avif_image = Default::default();
33+
mp4parse_avif_get_image(context, &mut avif_image);
3834

3935
mp4parse_avif_free(context);
4036
}

mp4parse_capi/src/lib.rs

Lines changed: 24 additions & 31 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,50 +1185,35 @@ 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` are set with 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();
1205+
*avif_image = Default::default();
1206+
let image_ref = &mut (*avif_image);
11961207

11971208
let context = (*parser).context();
11981209

1199-
(*primary_item).set_data(context.primary_item());
1200-
1201-
Mp4parseStatus::Ok
1202-
}
1203-
1204-
#[no_mangle]
1205-
pub unsafe extern "C" fn mp4parse_avif_get_alpha_item(
1206-
parser: *mut Mp4parseAvifParser,
1207-
alpha_item: *mut Mp4parseByteData,
1208-
premultiplied_alpha: *mut bool,
1209-
) -> Mp4parseStatus {
1210-
if parser.is_null() {
1211-
return Mp4parseStatus::BadArg;
1212-
}
1213-
1214-
*alpha_item = Default::default();
1215-
1216-
let context = (*parser).context();
1210+
image_ref.primary_item.set_data(context.primary_item());
12171211
if let Some(context_alpha_item) = context.alpha_item() {
1218-
(*alpha_item).set_data(context_alpha_item);
1219-
*premultiplied_alpha = context.premultiplied_alpha;
1220-
Mp4parseStatus::Ok
1221-
} else {
1222-
Mp4parseStatus::Invalid
1212+
image_ref.alpha_item.set_data(context_alpha_item);
1213+
image_ref.premultiplied_alpha = context.premultiplied_alpha;
12231214
}
1215+
1216+
Mp4parseStatus::Ok
12241217
}
12251218

12261219
/// Fill the supplied `Mp4parseByteData` with index information from `track`.

0 commit comments

Comments
 (0)