Audio codec and real-combat article (1) PCM go to AAC (AAC encoding)

Source: Internet
Author: User
Audio codec and real-combat article (1) PCM go to AAC (AAC encoding)Author: Liu · Poechant Blog: blog.csdn.net/poechant e-mail: zhongchao.ustc@gmail.com Date: April 7th, 2012

This uses FAAC to implement AAC encoding. 1 Download and install FAAC

The installation process here is implemented on MAC and Linux, and windows can be similar to the reference.

wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz
tar zxvf faac-1.28.tar.gz
cd faac-1.28
. Configure
make
sudo make install

If prefix path is used in the default configure, then the installed Lib and. h files are used in/usr/local/lib and/usr/local/include, and later when compiling.

If errors are found during compilation:

Mpeg4ip.h:126:error:new declaration ' char* strcasestr (const char*, const char*) '

Workaround:

Start modifying this file from 123 lines to the end of line 129 mpeg4ip.h. Before modification:

#ifdef __cplusplus
extern "C" {
#endif
char *strcasestr (const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif

After modification:

#ifdef __cplusplus
extern "C + +" {
#endif
const char *strcasestr (const char *haystack, const char *needle); c3/> #ifdef __cplusplus
}
#endif
2 FAAC API 2.1 Open FAAC engine Prototype:
Faacenchandle Faacencopen               //Returns a FAAC handle
(                   
    unsigned long   nsamplerate,        //sample rate in bps
    unsigned long   nchannels,          //channel, 1 mono, 2 for dual channel
    unsigned long   &ninputsamples,     //pass reference, Get the original data length that should be received each time the encoding is called
    unsigned long   &nmaxoutputbytes    //Pass reference, get the maximum length of AAC data generated each time the encoding is called
;
2.2 get/set Encoding configuration Prototype:

To obtain the configuration of the encoder:

Faacencconfigurationptr faacencgetcurrentconfiguration//Get a pointer to the current encoder configuration
(
    faacenchandle hencoder  // FAAC of handle
);

Set the encoder configuration:

int Faacapi faacencsetconfiguration
(
    faacdechandle hdecoder,         //FAAC handle previously obtained  configuration of faacencconfigurationptr Config//FAAC encoder
);
2.3 Encode Prototype:
int Faacencencode
(
    faacenchandle hencoder,     //FAAC handle short
    *inputbuffer,         //PCM raw data
    unsigned int samplesinput,  //Ninputsamples value obtained when calling Faacencopen
    unsigned char *outputbuffer,// The actual size of the buffer
    unsigned int buffersize     //OutputBuffer buffer with at least the nmaxoutputbytes byte length obtained when calling Faacencopen
);
2.4 Close FAAC engine Prototype
void Faacencclose
(
    faacenchandle hencoder  //FAAC handle previously obtained
);
3 Process 3.1 What to do to prepare.

Sample rate, number of channels (two-channel or mono). And your PCM's single sample is 8-bit or 16-bit. 3.2 Open FAAC Encoder, ready to encode before the call Faacencopen Open FAAC Encoder, a single input sample number ninputsamples and output data maximum bytes nmaxoutputbytes; The buffers are created for the PCM data and the resulting AAC data according to Ninputsamples and Nmaxoutputbytes respectively, and the call faacencgetcurrentconfiguration gets the current configuration, after modifying the configuration, Call Faacencsetconfiguration to set a new configuration. 3.3 Start Coding

Call Faacencencode, the preparation is ready just now, very simple. 3.4 Aftermath

Turn off the encoder, and don't forget to release the buffer, and if you use a file stream, don't forget to close it. 4 test Procedure 4.1 Full Code

Transfer PCM format audio file/HOME/MICHAEL/DEVELOPMENT/TESTSPACE/IN.PCM to AAC format file/HOME/MICHAEL/DEVELOPMENT/TESTSPACE/OUT.AAC.

#include <faac.h> #include <stdio.h> typedef unsigned long ULONG;
typedef unsigned int UINT;
typedef unsigned char BYTE;

typedef char _TCHAR;  int main (int argc, _tchar* argv[]) {ULONG nsamplerate = 11025;         Sample rate UINT nchannels = 1;      Channel number UINT npcmbitsize = 16;
    Single-sample number of ULONG ninputsamples = 0;

    ULONG nmaxoutputbytes = 0;
    int nret;
    Faacenchandle Hencoder; 

    Faacencconfigurationptr pconfiguration;
    int nbytesread;
    int npcmbuffersize;
    Byte* Pbpcmbuffer;

    Byte* Pbaacbuffer; File* Fpin; PCM file for input file* fpout;
    AAC file for Output Fpin = fopen ("/HOME/MICHAEL/DEVELOPMENT/TESTSPACE/IN.PCM", "RB");

    Fpout = fopen ("/HOME/MICHAEL/DEVELOPMENT/TESTSPACE/OUT.AAC", "WB");
    (1) Open FAAC engine Hencoder = Faacencopen (Nsamplerate, Nchannels, &ninputsamples, &nmaxoutputbytes); if (Hencoder = = NULL) {printf ("[ERROR] Failed to call Faacencopen () \ n");
    return-1;
    } npcmbuffersize = Ninputsamples * NPCMBITSIZE/8;
    Pbpcmbuffer = new BYTE [npcmbuffersize];

    Pbaacbuffer = new BYTE [nmaxoutputbytes];
    (2.1) Get current encoding Configuration pconfiguration = Faacencgetcurrentconfiguration (Hencoder);

    Pconfiguration->inputformat = Faac_input_16bit;

    (2.2) Set encoding Configuration nret = Faacencsetconfiguration (Hencoder, pconfiguration); for (int i = 0; 1; i++) {//read in the actual number of bytes, the maximum will not exceed npcmbuffersize, generally only read to the end of the file is not this value Nbytesread = fread (pbpcmbuf

        Fer, 1, npcmbuffersize, Fpin);
        Enter the number of samples, with the actual read-in bytes calculated, generally only read the end of the file is not npcmbuffersize/(NPCMBITSIZE/8);

        Ninputsamples = Nbytesread/(NPCMBITSIZE/8); (3) Encode nret = Faacencencode (Hencoder, (int*) Pbpcmbuffer, Ninputsamples, Pbaacbuffer, Nmaxoutputby

        TES);

        Fwrite (Pbaacbuffer, 1, nret, fpout);

        printf ("%d:faacencencode returns%d\n", I, nret); if (Nbytesread &Lt;= 0) {break; }}/* while (1) {//(3) Flushing nret = Faacencencode (Hencoder, (int*) pbpcmbuf

        Fer, 0, Pbaacbuffer, nmaxoutputbytes);
        if (nret <= 0) {break;

    }} *//(4) Close FAAC engine nret = Faacencclose (Hencoder);
    Delete[] Pbpcmbuffer;
    Delete[] Pbaacbuffer;
    Fclose (Fpin);

    Fclose (fpout);

    GetChar ();
return 0;
 }
4.2 Compiling and running

Save the above code as a "pcm2aac.cpp" file, and then compile:

g++ Pcm2aac.cpp-o Pcm2aac-l/usr/local/lib-lfaac-i/usr/local/include

Run:

./PCM2AAC

Then you generate the Out.aac file, and listen and see. ~ 5 Reference AUDIOCODING.COM-FAAC Dogfoot–재밌는개발

-

Reprint please indicate the CSDN blog from Liu: blog.csdn.net/poechant

Related Article

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.