summaryrefslogtreecommitdiffstats
path: root/third_party/rust/mp4parse_capi/examples/dump.rs
blob: 86cf9acdb5094cadbe821e0af57ac2c93505a633 (plain)
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
use log::info;
use mp4parse::ParseStrictness;
use mp4parse_capi::*;
use std::env;
use std::fs::File;
use std::io::Read;

extern "C" fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_void) -> isize {
    let input: &mut std::fs::File = unsafe { &mut *(userdata as *mut _) };
    let buf = unsafe { std::slice::from_raw_parts_mut(buf, size) };
    match input.read(buf) {
        Ok(n) => n as isize,
        Err(_) => -1,
    }
}

fn dump_avif(filename: &str, strictness: ParseStrictness) {
    let mut file = File::open(filename).expect("Unknown file");
    let io = Mp4parseIo {
        read: Some(buf_read),
        userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
    };

    unsafe {
        let mut parser = std::ptr::null_mut();
        let rv = mp4parse_avif_new(&io, strictness, &mut parser);
        println!("mp4parse_avif_new -> {rv:?}");
        if rv == Mp4parseStatus::Ok {
            println!(
                "mp4parse_avif_get_image_safe -> {:?}",
                mp4parse_avif_get_image_safe(&*parser)
            );
        }
    }
}

fn dump_file(filename: &str, strictness: ParseStrictness) {
    let mut file = File::open(filename).expect("Unknown file");
    let io = Mp4parseIo {
        read: Some(buf_read),
        userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
    };

    unsafe {
        let mut parser = std::ptr::null_mut();
        let rv = mp4parse_new(&io, &mut parser);

        match rv {
            Mp4parseStatus::Ok => (),
            Mp4parseStatus::Invalid => {
                println!("-- failed to parse as mp4 video, trying AVIF");
                dump_avif(filename, strictness);
            }
            _ => {
                println!("-- fail to parse: {rv:?}, '-v' for more info");
                return;
            }
        }

        let mut frag_info = Mp4parseFragmentInfo::default();
        match mp4parse_get_fragment_info(parser, &mut frag_info) {
            Mp4parseStatus::Ok => {
                println!("-- mp4parse_fragment_info {frag_info:?}");
            }
            rv => {
                println!("-- mp4parse_fragment_info failed with {rv:?}");
                return;
            }
        }

        let mut counts: u32 = 0;
        match mp4parse_get_track_count(parser, &mut counts) {
            Mp4parseStatus::Ok => (),
            _ => {
                println!("-- mp4parse_get_track_count failed");
                return;
            }
        }

        for i in 0..counts {
            let mut track_info = Mp4parseTrackInfo {
                track_type: Mp4parseTrackType::Audio,
                ..Default::default()
            };
            match mp4parse_get_track_info(parser, i, &mut track_info) {
                Mp4parseStatus::Ok => {
                    println!("-- mp4parse_get_track_info {track_info:?}");
                }
                _ => {
                    println!("-- mp4parse_get_track_info failed, track id: {i}");
                    return;
                }
            }

            match track_info.track_type {
                Mp4parseTrackType::Audio => {
                    let mut audio_info = Mp4parseTrackAudioInfo::default();
                    match mp4parse_get_track_audio_info(parser, i, &mut audio_info) {
                        Mp4parseStatus::Ok => {
                            println!("-- mp4parse_get_track_audio_info {audio_info:?}");
                            for i in 0..audio_info.sample_info_count as isize {
                                let sample_info = audio_info.sample_info.offset(i);
                                println!(
                                    "  -- mp4parse_get_track_audio_info sample_info[{:?}] {:?}",
                                    i, *sample_info
                                );
                            }
                        }
                        _ => {
                            println!("-- mp4parse_get_track_audio_info failed, track id: {i}");
                            return;
                        }
                    }
                }
                Mp4parseTrackType::Video
                | Mp4parseTrackType::Picture
                | Mp4parseTrackType::AuxiliaryVideo => {
                    let mut video_info = Mp4parseTrackVideoInfo::default();
                    match mp4parse_get_track_video_info(parser, i, &mut video_info) {
                        Mp4parseStatus::Ok => {
                            println!("-- mp4parse_get_track_video_info {video_info:?}");
                            for i in 0..video_info.sample_info_count as isize {
                                let sample_info = video_info.sample_info.offset(i);
                                println!(
                                    "  -- mp4parse_get_track_video_info sample_info[{:?}] {:?}",
                                    i, *sample_info
                                );
                            }
                        }
                        _ => {
                            println!("-- mp4parse_get_track_video_info failed, track id: {i}");
                            return;
                        }
                    }
                }
                Mp4parseTrackType::Metadata => {
                    println!("TODO metadata track");
                }
            }

            let mut indices = Mp4parseByteData::default();
            match mp4parse_get_indice_table(parser, track_info.track_id, &mut indices) {
                Mp4parseStatus::Ok => {
                    println!(
                        "-- mp4parse_get_indice_table track_id {} indices {:?}",
                        track_info.track_id, indices
                    );
                }
                _ => {
                    println!(
                        "-- mp4parse_get_indice_table failed, track_info.track_id: {}",
                        track_info.track_id
                    );
                    return;
                }
            }
        }
        mp4parse_free(parser);
    }
}

fn main() {
    let mut dump_func: fn(&str, ParseStrictness) = dump_file;
    let mut verbose = false;
    let mut strictness = ParseStrictness::Normal;
    let mut filenames = Vec::new();
    for arg in env::args().skip(1) {
        match arg.as_str() {
            "--avif" => dump_func = dump_avif,
            "--strict" => strictness = ParseStrictness::Strict,
            "--permissive" => strictness = ParseStrictness::Permissive,
            "-v" | "--verbose" => verbose = true,
            _ => {
                if let Some("-") = arg.get(0..1) {
                    eprintln!("Ignoring unknown switch {arg:?}");
                } else {
                    filenames.push(arg)
                }
            }
        }
    }

    if filenames.is_empty() {
        eprintln!("No files to dump, exiting...");
        return;
    }

    let env = env_logger::Env::default();
    let mut logger = env_logger::Builder::from_env(env);
    if verbose {
        logger.filter(None, log::LevelFilter::Debug);
    }
    logger.init();

    for filename in filenames {
        info!("-- dump of '{}' --", filename);
        dump_func(filename.as_str(), strictness);
        info!("-- end of '{}' --", filename);
    }
}