Search code examples
androidandroid-ndkffmpegjavacv

AMR-NB support in javacv -> ffmpeg


I am trying to use AMR_NB Codec in JavaCV Android but having problems:

This is the code i run:

avcodec.avcodec_register_all();
avformat.av_register_all();
AVCodec mCodec = avcodec.avcodec_find_encoder(avcodec.AV_CODEC_ID_AMR_NB);

Output: The mCodec is null but if I pass avcodec.AV_CODEC_ID_AAC than mCodec is not null

I am assuming that I have to recompile ffmpeg with --enable-libopencore-amrnb. So, I downloaded Android NDKr9+ffmpeg-2.0.2+last_stable_x264 and added them to javacv-cppjars folder, I have also installed opencore-amr library in /usr/local. In ffmpeg-2.0.2-android-arm.patch i added the --enable-libopencore-amrnb flag and run following command in terminal

ANDROID_NDK=/Users/android-ndk-r9b/ FFMPEG_VERSION=2.0.2 ./build_ffmpeg-android-arm.sh

The script runs but the libs are not generated and the jar is empty but if I remove the --enable-libopencore-amrnb flag back then the libs are generated successfully, Please advise what am I doing wrong? What should I do? Any help would be greatly appreciated!

by the way, I am using mac-osx

Thanks.


Solution

  • I assume that build_ffmpeg-android-arm.sh is this, if this is the case, to properly use --enable-libopencore-amrnb you also need to provide opencore-amr libraries (opencore source is not included in ffmpeg code), otherwise configure would complain about no opencore library found.

    In order to fix it you would need to:

    1. Download opencore-amr source code from here and copy it to javacv-cppjars folder.
    2. Modify build_ffmpeg-android-arm.sh to build and link with opencore's libraries:

    ANDROID_BIN=$ANDROID_NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/    
    ANDROID_ROOT=$ANDROID_NDK/platforms/android-9/arch-arm/
    tar -xjvf ffmpeg-$FFMPEG_VERSION.tar.bz2
    mv ffmpeg-$FFMPEG_VERSION ffmpeg-$FFMPEG_VERSION-android-arm
    cd ffmpeg-$FFMPEG_VERSION-android-arm
    tar -xjvf ../last_stable_x264.tar.bz2
    X264=`echo x264-snapshot-*`
    cd $X264
    ./configure --enable-static --enable-pic --disable-cli --cross-prefix=$ANDROID_BIN/arm-linux-androideabi- --sysroot=$ANDROID_ROOT --host=arm-linux --extra-cflags="-DANDROID -fPIC -ffunction-sections -funwind-tables -fstack-protector -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -mfpu=neon -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300" --extra-ldflags="-nostdlib -Wl,--fix-cortex-a8 -lgcc -ldl -lz -lm -lc"
    make -j4
    cd ../
    
    tar -xzf ../opencore-amr-0.1.3.tar.gz
    cd opencore-amr-0.1.3
    BUILD_FLAGS="--sysroot=$ANDROID_NDK/platforms/android-9/arch-arm"
    CFLAGS="$BUILD_FLAGS" CXXFLAGS="$BUILD_FLAGS" CPPFLAGS="$BUILD_FLAGS" RANLIB=$ANDROID_BIN/arm-linux-androideabi-ranlib CC=$ANDROID_BIN/arm-linux-androideabi-gcc CXX=$ANDROID_BIN/arm-linux-androideabi-g++ LD=$ANDROID_BIN/arm-linux-androideabi-ld CPP=$ANDROID_BIN/arm-linux-androideabi-cpp LDFLAGS="-L$ANDROID_NDK/platforms/android-9/arch-arm/usr/lib/" ./configure --host=arm-linux-gnu --disable-shared
    make
    mv amrnb opencore-amrnb
    cd ../
    
    patch -p1 < ../ffmpeg-$FFMPEG_VERSION-android-arm.patch
    ./configure --prefix=$HOME/android --enable-libopencore-amrnb --extra-cflags="-Iopencore-amr-0.1.3" --extra-ldflags="-Lopencore-amr-0.1.3/opencore-amrnb/.libs" --enable-shared --enable-gpl --enable-version3 --enable-runtime-cpudetect --disable-outdev=sdl --enable-libx264 --extra-cflags="-I$X264" --extra-ldflags="-L$X264" --enable-cross-compile --cc=$ANDROID_BIN/arm-linux-androideabi-gcc --sysroot=$ANDROID_ROOT --target-os=linux --arch=arm --extra-cflags="-DANDROID -fPIC -ffunction-sections -funwind-tables -fstack-protector -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -mfpu=neon -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300" --extra-ldflags="-nostdlib -Wl,--fix-cortex-a8" --extra-libs="-lgcc -ldl -lz -lm -lc" --disable-stripping --disable-symver --disable-programs
    make
    LIBS="libavcodec/libavcodec.so libavdevice/libavdevice.so libavfilter/libavfilter.so libavformat/libavformat.so libavutil/libavutil.so libpostproc/libpostproc.so libswresample/libswresample.so libswscale/libswscale.so"
    $ANDROID_NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip $LIBS
    mkdir -p com/googlecode/javacv/cpp/android-arm/
    cp $LIBS com/googlecode/javacv/cpp/android-arm/
    jar cvf ../ffmpeg-$FFMPEG_VERSION-android-arm.jar com/
    rm -Rf com/
    cd ../
    

    And build again. Please note new section for building opencore is based on verson 0.1.3 and the small trick renaming amrnb folder.

    Hope this helps.