From 7bf3b9cc840890f18ad56fc612e26c9cec91d2ac Mon Sep 17 00:00:00 2001 From: meh Date: Sun, 4 Oct 2020 18:28:14 +0200 Subject: [PATCH 01/12] chore: add nix-shell stuff --- shell.nix | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 shell.nix diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..024bb7b --- /dev/null +++ b/shell.nix @@ -0,0 +1,21 @@ +let + mozilla = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz); +in + +with (import { + overlays = [mozilla]; +}); + +mkShell { + name = "ffmpeg-sys"; + + buildInputs = [ + # For building. + clang rustChannels.stable.rust pkg-config ffmpeg + ]; + + RUST_BACKTRACE = 1; + RUSTFLAGS = "-C target-cpu=native"; + + LIBCLANG_PATH = "${llvmPackages.libclang}/lib"; +} From 0d86357d9f56f696c77a7e324aa1108120fbe07d Mon Sep 17 00:00:00 2001 From: ldm0 Date: Wed, 26 Aug 2020 23:28:41 +0000 Subject: [PATCH 02/12] Fix incorrect static inline function --- src/avutil/rational.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/avutil/rational.rs b/src/avutil/rational.rs index 641e09f..7809ee6 100644 --- a/src/avutil/rational.rs +++ b/src/avutil/rational.rs @@ -15,7 +15,7 @@ pub unsafe fn av_cmp_q(a: AVRational, b: AVRational) -> c_int { } else if b.den != 0 && a.den != 0 { 0 } else if a.num != 0 && b.num != 0 { - ((i64::from(a.num) >> 31) - (i64::from(b.num) >> 31)) as c_int + (a.num >> 31) - (b.num >> 31) } else { c_int::min_value() } From 37e940a69b062f36d8d245ed79bed3d93cf2cd70 Mon Sep 17 00:00:00 2001 From: Cole K Date: Thu, 11 Feb 2021 00:05:28 -0600 Subject: [PATCH 03/12] Spawn a new thread with a larger stack perform build script work in --- build.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/build.rs b/build.rs index 9a560e8..9d0de0a 100644 --- a/build.rs +++ b/build.rs @@ -627,6 +627,21 @@ fn link_to_libraries(statik: bool) { } fn main() { + // The long chain of `header` method calls for `bindgen::Builder` seems to be overflowing the default stack size on Windows. + // The main thread appears to have a hardcoded stack size which is unaffected by `RUST_MIN_STACK`. As a workaround, spawn a thread here with a stack size that works expermentally, and allow overriding it with `FFMPEG_SYS_BUILD_STACK_SIZE` just in case. + let stack_size = std::env::var("FFMPEG_SYS_BUILD_STACK_SIZE").map(|s| s.parse()).unwrap_or(Ok(3 * 1024 * 1024)); + eprintln!("Using stack size: {:?}", stack_size); + + std::thread::Builder::new() + .name("ffmpg-sys-build".into()) + .stack_size(stack_size.unwrap()) + .spawn(thread_main) + .unwrap() + .join() + .unwrap(); +} + +fn thread_main() { let statik = env::var("CARGO_FEATURE_STATIC").is_ok(); let include_paths: Vec = if env::var("CARGO_FEATURE_BUILD").is_ok() { From eeae002851c2abd4a9914e29e2cec1b28c0ff165 Mon Sep 17 00:00:00 2001 From: Per Larsen Date: Mon, 5 Oct 2020 22:59:46 -0700 Subject: [PATCH 04/12] fix: disable optimizations for debug builds --- build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/build.rs b/build.rs index 9d0de0a..8deac61 100644 --- a/build.rs +++ b/build.rs @@ -225,6 +225,7 @@ fn build() -> io::Result<()> { if env::var("DEBUG").is_ok() { configure.arg("--enable-debug"); configure.arg("--disable-stripping"); + configure.arg("--disable-optimizations"); } else { configure.arg("--disable-debug"); configure.arg("--enable-stripping"); From 0c0f6b5f1261e771e08ceae0b3ee79e927f1e697 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Wed, 3 Mar 2021 03:03:21 +0000 Subject: [PATCH 05/12] Conditionally add hwcontext_drm header via feature This is to get the code more in sync with meh/rust-ffmpeg-sys --- Cargo.toml | 1 + build.rs | 19 ++++--------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 638ca1e..104ef8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -104,3 +104,4 @@ avresample = [] postproc = [] swresample = [] swscale = [] +lib-drm = [] diff --git a/build.rs b/build.rs index 8deac61..bf58746 100644 --- a/build.rs +++ b/build.rs @@ -605,15 +605,6 @@ fn search_include(include_paths: &[PathBuf], header: &str) -> String { format!("/usr/include/{}", header) } -fn maybe_search_include(include_paths: &[PathBuf], header: &str) -> Option { - let path = search_include(include_paths, header); - if fs::metadata(&path).is_ok() { - Some(path) - } else { - None - } -} - fn link_to_libraries(statik: bool) { let ffmpeg_ty = if statik { "static" } else { "dylib" }; for lib in LIBRARIES { @@ -1224,7 +1215,6 @@ fn thread_main() { .header(search_include(&include_paths, "libavutil/frame.h")) .header(search_include(&include_paths, "libavutil/hash.h")) .header(search_include(&include_paths, "libavutil/hmac.h")) - .header(search_include(&include_paths, "libavutil/hwcontext.h")) .header(search_include(&include_paths, "libavutil/imgutils.h")) .header(search_include(&include_paths, "libavutil/lfg.h")) .header(search_include(&include_paths, "libavutil/log.h")) @@ -1253,7 +1243,8 @@ fn thread_main() { .header(search_include(&include_paths, "libavutil/timecode.h")) .header(search_include(&include_paths, "libavutil/twofish.h")) .header(search_include(&include_paths, "libavutil/avutil.h")) - .header(search_include(&include_paths, "libavutil/xtea.h")); + .header(search_include(&include_paths, "libavutil/xtea.h")) + .header(search_include(&include_paths, "libavutil/hwcontext.h")); if env::var("CARGO_FEATURE_POSTPROC").is_ok() { builder = builder.header(search_include(&include_paths, "libpostproc/postprocess.h")); @@ -1267,10 +1258,8 @@ fn thread_main() { builder = builder.header(search_include(&include_paths, "libswscale/swscale.h")); } - if let Some(hwcontext_drm_header) = - maybe_search_include(&include_paths, "libavutil/hwcontext_drm.h") - { - builder = builder.header(hwcontext_drm_header); + if env::var("CARGO_FEATURE_LIB_DRM").is_ok() { + builder = builder.header(search_include(&include_paths, "libavutil/hwcontext_drm.h")) } // Finish the builder and generate the bindings. From 744d5e89223f468a1a35013ac9915d04dd746cfd Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 30 Jan 2020 02:54:54 +0000 Subject: [PATCH 06/12] Fix include paths, stdc++ on macOS --- build.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/build.rs b/build.rs index bf58746..ba16e96 100644 --- a/build.rs +++ b/build.rs @@ -6,7 +6,7 @@ extern crate pkg_config; use std::env; use std::fs::{self, File}; use std::io::{self, BufRead, BufReader, Write}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::Command; use std::str; @@ -701,10 +701,12 @@ fn thread_main() { } // Fallback to pkg-config else { - pkg_config::Config::new() + let mut libavutil = pkg_config::Config::new() + .cargo_metadata(false) .statik(statik) .probe("libavutil") .unwrap(); + print_pkg_config_libs(statik, &libavutil); let libs = vec![ ("libavformat", "AVFORMAT"), @@ -717,18 +719,24 @@ fn thread_main() { for (lib_name, env_variable_name) in libs.iter() { if env::var(format!("CARGO_FEATURE_{}", env_variable_name)).is_ok() { - pkg_config::Config::new() + print_pkg_config_libs(statik, &pkg_config::Config::new() + .cargo_metadata(false) .statik(statik) .probe(lib_name) - .unwrap(); + .unwrap()); } } - pkg_config::Config::new() + let libavcodec = pkg_config::Config::new() + .cargo_metadata(false) .statik(statik) .probe("libavcodec") - .unwrap() - .include_paths + .unwrap(); + print_pkg_config_libs(statik, &libavcodec); + + let mut paths = libavcodec.include_paths; + paths.append(&mut libavutil.include_paths); + paths }; if statik && cfg!(target_os = "macos") { @@ -1273,3 +1281,46 @@ fn thread_main() { .write_to_file(output().join("bindings.rs")) .expect("Couldn't write bindings!"); } + +fn print_pkg_config_libs(statik: bool, lib: &pkg_config::Library) { + let target = env::var("TARGET").unwrap(); + let is_msvc = target.contains("msvc"); + let is_apple = target.contains("apple"); + + for val in &lib.link_paths { + println!("cargo:rustc-link-search=native={}", val.display()); + } + for val in &lib.framework_paths { + println!("cargo:rustc-link-search=framework={}", val.display()); + } + for val in &lib.frameworks { + println!("cargo:rustc-link=framework={}", val); + } + + for val in &lib.libs { + if is_msvc && ["m", "c", "pthread"].contains(&val.as_str()) { + continue; + } + if is_apple && val == "stdc++" { + println!("cargo:rustc-link-lib=c++"); + continue; + } + + if statik && is_static_available(val, &lib.include_paths) { + println!("cargo:rustc-link-lib=static={}", val); + } else { + println!("cargo:rustc-link-lib={}", val); + } + } +} + +fn is_static_available(lib: &str, dirs: &[PathBuf]) -> bool { + let libname = format!("lib{}.a", lib); + let has = dirs.iter().map(|d| d.as_path()) + .chain([Path::new("/usr/local/lib")].iter().copied()) + .any(|dir| dir.join(&libname).exists()); + if !has { + println!("cargo:warning=static {} not found", libname); + } + has +} From cc8921a557f534166e70e1981ebed8e9011244a5 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Wed, 3 Mar 2021 03:27:38 +0000 Subject: [PATCH 07/12] Cargo fmt --- build.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/build.rs b/build.rs index ba16e96..d769151 100644 --- a/build.rs +++ b/build.rs @@ -621,7 +621,9 @@ fn link_to_libraries(statik: bool) { fn main() { // The long chain of `header` method calls for `bindgen::Builder` seems to be overflowing the default stack size on Windows. // The main thread appears to have a hardcoded stack size which is unaffected by `RUST_MIN_STACK`. As a workaround, spawn a thread here with a stack size that works expermentally, and allow overriding it with `FFMPEG_SYS_BUILD_STACK_SIZE` just in case. - let stack_size = std::env::var("FFMPEG_SYS_BUILD_STACK_SIZE").map(|s| s.parse()).unwrap_or(Ok(3 * 1024 * 1024)); + let stack_size = std::env::var("FFMPEG_SYS_BUILD_STACK_SIZE") + .map(|s| s.parse()) + .unwrap_or(Ok(3 * 1024 * 1024)); eprintln!("Using stack size: {:?}", stack_size); std::thread::Builder::new() @@ -719,11 +721,14 @@ fn thread_main() { for (lib_name, env_variable_name) in libs.iter() { if env::var(format!("CARGO_FEATURE_{}", env_variable_name)).is_ok() { - print_pkg_config_libs(statik, &pkg_config::Config::new() - .cargo_metadata(false) - .statik(statik) - .probe(lib_name) - .unwrap()); + print_pkg_config_libs( + statik, + &pkg_config::Config::new() + .cargo_metadata(false) + .statik(statik) + .probe(lib_name) + .unwrap(), + ); } } @@ -1316,7 +1321,9 @@ fn print_pkg_config_libs(statik: bool, lib: &pkg_config::Library) { fn is_static_available(lib: &str, dirs: &[PathBuf]) -> bool { let libname = format!("lib{}.a", lib); - let has = dirs.iter().map(|d| d.as_path()) + let has = dirs + .iter() + .map(|d| d.as_path()) .chain([Path::new("/usr/local/lib")].iter().copied()) .any(|dir| dir.join(&libname).exists()); if !has { From b561a6ffb454358512b4470ba84f60d5d8427410 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Wed, 3 Mar 2021 03:36:35 +0000 Subject: [PATCH 08/12] Copy Travis CI config from meh/rust-ffmpeg-sys --- .travis.yml | 37 +++++++++++++++++++++++++++++++++++++ .travis/install_linux.sh | 17 +++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 .travis.yml create mode 100644 .travis/install_linux.sh diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a8f06c3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,37 @@ +sudo: required +language: rust +rust: + - stable + - beta + - nightly +os: + - linux + - osx +matrix: + allow_failures: + - rust: nightly +addons: + apt: + packages: + - build-essential +before_install: + # Without rustfmt, bindgen puts everything on one line and any warnings dump so many logs they break Travis + # See https://github.com/rust-lang/rust-bindgen/issues/1600 + # optional, because nightlies may not have it + - rustup component add rustfmt || true + - if [[ $TRAVIS_OS_NAME == 'linux' ]]; then ./.travis/install_linux.sh; fi + - if [[ $TRAVIS_OS_NAME == 'osx' ]]; then brew update; fi + - if [[ $TRAVIS_OS_NAME == 'osx' ]]; then brew install yasm; fi + +script: | + if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + # Current Travis Ubuntu version uses libav which doesn't come with libswresample + cargo build --verbose --no-default-features --features "avcodec avfilter avformat avresample swscale" && + cargo test --verbose --no-default-features --features "avcodec avfilter avformat avresample swscale" + else + travis_wait cargo build --verbose --features "build" + cargo test --verbose --features "build" + fi + +after_failure: + - find /usr -type f 2>/dev/null | grep -E 'lib(avcodec/version|avcodec/avcodec).h$' | xargs -I THEFILE -- sh -c 'echo "=== THEFILE ==="; cat THEFILE' diff --git a/.travis/install_linux.sh b/.travis/install_linux.sh new file mode 100644 index 0000000..96dc5ec --- /dev/null +++ b/.travis/install_linux.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +sudo apt-get update -q +# From https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu +sudo apt-get -y --force-yes install autoconf automake build-essential libass-dev libfreetype6-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo zlib1g-dev +sudo apt-get install yasm +pushd ~ +git clone https://github.com/FFmpeg/FFmpeg.git +cd FFmpeg +git checkout release/3.2 +mkdir ~/FFmpeg-build +cd ~/FFmpeg-build +../FFmpeg/configure --disable-ffprobe --disable-ffserver --disable-doc --enable-avresample +make -j +sudo make install +make distclean +popd From dca74848be9de2f7363a4d5fca80f575637a921d Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Wed, 3 Mar 2021 04:01:46 +0000 Subject: [PATCH 09/12] Cargo.toml: Specify readme --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 104ef8d..d2e7bc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "4.3.5" build = "build.rs" links = "ffmpeg" +readme = "README.md" authors = ["meh. ", "Zhiming Wang "] license = "WTFPL" From 08e5985c1e0a54fe237478087ceef1a58c808a9c Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Wed, 3 Mar 2021 04:05:20 +0000 Subject: [PATCH 10/12] Bump bindgen version to 0.56 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d2e7bc6..2d88bea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ libc = "0.2" num_cpus = "1.11" cc = "1.0" pkg-config = "0.3" -bindgen = { version = "0.54", default-features = false, features = ["runtime"] } +bindgen = { version = "0.56", default-features = false, features = ["runtime"] } [target.'cfg(target_env = "msvc")'.build-dependencies] vcpkg = "0.2" From 9f1a43aa5173526d91547d64f404592b90bcf129 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Wed, 3 Mar 2021 03:59:51 +0000 Subject: [PATCH 11/12] Update README.md --- README.md | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7e4d1a1..a6c4ab8 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,21 @@ -[![ffmpeg-sys-next on crates.io](https://img.shields.io/crates/v/ffmpeg-sys-next?cacheSeconds=3600)](https://crates.io/crates/ffmpeg-sys-next) -[![build](https://github.com/zmwangx/rust-ffmpeg-sys/workflows/build/badge.svg)](https://github.com/zmwangx/rust-ffmpeg-sys/actions) +# Rust FFI bindings for ffmpeg -This is a fork of the abandoned [ffmpeg-sys](https://github.com/meh/rust-ffmpeg-sys) crate. You can find this crate as [ffmpeg-sys-next](https://crates.io/crates/ffmpeg-sys-next) on crates.io. +Low-level bindings for ffmpeg autogenerated with bindgen. This crate supports cross-compilation automatically. -This crate contains low level bindings to FFmpeg. You're probably interested in the high level bindings instead: [ffmpeg-next](https://github.com/zmwangx/rust-ffmpeg). +For higher-level library, see [ffmpeg crate](https://lib.rs/ffmpeg). + +## Building + +By default, the crate will search for ffmpeg v4 installed on the system. + +This crate can also download, build and statically link its own copy of ffmpeg if you enable `build` feature: + +```toml +[dependencies] +ffmpeg-sys = { version = "4", features = ["build"] } +``` + +# Versioning A word on versioning: major and minor versions track major and minor versions of FFmpeg, e.g. 4.2.x of this crate has been updated to support the 4.2.x series of FFmpeg. Patch level is reserved for bug fixes of this crate and does not track FFmpeg patch versions. @@ -28,3 +40,10 @@ In addition to feature flags declared in `Cargo.toml`, this crate performs vario - `ff_api_`, e.g. `ff_api_vaapi`, corresponding to whether their respective uppercase deprecation guards evaluate to true. - `ff_api__is_defined`, e.g. `ff_api_vappi_is_defined`, similar to above except these are enabled as long as the corresponding deprecation guards are defined. + +See [Cargo features](https://github.com/meh/rust-ffmpeg/blob/HEAD/Cargo.toml) to control which codecs are included. + +# Based On + +This combines bits from [meh/rust-ffmpeg-sys](https://github.com/meh/rust-ffmpeg-sys) and [zmwangx/rust-ffmpeg-sys](https://github.com/zmwangx/rust-ffmpeg-sys) because +when you have two almost identical projects that aren't actively maintained what you _really_ need is a third! \ No newline at end of file From 5fbc0815b1512fb9420abf193ab32947f93f8ef0 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Wed, 3 Mar 2021 04:23:28 +0000 Subject: [PATCH 12/12] Cargo.toml: rename back to ffmpeg-sys --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2d88bea..aabd9b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ffmpeg-sys-next" +name = "ffmpeg-sys" version = "4.3.5" build = "build.rs" links = "ffmpeg"