Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

如何在 Android 平台上用 NDK 交叉編譯 faad2 實現 AAC 解碼

編輯:關於 Android 程式設計

最近專案裡需要處理 AAC 音訊的解碼工作,我在網上查閱資料時,發現主流大致可以分成兩條路走。這裡把整個嘗試過程記錄下來,希望能幫到同樣在做 AAC 解碼的開發者。

兩種思路的取捨

第一種思路是使用 FFmpeg。當我執行 ./configure --list-decoders 時,確實能看到內建的 AAC 解碼器,在 x86 平台上編譯後也能正常啟用。然而當我改用 NDK 進行交叉編譯時,即便加上 --enable-decoder=aac 參數,AAC 解碼器仍然沒有被真正打開,於是這條路只能先擱置。

第二種思路則是直接採用 faad2 來解碼。我先在 x86 環境下除錯,寫一個能成功解碼 AAC 的 demo,驗證無誤後再进行交叉編譯,最終也順利產出了可用的庫。因此我選擇了第二種方案,並把步驟整理如下。

交叉編譯步驟

1. 下載 faad2 源碼
前往官網下載 faad,當時的最新版本為 2.7。

2. 建立專案目錄
新建一個名為 AACDecoder 的文件夾,並進入該目錄。

3. 解壓並重命名
將下載的 faad 源碼加壓後,重新命名為 jni。

4. 建立 Android.mk 與 Application.mk
首先在 jni 目錄下建立 Android.mk:

LOCAL_PATH := $(call my-dir)

FAAD2_TOP := $(LOCAL_PATH)

include $(CLEAR_VARS)

include $(FAAD2_TOP)/libfaad/Android.mk

接著建立 Application.mk(若不建立也不影響編譯):

APP_ABI := armeabi

5. 建立 config.h
這個檔案通常由 configure 腳本生成,在 NDK 環境下需要手動建立,內容如下:

/* config.h.  Generated from config.h.in by configure.  */
/* Define to 1 if you have the <dlfcn.h> header file. */
#define HAVE_DLFCN_H 1
/* Define to 1 if you have the <errno.h> header file. */
#define HAVE_ERRNO_H 1
/* Define to 1 if you have the <float.h> header file. */
#define HAVE_FLOAT_H 1
/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
/* Define to 1 if you have the <limits.h> header file. */
#define HAVE_LIMITS_H 1
/* Define if you have C99's lrintf function. */
#define HAVE_LRINTF 1
/* Define to 1 if you have the <stdint.h> header file. */
#define HAVE_STDINT_H 1
/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
/* Define to 1 if you have the <strings.h> header file. */
#define HAVE_STRINGS_H 1
/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1
/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1
/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1
/* Name of package */
#define PACKAGE "faad2"
/* Version number of package */
#define VERSION "2.7.0"
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1

其餘如 DRM、DRM_PS、WORDS_BIGENDIAN 等選項在一般 Android 場景下保持 /* #undef */ 註釋狀態即可。

6. 在 libfaad 目錄下建立 Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
    bits.c \
    cfft.c \
    decoder.c \
    drc.c \
    drm_dec.c \
    error.c \
    filtbank.c \
    ic_predict.c \
    is.c \
    lt_predict.c \
    mdct.c \
    mp4.c \
    ms.c \
    output.c \
    pns.c \
    ps_dec.c \
    ps_syntax.c \
    pulse.c \
    specrec.c \
    syntax.c \
    tns.c \
    hcr.c \
    huffman.c \
    rvlc.c \
    ssr.c \
    ssr_fb.c \
    ssr_ipqf.c \
    common.c \
    sbr_dct.c \
    sbr_e_nf.c \
    sbr_fbt.c \
    sbr_hfadj.c \
    sbr_hfgen.c \
    sbr_huff.c \
    sbr_qmf.c \
    sbr_syntax.c \
    sbr_tf_grid.c \
    sbr_dec.c

LOCAL_MODULE:= libfaad

LOCAL_C_INCLUDES := \
    $(LOCAL_PATH) \
    $(FAAD2_TOP)/android \
    $(FAAD2_TOP)/include \
    $(LOCAL_PATH)/codebook

LOCAL_CFLAGS:=
    -DHAVE_CONFIG_H

include $(BUILD_SHARED_LIBRARY)

7. 執行編譯
在 jni 目錄下執行 ndk-build。

8. 編譯結果
當所有原始檔逐一編譯完成後,最後會看到類似以下的輸出:

SharedLibrary : libfaad.so
Install       : libfaad.so => libs/armeabi/libfaad.so

看到 libfaad.so 成功安裝到 libs/armeabi/ 目錄,就代表交叉編譯圓滿完成,接著便可在 Android 專案中透過 JNI 呼叫它來進行 AAC 解碼了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved