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 2 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(&mut 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
47 changes: 46 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};
use ::frame;
use ::util::format;
use ::codec::Context;
Expand All @@ -23,6 +24,14 @@ impl Audio {
}
}

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

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

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

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

fn next(&mut self) -> Option<Self::Item> {
if !unsafe { self.packet.is_empty() } {
let mut got: c_int = 0;

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

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

Choose a reason for hiding this comment

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

I think it would be better if the packet was borrowed immutably, and then copied over, then these two attributes modified in place in the internal packet.


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
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());
}
}
}
}