Skip to content

Commit 62f4acb

Browse files
authored
Merge pull request mozilla#200 from mozilla/read-in-new
Merge mp4parse[_avif]_read into mp4parse[_avif]_new
2 parents da2cb93 + 9953990 commit 62f4acb

File tree

10 files changed

+314
-349
lines changed

10 files changed

+314
-349
lines changed

mp4parse/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ impl AvifContext {
775775
/// A Media Data Box
776776
/// See ISO 14496-12:2015 § 8.1.1
777777
struct MediaDataBox {
778-
/// Offset of `data` from the beginning of the file. See ConstructionMethod::FileOffset
778+
/// Offset of `data` from the beginning of the file. See ConstructionMethod::File
779779
offset: u64,
780780
data: Vec<u8>,
781781
}
@@ -900,19 +900,19 @@ impl TryFrom<u8> for IlocVersion {
900900
struct ItemLocationBoxItem {
901901
item_id: u32,
902902
construction_method: ConstructionMethod,
903-
/// Unused for ConstructionMethod::IdatOffset
903+
/// Unused for ConstructionMethod::Idat
904904
extents: Vec<ItemLocationBoxExtent>,
905905
}
906906

907907
#[derive(Clone, Copy, Debug)]
908908
enum ConstructionMethod {
909-
FileOffset,
910-
IdatOffset,
909+
File,
910+
Idat,
911911
#[allow(dead_code)] // TODO: see https://github.com/mozilla/mp4parse-rust/issues/196
912-
ItemOffset,
912+
Item,
913913
}
914914

915-
/// `extent_index` is omitted since it's only used for ConstructionMethod::ItemOffset which
915+
/// `extent_index` is omitted since it's only used for ConstructionMethod::Item which
916916
/// is currently not implemented.
917917
#[derive(Clone, Debug)]
918918
struct ItemLocationBoxExtent {
@@ -1247,7 +1247,7 @@ pub fn read_avif<T: Read>(f: &mut T, context: &mut AvifContext) -> Result<()> {
12471247
read_meta = true;
12481248
let primary_item_loc = read_avif_meta(&mut b)?;
12491249
match primary_item_loc.construction_method {
1250-
ConstructionMethod::FileOffset => {
1250+
ConstructionMethod::File => {
12511251
primary_item_extents = Some(primary_item_loc.extents);
12521252
primary_item_extents_data = primary_item_extents.iter().map(|_| vec![]).collect();
12531253
}
@@ -1477,12 +1477,12 @@ fn read_iloc<T: Read>(src: &mut BMFFBox<T>) -> Result<Vec<ItemLocationBoxItem>>
14771477
// version 1 with `construction_method==0`, or version 2 when possible."
14781478
// We take this to imply version 0 can be interpreted as using file offsets.
14791479
let construction_method = match version {
1480-
IlocVersion::Zero => ConstructionMethod::FileOffset,
1480+
IlocVersion::Zero => ConstructionMethod::File,
14811481
IlocVersion::One | IlocVersion::Two => {
14821482
let _reserved = iloc.read_u16(12)?;
14831483
match iloc.read_u16(4)? {
1484-
0 => ConstructionMethod::FileOffset,
1485-
1 => ConstructionMethod::IdatOffset,
1484+
0 => ConstructionMethod::File,
1485+
1 => ConstructionMethod::Idat,
14861486
2 => return Err(Error::Unsupported("construction_method 'item_offset' is not supported")),
14871487
_ => return Err(Error::InvalidData("construction_method is taken from the set 0, 1 or 2 per ISO 14496-12:2015 § 8.11.3.3"))
14881488
}

mp4parse_capi/examples/dump.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ fn dump_file(filename: &str) {
2828
};
2929

3030
unsafe {
31-
let parser = mp4parse_new(&io);
31+
let mut rv = Mp4parseStatus::Invalid;
32+
let parser = mp4parse_new(&io, &mut rv);
3233

33-
match mp4parse_read(parser) {
34+
match rv {
3435
Mp4parseStatus::Ok => (),
3536
_ => {
3637
println!("-- fail to parse, '-v' for more info");

mp4parse_capi/examples/test.cc

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616

1717
#include "mp4parse.h"
1818

19-
intptr_t abort_read(uint8_t *buffer, uintptr_t size, void *userdata)
20-
{
21-
// This shouldn't be called when allocating a parser.
22-
abort();
23-
}
24-
2519
intptr_t error_read(uint8_t *buffer, uintptr_t size, void *userdata)
2620
{
2721
return -1;
@@ -39,36 +33,28 @@ intptr_t io_read(uint8_t *buffer, uintptr_t size, void *userdata)
3933
return r;
4034
}
4135

42-
void test_new_parser()
43-
{
44-
int dummy_value = 42;
45-
Mp4parseIo io = { abort_read, &dummy_value };
46-
Mp4parseParser *parser = mp4parse_new(&io);
47-
assert(parser != nullptr);
48-
mp4parse_free(parser);
49-
assert(dummy_value == 42);
50-
}
51-
5236
void test_arg_validation()
5337
{
54-
Mp4parseParser *parser = mp4parse_new(nullptr);
38+
Mp4parseParser *parser = mp4parse_new(nullptr, nullptr);
5539
assert(parser == nullptr);
5640

57-
Mp4parseIo io = { nullptr, nullptr };
58-
parser = mp4parse_new(&io);
41+
Mp4parseStatus rv = MP4PARSE_STATUS_INVALID;
42+
parser = mp4parse_new(nullptr, &rv);
43+
assert(rv == MP4PARSE_STATUS_BAD_ARG);
5944
assert(parser == nullptr);
6045

61-
io = { abort_read, nullptr };
62-
parser = mp4parse_new(&io);
46+
Mp4parseIo io = { nullptr, nullptr };
47+
rv = MP4PARSE_STATUS_INVALID;
48+
parser = mp4parse_new(&io, &rv);
49+
assert(rv == MP4PARSE_STATUS_BAD_ARG);
6350
assert(parser == nullptr);
6451

6552
int dummy_value = 42;
6653
io = { nullptr, &dummy_value };
67-
parser = mp4parse_new(&io);
68-
assert(parser == nullptr);
69-
70-
int32_t rv = mp4parse_read(nullptr);
54+
rv = MP4PARSE_STATUS_INVALID;
55+
parser = mp4parse_new(&io, &rv);
7156
assert(rv == MP4PARSE_STATUS_BAD_ARG);
57+
assert(parser == nullptr);
7258

7359
Mp4parseTrackInfo info;
7460
rv = mp4parse_get_track_info(nullptr, 0, &info);
@@ -89,11 +75,10 @@ void test_arg_validation_with_parser()
8975
{
9076
int dummy_value = 42;
9177
Mp4parseIo io = { error_read, &dummy_value };
92-
Mp4parseParser *parser = mp4parse_new(&io);
93-
assert(parser != nullptr);
94-
95-
int32_t rv = mp4parse_read(parser);
78+
Mp4parseStatus rv = MP4PARSE_STATUS_INVALID;
79+
Mp4parseParser *parser = mp4parse_new(&io, &rv);
9680
assert(rv == MP4PARSE_STATUS_IO);
81+
assert(parser == nullptr);
9782

9883
rv = mp4parse_get_track_info(parser, 0, nullptr);
9984
assert(rv == MP4PARSE_STATUS_BAD_ARG);
@@ -104,7 +89,6 @@ void test_arg_validation_with_parser()
10489
rv = mp4parse_get_track_audio_info(parser, 0, nullptr);
10590
assert(rv == MP4PARSE_STATUS_BAD_ARG);
10691

107-
mp4parse_free(parser);
10892
assert(dummy_value == 42);
10993
}
11094

@@ -113,11 +97,10 @@ void test_arg_validation_with_data(const std::string& filename)
11397
FILE* f = fopen(filename.c_str(), "rb");
11498
assert(f != nullptr);
11599
Mp4parseIo io = { io_read, f };
116-
Mp4parseParser *parser = mp4parse_new(&io);
117-
assert(parser != nullptr);
118-
119-
Mp4parseStatus rv = mp4parse_read(parser);
100+
Mp4parseStatus rv = MP4PARSE_STATUS_INVALID;
101+
Mp4parseParser *parser = mp4parse_new(&io, &rv);
120102
assert(rv == MP4PARSE_STATUS_OK);
103+
assert(parser != nullptr);
121104

122105
uint32_t tracks;
123106
rv = mp4parse_get_track_count(parser, &tracks);
@@ -198,14 +181,13 @@ int32_t read_file(const char* filename)
198181
FILE* f = fopen(filename, "rb");
199182
assert(f != nullptr);
200183

184+
fprintf(stderr, "Parsing file '%s'.\n", filename);
201185
Mp4parseIo io = { io_read, f };
202-
Mp4parseParser *parser = mp4parse_new(&io);
203-
assert(parser != nullptr);
186+
Mp4parseStatus rv = MP4PARSE_STATUS_INVALID;
187+
Mp4parseParser *parser = mp4parse_new(&io, &rv);
204188

205-
fprintf(stderr, "Parsing file '%s'.\n", filename);
206-
Mp4parseStatus rv = mp4parse_read(parser);
207189
if (rv != MP4PARSE_STATUS_OK) {
208-
mp4parse_free(parser);
190+
assert(parser == nullptr);
209191
fclose(f);
210192
fprintf(stderr, "Parsing failed: %s\n", errorstring(rv));
211193
return rv;
@@ -234,7 +216,6 @@ int main(int argc, char* argv[])
234216
// Parse command line options.
235217
std::vector<std::string> args(argv + 1, argv + argc);
236218

237-
test_new_parser();
238219
test_arg_validation();
239220
test_arg_validation_with_parser();
240221

0 commit comments

Comments
 (0)