Skip to main content

ffmpeg_next/format/chapter/
chapter_mut.rs

1use std::mem;
2use std::ops::Deref;
3
4use super::Chapter;
5use crate::ffi::*;
6use crate::format::context::common::Context;
7use crate::{Dictionary, DictionaryMut, Rational};
8
9// WARNING: index refers to the offset in the chapters array (starting from 0)
10// it is not necessarily equal to the id (which may start at 1)
11pub struct ChapterMut<'a> {
12    context: &'a mut Context,
13    index: usize,
14
15    immutable: Chapter<'a>,
16}
17
18impl<'a> ChapterMut<'a> {
19    pub unsafe fn wrap(context: &mut Context, index: usize) -> ChapterMut<'_> {
20        unsafe {
21            ChapterMut {
22                context: mem::transmute_copy(&context),
23                index,
24
25                immutable: Chapter::wrap(mem::transmute_copy(&context), index),
26            }
27        }
28    }
29
30    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVChapter {
31        unsafe { *(*self.context.as_mut_ptr()).chapters.add(self.index) }
32    }
33}
34
35impl<'a> ChapterMut<'a> {
36    pub fn set_id(&mut self, value: i64) {
37        unsafe {
38            (*self.as_mut_ptr()).id = value as _;
39        }
40    }
41
42    pub fn set_time_base<R: Into<Rational>>(&mut self, value: R) {
43        unsafe {
44            (*self.as_mut_ptr()).time_base = value.into().into();
45        }
46    }
47
48    pub fn set_start(&mut self, value: i64) {
49        unsafe {
50            (*self.as_mut_ptr()).start = value;
51        }
52    }
53
54    pub fn set_end(&mut self, value: i64) {
55        unsafe {
56            (*self.as_mut_ptr()).end = value;
57        }
58    }
59
60    pub fn set_metadata<K: AsRef<str>, V: AsRef<str>>(&mut self, key: K, value: V) {
61        // dictionary.set() allocates the AVDictionary the first time a key/value is inserted
62        // so we want to update the metadata dictionary afterwards
63        unsafe {
64            let mut dictionary = Dictionary::own(self.metadata().as_mut_ptr());
65            dictionary.set(key.as_ref(), value.as_ref());
66            (*self.as_mut_ptr()).metadata = dictionary.disown();
67        }
68    }
69
70    pub fn metadata(&mut self) -> DictionaryMut<'_> {
71        unsafe { DictionaryMut::wrap((*self.as_mut_ptr()).metadata) }
72    }
73}
74
75impl<'a> Deref for ChapterMut<'a> {
76    type Target = Chapter<'a>;
77
78    fn deref(&self) -> &Self::Target {
79        &self.immutable
80    }
81}