x264 を Windows 上でビルドする

この記事は 2017/9 時点で古い情報です。2017/9 時点のビルド手順はこちらを参照してください。

Update

  • 2017/9/10 この環境では x264公式 rev2803 (d2b5f48) 以降のコミットに対応できなくなったので新しいやりかたで書き直しました(AVX512対応のため yasm から nasm への移行があったため)。
  • 2011/1/31 pthread が不要になったので新しいやり方で書き直しました。
  • 2010/4/22 mp4 output 対応できたのでその部分を修正・追記

以下元記事

ビルドするもの

pthread を static link した mp4 で出力できる x264

用意しておくもの

  • MinGW猫科研究所パック
    • ググってダウンロードしてね
    • ここでは msys_felidlabo_a005.7z を使ってます
  • git 環境
    • リポジトリから最新の x264 を引っ張ってくるのに使います
    • msysgit あたりで環境作っといてね
  • GPAC
    • mp4 出力させるのに必要(不要ならパスしてOK)
    • http://gpac.sourceforge.net/home_download.php から Download GPAC source の Generic Archive (tar.gz) をダウンロードのこと
    • ここでは gpac-0.4.5.tar.gz を使ってます

x264 をマルチスレッドで回すための pthread-w32 は猫科研究所パック同封のものを使います。

MinGW猫科研究所の準備

猫科研究所パックを適当な場所に展開する。 230MB ぐらい食うので余裕のある場所に。
展開した先の msys\_felidlabo の中にある 04_gccinstall.bat を実行する。

1) gcc-4.4.1-tdm
2) gcc-3.4.5-20060117-3
3) cancel
select gcc version :

と聞いてくるので 1 を選ぶ(pthread のコンパイルに 4 系が要求される模様)。

INFO: gcc-4.4.1-tdm installed.

続行するには何かキーを押してください . . .

続いて 05_plus-build.bat を実行。結構時間かかる。手元では30分弱。

pthread のビルド

msys.bat を実行。ここからしばらく MINGW32 のシェル上での操作。

ホームディレクトリに pthread のソースをコピーして make 。

$ cp -r /src/pthreads-w32-2-8-0-release/ .

$ cd pthreads-w32-2-8-0-release/

$ make GC-static
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FL
AGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[1]: Entering directory `/home/xxxxx/pthreads-w32-2-8-0-release'
gcc -c -o pthread.o -D__CLEANUP_C -O3 -finline-functions -DPTW32_BUILD_INLINED -
DPTW32_STATIC_LIB -I. -DHAVE_CONFIG_H -Wall  pthread.c
windres --include-dir=. -D__CLEANUP_C -o version.o version.rc
rm -f libpthreadGC2.a
ar -rv libpthreadGC2.a pthread.o version.o
C:\msys\mingw\bin\ar.exe: creating libpthreadGC2.a
a - pthread.o
a - version.o
ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[1]: Leaving directory `/home/xxxxx/pthreads-w32-2-8-0-release'

$

x264 のビルドに必要なファイルをコピーする。

$ cp sched.h /mingw/include
$ cp pthread.h /mingw/include
$ cp libpthreadGC2.a /mingw/lib

最後にホームディレクトリに戻っておく。

$ cd ~

$

GPAC のビルド

Windowsexplorer なりシェルなりでダウンロードしておいた gpac-0.4.5.tar.gz を msys のホームディレクトリにコピーします。

C:\>copy gpac-0.4.5.tar.gz C:/msys/home/xxxxx/

同じくmsys のホームディレクトリに次のパッチファイルを作っておきます。
ファイル名は適当でいいですが、libpng14-infopp-null.patch とでもしておきます。

--- src/media_tools/img.c
+++ src/media_tools/img.c
@@ -551,7 +551,7 @@
	/* Allocate/initialize the image information data.  REQUIRED */
	info_ptr = png_create_info_struct(png_ptr);
	if (info_ptr == NULL) {
-		png_destroy_write_struct(&png_ptr,  png_infopp_NULL);
+		png_destroy_write_struct(&png_ptr,  NULL);
		return GF_IO_ERR;
	}

msys のシェルに戻り、gpac を展開して展開先のディレクトリに移動します。

$ tar zxf gpac-0.4.5.tar.gz

$ cd gpac

$

先ほど作っておいたパッチをあてます。

$ patch -p0 -N < ../libpng14-infopp-null.patch
(Stripping trailing CRs from patch.)
patching file src/media_tools/img.c
Hunk #1 succeeded at 551 with fuzz 1.

$

./configure。 --prefix=/mingw を付けるのがこの環境でのポイント。

$ ./configure --prefix=/mingw
(省略)
Creating config.mak
config.h is unchanged
Done - type 'make help' for make info, 'make' to build

$

そして make と make install

$ make lib install-lib
(省略)
mkdir -p "/mingw/lib"
install -m 644 "./bin/gcc/libgpac_static.a" "/mingw/lib"
mkdir -p "/mingw/lib"
install -m 644 "./bin/gcc/libgpac_static.a" "/mingw/lib"

$

x264 のビルド

まずは x264 のソースをリポジトリから引っ張ってくる。
(コマンドラインから git を叩ける状態になっていないとできないので注意)

$ git clone git://git.videolan.org/x264.git x264
Initialized empty Git repository in C:/msys/home/xxxxx/x264/.git/
remote: Counting objects: 10583, done.
remote: Compressing objects: 100% (2328/2328), done.
remote: Total 10583 (delta 8551), reused 10173 (delta 8215)
Receiving objects: 100% (10583/10583), 2.55 MiB | 185 KiB/s, done.
Resolving deltas: 100% (8551/8551), done.

そして ./configure。mp4 output と pthread が yes になってるか確認。

$ cd x264
$ ./configure
Platform:   X86
System:     MINGW
asm:        yes
avs input:  yes
lavf input: no
ffms input: no
mp4 output: yes
pthread:    yes
debug:      no
gprof:      no
PIC:        no
shared:     no
visualize:  no

You can run 'make' or 'make fprofiled' now.

最後に make して x264.exe が出来ればできあがり。

$ make
(省略)
gcc -o x264.exe x264.o input/timecode.o input/yuv.o input/y4m.o output/raw.o out
put/matroska.o output/matroska_ebml.o output/flv.o output/flv_bytestream.o input
/avs.o input/thread.o output/mp4.o libx264.a -lpthreadGC2 -lwsock32 -s -lgpac_st
atic -lwinmm

$ ls x264.exe
x264.exe

$

exit して msys\home\(ユーザ名)\x264.exe を適当な場所にコピーして動くか確認。
動くはず。

C:\>x264 --help
x264 core:93 r1542 5b86182
Syntax: x264 [options] -o outfile infile [widthxheight]

Infile can be raw YUV 4:2:0 (in which case resolution is required),
  or YUV4MPEG 4:2:0 (*.y4m),
  or Avisynth if compiled with support (yes).
  or libav* formats if compiled with lavf support (no) or ffms support (no).
Outfile type is selected by filename:
 .264 -> Raw bytestream
 .mkv -> Matroska
 .flv -> Flash Video
 .mp4 -> MP4 if compiled with GPAC support (yes)

(省略)
C:\>

謝辞

msys の環境を作るのが面倒なのですが、そこをさくっとできるようなパッケージを作成&公開されている猫科研究所の中の人に感謝です。

参考: