aviutl2 0.11.1

AviUtl2 SDKのRustラッパー
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
pub use anyhow::Result as AnyResult;
use zerocopy::{Immutable, IntoBytes};

pub use half::{self, f16};
pub use num_rational::{self, Rational32};
pub use raw_window_handle::{self, Win32WindowHandle};

/// AviUtl2の情報。
#[derive(Debug, Clone)]
pub struct AviUtl2Info {
    /// AviUtl2のバージョン。
    pub version: AviUtl2Version,
}

/// AviUtl2のバージョン。
///
/// # Note
///
/// バージョン番号の形式は公式に定義されていないため、暫定的に以下のように解釈しています。
/// ```text
/// 2001500
/// || | |
/// || | +-- ビルドバージョン
/// || +---- ベータバージョン
/// |+------ マイナーバージョン
/// +------- メジャーバージョン
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct AviUtl2Version(pub u32);
impl From<u32> for AviUtl2Version {
    fn from(value: u32) -> Self {
        Self(value)
    }
}
impl From<AviUtl2Version> for u32 {
    fn from(value: AviUtl2Version) -> Self {
        value.0
    }
}
impl AviUtl2Version {
    /// 新しいバージョンを作成します。
    ///
    /// # Panics
    ///
    /// `minor`、`patch`、`build`がそれぞれ100以上の場合にパニックします。
    pub fn new(major: u8, minor: u8, patch: u8, build: u8) -> Self {
        assert!(minor < 100, "minor version must be less than 100");
        assert!(patch < 100, "patch version must be less than 100");
        assert!(build < 100, "build version must be less than 100");
        Self(
            (major as u32) * 1_000_000
                + (minor as u32) * 10_000
                + (patch as u32) * 100
                + (build as u32),
        )
    }

    /// メジャーバージョンを取得します。
    pub fn major(self) -> u32 {
        self.0 / 1000000
    }

    /// マイナーバージョンを取得します。
    pub fn minor(self) -> u32 {
        (self.0 / 10000) % 100
    }

    /// ベータバージョンを取得します。
    pub fn patch(self) -> u32 {
        (self.0 / 100) % 100
    }

    /// ビルドバージョンを取得します。
    pub fn build(self) -> u32 {
        self.0 % 100
    }
}
#[cfg(feature = "aviutl2-alias")]
impl aviutl2_alias::FromTableValue for AviUtl2Version {
    type Err = std::num::ParseIntError;

    fn from_table_value(value: &str) -> Result<Self, Self::Err> {
        let v: u32 = value.parse()?;
        Ok(Self::from(v))
    }
}

/// ファイル選択ダイアログのフィルタを表す構造体。
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileFilter {
    /// フィルタの名前。
    pub name: String,
    /// フィルタが適用される拡張子のリスト。
    pub extensions: Vec<String>,
}

/// [`Vec<FileFilter>`]を簡単に作成するためのマクロ。
///
/// # Example
///
/// ```rust
/// let filters = aviutl2::file_filters! {
///     "Image Files" => ["png", "jpg"],
///     "All Files" => []
/// };
/// ```
#[macro_export]
macro_rules! file_filters {
    ($($name:expr => [$($ext:expr),* $(,)?] ),* $(,)?) => {
        vec![
            $(
                $crate::FileFilter {
                    name: $name.to_string(),
                    extensions: vec![$($ext.to_string()),*],
                }
            ),*
        ]
    };
}

/// YC48のピクセルフォーマットを表す構造体。
///
/// # See Also
/// <https://makiuchi-d.github.io/mksoft/doc/aviutlyc.html>
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntoBytes, Immutable)]
#[repr(C)]
pub struct Yc48 {
    /// Y成分の値。
    /// 0から4096までの値を取ります。
    pub y: i16,
    /// Cb成分の値。
    /// -2048から2048までの値を取ります。
    pub cb: i16,
    /// Cr成分の値。
    /// -2048から2048までの値を取ります。
    pub cr: i16,
}
impl Yc48 {
    /// YUV 4:2:2(YUY2)からYC48に変換します。
    pub fn from_yuy2(yuy2: (u8, u8, u8, u8)) -> (Self, Self) {
        let (y1, u, y2, v) = yuy2;
        let ny1 = ((y1 as u16 * 1197) >> 6) - 299;
        let ny2 = ((y2 as u16 * 1197) >> 6) - 299;
        let ncb = ((u as u16 - 128) * 4681 + 164) >> 8;
        let ncr = ((v as u16 - 128) * 4681 + 164) >> 8;
        (
            Self {
                y: ny1 as i16,
                cb: ncb as i16,
                cr: ncr as i16,
            },
            Self {
                y: ((ny1 + ny2) >> 1) as i16,
                cb: ncb as i16,
                cr: ncr as i16,
            },
        )
    }

    /// YC48からYUV 4:2:2(YUY2)に変換します。
    pub fn to_yuy2(self, other: Yc48) -> (u8, u8, u8, u8) {
        let y1 = ((self.y * 219 + 383) >> 12) + 16;
        let y2 = ((other.y * 219 + 383) >> 12) + 16;
        let u = (((self.cb + 2048) * 7 + 66) >> 7) + 16;
        let v = (((self.cr + 2048) * 7 + 66) >> 7) + 16;
        let y1 = y1.min(255) as u8;
        let y2 = y2.min(255) as u8;
        let u = u.min(255) as u8;
        let v = v.min(255) as u8;
        (y1, u, y2, v)
    }

    /// RGBからYC48に変換します。
    pub fn from_rgb(self, rgb: (u8, u8, u8)) -> Self {
        let (r, g, b) = rgb;
        let r = i16::from(r);
        let g = i16::from(g);
        let b = i16::from(b);
        let y = ((4918 * r + 354) >> 10) + ((9655 * g + 585) >> 10) + ((1875 * b + 523) >> 10);
        let cb = ((-2775 * r + 240) >> 10) + ((-5449 * g + 515) >> 10) + ((8224 * b + 256) >> 10);
        let cr = ((8224 * r + 256) >> 10) + ((-6887 * g + 110) >> 10) + ((-1337 * b + 646) >> 10);

        Yc48 { y, cb, cr }
    }

    /// YC48からRGBに変換します。
    pub fn to_rgb(self) -> (u8, u8, u8) {
        let y = self.y as i32;
        let cr = self.cr as i32;
        let cb = self.cr as i32;
        let r = (255 * y + ((((22881 * cr) >> 16) + 3) << 10)) >> 12;
        let g = (255 * y + ((((-5616 * cb) >> 16) + ((-11655 * cr) >> 16) + 3) << 10)) >> 12;
        let b = (255 * y + ((((28919 * cb) >> 16) + 3) << 10)) >> 12;

        let r = r.min(255) as u8;
        let g = g.min(255) as u8;
        let b = b.min(255) as u8;
        (r, g, b)
    }
}

pub(crate) fn format_file_filters(file_filters: &[FileFilter]) -> String {
    let mut file_filter = String::new();
    for filter in file_filters {
        let display = format!(
            "{} ({})",
            filter.name,
            if filter.extensions.is_empty() {
                "*".to_string()
            } else {
                filter
                    .extensions
                    .iter()
                    .map(|ext| format!(".{ext}"))
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        );
        file_filter.push_str(&display);
        file_filter.push('\x00');
        if filter.extensions.is_empty() {
            file_filter.push('*');
        } else {
            file_filter.push_str(
                &filter
                    .extensions
                    .iter()
                    .map(|ext| format!("*.{ext}"))
                    .collect::<Vec<_>>()
                    .join(";"),
            );
        }
        file_filter.push('\x00');
    }

    file_filter
}

pub(crate) enum LeakType {
    WideString,
    ValueVector { len: usize, name: String },
    Null,
    Other(String),
}

pub(crate) struct LeakManager {
    ptrs: std::sync::Mutex<Vec<(LeakType, usize)>>,
}

pub(crate) trait IntoLeakedPtr {
    fn into_leaked_ptr(self) -> (LeakType, usize);
}
pub(crate) trait LeakableValue {}

impl LeakManager {
    pub fn new() -> Self {
        Self {
            ptrs: std::sync::Mutex::new(Vec::new()),
        }
    }

    pub fn leak<T: IntoLeakedPtr>(&self, value: T) -> *const T {
        log::debug!("Leaking memory for type {}", std::any::type_name::<T>());
        let mut ptrs = self.ptrs.lock().unwrap();
        let leaked = value.into_leaked_ptr();
        let ptr = leaked.1;
        ptrs.push(leaked);
        ptr as *const T
    }

    pub fn leak_as_wide_string(&self, s: &str) -> *const u16 {
        log::debug!("Leaking wide string: {}", s);
        let mut wide: Vec<u16> = s.encode_utf16().collect();
        wide.push(0); // Null-terminate the string
        let boxed = wide.into_boxed_slice();
        let ptr = Box::into_raw(boxed) as *mut u16 as usize;
        let mut ptrs = self.ptrs.lock().unwrap();
        ptrs.push((LeakType::WideString, ptr));
        ptr as *const u16
    }

    // pub fn leak_ptr_vec<T: IntoLeakedPtr>(&self, vec: Vec<T>) -> *const *const T {
    //     log::debug!("Leaking vector of type {}", std::any::type_name::<T>());
    //     let mut raw_ptrs = Vec::with_capacity(vec.len() + 1);
    //     for item in vec {
    //         let leaked = item.into_leaked_ptr();
    //         let ptr = leaked.1;
    //         raw_ptrs.push(ptr);
    //         let mut ptrs = self.ptrs.lock().unwrap();
    //         ptrs.push(leaked);
    //     }
    //     self.leak_value_vec(raw_ptrs) as _
    // }

    pub fn leak_value_vec<T: LeakableValue>(&self, vec: Vec<T>) -> *const T {
        log::debug!(
            "Leaking value vector of type {}",
            std::any::type_name::<T>()
        );
        let len = vec.len();
        let boxed = vec.into_boxed_slice();
        let ptr = Box::into_raw(boxed) as *mut T as usize;
        let mut ptrs = self.ptrs.lock().unwrap();
        ptrs.push((
            LeakType::ValueVector {
                len,
                name: std::any::type_name::<T>().to_string(),
            },
            ptr,
        ));
        ptr as *const T
    }

    pub fn free_leaked_memory(&self) {
        let mut ptrs = self.ptrs.lock().unwrap();
        while let Some((ptr_type, ptr)) = ptrs.pop() {
            match ptr_type {
                LeakType::WideString => unsafe {
                    let _ = Box::from_raw(ptr as *mut u16);
                },
                LeakType::ValueVector { len, name } => {
                    Self::free_leaked_memory_leakable_value(&name, ptr, len);
                }
                LeakType::Null => {
                    assert!(ptr == 0);
                }
                LeakType::Other(ref type_name) => {
                    Self::free_leaked_memory_other_ptr(type_name, ptr);
                }
            }
        }
    }
}
macro_rules! impl_leak_ptr {
    ($($t:ty),* $(,)?) => {
        $(
            impl IntoLeakedPtr for $t {
                fn into_leaked_ptr(self) -> (LeakType, usize) {
                    let boxed = Box::new(self);
                    let ptr = Box::into_raw(boxed) as usize;
                    (LeakType::Other(std::any::type_name::<$t>().to_string()), ptr)
                }
            }
        )*

        impl LeakManager {
            fn free_leaked_memory_other_ptr(ptr_type: &str, ptr: usize) {
                unsafe {
                    match ptr_type {
                        $(
                            t if t == std::any::type_name::<$t>() => {
                                let _ = Box::from_raw(ptr as *mut $t);
                            },
                        )*
                        _ => {
                            unreachable!("Unknown leaked pointer type: {}", ptr_type);
                        }
                    }
                }
            }
        }
    };
}
macro_rules! impl_leakable_value {
    ($($t:ty),* $(,)?) => {
        $(
            impl LeakableValue for $t {}
        )*
        impl LeakManager {
            fn free_leaked_memory_leakable_value(type_name: &str, ptr: usize, len: usize) {
                unsafe {
                    match type_name {
                        $(
                            t if t == std::any::type_name::<$t>() => {
                                let _ = Box::from_raw(std::slice::from_raw_parts_mut(ptr as *mut $t, len));
                            },
                        )*
                        _ => {
                            unreachable!("Unknown leaked value vector type: {}", type_name);
                        }
                    }
                }
            }
        }
    };
}
impl_leak_ptr!(
    aviutl2_sys::input2::WAVEFORMATEX,
    aviutl2_sys::output2::BITMAPINFOHEADER,
    aviutl2_sys::filter2::FILTER_ITEM,
);
impl_leakable_value!(
    aviutl2_sys::filter2::FILTER_ITEM_SELECT_ITEM,
    aviutl2_sys::module2::SCRIPT_MODULE_FUNCTION,
    usize
);

impl<T: IntoLeakedPtr> IntoLeakedPtr for Option<T> {
    fn into_leaked_ptr(self) -> (LeakType, usize) {
        match self {
            Some(value) => value.into_leaked_ptr(),
            None => (LeakType::Null, 0),
        }
    }
}

impl Drop for LeakManager {
    fn drop(&mut self) {
        self.free_leaked_memory();
    }
}

/// # Safety
///
/// - `ptr` は有効なLPCWSTRであること。
/// - `ptr` はNull Terminatedなu16文字列を指していること。
pub(crate) unsafe fn load_wide_string(ptr: *const u16) -> String {
    if ptr.is_null() {
        return String::new();
    }

    let mut len = 0;
    while unsafe { *ptr.add(len) } != 0 {
        len += 1;
    }

    unsafe { String::from_utf16_lossy(std::slice::from_raw_parts(ptr, len)) }
}

pub(crate) fn alert_error(error: &anyhow::Error) {
    let _ = native_dialog::DialogBuilder::message()
        .set_title("エラー")
        .set_level(native_dialog::MessageLevel::Error)
        .set_text(format!("エラーが発生しました: {error}"))
        .alert()
        .show();
}

/// ワイド文字列(LPCWSTR)を所有するRust文字列として扱うためのラッパー。
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct CWString(Vec<u16>);

/// ヌルバイトを含む文字列を作成しようとした際のエラー。
#[derive(thiserror::Error, Debug)]
#[error("String contains null byte")]
pub struct NullByteError {
    position: usize,
    u16_seq: Vec<u16>,
}
impl NullByteError {
    pub fn nul_position(&self) -> usize {
        self.position
    }

    pub fn into_vec(self) -> Vec<u16> {
        self.u16_seq
    }
}

impl CWString {
    /// 新しいCWStringを作成します。
    ///
    /// # Errors
    ///
    /// `string`にヌルバイトが含まれている場合、`NullByteError`を返します。
    pub fn new(string: &str) -> Result<Self, NullByteError> {
        let mut wide: Vec<u16> = string.encode_utf16().collect();
        if let Some((pos, _)) = wide.iter().enumerate().find(|&(_, &c)| c == 0) {
            return Err(NullByteError {
                position: pos,
                u16_seq: wide,
            });
        }
        wide.push(0); // Null-terminate the string
        Ok(Self(wide))
    }

    /// 内部のポインタを取得します。
    ///
    /// # Warning
    ///
    /// <div class="warning">
    ///
    /// この`CWString`がDropされた後にこのポインタを使用すると未定義動作を引き起こします。
    ///
    /// </div>
    pub fn as_ptr(&self) -> *const u16 {
        self.0.as_ptr()
    }
}
impl std::fmt::Display for CWString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = String::from_utf16_lossy(&self.0[..self.0.len() - 1]);
        write!(f, "{}", s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_file_filters() {
        let filters = vec![
            FileFilter {
                name: "Image Files".to_string(),
                extensions: vec!["png".to_string(), "jpg".to_string()],
            },
            FileFilter {
                name: "All Files".to_string(),
                extensions: vec![],
            },
        ];
        let formatted = format_file_filters(&filters);
        let expected = "Image Files (.png, .jpg)\x00*.png;*.jpg\x00All Files (*)\x00*\x00";
        assert_eq!(formatted, expected);
    }

    #[test]
    fn test_file_filters_macro() {
        let filters = file_filters! {
            "Image Files" => ["png", "jpg"],
            "All Files" => []
        };
        assert_eq!(filters.len(), 2);
        assert_eq!(filters[0].name, "Image Files");
        assert_eq!(
            filters[0].extensions,
            vec!["png".to_string(), "jpg".to_string()]
        );
        assert_eq!(filters[1].name, "All Files");
        assert_eq!(filters[1].extensions, Vec::<String>::new());
    }

    #[test]
    fn test_aviutl2_version() {
        let version = AviUtl2Version::new(2, 0, 15, 0);
        assert_eq!(version.0, 2001500);
        assert_eq!(version.major(), 2);
        assert_eq!(version.minor(), 0);
        assert_eq!(version.patch(), 15);
        assert_eq!(version.build(), 0);

        let version_from_u32: AviUtl2Version = 2001500u32.into();
        assert_eq!(version_from_u32, version);

        let u32_from_version: u32 = version.into();
        assert_eq!(u32_from_version, 2001500);
    }

    #[test]
    fn test_cwstring_new() {
        let s = "Hello, world!";
        let cwstring = CWString::new(s).unwrap();
        assert_eq!(unsafe { load_wide_string(cwstring.as_ptr()) }, s);

        let s_with_nul = "Hello\0world!";
        let err = CWString::new(s_with_nul).unwrap_err();
        assert_eq!(err.nul_position(), 5);
    }
}