Friday, 5 April 2013

ffmpeg with libfaac support on QNAP TS-419P II NAS

Having recently upgraded to a QNAP TS-419P II NAS, I opted to replace the default ffmpeg installation with one that is compiled against libfaac for proper AAC encoding/decoding support.  On "normal" Linux distributions this is an easy task, and in all fairness it isn't particularly difficult on the QNAP either... but it did take me a couple of hours to sort the quirks out. And since I could only find a few random forum posts about the topic, I decided to create a simple step-by-step tutorial.

The TS-419P II has a 2 GHz Marvell Feroceon "Kirkwood" CPU, which is an armv5tel architecture, according to "uname -m" - we have to keep this in mind when compiling some of the packages, since we will be tuning builds for this architecture and will have to disable platform-specific assembly optimizations for the most part. Now, onto the steps involved:

  1. Install "Optware IPKG" via the QNAP Qpkg centre, if you haven't already.
  2. Log into the NAS via ssh.
  3. Install the some packages required for building ffmpeg and its dependencies via ipkg, by typing:
    ipkg update
    ipkg install gcc make pkgconfig libstdc++ git coreutils
    
  4. Ensure that /etc/ld.so.conf is set up so that /opt/lib is a location for shared libraries:
    if ! grep -q /opt/lib /etc/ld.so.conf; then echo /opt/lib >> /etc/ld.so.conf; else echo no; fi
    Alternatively, you can manually edit /etc/ld.so.conf and make sure it looks like this:
    /lib
    /usr/lib
    /usr/local/lib
    /opt/lib
    
  5. Install some codec libraries for ffmpeg via ipkg:
    ipkg install lame faad2 libogg libtheora libvorbis libmatroska libmpeg2
  6. Next, create a directory in which you can download and compile some required packages:
    mkdir /opt/build
  7. Build & install faac:
    cd /opt/build/
    wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
    tar -xzvf faac-1.28.tar.gz
    cd faac-1.28/
    CFLAGS="-march=armv5te -mtune=marvell-f -Wall -O2 -fstrength-reduce -finline-functions -ffast-math -fomit-frame-pointer" ./configure --prefix=/opt --enable-static=no
    make
    make install
    
  8. Build & install x264 (the version shipped by ipkg is too old for ffmpeg):
    cd /opt/build/
    git clone git://git.videolan.org/x264.git
    cd x264/
    CFLAGS="-march=armv5te -Wall -O2 -fstrength-reduce -finline-functions -ffast-math -fomit-frame-pointer" ./configure --prefix=/opt --enable-shared --system-libx264 --disable-asm
    make
    make install
    
  9. Build & install xvidcore:
    Note: the configure script for the latest xvidcore isn't compatible with the awk tool available on the QNAP NAS. The workaround is to download an earlier version of xvidcore as well and replace the current version's configure script with the older one; this is done in the example below:
    cd /opt/build/
    wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
    tar -xzvf xvidcore-1.3.2.tar.gz
    cd xvidcore/build/generic/
    wget http://downloads.xvid.org/downloads/xvidcore-1.3.0.tar.gz
    tar -xzvf vidcore-1.3.0.tar.gz
    cp -v xvidcore/build/generic/configure .
    CFLAGS="-march=armv5te -mtune=marvell-f -Wall -O2 -fstrength-reduce -finline-functions -ffast-math -fomit-frame-pointer" ./configure --prefix=/opt
    make
    make install
    
  10. Finally, build & install ffmpeg:
    cd /opt/build/
    wget http://ffmpeg.org/releases/ffmpeg-1.2.tar.gz
    tar -xzvf ffmpeg-1.2.tar.gz
    cd ffmpeg-1.2/
    CFLAGS="-march=armv5te -Wall -O2 -fstrength-reduce -finline-functions -ffast-math -fomit-frame-pointer" ./configure --prefix=/opt --enable-gpl --enable-nonfree --enable-bzlib --enable-libfaac --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --disable-asm --disable-debug
    make
    make install
    
  11. Run ldconfig to make sure all shared library additions are picked up:
    ldconfig
...and that's it. You now have a libfaac-enabled version of ffmpeg at /opt/bin/ffmpeg.

If you would like to override the system's ffmpeg with your new custom version, simply edit your PATH and make sure /opt/bin comes before /usr/bin.

15 comments:

  1. Hey i am trying to follow your step but receiving the following error message. Can you help. Thanks


    [/opt/build] # cd faac-1.28/
    [/opt/build/faac-1.28] # CFLAGS="-march=armv5te -mtune=marvell-f -Wall -O2 -fstrength-reduce -finline-functions -ffast-math -fomit-frame-pointer" ./configure --prefix=/opt --enable-static=no
    checking for a BSD-compatible install... /usr/bin/install -c
    checking whether build environment is sane... yes
    checking for gawk... no
    checking for mawk... no
    checking for nawk... no
    checking for awk... awk
    checking whether make sets $(MAKE)... yes
    checking for gcc... gcc
    checking for C compiler default output file name...
    configure: error: C compiler cannot create executables
    See `config.log' for more details.

    ReplyDelete
    Replies
    1. I'll need to see the contents of your "config.log" file to be sure, but it looks like your build system (gcc, ld, etc) has been corrupted somehow.

      I'd start by removing the gcc and libstdc++ packages that you installed with ipkg, restarting the machine, and reinstalling them:
      ipkg remove gcc
      ipkg remove libstdc++
      ipkg remove pkgconfig
      ipkg remove coreutils
      ...[reboot]...
      ipkg install gcc libstdc++ pkgconfig coreutils

      As I said though, we need a bit more information. Could you post the contents of your config.log file?

      Delete
    2. Also, during the above, please reinstall "binutils", i.e.:

      ipkg remove binutils
      ...[reboot]...
      ipkg install binutils

      Delete
    3. http://speedy.sh/wEsTm/config.log

      Thanks for your prompt response. Before contacted you, i removed and installed gcc but that did not solve the problem. Please take a look at my config.log file and let me know when to proceed with the steps you provided above if necessary.

      Delete
    4. Thanks. Unfortunately it appears you are attempting to use this guide to compile libfaac on a different QNAP model - yours uses a x86_64 CPU architecture. This guide is specifically tuned to compile these packages on an ARM-based CPU (specifically the QNAP 419P II); the instructions here will not work on your QNAP model.

      For the most part, however, it should be simpler on your QNAP. Remove all of the "-march", "-mtune" options from CFLAGS and remove the "--disable-asm" switches from x264 and ffmpeg's configure statements, and it should be OK.

      Delete
    5. Thanks a lot for your help. I am still having issues. I followed your steps and re moved the -march and -mtune switches but i am receiving a different error message now.

      [/opt/build/faac-1.28] # CFLAGS="-Wall -O2 -fstrength-reduce -finline-functions -ffast-math -fomit-frame-pointer" ./configure --prefix=/opt --enable-static=no
      checking for a BSD-compatible install... /usr/bin/install -c
      checking whether build environment is sane... yes
      checking for gawk... gawk
      checking whether make sets $(MAKE)... yes
      checking for gcc... gcc
      checking for C compiler default output file name... a.out
      checking whether the C compiler works... yes
      checking whether we are cross compiling... no
      checking for suffix of executables...
      checking for suffix of object files... o
      checking whether we are using the GNU C compiler... yes
      checking whether gcc accepts -g... yes
      checking for gcc option to accept ISO C89... none needed
      checking for style of include used by make... GNU
      checking dependency style of gcc... gcc3
      checking for g++... g++
      checking whether we are using the GNU C++ compiler... yes
      checking whether g++ accepts -g... yes
      checking dependency style of g++... gcc3
      checking build system type... x86_64-unknown-linux-gnu
      checking host system type... x86_64-unknown-linux-gnu
      checking for a sed that does not truncate output... /bin/sed
      checking for grep that handles long lines and -e... configure: error: no accepta ble grep could be found in /bin:/sbin:/usr/bin:/usr/sbin:/usr/bin/X11:/usr/local /sbin:/opt/bin:/opt/sbin:/usr/xpg4/bin
      [/opt/build/faac-1.28] # make
      make: *** No targets specified and no makefile found. Stop.

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. My Turbostation TS-212 converts with 0.9 kbit/s
    What am i doing wrong?

    log:
    [/share/MD0_DATA/Multimedia/Movies/Umrechnen] # ffmpeg -i Argo.mkv -r 30 -ab 3>
    ffmpeg version 1.2 Copyright (c) 2000-2013 the FFmpeg developers
    built on Jun 25 2013 19:55:19 with gcc 4.2.3 (GCC)
    configuration: --prefix=/opt --enable-gpl --enable-nonfree --enable-bzlib --enable-libfaac --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --disable-asm --disable-debug
    libavutil 52. 18.100 / 52. 18.100
    libavcodec 54. 92.100 / 54. 92.100
    libavformat 54. 63.104 / 54. 63.104
    libavdevice 54. 3.103 / 54. 3.103
    libavfilter 3. 42.103 / 3. 42.103
    libswscale 2. 2.100 / 2. 2.100
    libswresample 0. 17.102 / 0. 17.102
    libpostproc 52. 2.100 / 52. 2.100
    Input #0, matroska,webm, from 'Argo.mkv':
    Metadata:
    creation_time : 2013-03-09 15:34:08
    Duration: 02:09:35.68, start: 0.000000, bitrate: 13514 kb/s
    Stream #0:0(eng): Video: h264 (High), yuv420p, 1920x800, SAR 1:1 DAR 12:5, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc
    Stream #0:1(ger): Audio: ac3, 48000 Hz, 5.1(side), fltp, 640 kb/s
    Stream #0:2(ger): Subtitle: subrip (forced)
    [libx264 @ 0xfa1ff0] using SAR=27/20
    [libx264 @ 0xfa1ff0] using cpu capabilities: none!
    [libx264 @ 0xfa1ff0] profile High, level 4.0
    [libx264 @ 0xfa1ff0] 264 - core 133 r2334 a3ac64b - H.264/MPEG-4 AVC codec - Copyleft 2003-2013 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=1 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=30 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
    Output #0, mp4, to 'Argo.mp4':
    Metadata:
    encoder : Lavf54.63.104
    Stream #0:0(eng): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [SAR 27:20 DAR 12:5], q=-1--1, 15360 tbn, 30 tbc
    Stream #0:1(ger): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, 5.1, s16, 320 kb/s
    Stream mapping:
    Stream #0:0 -> #0:0 (h264 -> libx264)
    Stream #0:1 -> #0:1 (ac3 -> libfaac)
    Press [q] to stop, [?] for help
    frame= 11 fps=0.0 q=0.0 20000000000000000000000000000000size= 0kB time=00:00:00.41 bitrate= 0.9kbits/s dup=2 drop=0

    ReplyDelete
  4. A great tutorial, very helpful!! Thanks

    ReplyDelete
  5. Dude, thank you so much for posting this!

    ReplyDelete
  6. Great post! Only thing is that after a NAS reload the config of /etc/ld.so.conf is reset. Any idea to make it permament?

    ReplyDelete
  7. Thanks so much, what a great step-through. Much appreciated.

    ReplyDelete
  8. Used this guide today for compiling ffmpeg on my QNAP TS-870 Pro and it worked (mostly) -- had an issue compiling x264 as the x70 series is 64bit, so had to configure it using the following:

    CFLAGS="-march=i486 -Wall -O2 -fstrength-reduce -finline-functions -ffast-math -fomit-frame-pointer" ./configure --prefix=/opt --enable-shared --system-libx264 --disable-asm --host=i686-linux

    and also compiled using latest FFMPEG version available as of today (2.7.3), and it works beautifully!

    You can create a script to run via crontab to define the ldconfig settings on startup, just add the following lines to it:

    if ! grep -q /opt/lib /etc/ld.so.conf; then echo /opt/lib >> /etc/ld.so.conf; else echo no; fi
    ldconfig


    Thanks for the guide! :)

    ReplyDelete
  9. I think that there is a typo. On step #9, line:
    tar -xzvf vidcore-1.3.0.tar.gz

    should be this instead:
    tar -xzvf xvidcore-1.3.0.tar.gz

    ReplyDelete
  10. Thanks a bunch! Seems to have worked. :)

    ReplyDelete