[FFmpeg] エンコード中にglibcがabortしたとき

ffmpeg Advent Calendar 2014 11日の記事です。よろしくお願いいたします。

## 事案概要
とあるmpgファイルをmjpegに変換していたとき、特定のフレームでffmpegがabortするという現象が発生。

エラーメッセージは、下記のとおり。

```shell-session
$ ffmpeg -y -threads 0 -i hoge.mpg -vcodec mjpeg -qmin 1 -qmax 1 img/%d.jpg
ffmpeg version N-68076-gea38e5a Copyright (c) 2000-2014 the FFmpeg developers
  built on Nov 28 2014 17:19:04 with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-4)
  configuration: --prefix=/usr/local --enable-gpl --enable-version3 --enable-nonfree --enable-static --disable-shared --disable-doc --disabmlpages --disable-manpages --disable-podpages --disable-txtpages --enable-memalign-hack
  libavutil      54. 15.100 / 54. 15.100
  libavcodec     56. 13.100 / 56. 13.100
  libavformat    56. 15.101 / 56. 15.101
  libavdevice    56.  3.100 / 56.  3.100
  libavfilter     5.  2.103 /  5.  2.103
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Guessed Channel Layout for  Input Stream #0.1 : stereo
Input #0, mpeg, from '2364948.mpg':
  Duration: 00:04:09.95, start: 0.250000, bitrate: 101802 kb/s
    Stream #0:0[0x1e0]: Video: mpeg2video (4:2:2), yuv422p(tv), 1920x1080 [SAR 1:1 DAR 16:9], 100000 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 5tbc
    Stream #0:1[0xa0]: Audio: pcm_dvd, 48000 Hz, 2 channels, s16, 1536 kb/s
[swscaler @ 0x23f2440] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'img/%d.jpg':
  Metadata:
    encoder         : Lavf56.15.101
    Stream #0:0: Video: mjpeg, yuvj422p(pc), 1920x1080 [SAR 1:1 DAR 16:9], q=1-1, 200 kb/s, 29.97 fps, 29.97 tbn, 29.97 tbc
    Metadata:
      encoder         : Lavc56.13.100 mjpeg
Stream mapping:
  Stream #0:0 -> #0:0 (mpeg2video (native) -> mjpeg (native))
Press [q] to stop, [?] for help
*** glibc detected *** ffmpeg: free(): invalid next size (normal): 0x0000000003299f90 ***
======= Backtrace: =========
/lib64/libc.so.6[0x31ede760e6]
/lib64/libc.so.6[0x31ede78c13]
ffmpeg[0x9cef6f]
ffmpeg[0xaedf1f]
ffmpeg[0x9c6261]
ffmpeg[0x9d4506]
ffmpeg[0xaefe83]
ffmpeg[0x47a3e2]
ffmpeg[0x47e9c5]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x31ede1ecdd]
ffmpeg[0x462ff1]
======= Memory map: ========
00400000-01232000 r-xp 00000000 fd:00 412589                             /usr/local/bin/ffmpeg
01432000-01469000 rw-p 00e32000 fd:00 412589                             /usr/local/bin/ffmpeg
01469000-01ad1000 rw-p 00000000 00:00 0
023e2000-04765000 rw-p 00000000 00:00 0                                  [heap]
31ed600000-31ed620000 r-xp 00000000 fd:00 262046                         /lib64/ld-2.12.so
31ed81f000-31ed820000 r--p 0001f000 fd:00 262046                         /lib64/ld-2.12.so
31ed820000-31ed821000 rw-p 00020000 fd:00 262046                         /lib64/ld-2.12.so
31ed821000-31ed822000 rw-p 00000000 00:00 0
31ede00000-31edf8a000 r-xp 00000000 fd:00 262047                         /lib64/libc-2.12.so
31edf8a000-31ee189000 ---p 0018a000 fd:00 262047                         /lib64/libc-2.12.so
31ee189000-31ee18d000 r--p 00189000 fd:00 262047                         /lib64/libc-2.12.so
31ee18d000-31ee18e000 rw-p 0018d000 fd:00 262047                         /lib64/libc-2.12.so
31ee18e000-31ee193000 rw-p 00000000 00:00 0
31ee200000-31ee217000 r-xp 00000000 fd:00 262056                         /lib64/libpthread-2.12.so
31ee217000-31ee417000 ---p 00017000 fd:00 262056                         /lib64/libpthread-2.12.so
31ee417000-31ee418000 r--p 00017000 fd:00 262056                         /lib64/libpthread-2.12.so
31ee418000-31ee419000 rw-p 00018000 fd:00 262056                         /lib64/libpthread-2.12.so
31ee419000-31ee41d000 rw-p 00000000 00:00 0
31ee600000-31ee615000 r-xp 00000000 fd:00 262054                         /lib64/libz.so.1.2.3
31ee615000-31ee814000 ---p 00015000 fd:00 262054                         /lib64/libz.so.1.2.3
31ee814000-31ee815000 r--p 00014000 fd:00 262054                         /lib64/libz.so.1.2.3
31ee815000-31ee816000 rw-p 00015000 fd:00 262054                         /lib64/libz.so.1.2.3
31eea00000-31eea83000 r-xp 00000000 fd:00 262055                         /lib64/libm-2.12.so
31eea83000-31eec82000 ---p 00083000 fd:00 262055                         /lib64/libm-2.12.so
31eec82000-31eec83000 r--p 00082000 fd:00 262055                         /lib64/libm-2.12.so
31eec83000-31eec84000 rw-p 00083000 fd:00 262055                         /lib64/libm-2.12.so
31eee00000-31eee07000 r-xp 00000000 fd:00 262057                         /lib64/librt-2.12.so
31eee07000-31ef006000 ---p 00007000 fd:00 262057                         /lib64/librt-2.12.so
31ef006000-31ef007000 r--p 00006000 fd:00 262057                         /lib64/librt-2.12.so
31ef007000-31ef008000 rw-p 00007000 fd:00 262057                         /lib64/librt-2.12.so
31f2e00000-31f2e02000 r-xp 00000000 fd:00 395090                         /usr/lib64/libXau.so.6.0.0
31f2e02000-31f3002000 ---p 00002000 fd:00 395090                         /usr/lib64/libXau.so.6.0.0
31f3002000-31f3003000 rw-p 00002000 fd:00 395090                         /usr/lib64/libXau.so.6.0.0
31f3200000-31f3210000 r-xp 00000000 fd:00 261686                         /lib64/libbz2.so.1.0.4
31f3210000-31f340f000 ---p 00010000 fd:00 261686                         /lib64/libbz2.so.1.0.4
31f340f000-31f3411000 rw-p 0000f000 fd:00 261686                         /lib64/libbz2.so.1.0.4
31f6200000-31f621d000 r-xp 00000000 fd:00 399336                         /usr/lib64/libxcb.so.1.1.0
31f621d000-31f641d000 ---p 0001d000 fd:00 399336                         /usr/lib64/libxcb.so.1.1.0
31f641d000-31f641e000 rw-p 0001d000 fd:00 399336                         /usr/lib64/libxcb.so.1.1.0
36f8800000-36f8816000 r-xp 00000000 fd:00 261747                         /lib64/libgcc_s-4.4.7-20120601.so.1
36f8816000-36f8a15000 ---p 00016000 fd:00 261747                         /lib64/libgcc_s-4.4.7-20120601.so.1
36f8a15000-36f8a16000 rw-p 00015000 fd:00 261747                         /lib64/libgcc_s-4.4.7-20120601.so.1
7fea79207000-7fea79864000 rw-p 00000000 00:00 0
7fea79864000-7fea79865000 ---p 00000000 00:00 0
7fea79865000-7fea7a265000 rw-p 00000000 00:00 0
7fea7a265000-7fea7a266000 ---p 00000000 00:00 0
7fea7a266000-7fea7ac66000 rw-p 00000000 00:00 0
7fea7ac66000-7fea7ac67000 ---p 00000000 00:00 0
7fea7ac67000-7fea7b667000 rw-p 00000000 00:00 0
7fea7b667000-7fea7b668000 ---p 00000000 00:00 0
7fea7b668000-7fea7c068000 rw-p 00000000 00:00 0
7fea7c068000-7fea7c069000 ---p 00000000 00:00 0
7fea7c069000-7fea7ca69000 rw-p 00000000 00:00 0
7fea7ca69000-7fea7ca6a000 ---p 00000000 00:00 0
7fea7ca6a000-7fea7d470000 rw-p 00000000 00:00 0
7fea7d470000-7fea7d472000 r-xp 00000000 fd:00 399358                     /usr/lib64/libxcb-shape.so.0.0.0
7fea7d472000-7fea7d672000 ---p 00002000 fd:00 399358                     /usr/lib64/libxcb-shape.so.0.0.0
7fea7d672000-7fea7d673000 rw-p 00002000 fd:00 399358                     /usr/lib64/libxcb-shape.so.0.0.0
7fea7d673000-7fea7d678000 r-xp 00000000 fd:00 399368                     /usr/lib64/libxcb-xfixes.so.0.0.0
7fea7d678000-7fea7d878000 ---p 00005000 fd:00 399368                     /usr/lib64/libxcb-xfixes.so.0.0.0
7fea7d878000-7fea7d879000 rw-p 00005000 fd:00 399368                     /usr/lib64/libxcb-xfixes.so.0.0.0
7fea7d879000-7fea7d87a000 rw-p 00000000 00:00 0
7fea7d87a000-7fea7d87c000 r-xp 00000000 fd:00 399360                     /usr/lib64/libxcb-shm.so.0.0.0
7fea7d87c000-7fea7da7b000 ---p 00002000 fd:00 399360                     /usr/lib64/libxcb-shm.so.0.0.0
7fea7da7b000-7fea7da7c000 rw-p 00001000 fd:00 399360                     /usr/lib64/libxcb-shm.so.0.0.0
7fea7da87000-7fea7da89000 rw-p 00000000 00:00 0
7fffb568c000-7fffb56a1000 rw-p 00000000 00:00 0                          [stack]
7fffb57ff000-7fffb5800000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)
```

最新リビジョンにアップデートしてみたりしましたが、治らず。
けっこうお手上げ状態でした。


## 解決方法1
### FFmpeg オプションを変更

```diff
- ffmpeg -y -threads 0 -i hoge.mpg -vcodec mjpeg -qmin 1 -qmax 1 img/%d.jpg
+ ffmpeg -y -threads 0 -i hoge.mpg -vcodec mjpeg -qmin 1 -qmax 2 img/%d.jpg
```

``-qmax``を大きくすることで、画質をちょっと落とす。


## 解決方法2
### glibcのAbortを無視する
 * [How to force abort on “glibc detected *** free(): invalid pointer”](http://stackoverflow.com/questions/151268/how-to-force-abort-on-glibc-detected-free-invalid-pointer)
 * [バッファオーバーフロー: #4 あふれを検出するデバッグ
](https://www.ipa.go.jp/security/awareness/vendor/programmingv2/contents/c904.html)
 * などなど``MALLOC_CHECK_``をぐぐってください


```shell-session
$ export MALLOC_CHECK_=0
$ ffmpeg -y -threads 0 -i hoge.mpg -vcodec mjpeg -qmin 1 -qmax 1 img/%d.jpg
```