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

fix seeking - need to handle whence properly #73

Open
wants to merge 2 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
13 changes: 11 additions & 2 deletions ac-ffmpeg/src/format/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ typedef int read_packet_t(void*, uint8_t*, int);
typedef int write_packet_t(void*, uint8_t*, int);
typedef int64_t seek_t(void*, int64_t, int);

int ffw_io_is_avseek_size(int whence) {
return whence & AVSEEK_SIZE;
int ffw_io_whence_to_seek_mode(int whence) {
if (whence & AVSEEK_SIZE) {
return 0;
}

switch (whence) {
case SEEK_SET: return 1;
case SEEK_CUR: return 2;
case SEEK_END: return 3;
default: return -1;
}
}

AVIOContext * ffw_io_context_new(
Expand Down
47 changes: 37 additions & 10 deletions ac-ffmpeg/src/format/io.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Elementary IO used by the muxer and demuxer.

use std::{
convert::{TryFrom, TryInto},
io::{self, Read, Seek, SeekFrom, Write},
os::raw::{c_int, c_void},
slice,
Expand All @@ -13,7 +14,7 @@ type WritePacketCallback =
type SeekCallback = extern "C" fn(opaque: *mut c_void, offset: i64, whence: c_int) -> i64;

extern "C" {
fn ffw_io_is_avseek_size(whence: c_int) -> c_int;
fn ffw_io_whence_to_seek_mode(whence: c_int) -> c_int;

fn ffw_io_context_new(
buffer_size: c_int,
Expand Down Expand Up @@ -72,19 +73,45 @@ extern "C" fn io_seek<T>(opaque: *mut c_void, offset: i64, whence: c_int) -> i64
where
T: Seek,
{
let input_ptr = opaque as *mut T;
fn inner<U>(input: &mut U, offset: i64, mode: c_int) -> io::Result<i64>
where
U: Seek,
{
// 0 indicates that the AVSEEK_SIZE flag was set and we should return the current position
if mode == 0 {
return get_seekable_length(input)?
.try_into()
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "out of range"));
}

let input = unsafe { &mut *input_ptr };
let pos = match mode {
// 1 means seek relative to the beginning
1 => u64::try_from(offset)
.map(SeekFrom::Start)
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid offset"))?,
// 2 means seek relative to the current position
2 => SeekFrom::Current(offset),
// 3 means seek relative to the end position
3 => SeekFrom::End(offset),
_ => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid whence",
))
}
};

let is_avseek_size = unsafe { ffw_io_is_avseek_size(whence) != 0 };
input
.seek(pos)?
.try_into()
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "out of range"))
}

let seek = if is_avseek_size {
get_seekable_length(input)
} else {
input.seek(SeekFrom::Start(offset as u64))
};
let input_ptr = opaque as *mut T;
let input = unsafe { &mut *input_ptr };
let mode = unsafe { ffw_io_whence_to_seek_mode(whence) };

match seek {
match inner(input, offset, mode) {
Ok(len) => len as i64,
Err(err) => err
.raw_os_error()
Expand Down