ffmpeg_the_third/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 necessarly 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        ChapterMut {
21            context: mem::transmute_copy(&context),
22            index,
23
24            immutable: Chapter::wrap(mem::transmute_copy(&context), index),
25        }
26    }
27
28    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVChapter {
29        *(*self.context.as_mut_ptr()).chapters.add(self.index)
30    }
31}
32
33impl<'a> ChapterMut<'a> {
34    pub fn set_id(&mut self, value: i64) {
35        unsafe {
36            (*self.as_mut_ptr()).id = value as _;
37        }
38    }
39
40    pub fn set_time_base<R: Into<Rational>>(&mut self, value: R) {
41        unsafe {
42            (*self.as_mut_ptr()).time_base = value.into().into();
43        }
44    }
45
46    pub fn set_start(&mut self, value: i64) {
47        unsafe {
48            (*self.as_mut_ptr()).start = value;
49        }
50    }
51
52    pub fn set_end(&mut self, value: i64) {
53        unsafe {
54            (*self.as_mut_ptr()).end = value;
55        }
56    }
57
58    pub fn set_metadata<K: AsRef<str>, V: AsRef<str>>(&mut self, key: K, value: V) {
59        // dictionary.set() allocates the AVDictionary the first time a key/value is inserted
60        // so we want to update the metadata dictionary afterwards
61        unsafe {
62            let mut dictionary = Dictionary::own(self.metadata().as_mut_ptr());
63            dictionary.set(key.as_ref(), value.as_ref());
64            (*self.as_mut_ptr()).metadata = dictionary.disown();
65        }
66    }
67
68    pub fn metadata(&mut self) -> DictionaryMut<'_> {
69        unsafe { DictionaryMut::wrap((*self.as_mut_ptr()).metadata) }
70    }
71}
72
73impl<'a> Deref for ChapterMut<'a> {
74    type Target = Chapter<'a>;
75
76    fn deref(&self) -> &Self::Target {
77        &self.immutable
78    }
79}