mirror of
https://github.com/encounter/rust-ffmpeg-zmwangx.git
synced 2026-07-10 21:18:40 -07:00
refactor: remove the lifetime from an owned dictionary
This commit is contained in:
+10
-6
@@ -24,18 +24,22 @@ fn main() {
|
||||
}
|
||||
|
||||
println!(
|
||||
"duration (seconds): {:.2}",
|
||||
context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
|
||||
"duration (seconds): {:?}",
|
||||
context
|
||||
.duration()
|
||||
.map(|d| d as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE))
|
||||
);
|
||||
|
||||
for stream in context.streams() {
|
||||
println!("stream index {}:", stream.index());
|
||||
println!("\ttime_base: {}", stream.time_base());
|
||||
println!("\tstart_time: {}", stream.start_time());
|
||||
println!("\tduration (stream timebase): {}", stream.duration());
|
||||
println!("\tstart_time: {:?}", stream.start_time());
|
||||
println!("\tduration (stream timebase): {:?}", stream.duration());
|
||||
println!(
|
||||
"\tduration (seconds): {:.2}",
|
||||
stream.duration() as f64 * f64::from(stream.time_base())
|
||||
"\tduration (seconds): {:?}",
|
||||
stream
|
||||
.duration()
|
||||
.map(|d| d as f64 * f64::from(stream.time_base()))
|
||||
);
|
||||
println!("\tframes: {}", stream.frames());
|
||||
println!("\tdisposition: {:?}", stream.disposition());
|
||||
|
||||
@@ -39,7 +39,7 @@ impl Decoder {
|
||||
pub fn open_as_with<D: traits::Decoder>(
|
||||
mut self,
|
||||
codec: D,
|
||||
options: Dictionary<'_>,
|
||||
options: Dictionary,
|
||||
) -> Result<Opened, Error> {
|
||||
unsafe {
|
||||
if let Some(codec) = codec.decoder() {
|
||||
|
||||
@@ -40,7 +40,7 @@ impl Audio {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_with(mut self, options: Dictionary<'_>) -> Result<Encoder, Error> {
|
||||
pub fn open_with(mut self, options: Dictionary) -> Result<Encoder, Error> {
|
||||
unsafe {
|
||||
let mut opts = options.disown();
|
||||
let res = avcodec_open2(self.as_mut_ptr(), ptr::null(), &mut opts);
|
||||
@@ -57,7 +57,7 @@ impl Audio {
|
||||
pub fn open_as_with<E: traits::Encoder>(
|
||||
mut self,
|
||||
codec: E,
|
||||
options: Dictionary<'_>,
|
||||
options: Dictionary,
|
||||
) -> Result<Encoder, Error> {
|
||||
unsafe {
|
||||
if let Some(codec) = codec.encoder() {
|
||||
|
||||
@@ -41,7 +41,7 @@ impl Subtitle {
|
||||
pub fn open_as_with<E: traits::Encoder>(
|
||||
mut self,
|
||||
codec: E,
|
||||
options: Dictionary<'_>,
|
||||
options: Dictionary,
|
||||
) -> Result<Encoder, Error> {
|
||||
unsafe {
|
||||
if let Some(codec) = codec.encoder() {
|
||||
|
||||
@@ -42,7 +42,7 @@ impl Video {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn open_with(mut self, options: Dictionary<'_>) -> Result<Encoder, Error> {
|
||||
pub fn open_with(mut self, options: Dictionary) -> Result<Encoder, Error> {
|
||||
unsafe {
|
||||
let mut opts = options.disown();
|
||||
let res = avcodec_open2(self.as_mut_ptr(), ptr::null(), &mut opts);
|
||||
@@ -60,7 +60,7 @@ impl Video {
|
||||
pub fn open_as_with<E: traits::Encoder>(
|
||||
mut self,
|
||||
codec: E,
|
||||
options: Dictionary<'_>,
|
||||
options: Dictionary,
|
||||
) -> Result<Encoder, Error> {
|
||||
unsafe {
|
||||
if let Some(codec) = codec.encoder() {
|
||||
|
||||
@@ -56,8 +56,8 @@ impl<'a> ChapterMut<'a> {
|
||||
// dictionary.set() allocates the AVDictionary the first time a key/value is
|
||||
// inserted so we want to update the metadata dictionary afterwards
|
||||
unsafe {
|
||||
let mut dictionary = Dictionary::own(self.metadata().as_mut_ptr());
|
||||
dictionary.set(key.as_ref(), value.as_ref());
|
||||
let dictionary = Dictionary::own(self.metadata().as_mut_ptr());
|
||||
dictionary.as_mut().set(key.as_ref(), value.as_ref());
|
||||
(*self.as_mut_ptr()).metadata = dictionary.disown();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ impl Output {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_header_with(&mut self, options: Dictionary<'_>) -> Result<Dictionary<'_>, Error> {
|
||||
pub fn write_header_with(&mut self, options: Dictionary) -> Result<Dictionary, Error> {
|
||||
unsafe {
|
||||
let mut opts = options.disown();
|
||||
let res = avformat_write_header(self.as_mut_ptr(), &mut opts);
|
||||
@@ -161,7 +161,7 @@ impl Output {
|
||||
Ok(chapter)
|
||||
}
|
||||
|
||||
pub fn set_metadata(&mut self, dictionary: Dictionary<'_>) {
|
||||
pub fn set_metadata(&mut self, dictionary: Dictionary) {
|
||||
unsafe {
|
||||
(*self.as_mut_ptr()).metadata = dictionary.disown();
|
||||
}
|
||||
|
||||
+4
-4
@@ -92,7 +92,7 @@ pub fn open(path_or_url: impl AsRef<OsStr>, format: &Format) -> Result<Context,
|
||||
pub fn open_with(
|
||||
path_or_url: impl AsRef<OsStr>,
|
||||
format: &Format,
|
||||
options: Dictionary<'_>,
|
||||
options: Dictionary,
|
||||
) -> Result<Context, Error> {
|
||||
unsafe {
|
||||
let mut ps = ptr::null_mut();
|
||||
@@ -153,7 +153,7 @@ pub fn input(path_or_url: impl AsRef<OsStr>) -> Result<context::Input, Error> {
|
||||
|
||||
pub fn input_with_dictionary(
|
||||
path_or_url: impl AsRef<OsStr>,
|
||||
options: Dictionary<'_>,
|
||||
options: Dictionary,
|
||||
) -> Result<context::Input, Error> {
|
||||
unsafe {
|
||||
let mut ps = ptr::null_mut();
|
||||
@@ -218,7 +218,7 @@ pub fn output(path_or_url: impl AsRef<OsStr>) -> Result<context::Output, Error>
|
||||
|
||||
pub fn output_with(
|
||||
path_or_url: impl AsRef<OsStr>,
|
||||
options: Dictionary<'_>,
|
||||
options: Dictionary,
|
||||
) -> Result<context::Output, Error> {
|
||||
unsafe {
|
||||
let mut ps = ptr::null_mut();
|
||||
@@ -275,7 +275,7 @@ pub fn output_as(
|
||||
pub fn output_as_with(
|
||||
path_or_url: impl AsRef<OsStr>,
|
||||
mut format: format::Output,
|
||||
options: Dictionary<'_>,
|
||||
options: Dictionary,
|
||||
) -> Result<context::Output, Error> {
|
||||
unsafe {
|
||||
let mut ps = ptr::null_mut();
|
||||
|
||||
@@ -59,7 +59,7 @@ impl<'a> StreamMut<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_metadata(&mut self, metadata: Dictionary<'_>) {
|
||||
pub fn set_metadata(&mut self, metadata: Dictionary) {
|
||||
unsafe {
|
||||
let metadata = metadata.disown();
|
||||
(*self.as_mut_ptr()).metadata = metadata;
|
||||
|
||||
@@ -49,7 +49,7 @@ impl<'a> Ref<'a> {
|
||||
unsafe { Iter::new(self.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn to_owned<'b>(&self) -> Owned<'b> {
|
||||
pub fn to_owned(&self) -> Owned {
|
||||
self.iter().collect()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,111 +1,103 @@
|
||||
use std::{
|
||||
fmt,
|
||||
iter::FromIterator,
|
||||
ops::{Deref, DerefMut},
|
||||
ptr,
|
||||
};
|
||||
use std::{fmt, iter::FromIterator, ptr};
|
||||
|
||||
use super::mutable;
|
||||
use super::{immutable, mutable};
|
||||
use crate::ffi::*;
|
||||
|
||||
pub struct Owned<'a> {
|
||||
inner: mutable::Ref<'a>,
|
||||
pub struct Owned {
|
||||
ptr: *mut AVDictionary,
|
||||
}
|
||||
|
||||
impl<'a> Default for Owned<'a> {
|
||||
impl Default for Owned {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Owned<'a> {
|
||||
impl Owned {
|
||||
pub unsafe fn own(ptr: *mut AVDictionary) -> Self {
|
||||
Owned {
|
||||
inner: mutable::Ref::wrap(ptr),
|
||||
}
|
||||
Owned { ptr }
|
||||
}
|
||||
|
||||
pub unsafe fn disown(mut self) -> *mut AVDictionary {
|
||||
let result = self.inner.as_mut_ptr();
|
||||
self.inner = mutable::Ref::wrap(ptr::null_mut());
|
||||
|
||||
let result = self.ptr;
|
||||
self.ptr = ptr::null_mut();
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Owned<'a> {
|
||||
pub fn new() -> Self {
|
||||
unsafe {
|
||||
Owned {
|
||||
inner: mutable::Ref::wrap(ptr::null_mut()),
|
||||
}
|
||||
}
|
||||
pub fn as_ptr(&self) -> *const AVDictionary {
|
||||
self.ptr
|
||||
}
|
||||
|
||||
pub fn as_mut_ptr(&mut self) -> *mut AVDictionary {
|
||||
self.ptr
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> FromIterator<(&'b str, &'b str)> for Owned<'a> {
|
||||
fn from_iter<T: IntoIterator<Item = (&'b str, &'b str)>>(iterator: T) -> Self {
|
||||
let mut result = Owned::new();
|
||||
impl Owned {
|
||||
pub fn new() -> Self {
|
||||
Owned {
|
||||
ptr: ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_ref(&self) -> immutable::Ref {
|
||||
unsafe { immutable::Ref::wrap(self.ptr) }
|
||||
}
|
||||
|
||||
pub fn as_mut(&self) -> mutable::Ref {
|
||||
unsafe { mutable::Ref::wrap(self.ptr) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromIterator<(&'a str, &'a str)> for Owned {
|
||||
fn from_iter<T: IntoIterator<Item = (&'a str, &'a str)>>(iterator: T) -> Self {
|
||||
let result = Owned::new();
|
||||
|
||||
for (key, value) in iterator {
|
||||
result.set(key, value);
|
||||
result.as_mut().set(key, value);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> FromIterator<&'b (&'b str, &'b str)> for Owned<'a> {
|
||||
fn from_iter<T: IntoIterator<Item = &'b (&'b str, &'b str)>>(iterator: T) -> Self {
|
||||
let mut result = Owned::new();
|
||||
impl<'a> FromIterator<&'a (&'a str, &'a str)> for Owned {
|
||||
fn from_iter<T: IntoIterator<Item = &'a (&'a str, &'a str)>>(iterator: T) -> Self {
|
||||
let result = Owned::new();
|
||||
|
||||
for &(key, value) in iterator {
|
||||
result.set(key, value);
|
||||
result.as_mut().set(key, value);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromIterator<(String, String)> for Owned<'a> {
|
||||
impl FromIterator<(String, String)> for Owned {
|
||||
fn from_iter<T: IntoIterator<Item = (String, String)>>(iterator: T) -> Self {
|
||||
let mut result = Owned::new();
|
||||
let result = Owned::new();
|
||||
|
||||
for (key, value) in iterator {
|
||||
result.set(&key, &value);
|
||||
result.as_mut().set(&key, &value);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> FromIterator<&'b (String, String)> for Owned<'a> {
|
||||
fn from_iter<T: IntoIterator<Item = &'b (String, String)>>(iterator: T) -> Self {
|
||||
let mut result = Owned::new();
|
||||
impl<'a> FromIterator<&'a (String, String)> for Owned {
|
||||
fn from_iter<T: IntoIterator<Item = &'a (String, String)>>(iterator: T) -> Self {
|
||||
let result = Owned::new();
|
||||
|
||||
for &(ref key, ref value) in iterator {
|
||||
result.set(key, value);
|
||||
result.as_mut().set(key, value);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Deref for Owned<'a> {
|
||||
type Target = mutable::Ref<'a>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DerefMut for Owned<'a> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Clone for Owned<'a> {
|
||||
impl Clone for Owned {
|
||||
fn clone(&self) -> Self {
|
||||
let mut dictionary = Owned::new();
|
||||
dictionary.clone_from(self);
|
||||
@@ -115,23 +107,23 @@ impl<'a> Clone for Owned<'a> {
|
||||
|
||||
fn clone_from(&mut self, source: &Self) {
|
||||
unsafe {
|
||||
let mut ptr = self.as_mut_ptr();
|
||||
av_dict_copy(&mut ptr, source.as_ptr(), 0);
|
||||
self.inner = mutable::Ref::wrap(ptr);
|
||||
av_dict_copy(&mut self.ptr, source.as_ptr(), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Owned<'a> {
|
||||
impl Drop for Owned {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
av_dict_free(&mut self.inner.as_mut_ptr());
|
||||
if !self.ptr.is_null() {
|
||||
av_dict_free(&mut self.ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Debug for Owned<'a> {
|
||||
impl fmt::Debug for Owned {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.inner.fmt(fmt)
|
||||
unsafe { mutable::Ref::wrap(self.ptr) }.fmt(fmt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ impl Frame {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_metadata(&mut self, value: Dictionary<'_>) {
|
||||
pub fn set_metadata(&mut self, value: Dictionary) {
|
||||
unsafe {
|
||||
av_frame_set_metadata(self.as_mut_ptr(), value.disown());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user