summary refs log tree commit diff
path: root/pkgs/development/libraries/ffmpeg/generic.nix
blob: 33ee2f80a6ed91c915791db857f0be36c63425dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{ stdenv, fetchurl, pkgconfig, perl, texinfo, yasm
, alsaLib, bzip2, fontconfig, freetype, gnutls, libiconv, lame, libass, libogg
, libtheora, libva, libvorbis, libvpx, lzma, libpulseaudio, soxr
, x264, x265, xvidcore, zlib, libopus
, openglSupport ? false, mesa ? null
# Build options
, runtimeCpuDetectBuild ? true # Detect CPU capabilities at runtime
, multithreadBuild ? true # Multithreading via pthreads/win32 threads
, sdlSupport ? !stdenv.isArm, SDL ? null
, vdpauSupport ? !stdenv.isArm, libvdpau ? null
# Developer options
, debugDeveloper ? false
, optimizationsDeveloper ? true
, extraWarningsDeveloper ? false
# Darwin frameworks
, Cocoa, darwinFrameworks ? [ Cocoa ]
# Inherit generics
, branch, sha256, version, patches ? [], ...
}:

/* Maintainer notes:
 *
 * THIS IS A MINIMAL BUILD OF FFMPEG, do not include dependencies unless
 * a build that depends on ffmpeg requires them to be compiled into ffmpeg,
 * see `ffmpeg-full' for an ffmpeg build with all features included.
 *
 * Need fixes to support Darwin:
 *   libvpx pulseaudio
 *
 * Known issues:
 * 0.6     - fails to compile (unresolved) (so far, only disabling a number of
 *           features works, but that is not a feasible solution)
 * 0.6.90  - mmx: compile errors (fix: disable for 0.6.90-rc0)
 * 1.1     - libsoxr: compile error (fix: disable for 1.1)
 *           Support was initially added in 1.1 before soxr api change, fix
 *           would probably be to add soxr-1.0
 * ALL     - Cross-compiling will disable features not present on host OS
 *           (e.g. dxva2 support [DirectX] will not be enabled unless natively
 *           compiled on Cygwin)
 *
 */

let
  inherit (stdenv) icCygwin isDarwin isFreeBSD isLinux isArm;
  inherit (stdenv.lib) optional optionals enableFeature;

  cmpVer = builtins.compareVersions;
  reqMin = requiredVersion: (cmpVer requiredVersion branch != 1);
  reqMatch = requiredVersion: (cmpVer requiredVersion branch == 0);

  ifMinVer = minVer: flag: if reqMin minVer then flag else null;

  # Version specific fix
  verFix = withoutFix: fixVer: withFix: if reqMatch fixVer then withFix else withoutFix;

  # Disable dependency that needs fixes before it will work on Darwin or Arm
  disDarwinOrArmFix = origArg: minVer: fixArg: if ((isDarwin || isArm) && reqMin minVer) then fixArg else origArg;
in

assert openglSupport -> mesa != null;

stdenv.mkDerivation rec {

  name = "ffmpeg-${version}";
  inherit version;

  src = fetchurl {
    url = "https://www.ffmpeg.org/releases/${name}.tar.bz2";
    inherit sha256;
  };

  postPatch = ''patchShebangs .'';
  inherit patches;

  outputs = [ "dev" "out" "bin" ]
    ++ optional (reqMin "1.0") "doc" ; # just dev-doc
  setOutputFlags = false; # doesn't accept all and stores configureFlags in libs!

  configureFlags = [
    # License
      "--enable-gpl"
      "--enable-version3"
    # Build flags
      "--enable-shared"
      "--disable-static"
      (ifMinVer "0.6" "--enable-pic")
      (enableFeature runtimeCpuDetectBuild "runtime-cpudetect")
      "--enable-hardcoded-tables"
      (if multithreadBuild then (
         if stdenv.isCygwin then
           "--disable-pthreads --enable-w32threads"
         else # Use POSIX threads by default
           "--enable-pthreads --disable-w32threads")
       else
         "--disable-pthreads --disable-w32threads")
      (ifMinVer "0.9" "--disable-os2threads") # We don't support OS/2
      "--enable-network"
      (ifMinVer "2.4" "--enable-pixelutils")
    # Executables
      "--enable-ffmpeg"
      "--disable-ffplay"
      (ifMinVer "0.6" "--enable-ffprobe")
      "--disable-ffserver"
    # Libraries
      (ifMinVer "0.6" "--enable-avcodec")
      (ifMinVer "0.6" "--enable-avdevice")
      "--enable-avfilter"
      (ifMinVer "0.6" "--enable-avformat")
      (ifMinVer "1.0" "--enable-avresample")
      (ifMinVer "1.1" "--enable-avutil")
      "--enable-postproc"
      (ifMinVer "0.9" "--enable-swresample")
      "--enable-swscale"
    # Docs
      (ifMinVer "0.6" "--disable-doc")
    # External Libraries
      "--enable-bzlib"
      "--enable-gnutls"
      (ifMinVer "1.0" "--enable-fontconfig")
      (ifMinVer "0.7" "--enable-libfreetype")
      "--enable-libmp3lame"
      (ifMinVer "1.2" "--enable-iconv")
      "--enable-libtheora"
      (ifMinVer "0.6" (enableFeature (isLinux || isFreeBSD) "vaapi"))
      "--enable-vdpau"
      "--enable-libvorbis"
      (disDarwinOrArmFix (ifMinVer "0.6" "--enable-libvpx") "0.6" "--disable-libvpx")
      (ifMinVer "2.4" "--enable-lzma")
      (ifMinVer "2.2" (enableFeature openglSupport "opengl"))
      (disDarwinOrArmFix (ifMinVer "0.9" "--enable-libpulse") "0.9" "--disable-libpulse")
      (ifMinVer "2.5" (if sdlSupport then "--enable-sdl" else "")) # Only configurable since 2.5, auto detected before then
      (ifMinVer "1.2" "--enable-libsoxr")
      "--enable-libx264"
      "--enable-libxvid"
      "--enable-zlib"
      (ifMinVer "2.8" "--enable-libopus")
      (ifMinVer "2.8" "--enable-libx265")
    # Developer flags
      (enableFeature debugDeveloper "debug")
      (enableFeature optimizationsDeveloper "optimizations")
      (enableFeature extraWarningsDeveloper "extra-warnings")
      "--disable-stripping"
    # Disable mmx support for 0.6.90
      (verFix null "0.6.90" "--disable-mmx")
  ] ++ optional stdenv.cc.isClang "--cc=clang";

  nativeBuildInputs = [ perl pkgconfig texinfo yasm ];

  buildInputs = [
    bzip2 fontconfig freetype gnutls libiconv lame libass libogg libtheora
    libvdpau libvorbis lzma SDL soxr x264 x265 xvidcore zlib libopus
  ] ++ optional openglSupport mesa
    ++ optionals (!isDarwin && !isArm) [ libvpx libpulseaudio ] # Need to be fixed on Darwin and ARM
    ++ optional ((isLinux || isFreeBSD) && !isArm) libva
    ++ optional isLinux alsaLib
    ++ optionals isDarwin darwinFrameworks
    ++ optional vdpauSupport libvdpau
    ++ optional sdlSupport SDL;


  enableParallelBuilding = true;

  postFixup = ''
    moveToOutput bin "$bin"
    moveToOutput share/ffmpeg/examples "$doc"
  '';

  /* Cross-compilation is untested, consider this an outline, more work
     needs to be done to portions of the build to get it to work correctly */
  crossAttrs = let
    os = ''
      if [ "${stdenv.cross.config}" = "*cygwin*" ] ; then
        # Probably should look for mingw too
        echo "cygwin"
      elif [ "${stdenv.cross.config}" = "*darwin*" ] ; then
        echo "darwin"
      elif [ "${stdenv.cross.config}" = "*freebsd*" ] ; then
        echo "freebsd"
      elif [ "${stdenv.cross.config}" = "*linux*" ] ; then
        echo "linux"
      elif [ "${stdenv.cross.config}" = "*netbsd*" ] ; then
        echo "netbsd"
      elif [ "${stdenv.cross.config}" = "*openbsd*" ] ; then
        echo "openbsd"
      fi
    '';
  in {
    dontSetConfigureCross = true;
    configureFlags = configureFlags ++ [
      "--cross-prefix=${stdenv.cross.config}-"
      "--enable-cross-compile"
      "--target_os=${os}"
      "--arch=${stdenv.cross.arch}"
    ];
  };

  installFlags = [ "install-man" ];

  passthru = {
    vaapiSupport = if reqMin "0.6" && ((isLinux || isFreeBSD) && !isArm) then true else false;
    inherit vdpauSupport;
  };

  meta = with stdenv.lib; {
    description = "A complete, cross-platform solution to record, convert and stream audio and video";
    homepage = http://www.ffmpeg.org/;
    longDescription = ''
      FFmpeg is the leading multimedia framework, able to decode, encode, transcode, 
      mux, demux, stream, filter and play pretty much anything that humans and machines 
      have created. It supports the most obscure ancient formats up to the cutting edge. 
      No matter if they were designed by some standards committee, the community or 
      a corporation. 
    '';
    license = licenses.gpl3;
    platforms = platforms.all;
    maintainers = with maintainers; [ codyopel fuuzetsu ];
    inherit branch;
  };
}