Skip to content

Commit ac918ef

Browse files
Merge pull request #164 from encounter/updates
Improve time_base support & additions for ffmpeg 5-6
2 parents c53730e + 18c05b7 commit ac918ef

File tree

10 files changed

+98
-41
lines changed

10 files changed

+98
-41
lines changed

examples/transcode-audio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct Transcoder {
6262
out_time_base: ffmpeg::Rational,
6363
}
6464

65-
fn transcoder<P: AsRef<Path>>(
65+
fn transcoder<P: AsRef<Path> + ?Sized>(
6666
ictx: &mut format::context::Input,
6767
octx: &mut format::context::Output,
6868
path: &P,

src/codec/codec.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,20 @@ use {media, Error};
77

88
#[derive(PartialEq, Eq, Copy, Clone)]
99
pub struct Codec {
10-
ptr: *mut AVCodec,
10+
ptr: *const AVCodec,
1111
}
1212

1313
unsafe impl Send for Codec {}
1414
unsafe impl Sync for Codec {}
1515

1616
impl Codec {
17-
pub unsafe fn wrap(ptr: *mut AVCodec) -> Self {
17+
pub unsafe fn wrap(ptr: *const AVCodec) -> Self {
1818
Codec { ptr }
1919
}
2020

2121
pub unsafe fn as_ptr(&self) -> *const AVCodec {
2222
self.ptr as *const _
2323
}
24-
25-
pub unsafe fn as_mut_ptr(&mut self) -> *mut AVCodec {
26-
self.ptr
27-
}
2824
}
2925

3026
impl Codec {

src/codec/context.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::{threading, Compliance, Debug, Flags, Id, Parameters};
88
use ffi::*;
99
use libc::c_int;
1010
use media;
11-
use {Codec, Error};
11+
use {Codec, Error, Rational};
1212

1313
pub struct Context {
1414
ptr: *mut AVCodecContext,
@@ -41,6 +41,15 @@ impl Context {
4141
}
4242
}
4343

44+
pub fn new_with_codec(codec: Codec) -> Self {
45+
unsafe {
46+
Context {
47+
ptr: avcodec_alloc_context3(codec.as_ptr()),
48+
owner: None,
49+
}
50+
}
51+
}
52+
4453
pub fn from_parameters<P: Into<Parameters>>(parameters: P) -> Result<Self, Error> {
4554
let parameters = parameters.into();
4655
let mut context = Self::new();
@@ -129,6 +138,31 @@ impl Context {
129138
}
130139
}
131140
}
141+
142+
pub fn time_base(&self) -> Rational {
143+
unsafe { Rational::from((*self.as_ptr()).time_base) }
144+
}
145+
146+
pub fn set_time_base<R: Into<Rational>>(&mut self, value: R) {
147+
unsafe {
148+
(*self.as_mut_ptr()).time_base = value.into().into();
149+
}
150+
}
151+
152+
pub fn frame_rate(&self) -> Rational {
153+
unsafe { Rational::from((*self.as_ptr()).framerate) }
154+
}
155+
156+
pub fn set_frame_rate<R: Into<Rational>>(&mut self, value: Option<R>) {
157+
unsafe {
158+
if let Some(value) = value {
159+
(*self.as_mut_ptr()).framerate = value.into().into();
160+
} else {
161+
(*self.as_mut_ptr()).framerate.num = 0;
162+
(*self.as_mut_ptr()).framerate.den = 1;
163+
}
164+
}
165+
}
132166
}
133167

134168
impl Default for Context {

src/codec/decoder/decoder.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,14 @@ impl Decoder {
107107
}
108108
}
109109

110-
pub fn time_base(&self) -> Rational {
111-
unsafe { Rational::from((*self.as_ptr()).time_base) }
110+
pub fn packet_time_base(&self) -> Rational {
111+
unsafe { Rational::from((*self.as_ptr()).pkt_timebase) }
112+
}
113+
114+
pub fn set_packet_time_base<R: Into<Rational>>(&mut self, value: R) {
115+
unsafe {
116+
(*self.as_mut_ptr()).pkt_timebase = value.into().into();
117+
}
112118
}
113119
}
114120

src/codec/encoder/encoder.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use libc::c_int;
66

77
use super::{audio, subtitle, video};
88
use codec::Context;
9-
use {media, packet, Error, Frame, Rational};
9+
use {media, packet, Error, Frame};
1010

1111
pub struct Encoder(pub Context);
1212

@@ -116,23 +116,6 @@ impl Encoder {
116116
}
117117
}
118118
}
119-
120-
pub fn set_time_base<R: Into<Rational>>(&mut self, value: R) {
121-
unsafe {
122-
(*self.as_mut_ptr()).time_base = value.into().into();
123-
}
124-
}
125-
126-
pub fn set_frame_rate<R: Into<Rational>>(&mut self, value: Option<R>) {
127-
unsafe {
128-
if let Some(value) = value {
129-
(*self.as_mut_ptr()).framerate = value.into().into();
130-
} else {
131-
(*self.as_mut_ptr()).framerate.num = 0;
132-
(*self.as_mut_ptr()).framerate.den = 1;
133-
}
134-
}
135-
}
136119
}
137120

138121
impl Deref for Encoder {

src/codec/packet/packet.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ impl Packet {
143143
self.0.dts = value.unwrap_or(AV_NOPTS_VALUE);
144144
}
145145

146+
#[inline]
147+
#[cfg(feature = "ffmpeg_5_0")]
148+
pub fn time_base(&self) -> Rational {
149+
self.0.time_base.into()
150+
}
151+
152+
#[inline]
153+
#[cfg(feature = "ffmpeg_5_0")]
154+
pub fn set_time_base(&mut self, value: Rational) {
155+
self.0.time_base = value.into();
156+
}
157+
146158
#[inline]
147159
pub fn size(&self) -> usize {
148160
self.0.size as usize

src/filter/context/sink.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::Context;
22
use ffi::*;
33
use libc::c_int;
4-
use {Error, Frame};
4+
use {Error, Frame, Rational};
55

66
pub struct Sink<'a> {
77
ctx: &'a mut Context<'a>,
@@ -41,4 +41,8 @@ impl<'a> Sink<'a> {
4141
av_buffersink_set_frame_size(self.ctx.as_mut_ptr(), value);
4242
}
4343
}
44+
45+
pub fn time_base(&self) -> Rational {
46+
unsafe { av_buffersink_get_time_base(self.ctx.as_ptr()) }.into()
47+
}
4448
}

src/format/context/output.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::common::Context;
99
use super::destructor;
1010
use codec::traits;
1111
use ffi::*;
12-
use {format, ChapterMut, Dictionary, Error, Rational, StreamMut};
12+
use {codec, format, ChapterMut, Dictionary, Error, Rational, StreamMut};
1313

1414
pub struct Output {
1515
ptr: *mut AVFormatContext,
@@ -90,6 +90,25 @@ impl Output {
9090
}
9191
}
9292

93+
pub fn add_stream_with(&mut self, context: &codec::Context) -> Result<StreamMut, Error> {
94+
unsafe {
95+
let ptr = avformat_new_stream(self.as_mut_ptr(), ptr::null());
96+
97+
if ptr.is_null() {
98+
return Err(Error::Unknown);
99+
}
100+
101+
match avcodec_parameters_from_context((*ptr).codecpar, context.as_ptr()) {
102+
0 => (),
103+
e => return Err(Error::from(e)),
104+
}
105+
106+
let index = (*self.ctx.as_ptr()).nb_streams - 1;
107+
108+
Ok(StreamMut::wrap(&mut self.ctx, index as usize))
109+
}
110+
}
111+
93112
pub fn add_chapter<R: Into<Rational>, S: AsRef<str>>(
94113
&mut self,
95114
id: i64,

src/format/format/output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Output {
6363
}
6464
}
6565

66-
pub fn codec<P: AsRef<Path>>(&self, path: &P, kind: media::Type) -> codec::Id {
66+
pub fn codec<P: AsRef<Path> + ?Sized>(&self, path: &P, kind: media::Type) -> codec::Id {
6767
// XXX: use to_cstring when stable
6868
let path = CString::new(path.as_ref().as_os_str().to_str().unwrap()).unwrap();
6969

src/format/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ pub fn license() -> &'static str {
5858
}
5959

6060
// XXX: use to_cstring when stable
61-
fn from_path<P: AsRef<Path>>(path: &P) -> CString {
61+
fn from_path<P: AsRef<Path> + ?Sized>(path: &P) -> CString {
6262
CString::new(path.as_ref().as_os_str().to_str().unwrap()).unwrap()
6363
}
6464

6565
// NOTE: this will be better with specialization or anonymous return types
66-
pub fn open<P: AsRef<Path>>(path: &P, format: &Format) -> Result<Context, Error> {
66+
pub fn open<P: AsRef<Path> + ?Sized>(path: &P, format: &Format) -> Result<Context, Error> {
6767
unsafe {
6868
let mut ps = ptr::null_mut();
6969
let path = from_path(path);
@@ -100,7 +100,7 @@ pub fn open<P: AsRef<Path>>(path: &P, format: &Format) -> Result<Context, Error>
100100
}
101101
}
102102

103-
pub fn open_with<P: AsRef<Path>>(
103+
pub fn open_with<P: AsRef<Path> + ?Sized>(
104104
path: &P,
105105
format: &Format,
106106
options: Dictionary,
@@ -148,7 +148,7 @@ pub fn open_with<P: AsRef<Path>>(
148148
}
149149
}
150150

151-
pub fn input<P: AsRef<Path>>(path: &P) -> Result<context::Input, Error> {
151+
pub fn input<P: AsRef<Path> + ?Sized>(path: &P) -> Result<context::Input, Error> {
152152
unsafe {
153153
let mut ps = ptr::null_mut();
154154
let path = from_path(path);
@@ -167,7 +167,7 @@ pub fn input<P: AsRef<Path>>(path: &P) -> Result<context::Input, Error> {
167167
}
168168
}
169169

170-
pub fn input_with_dictionary<P: AsRef<Path>>(
170+
pub fn input_with_dictionary<P: AsRef<Path> + ?Sized>(
171171
path: &P,
172172
options: Dictionary,
173173
) -> Result<context::Input, Error> {
@@ -193,7 +193,7 @@ pub fn input_with_dictionary<P: AsRef<Path>>(
193193
}
194194
}
195195

196-
pub fn input_with_interrupt<P: AsRef<Path>, F>(
196+
pub fn input_with_interrupt<P: AsRef<Path> + ?Sized, F>(
197197
path: &P,
198198
closure: F,
199199
) -> Result<context::Input, Error>
@@ -219,7 +219,7 @@ where
219219
}
220220
}
221221

222-
pub fn output<P: AsRef<Path>>(path: &P) -> Result<context::Output, Error> {
222+
pub fn output<P: AsRef<Path> + ?Sized>(path: &P) -> Result<context::Output, Error> {
223223
unsafe {
224224
let mut ps = ptr::null_mut();
225225
let path = from_path(path);
@@ -235,7 +235,7 @@ pub fn output<P: AsRef<Path>>(path: &P) -> Result<context::Output, Error> {
235235
}
236236
}
237237

238-
pub fn output_with<P: AsRef<Path>>(
238+
pub fn output_with<P: AsRef<Path> + ?Sized>(
239239
path: &P,
240240
options: Dictionary,
241241
) -> Result<context::Output, Error> {
@@ -267,7 +267,10 @@ pub fn output_with<P: AsRef<Path>>(
267267
}
268268
}
269269

270-
pub fn output_as<P: AsRef<Path>>(path: &P, format: &str) -> Result<context::Output, Error> {
270+
pub fn output_as<P: AsRef<Path> + ?Sized>(
271+
path: &P,
272+
format: &str,
273+
) -> Result<context::Output, Error> {
271274
unsafe {
272275
let mut ps = ptr::null_mut();
273276
let path = from_path(path);
@@ -289,7 +292,7 @@ pub fn output_as<P: AsRef<Path>>(path: &P, format: &str) -> Result<context::Outp
289292
}
290293
}
291294

292-
pub fn output_as_with<P: AsRef<Path>>(
295+
pub fn output_as_with<P: AsRef<Path> + ?Sized>(
293296
path: &P,
294297
format: &str,
295298
options: Dictionary,

0 commit comments

Comments
 (0)