• T31开发笔记:Faac移植


    若该文为原创文章,转载请注明原文出处

    在传输RTSP时,需要使用到 AAC,T31采集的音频数据为PCM,记录下使用faac库,把PCM数据编码成AAC数据 。

    一、硬件和开发环境

    1、硬件:T31X+SC5235 

    2、开发环境: ubuntu16.04-64bit

    3、编译器:mips-gcc540-glibc222-32bit-r3.3.0.tar.gz

    注:板子和和WIFI模块是某淘上淘的,使用的是RTL8188,使用的是USB接口,uboot和内核是自己裁剪移植的,内核默认自带WIFI驱动,所以不用移植可以直接使用。

    二、FAAC库移植

    移植过程相对简单,交叉编译一下就可以,基本没问题。

    1、编译faac1.28

    1. ./configure --host=mips-linux-gnu --target=mips-linux-gnu --prefix=/home/yifeng/Downloads/faac-1.28/lib
    2. make 
    3. make install

    编译时遇到的问题:

    1、mpeg4ip.h:126:58: error: ambiguating new declaration of 'char* strcasestr(const char*, const char*)'
    处理:
    从123行开始修改此文件mpeg4ip.h,到129行结束。
    修改前:

    1. #ifdef __cplusplus
    2. extern "C" {undefined
    3. #endif
    4. char *strcasestr(const char *haystack, const char *needle);
    5. #ifdef __cplusplus
    6. }
    7. #endif

    修改后:

    1. #ifdef __cplusplus
    2. extern "C++" {undefined
    3. #endif
    4. const char *strcasestr(const char *haystack, const char *needle);
    5. #ifdef __cplusplus
    6. }
    7. #endif

    2、在不修改源码的情况下,faac的内存占用非常高,每路音频在13M左右。如果多路音频的话,内存将很快耗尽。

    搜索MAX_CHANNELS的定义,默认是6 和64,全部改成1(一般都是单声道)。

    重新编译,运行,内存占用降为2.5M左右。

    三、测试

    这里测试是把PCM文件编码成AAC,没有实时采集。

    把编译好的/faac-1.28/lib/下的so文件拷贝到开发板/usr/lib上。

    1、代码

    pcm_encode_aac.c

    1. /*!
    2. *****************************************************************************
    3. *
    4. * Copyright ? 2017-2018 yifeng. All Rights Reserved.
    5. *
    6. * \file sample-Ai.c
    7. * \author yifeng
    8. * \version 1.0
    9. * \date 2022年2月3日
    10. * \brief sample-Ai
    11. *
    12. *----------------------------------------------------------------------------
    13. * \attention
    14. *
    15. *
    16. *****************************************************************************
    17. */
    18. /*****************************************************************************
    19. change history:
    20. 1.date : 202223
    21. author: yifeng
    22. change: create file
    23. *****************************************************************************/
    24. /*==========================================================================================
    25.                本源程序包括的头文件
    26. 建议:包含本项目的文件使用 #include "文件名.扩展名"
    27.    包含系统库的文件使用 #include <文件名.扩展名>
    28. ==========================================================================================*/
    29. #include <unistd.h>
    30. #include <sys/ioctl.h>
    31. #include <sys/prctl.h>
    32. #include <string.h>
    33. #include <stdio.h>
    34. #include <stdlib.h>
    35. #include <math.h>
    36. #include <stdint.h>
    37. #include <errno.h>
    38. #include <pthread.h>
    39. #include <semaphore.h>
    40. #include <fcntl.h>
    41. #include <sys/time.h>
    42. #include "faac/faac.h"
    43. #include <imp/imp_audio.h>
    44. #include <imp/imp_log.h>
    45. #define TAG "Sample-AI"
    46. typedef unsigned long ULONG;
    47. typedef unsigned int UINT;
    48. typedef unsigned char BYTE;
    49. typedef char _TCHAR;
    50. int main(int argc, _TCHAR* argv[])
    51. {
    52. ULONG nSampleRate = 16000; // 采样率
    53. UINT nChannels = 1; // 声道数
    54. UINT nPCMBitSize = 16; // 单样本位数
    55. ULONG nInputSamples = 0;
    56. ULONG nMaxOutputBytes = 0;
    57. int nRet;
    58. faacEncHandle hEncoder;
    59. faacEncConfigurationPtr pConfiguration;
    60. int nBytesRead;
    61. int nPCMBufferSize;
    62. BYTE* pbPCMBuffer;
    63. BYTE* pbAACBuffer;
    64. FILE* fpIn; // PCM file for input
    65. FILE* fpOut; // AAC file for output
    66. fpIn = fopen("in.pcm", "rb");
    67. fpOut = fopen("out.aac", "wb");
    68. // (1) Open FAAC engine
    69. hEncoder = faacEncOpen(nSampleRate, nChannels, &nInputSamples, &nMaxOutputBytes);
    70. if(hEncoder == NULL)
    71. {
    72. printf("[ERROR] Failed to call faacEncOpen()\n");
    73. return -1;
    74. }
    75. nPCMBufferSize = nInputSamples * nPCMBitSize / 8;
    76. pbPCMBuffer = malloc(nPCMBufferSize);
    77. pbAACBuffer = malloc(nMaxOutputBytes);
    78. printf("nPCMBufferSize is %d, nInputSamples is %d\n", nPCMBufferSize, nInputSamples);
    79. // (2.1) Get current encoding configuration
    80. pConfiguration = faacEncGetCurrentConfiguration(hEncoder);
    81. pConfiguration->inputFormat = FAAC_INPUT_16BIT;
    82. // (2.2) Set encoding configuration
    83. nRet = faacEncSetConfiguration(hEncoder, pConfiguration);
    84. for(int i = 0; 1; i++)
    85. {
    86. // 读入的实际字节数,最大不会超过nPCMBufferSize,一般只有读到文件尾时才不是这个值
    87. nBytesRead = fread(pbPCMBuffer, 1, nPCMBufferSize, fpIn);
    88. // 输入样本数,用实际读入字节数计算,一般只有读到文件尾时才不是nPCMBufferSize/(nPCMBitSize/8);
    89. nInputSamples = nBytesRead / (nPCMBitSize / 8);
    90. printf("nInputSamples is %d\n", nInputSamples);
    91. // (3) Encode
    92. nRet = faacEncEncode(
    93. hEncoder, (int*) pbPCMBuffer, nInputSamples, pbAACBuffer, nMaxOutputBytes);
    94. fwrite(pbAACBuffer, 1, nRet, fpOut);
    95. printf("%d: faacEncEncode returns %d\n", i, nRet);
    96. if(nBytesRead <= 0)
    97. {
    98. break;
    99. }
    100. }
    101. // (4) Close FAAC engine
    102. nRet = faacEncClose(hEncoder);
    103. free(pbPCMBuffer);
    104. free(pbAACBuffer);
    105. fclose(fpIn);
    106. fclose(fpOut);
    107. //getchar();
    108. return 0;
    109. }

    Makefile

    1. INCLUDE=-I./ -I../include
    2. LIBS= ../lib/glibc/libimp.a ../lib/glibc/libalog.a ../lib/faac/libfaac.a -lpthread -lm -lrt -ldl
    3. all:pcm_encode_aac
    4. pcm_encode_aac:
    5. mips-linux-gnu-gcc -o pcm_encode_aac -O2 -w -march=mips32r2 pcm_encode_aac.c sample-common.c $(INCLUDE) $(LIBS)
    6. clean:
    7. rm -rf pcm_encode_aac

    库自行添加。

    使用make生成可执行文件,通过tftp方式把pcm文件和可执行文件上传到开发板上运行。

    如有侵权,请及时联系博主删除。

  • 相关阅读:
    【堡塔企业级防篡改-重构版】使用手册
    VR智能家居虚拟连接仿真培训系统重塑传统家居行业
    Flink 1.13 源码解析——Flink 作业提交流程
    使用Docker本地安装部署Drawio绘图工具并实现公网访问
    弘辽科技:淘宝没提升销量就没流量吗?怎么挽救?
    华为正式捐赠欧拉:操作系统领域的重量级开源项目
    小程序使用MQTT链接
    Redis持久化
    梦中情盘!基于NextCloud搭建个人私有云!
    考研C语言复习进阶(5)
  • 原文地址:https://blog.csdn.net/weixin_38807927/article/details/128128987