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
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -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();
}
}
}
}
Expand All @@ -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);
Expand Down
57 changes: 56 additions & 1 deletion src/codec/decoder/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use libc::c_int;
use ffi::*;

use super::Opened;
use ::{packet, Error, AudioService, ChannelLayout};
use ::{Error, AudioService, ChannelLayout};
use ::packet::{self, Mut, Packet};
use ::frame;
use ::util::format;
use ::codec::Context;
Expand All @@ -23,6 +24,10 @@ impl Audio {
}
}

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

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

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

impl<'a, 'b> AudioFrameIter<'a, 'b> {
fn new(audio: &'a mut Audio, packet: &'b Packet) -> AudioFrameIter<'a, 'b> {
let mut copied = Packet::empty();
copied.clone_from(packet);

AudioFrameIter {
audio: audio,
_packet: packet,
packet: copied,
}
}
}

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

fn next(&mut self) -> Option<Self::Item> {
if self.packet.size() != 0 {
let mut frame = frame::Audio::empty();
let mut got: c_int = 0;

unsafe {
let avpkt = self.packet.as_mut_ptr();

match avcodec_decode_audio4(self.audio.as_mut_ptr(), frame.as_mut_ptr(), &mut got, avpkt) {
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)))
} 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
Expand Up @@ -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());
}
}
}
}