FAAC库实现PCM编码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hiwubihe/article/details/81260931

技术在于交流、沟通,转载请注明出处并保持作品的完整性。

原文:https://blog.csdn.net/hiwubihe/article/details/81260931

[音频编解码系列文章]

  1. 音频编解码基础
  2. FFMPEG实现音频重采样
  3. FFMPEG实现PCM编码(采用封装格式实现)
  4. FFMPEG实现PCM编码(不采用封装格式实现)
  5. FAAC库实现PCM编码
  6. FAAD库实现RAW格式AAC解码
  7. FAAD库实现RAW格式AAC封装成ADTS格式
  8. FAAD库实现ADTS格式解码
  9. FFMPEG实现对AAC解码(采用封装格式实现)
  10. FFMPEG实现对AAC解码(不采用封装格式实现)

faac对pcm编码很简单,测试一下31bit样本深度,编码后没有声音,16bit编码没问题,估计是faac不支持这种样本格式,实际中如果需要对32bit采用aac编码,可能需要重采样。

faac调用流程:

demo代码如下:

/*******************************************************************************
Copyright (c) wubihe Tech. Co., Ltd. All rights reserved.
--------------------------------------------------------------------------------

Date Created:	2014-10-25
Author:			wubihe QQ:1269122125 Email:[email protected]
Description:	使用aac库把PCM编码成aac 测试时 源文件是f32le格式 转换的AAC文件听不见声音 
				估计是faac不支持这种格式,如果需要编码可能要重新采样。
--------------------------------------------------------------------------------
Modification History
DATE          AUTHOR          DESCRIPTION
--------------------------------------------------------------------------------

********************************************************************************/
  
#include <stdio.h> 
#include "faac.h"
 
#ifndef BYTE
typedef unsigned char BYTE;
#endif



int main()
{ 	
	//设置PCM 参数
	unsigned long   nSampleRate = 48000;
	unsigned int    nChannels	= 2;
	unsigned int    nPCMBitSize = 16;
	//编码时一帧一帧送入编码器编码的 音频帧的概念是各个编码器自己定义的 AAC定义为1024个样本数
	//MP3定义为1152个样本数,双通道此值要*2
	unsigned long   nInputSamples   = 0;
	unsigned long   nMaxOutputBytes = 0;
	//faac操作句柄
	faacEncHandle   hEncoder = { 0 };

	// 设置输入输出文件  
	FILE* fpIn  = NULL;
	FILE* fpOut = NULL;
	fopen_s(&fpIn, "huangdun_r48000_s16le_c2.pcm","rb");
	fopen_s(&fpOut, "huangdun.aac", "wb");

	if (fpIn == NULL || fpOut == NULL)
	{
		printf("打开文件失败!\n");
		return -1;
	}

	// 打开faac编码器引擎   nInputSamples 返回2048 双通道,nMaxOutputBytes每次编码后最大返回值
	hEncoder = faacEncOpen(nSampleRate, nChannels, &nInputSamples, &nMaxOutputBytes);


	if (hEncoder == NULL)
	{
		printf("faacEncOpen失败!\n");
		return -1;
	}

	// 分配内存信息  
	int     nPCMBufferSize = nInputSamples*nPCMBitSize / 8;
	BYTE*   pbPCMBuffer = new BYTE[nPCMBufferSize];
	BYTE*   pbAACBuffer = new BYTE[nMaxOutputBytes];

	// 获取当前编码器信息  -- 不能缺少,只有获取以后才可以Set
	faacEncConfigurationPtr pConfiguration = { 0 };
	pConfiguration = faacEncGetCurrentConfiguration(hEncoder);

	// 设置编码配置信息  
	/*
	PCM Sample Input Format
	0   FAAC_INPUT_NULL         invalid, signifies a misconfigured config
	1   FAAC_INPUT_16BIT        native endian 16bit
	2   FAAC_INPUT_24BIT        native endian 24bit in 24 bits      (not implemented)
	3   FAAC_INPUT_32BIT        native endian 24bit in 32 bits      (DEFAULT)
	4   FAAC_INPUT_FLOAT        32bit floating point
	*/
	pConfiguration->inputFormat = FAAC_INPUT_16BIT;

	//AAC编码等级
	// AAC object types   
	//#define MAIN 1  
	//#define LOW  2  
	//#define SSR  3  
	//#define LTP  4  
	pConfiguration->aacObjectType = LOW;

	//设定AAC单通道比特率 
	pConfiguration->bitRate   = 48000;    // or 0
	pConfiguration->bandWidth = 64000;  //or 0 or 32000

	/*下面可以选择设置*/
	pConfiguration->allowMidside = 1;
	pConfiguration->useLfe = 0;
	pConfiguration->useTns = 0;   
	//AAC品质
	pConfiguration->quantqual = 100;
	//outputformat 0 = Raw; 1 = ADTS  
	pConfiguration->outputFormat = 0;
	pConfiguration->shortctl = SHORTCTL_NORMAL;  

	// 重置编码器的配置信息  
	faacEncSetConfiguration(hEncoder, pConfiguration);

	size_t nRet = 0;

	int i = 0;
	while ((nRet = fread(pbPCMBuffer, 1, nPCMBufferSize, fpIn)) > 0)
	{
		//实际读取的样本数 两个通道总样本数
		nInputSamples = nRet / (nPCMBitSize / 8);

		// 编码  
		nRet = faacEncEncode(hEncoder, (int*)pbPCMBuffer, nInputSamples, pbAACBuffer, nMaxOutputBytes);

		// 写入转码后的数据  
		fwrite(pbAACBuffer, 1, nRet, fpOut);
	}

	faacEncClose(hEncoder);
	fclose(fpOut);
	fclose(fpIn);

	delete[] pbAACBuffer;
	delete[] pbPCMBuffer;

	printf("faac complete aac encode!!\n");
	getchar();

	return 0;
}

猜你喜欢

转载自blog.csdn.net/hiwubihe/article/details/81260931