Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codec/decoder/audio: impl Iterator for Audio::decode #74

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions examples/transcode-audio.rs
Original file line number Diff line number Diff line change
@@ -123,24 +123,25 @@ fn main() {
let in_time_base = transcoder.decoder.time_base();
let out_time_base = octx.stream(0).unwrap().time_base();

let mut decoded = frame::Audio::empty();
let mut encoded = ffmpeg::Packet::empty();

for (stream, mut packet) in ictx.packets() {
if stream.index() == transcoder.stream {
packet.rescale_ts(stream.time_base(), in_time_base);

if let Ok(true) = transcoder.decoder.decode(&packet, &mut decoded) {
let timestamp = decoded.timestamp();
decoded.set_pts(timestamp);
for decoded in transcoder.decoder.decode_iter(&packet) {
if let Ok(Some(mut decoded)) = decoded {
let timestamp = decoded.timestamp();
decoded.set_pts(timestamp);

transcoder.filter.get("in").unwrap().source().add(&decoded).unwrap();
transcoder.filter.get("in").unwrap().source().add(&decoded).unwrap();

while let Ok(..) = transcoder.filter.get("out").unwrap().sink().frame(&mut decoded) {
if let Ok(true) = transcoder.encoder.encode(&decoded, &mut encoded) {
encoded.set_stream(0);
encoded.rescale_ts(in_time_base, out_time_base);
encoded.write_interleaved(&mut octx).unwrap();
while let Ok(..) = transcoder.filter.get("out").unwrap().sink().frame(&mut decoded) {
if let Ok(true) = transcoder.encoder.encode(&decoded, &mut encoded) {
encoded.set_stream(0);
encoded.rescale_ts(in_time_base, out_time_base);
encoded.write_interleaved(&mut octx).unwrap();
}
}
}
}
@@ -149,6 +150,8 @@ fn main() {

transcoder.filter.get("in").unwrap().source().flush().unwrap();

let mut decoded = frame::Audio::empty();

while let Ok(..) = transcoder.filter.get("out").unwrap().sink().frame(&mut decoded) {
if let Ok(true) = transcoder.encoder.encode(&decoded, &mut encoded) {
encoded.set_stream(0);
62 changes: 61 additions & 1 deletion src/codec/decoder/audio.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::{mem, ptr};
use std::ops::{Deref, DerefMut};

use libc::c_int;
use ffi::*;

use super::Opened;
use ::{packet, Error, AudioService, ChannelLayout};
use ::{Error, AudioService, ChannelLayout};
use ::packet::{self, Ref};
use ::frame;
use ::util::format;
use ::codec::Context;
@@ -23,6 +25,10 @@ impl Audio {
}
}

pub fn decode_iter<'a, 'b>(&'a mut self, packet: &'b packet::Packet) -> AudioFrameIter<'a, 'b> {
AudioFrameIter::new(self, packet)
}

pub fn rate(&self) -> u32 {
unsafe {
(*self.as_ptr()).sample_rate as u32
@@ -130,3 +136,57 @@ impl AsMut<Context> for Audio {
&mut self.0
}
}

pub struct AudioFrameIter<'a, 'b> {
audio: &'a mut Audio,
_packet: &'b packet::Packet,
avpkt: AVPacket,
frame: frame::Audio,
}

impl<'a, 'b> AudioFrameIter<'a, 'b> {
fn new(audio: &'a mut Audio, packet: &'b packet::Packet) -> AudioFrameIter<'a, 'b> {
let avpkt = unsafe {
let mut avpkt: AVPacket = mem::zeroed();
ptr::copy(packet.as_ptr(), (&mut avpkt) as *mut AVPacket, 1);
avpkt
};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used unsafe code to make internal copy of AVPacket. This is to avoid packet.clone() which seems to be expensive.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just use clone_from?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because av_copy_packet has some overhead which I think unnecessary but no problem to change it if you prefer.


AudioFrameIter {
audio: audio,
_packet: packet,
avpkt: avpkt,
frame: frame::Audio::empty(),
}
}
}

impl<'a, 'b> Iterator for AudioFrameIter<'a, 'b> {
type Item = Result<Option<frame::Audio>, Error>;

fn next(&mut self) -> Option<Self::Item> {
let avpkt = &mut self.avpkt;

if avpkt.size != 0 {
let mut got: c_int = 0;

unsafe {
match avcodec_decode_audio4(self.audio.as_mut_ptr(), self.frame.as_mut_ptr(), &mut got, avpkt as *const AVPacket) {
e if e < 0 => Some(Err(Error::from(e))),
n => {
avpkt.data = avpkt.data.offset(n as isize);
avpkt.size -= n;

if got != 0 {
Some(Ok(Some(frame::Audio::wrap(self.frame.as_mut_ptr()))))
} else {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A problem here is the wrapped frame can live longer than the original frame.

Some(Ok(None))
}
}
}
}
} else {
None
}
}
}
6 changes: 4 additions & 2 deletions src/util/frame/mod.rs
Original file line number Diff line number Diff line change
@@ -182,8 +182,10 @@ impl Frame {
impl Drop for Frame {
#[inline]
fn drop(&mut self) {
unsafe {
av_frame_free(&mut self.as_mut_ptr());
if self._own {
unsafe {
av_frame_free(&mut self.as_mut_ptr());
}
}
}
}