Ffmpeg x264 video coding and FAAC audio coding (IV). These details should be paid attention to in the Android interview

int s = x264_encoder_headers(enc, &nals, &nal_count);

x264 code :

Initialize picture information :

x264_picture_t picin, picout;


Set the picture information parameters :

picin.i_pts = ts;

picin.i_type = X264_TYPE_AUTO;

picin.i_qpplus1 = 0;

picin.img.i_csp = (csp == 17) ? X264_CSP_NV12 : csp;//special hack for android




Frame x264 code :

ret = x264_encoder_encode(enc, &nals, &nnal, &picin, &picout);


(1) Initialize and set x264_param_t

(2) Initialize and set x264_picture_t

(3)x264 code

Two 、 Audio codec · Actual combat WAV Transferred to AAC(AAC code )


Here use FAAC To achieve AAC code . in addition ,WAV The data segment is PCM, There will be a lot of code PCM abbreviation .

1 Download and install FAAC

The installation process here is Mac and Linux Implemented on the ,Windows Similar reference can be made to .

wget downloads.sourceforge.net/faac/faac-1.28.tar.gz

tar zxvf faac-1.28.tar.gz

cd faac-1.28



sudo make install

If you just use the default configure Medium prefix path, So after installation lib and .h The documents are in /usr/local/lib and /usr/local/include, It will be used when compiling later .


2.1 Open FAAC engine


faacEncHandle faacEncOpen

// Return to one FAAC Of handle(

unsigned long nSampleRate, // Sampling rate , The unit is bps

unsigned long nChannels, // channel ,1 For mono ,2 Dual channel

unsigned long &nInputSamples, // The reference , Get the length of the original data that should be received each time the encoding is called

unsigned long &nMaxOutputBytes // The reference , Get the code generated each time the code is called AAC Maximum length of data );

2.2 Get/Set encoding configuration


Get the encoder configuration :

faacEncConfigurationPtr faacEncGetCurrentConfiguration // Get a pointer to the current encoder configuration


faacEncHandle hEncoder // FAAC Of handle


Set the encoder configuration :

int FAACAPI faacEncSetConfiguration


faacDecHandle hDecoder, // Previously obtained FAAC Of handle

faacEncConfigurationPtr config // FAAC Encoder configuration


2.3 Encode


int faacEncEncode


faacEncHandle hEncoder, // FAAC Of handle

short *inputBuffer, // WAV Raw data

unsigned int samplesInput, // call faacEncOpen When you get nInputSamples value

unsigned char *outputBuffer,// At least have the ability to call faacEncOpen When you get nMaxOutputBytes Byte long buffer

unsigned int bufferSize // outputBuffer The actual size of the buffer


2.4 Close FAAC engine


void faacEncClose


faacEncHandle hEncoder // Previously obtained FAAC handle


3 technological process

3.1 What to prepare for ?

Sampling rate , Track number ( Dual or mono ?), And yours WAV A single sample of is 8 It’s still 16 Bit ?

3.2 Turn on FAAC Encoder , Prepare for coding

  1. call faacEncOpen Turn on FAAC After the encoder , The number of single input samples nInputSamples And the maximum number of bytes of output data nMaxOutputBytes;

  2. according to nInputSamples and nMaxOutputBytes, Respectively WAV Data and what will be obtained AAC Data creation buffer ;

  3. call faacEncGetCurrentConfiguration Get the current configuration , After modifying the configuration , call faacEncSetConfiguration Set up a new configuration .

3.3 Start coding

call faacEncEncode, What should be prepared was just ready , It’s simple .

3.4 aftermath

Turn off the encoder , Also, don’t forget to release the buffer , If a file stream is used , And don’t forget to close .

4 The test program

4.1 Complete code

take WAV Format audio file /home/michael/Development/testspace/in.wav Transferred 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; // Sampling rate

UINT nChannels = 1; // Track number

UINT nPCMBitSize = 16; // Single sample digits

ULONG nInputSamples = 0;

ULONG nMaxOutputBytes = 0;

int nRet;

faacEncHandle hEncoder;

faacEncConfigurationPtr pConfiguration;

int nBytesRead;

int nPCMBufferSize;

BYTE* pbPCMBuffer;

BYTE* pbAACBuffer;

FILE* fpIn; // WAV file for input

FILE* fpOut; // AAC file for output

fpIn = fopen(“/home/michael/Development/testspace/in.wav”, “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++)


// Actual bytes read in , Not more than nPCMBufferSize, Generally, it is not this value only when reading the end of the file

nBytesRead = fread(pbPCMBuffer, 1, nPCMBufferSize, fpIn);

// Enter the number of samples , Use the actual number of bytes read to calculate , Generally, it is not… Only when you read the end of the file nPCMBufferSize/(nPCMBitSize/8);

nInputSamples = nBytesRea

《Android Summary of learning notes + Latest mobile architecture video + Big Android interview questions + Project actual combat source code handout 》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 Full content open source sharing

d / (nPCMBitSize / 8);

// (3) Encode

nRet = faacEncEncode(

hEncoder, (int*) pbPCMBuffer, nInputSamples, pbAACBuffer, nMaxOutputBytes);

fwrite(pbAACBuffer, 1, nRet, fpOut);

printf(“%d: faacEncEncode returns %dn”, i, nRet);

if(nBytesRead <= 0)

Read more here: Source link