rav1d-cli 1.1.0

Rust port of the dav1d AV1 decoder CLI tools
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::compat::stdio::snprintf;
use crate::compat::stdio::stderr;
use libc::fprintf;
use libc::free;
use libc::malloc;
use libc::memcpy;
use libc::strchr;
use libc::strcmp;
use libc::strlen;
use libc::strncmp;
use libc::ENOMEM;
use libc::ENOPROTOOPT;
use rav1d::include::dav1d::picture::Dav1dPicture;
use rav1d::include::dav1d::picture::Dav1dPictureParameters;
use std::cmp;
use std::ffi::c_char;
use std::ffi::c_int;
use std::ffi::c_long;
use std::ffi::c_uint;
use std::ffi::c_ulong;
use std::ffi::c_void;
use std::mem;

extern "C" {
    static null_muxer: Muxer;
    static md5_muxer: Muxer;
    static yuv_muxer: Muxer;
    static y4m2_muxer: Muxer;
}

#[repr(C)]
pub struct MuxerPriv {
    _unused: (),
}

#[repr(C)]
pub struct MuxerContext {
    pub data: *mut MuxerPriv,
    pub impl_0: *const Muxer,
    pub one_file_per_frame: c_int,
    pub fps: [c_uint; 2],
    pub filename: *const c_char,
    pub framenum: c_int,
    pub priv_data: [u64; 0],
}

#[repr(C)]
pub struct Muxer {
    pub priv_data_size: c_int,
    pub name: *const c_char,
    pub extension: *const c_char,
    pub write_header: Option<
        unsafe extern "C" fn(
            *mut MuxerPriv,
            *const c_char,
            *const Dav1dPictureParameters,
            *const c_uint,
        ) -> c_int,
    >,
    pub write_picture: Option<unsafe extern "C" fn(*mut MuxerPriv, *mut Dav1dPicture) -> c_int>,
    pub write_trailer: Option<unsafe extern "C" fn(*mut MuxerPriv) -> ()>,
    pub verify: Option<unsafe extern "C" fn(*mut MuxerPriv, *const c_char) -> c_int>,
}

static mut muxers: [*const Muxer; 5] = unsafe {
    [
        &null_muxer as *const Muxer,
        &md5_muxer as *const Muxer,
        &yuv_muxer as *const Muxer,
        &y4m2_muxer as *const Muxer,
        0 as *const Muxer,
    ]
};

unsafe fn find_extension(f: *const c_char) -> *const c_char {
    let l: usize = strlen(f);
    if l == 0 {
        return 0 as *const c_char;
    }
    let end: *const c_char = &*f.offset(l.wrapping_sub(1) as isize) as *const c_char;
    let mut step: *const c_char = end;
    while *step as c_int >= 'a' as i32 && *step as c_int <= 'z' as i32
        || *step as c_int >= 'A' as i32 && *step as c_int <= 'Z' as i32
        || *step as c_int >= '0' as i32 && *step as c_int <= '9' as i32
    {
        step = step.offset(-1);
    }
    return if step < end
        && step > f
        && *step as c_int == '.' as i32
        && *step.offset(-1 as isize) as c_int != '/' as i32
    {
        &*step.offset(1) as *const c_char
    } else {
        0 as *const c_char
    };
}

// TODO(kkysen) These are used in `dav1d.rs` and `seek_stress.rs`
// but are still marked as unused since `[[bin]]` are only supposed to be one file in `cargo`.
#[allow(dead_code)]
pub unsafe fn output_open(
    c_out: *mut *mut MuxerContext,
    name: *const c_char,
    filename: *const c_char,
    p: *const Dav1dPictureParameters,
    fps: *const c_uint,
) -> c_int {
    let mut impl_0: *const Muxer = 0 as *const Muxer;
    let c: *mut MuxerContext;
    let mut i: c_uint;
    let mut res = 0;
    let mut name_offset = 0;
    if !name.is_null() {
        name_offset =
            5 as c_int * (strncmp(name, b"frame\0" as *const u8 as *const c_char, 5) == 0) as c_int;
        i = 0 as c_int as c_uint;
        while !(muxers[i as usize]).is_null() {
            if strcmp(
                (*muxers[i as usize]).name,
                &*name.offset(name_offset as isize),
            ) == 0
            {
                impl_0 = muxers[i as usize];
                break;
            } else {
                i = i.wrapping_add(1);
            }
        }
        if (muxers[i as usize]).is_null() {
            fprintf(
                stderr(),
                b"Failed to find muxer named \"%s\"\n\0" as *const u8 as *const c_char,
                name,
            );
            return -ENOPROTOOPT;
        }
    } else if strcmp(filename, b"/dev/null\0" as *const u8 as *const c_char) == 0 {
        impl_0 = muxers[0];
    } else {
        let ext: *const c_char = find_extension(filename);
        if ext.is_null() {
            fprintf(
                stderr(),
                b"No extension found for file %s\n\0" as *const u8 as *const c_char,
                filename,
            );
            return -1;
        }
        i = 0 as c_int as c_uint;
        while !(muxers[i as usize]).is_null() {
            if strcmp((*muxers[i as usize]).extension, ext) == 0 {
                impl_0 = muxers[i as usize];
                break;
            } else {
                i = i.wrapping_add(1);
            }
        }
        if (muxers[i as usize]).is_null() {
            fprintf(
                stderr(),
                b"Failed to find muxer for extension \"%s\"\n\0" as *const u8 as *const c_char,
                ext,
            );
            return -ENOPROTOOPT;
        }
    }
    c = malloc(mem::size_of::<MuxerContext>() + (*impl_0).priv_data_size as usize)
        as *mut MuxerContext;
    if c.is_null() {
        fprintf(
            stderr(),
            b"Failed to allocate memory\n\0" as *const u8 as *const c_char,
        );
        return -ENOMEM;
    }
    (*c).impl_0 = impl_0;
    (*c).data = ((*c).priv_data).as_mut_ptr() as *mut MuxerPriv;
    let mut have_num_pattern = 0;
    let mut ptr: *const c_char = if !filename.is_null() {
        strchr(filename, '%' as i32)
    } else {
        0 as *mut c_char
    };
    while have_num_pattern == 0 && !ptr.is_null() {
        ptr = ptr.offset(1);
        while *ptr as c_int >= '0' as i32 && *ptr as c_int <= '9' as i32 {
            ptr = ptr.offset(1);
        }
        have_num_pattern = (*ptr as c_int == 'n' as i32) as c_int;
        ptr = strchr(ptr, '%' as i32);
    }
    (*c).one_file_per_frame =
        (name_offset != 0 || name.is_null() && have_num_pattern != 0) as c_int;
    if (*c).one_file_per_frame != 0 {
        (*c).fps[0] = *fps.offset(0);
        (*c).fps[1] = *fps.offset(1);
        (*c).filename = filename;
        (*c).framenum = 0 as c_int;
    } else if ((*impl_0).write_header).is_some() && {
        res = ((*impl_0).write_header).expect("non-null function pointer")(
            (*c).data,
            filename,
            p,
            fps,
        );
        res < 0
    } {
        free(c as *mut c_void);
        return res;
    }
    *c_out = c;
    return 0 as c_int;
}

unsafe fn safe_strncat(dst: *mut c_char, dst_len: c_int, src: *const c_char, src_len: c_int) {
    if src_len == 0 {
        return;
    }
    let dst_fill = strlen(dst) as c_int;
    if !(dst_fill < dst_len) {
        unreachable!();
    }
    let to_copy = cmp::min(src_len, dst_len - dst_fill - 1);
    if to_copy == 0 {
        return;
    }
    memcpy(
        dst.offset(dst_fill as isize) as *mut c_void,
        src as *const c_void,
        to_copy as usize,
    );
    *dst.offset((dst_fill + to_copy) as isize) = 0 as c_int as c_char;
}

unsafe fn assemble_field(
    dst: *mut c_char,
    dst_len: c_int,
    fmt: *const c_char,
    fmt_len: c_int,
    field: c_int,
) {
    let mut fmt_copy: [c_char; 32] = [0; 32];
    if !(*fmt.offset(0) as c_int == '%' as i32) {
        unreachable!();
    }
    fmt_copy[0] = '%' as i32 as c_char;
    if *fmt.offset(1) as c_int >= '1' as i32 && *fmt.offset(1) as c_int <= '9' as i32 {
        fmt_copy[1] = '0' as i32 as c_char;
        fmt_copy[2] = 0 as c_int as c_char;
    } else {
        fmt_copy[1] = 0 as c_int as c_char;
    }
    safe_strncat(
        fmt_copy.as_mut_ptr(),
        ::core::mem::size_of::<[c_char; 32]>() as c_ulong as c_int,
        &*fmt.offset(1),
        fmt_len - 1,
    );
    safe_strncat(
        fmt_copy.as_mut_ptr(),
        ::core::mem::size_of::<[c_char; 32]>() as c_ulong as c_int,
        b"d\0" as *const u8 as *const c_char,
        1 as c_int,
    );
    let mut tmp: [c_char; 32] = [0; 32];
    snprintf(
        tmp.as_mut_ptr(),
        ::core::mem::size_of::<[c_char; 32]>(),
        fmt_copy.as_mut_ptr(),
        field,
    );
    safe_strncat(
        dst,
        dst_len,
        tmp.as_mut_ptr(),
        strlen(tmp.as_mut_ptr()) as c_int,
    );
}

unsafe fn assemble_filename(
    ctx: *mut MuxerContext,
    filename: *mut c_char,
    filename_size: c_int,
    p: *const Dav1dPictureParameters,
) {
    *filename.offset(0) = 0 as c_int as c_char;
    let fresh0 = (*ctx).framenum;
    (*ctx).framenum = (*ctx).framenum + 1;
    let framenum = fresh0;
    if ((*ctx).filename).is_null() {
        unreachable!();
    }
    let mut ptr: *const c_char = (*ctx).filename;
    let mut iptr: *const c_char;
    loop {
        iptr = strchr(ptr, '%' as i32);
        if iptr.is_null() {
            break;
        }
        safe_strncat(
            filename,
            filename_size,
            ptr,
            iptr.offset_from(ptr) as c_long as c_int,
        );
        ptr = iptr;
        let mut iiptr: *const c_char = &*iptr.offset(1) as *const c_char;
        while *iiptr as c_int >= '0' as i32 && *iiptr as c_int <= '9' as i32 {
            iiptr = iiptr.offset(1);
        }
        match *iiptr as c_int {
            119 => {
                assemble_field(
                    filename,
                    filename_size,
                    ptr,
                    iiptr.offset_from(ptr) as c_long as c_int,
                    (*p).w,
                );
            }
            104 => {
                assemble_field(
                    filename,
                    filename_size,
                    ptr,
                    iiptr.offset_from(ptr) as c_long as c_int,
                    (*p).h,
                );
            }
            110 => {
                assemble_field(
                    filename,
                    filename_size,
                    ptr,
                    iiptr.offset_from(ptr) as c_long as c_int,
                    framenum,
                );
            }
            _ => {
                safe_strncat(
                    filename,
                    filename_size,
                    b"%\0" as *const u8 as *const c_char,
                    1 as c_int,
                );
                ptr = &*iptr.offset(1) as *const c_char;
                continue;
            }
        }
        ptr = &*iiptr.offset(1) as *const c_char;
    }
    safe_strncat(filename, filename_size, ptr, strlen(ptr) as c_int);
}

// TODO(kkysen) These are used in `dav1d.rs` and `seek_stress.rs`
// but are still marked as unused since `[[bin]]` are only supposed to be one file in `cargo`.
#[allow(dead_code)]
pub unsafe fn output_write(ctx: *mut MuxerContext, p: *mut Dav1dPicture) -> c_int {
    let mut res;
    if (*ctx).one_file_per_frame != 0 && ((*(*ctx).impl_0).write_header).is_some() {
        let mut filename: [c_char; 1024] = [0; 1024];
        assemble_filename(
            ctx,
            filename.as_mut_ptr(),
            ::core::mem::size_of::<[c_char; 1024]>() as c_ulong as c_int,
            &mut (*p).p,
        );
        res = ((*(*ctx).impl_0).write_header).expect("non-null function pointer")(
            (*ctx).data,
            filename.as_mut_ptr(),
            &mut (*p).p,
            ((*ctx).fps).as_mut_ptr() as *const c_uint,
        );
        if res < 0 {
            return res;
        }
    }
    res = ((*(*ctx).impl_0).write_picture).expect("non-null function pointer")((*ctx).data, p);
    if res < 0 {
        return res;
    }
    if (*ctx).one_file_per_frame != 0 && ((*(*ctx).impl_0).write_trailer).is_some() {
        ((*(*ctx).impl_0).write_trailer).expect("non-null function pointer")((*ctx).data);
    }
    return 0 as c_int;
}

// TODO(kkysen) These are used in `dav1d.rs` and `seek_stress.rs`
// but are still marked as unused since `[[bin]]` are only supposed to be one file in `cargo`.
#[allow(dead_code)]
pub unsafe fn output_close(ctx: *mut MuxerContext) {
    if (*ctx).one_file_per_frame == 0 && ((*(*ctx).impl_0).write_trailer).is_some() {
        ((*(*ctx).impl_0).write_trailer).expect("non-null function pointer")((*ctx).data);
    }
    free(ctx as *mut c_void);
}

// TODO(kkysen) These are used in `dav1d.rs` and `seek_stress.rs`
// but are still marked as unused since `[[bin]]` are only supposed to be one file in `cargo`.
#[allow(dead_code)]
pub unsafe fn output_verify(ctx: *mut MuxerContext, md5_str: *const c_char) -> c_int {
    let res = if ((*(*ctx).impl_0).verify).is_some() {
        ((*(*ctx).impl_0).verify).expect("non-null function pointer")((*ctx).data, md5_str)
    } else {
        0 as c_int
    };
    free(ctx as *mut c_void);
    return res;
}