Convert PCM to AAC using FAAC

Source: Internet
Author: User

About FAAC

FAAC is a MPEG-4 and MPEG-2 AAC encoder, characterized by: portability, fast, support LC/MAIN/LTP, through dream support DRM, code small relative to the ffmpeg of the AAC transcoding, FAAC is really very little, And you can directly add code to the project to compile, you can also use the static library, and no huge dynamic library of the trouble.


Download installation

    • Compile the static library for use by our program directly as shown in the official documentation. (I did not do so, the twists and turns of the error is not detailed)
    • FAAC code out, add directly to our project, or key out to compile a static library, simple brute Force, the code required is the LIBFAAC and include two directories of all files (excluding subdirectory files). The list of directory files is as follows:
      Aacquant.caacquant.hbackpred.cbackpred.hbitstream.cbitstream.hchannels.cchannels.hcoder.hfaac.hfaaccfg.hfft.cfft.hfiltban K.cfiltbank.hframe.cframe.hhuffman.chuffman.hhufftab.hltp.cltp.hmidside.cmidside.hpsych.hpsychkni.ctns.ctns.hutil.cutil.h Version.h
      The second method is strongly recommended
The main functions are introduced
Faacenchandle Faacapi faacencopen (unsigned long samplerate,unsigned int numchannels,unsigned long *inputsamples, unsigned long *maxoutputbytes);//Description: Open and Initialize encoder//samplerate: Sample rate of encoded input information//numchannels: Number of channels encoded input information, 1-Mono 2-stereo// Inputsamples: Encoded data length//maxoutputbytes: Maximum length of encoded information
int Faacapi faacencclose (faacenchandle hencoder);//Description: Encoder handle returned by closing encoder//hencoder:faacencopen
Faacencconfigurationptr Faacapi faacencgetcurrentconfiguration (Faacenchandle hencoder);//Description: Gets the configuration information for the current encoder// Hencoder:faacencopen the encoder handle returned
int Faacapi faacencsetconfiguration (faacenchandle hencoder, faacencconfigurationptr config);//Description: Configure the parameters of the decoder//hencoder : Faacencopen return encoder handle//config: Encoder configuration information
int Faacapi Faacencencode (faacenchandle hencoder, int32_t * inputbuffer, unsigned int samplesinput,unsigned char *outputB uffer,unsigned int buffersize);//Description: Encode a frame information//hencoder:faacencopen The encoder handle returned//inputbuffer: input information buffer//samplesinput: Faacencopen encoded data length, that is, buffer length//outputbuffer: Encoded output information buffer//buffersize: Output information length
int Faacapi faacencgetversion (char **faac_id_string, char **faac_copyright_string);//Description: Get FAAC version information for reference purposes, not required api// FAAC_ID_STRING:FAAC version number//faac_copyright_string: Copyright information

The workflow for code sample code is:
    1. open input Output file
    2. use Faacencopen to open the encoder engine
    3. use Faacencgetcurrentconfiguratio to get the encoder configuration
    4. Configure encoder parameters
    5. use Faacencsetconfiguration to set encoder configuration
    6. read a frame input data
    7. Use Faacencencode to encode frame data
    8. write encoded data to output file
    9. Use Faacencclose to turn off the encoding engine
FAAC example code//PCM to ACC////Created by Arbboter on 15/1/26. Copyright (c) 2015 Arbboter.  All rights reserved. #include "faac.h" #include <stdio.h>int main () {//definition alias typedef unsigned char byte;unsigned longnsamplerate = 4410 0;unsigned Intnchannels = 2;unsigned Intnpcmbitsize = 16;unsigned Longninputsamples = 0;unsigned longnMaxOutputBytes = 0;f Aacenchandlehencoder = {0};//Set input/output file file* Fpin = fopen ("BEYOND.PCM", "RB"); file* fpout = fopen ("BEYOND.AAC", "WB"), if (Fpin==null | | fpout==null) {printf ("Open file failed!\n"); return-1;} Open FAAC Encoder Engine Hencoder = Faacencopen (Nsamplerate, Nchannels, &ninputsamples, &nmaxoutputbytes); if (HEncoder = = NULL) {printf ("Open FAAC Encoder engine failed!\n"); return-1;} allocating memory information intnpcmbuffersize = NINPUTSAMPLES*NPCMBITSIZE/8; Byte*pbpcmbuffer = new Byte[npcmbuffersize]; Byte*pbaacbuffer = new byte[nmaxoutputbytes];//Gets the current encoder information Faacencconfigurationptr pconfiguration = {0};p configuration = Faacencgetcurrentconfiguration (Hencoder);//Set encoding configuration information/*PCMSample Input Format0faac_input_nullinvalid, signifies a misconfigured config1faac_input_16bitnative endian 16bit2FAAC_ Input_24bitnative endian 24bit in, bits (not implemented) 3faac_input_32bitnative endian 24bit in + bits (DEFAULT) 4faac_i Nput_float32bit floating point */pconfiguration->inputformat = faac_input_16bit;//0 = Raw; 1 = Adtspconfiguration->outputformat = 1;//AAC Object types//#define MAIN 1//#define LOW 2//#define SSR 3//#define LTP 4pconfiguration->aacobjecttype = low;pconfiguration->allowmidside = 0;pconfiguration->uselfe = 0; Pconfiguration->bitrate = 48000;pconfiguration->bandwidth = 32000;//Other parameters do not know how to configure, after all, the audio is not ripe//But the current settings can be converted, But the sound seems to be lost and weird//This piece of configuration information is important, wrong will lead to transcoding failure, and then you think the code elsewhere wrong//reset the Encoder configuration information faacencsetconfiguration (Hencoder, pconfiguration); size_t nret = 0;printf ("Data conversion:"); int i = 0;while ((nret = Fread (Pbpcmbuffer, 1, npcmbuffersize, fpin)) > 0) {pri NTF ("\b\b\b\b\b\b\b\b%-8d", ++i); ninputsamples = Nret/(NPCMBITSIZE/8);//Code nret = Faacencencode (Hencoder, (int*) Pbpcmbuffer, Ninputsamples, Pbaacbuffer, nmaxoutputbytes);//write the data after transcoding fwrite ( Pbaacbuffer, 1, nret, fpout);} Mop up work faacencclose (Hencoder) fclose (fpout) fclose (fpin);d elete[] pbaacbuffer;delete[] Pbpcmbuffer;return 0;}

Encoder parameter settings, you can find the correct decoding example as usual to modify or ask a professional, if something goes wrong.

Convert PCM to AAC using FAAC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.