先看代码吧,有代码有真相,具体代码的demo(下载demo的朋友请勿在网上上传我的demo,谢谢)下载连接为:
http://download.csdn.net/detail/skdkjxy/8071721
注:代码demo是一个完整的工程,直接进行make x86或者make arm 就可以到目录src下运行可执行文件了。
[cpp] view plain copy
- <span style="font-family:Courier New;font-size:12px;">
- </span>
[cpp] view plain copy
- <span style="font-family:Courier New;font-size:12px;">#include <fcntl.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include <unistd.h>
- #include <signal.h>
- #include <dirent.h>
- #include <pthread.h>
- #include <asm/types.h>
- #include <arpa/inet.h>
- #include <sys/vfs.h>
- #include <sys/time.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/utsname.h>
- #include <netdb.h>
- #include <net/if.h>
- #include <netinet/in.h>
- #include <net/route.h>
- #include <net/if_arp.h>
- #include <linux/fs.h>
- #include <linux/sockios.h>
- #include <linux/netlink.h>
- #include <linux/rtnetlink.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netinet/ether.h>
- #include <netinet/ip_icmp.h>
- #include "faac.h"
- #include "mp4v2.h"
- #define JOSEPH_G711A_LOCATION "../av_file/test1.g711a"
- #define JOSEPH_H264_LOCALTION "../av_file/"
- #define JOSEPH_MP4_FILE "test.mp4"
- #define MP4_DETAILS_ALL 0xFFFFFFFF
- #define NALU_SPS 0
- #define NALU_PPS 1
- #define NALU_I 2
- #define NALU_P 3
- #define NALU_SET 4
- typedef unsigned int uint32_t;
- typedef unsigned char uint8_t;
- typedef struct Joseph_Acc_Config
- {
- FILE* fpIn; //打开的音频文件
- faacEncHandle hEncoder; //音频文件描述符
- unsigned long nSampleRate; //音频采样数
- unsigned int nChannels; //音频声道数
- unsigned int nPCMBitSize; //音频采样精度
- unsigned long nInputSamples; //每次调用编码时所应接收的原始数据长度
- unsigned long nMaxOutputBytes; //每次调用编码时生成的AAC数据的最大长度
- unsigned char* pbPCMBuffer; //pcm数据
- unsigned char* pbAACBuffer; //aac数据
- }JOSEPH_ACC_CONFIG;
- typedef struct Joseph_Mp4_Config
- {
- FILE* fpInVideo; //打开的视频文件
- MP4FileHandle hFile; //mp4文件描述符
- MP4TrackId video; //视频轨道标志符
- MP4TrackId audio; //音频轨道标志符
- int m_vFrameDur; //帧间隔时间
- unsigned int timeScale; //视频每秒的ticks数,如90000
- unsigned int fps; //视频帧率
- unsigned short width; //视频宽
- unsigned short height; //视频高
- }JOSEPH_MP4_CONFIG;
- /********************************************************g711a encode decode**********************************************/
- static const int16_t alawtos16[256] =
- {
- -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
- -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
- -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
- -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
- -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
- -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
- -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
- -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
- -344, -328, -376, -360, -280, -264, -312, -296,
- -472, -456, -504, -488, -408, -392, -440, -424,
- -88, -72, -120, -104, -24, -8, -56, -40,
- -216, -200, -248, -232, -152, -136, -184, -168,
- -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
- -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
- -688, -656, -752, -720, -560, -528, -624, -592,
- -944, -912, -1008, -976, -816, -784, -880, -848,
- 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
- 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
- 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
- 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
- 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
- 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
- 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
- 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
- 344, 328, 376, 360, 280, 264, 312, 296,
- 472, 456, 504, 488, 408, 392, 440, 424,
- 88, 72, 120, 104, 24, 8, 56, 40,
- 216, 200, 248, 232, 152, 136, 184, 168,
- 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
- 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
- 688, 656, 752, 720, 560, 528, 624, 592,
- 944, 912, 1008, 976, 816, 784, 880, 848
- };
- static const int8_t alaw_encode[2049] =
- {
- 0xD5, 0xD4, 0xD7, 0xD6, 0xD1, 0xD0, 0xD3, 0xD2, 0xDD, 0xDC, 0xDF, 0xDE,
- 0xD9, 0xD8, 0xDB, 0xDA, 0xC5, 0xC4, 0xC7, 0xC6, 0xC1, 0xC0, 0xC3, 0xC2,
- 0xCD, 0xCC, 0xCF, 0xCE, 0xC9, 0xC8, 0xCB, 0xCA, 0xF5, 0xF5, 0xF4, 0xF4,
- 0xF7, 0xF7, 0xF6, 0xF6, 0xF1, 0xF1, 0xF0, 0xF0, 0xF3, 0xF3, 0xF2, 0xF2,
- 0xFD, 0xFD, 0xFC, 0xFC, 0xFF, 0xFF, 0xFE, 0xFE, 0xF9, 0xF9, 0xF8, 0xF8,
- 0xFB, 0xFB, 0xFA, 0xFA, 0xE5, 0xE5, 0xE5, 0xE5, 0xE4, 0xE4, 0xE4, 0xE4,
- 0xE7, 0xE7, 0xE7, 0xE7, 0xE6, 0xE6, 0xE6, 0xE6, 0xE1, 0xE1, 0xE1, 0xE1,
- 0xE0, 0xE0, 0xE0, 0xE0, 0xE3, 0xE3, 0xE3, 0xE3, 0xE2, 0xE2, 0xE2, 0xE2,
- 0xED, 0xED, 0xED, 0xED, 0xEC, 0xEC, 0xEC, 0xEC, 0xEF, 0xEF, 0xEF, 0xEF,
- 0xEE, 0xEE, 0xEE, 0xEE, 0xE9, 0xE9, 0xE9, 0xE9, 0xE8, 0xE8, 0xE8, 0xE8,
- 0xEB, 0xEB, 0xEB, 0xEB, 0xEA, 0xEA, 0xEA, 0xEA, 0x95, 0x95, 0x95, 0x95,
- 0x95, 0x95, 0x95, 0x95, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
- 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x96, 0x96, 0x96, 0x96,
- 0x96, 0x96, 0x96, 0x96, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
- 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x93, 0x93, 0x93, 0x93,
- 0x93, 0x93, 0x93, 0x93, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
- 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9C, 0x9C, 0x9C, 0x9C,
- 0x9C, 0x9C, 0x9C, 0x9C, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
- 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x99, 0x99, 0x99, 0x99,
- 0x99, 0x99, 0x99, 0x99, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
- 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9A, 0x9A, 0x9A, 0x9A,
- 0x9A, 0x9A, 0x9A, 0x9A, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
- 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x84, 0x84, 0x84, 0x84,
- 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
- 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
- 0x87, 0x87, 0x87, 0x87, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
- 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x81, 0x81, 0x81, 0x81,
- 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
- 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
- 0x80, 0x80, 0x80, 0x80, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
- 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x82, 0x82, 0x82, 0x82,
- 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
- 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
- 0x8D, 0x8D, 0x8D, 0x8D, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
- 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8F, 0x8F, 0x8F, 0x8F,
- 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
- 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
- 0x8E, 0x8E, 0x8E, 0x8E, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
- 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x88, 0x88, 0x88, 0x88,
- 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
- 0x8B, 0x8B, 0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 0xAB, 0xAB, 0xAB,
- 0xAB, 0xAB, 0xAB, 0xAB, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
- 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
- 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
- 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
- 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
- 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0x2A
- };
- int g711a_decode(void *pout_buf, int *pout_len, const void *pin_buf, const int in_len)
- {
- int16_t *dst = (int16_t *)pout_buf;
- uint8_t *src = (uint8_t *)pin_buf;
- uint32_t i = 0;
- int Ret = 0;
- if ( (NULL == pout_buf)
- || (NULL == pout_len)
- || (NULL == pin_buf)
- || (0 == in_len) )
- {
- return -1;
- }
- if ( *pout_len < 2 * in_len )
- {
- return -2;
- }
- for( i = 0; i < in_len; i++ )
- {
- *(dst++) = alawtos16[*(src++)];
- }
- Ret = 2 * in_len;
- return Ret;
- }
- int g711a_encode(void *pout_buf, int *pout_len, const void *pin_buf, const int in_len)
- {
- int8_t *dst = (int8_t *)pout_buf;
- int16_t *src = (int16_t *)pin_buf;
- uint32_t i = 0;
- int Ret = 0;
- if ( (NULL == pout_buf)
- || (NULL == pout_len)
- || (NULL == pin_buf)
- || (0 == in_len) )
- {
- return -1;
- }
- if ( *pout_len < in_len / 2 )
- {
- return -2;
- }
- for( i = 0; i < in_len / 2; i++ )
- {
- int16_t s = *(src++);
- if( s >= 0)
- {
- *(dst++) = alaw_encode[s / 16];
- }
- else
- {
- *(dst++) = 0x7F & alaw_encode[s / -16];
- }
- }
- Ret = in_len / 2;
- return Ret;
- }
- int joseph_get_g711a_frame(JOSEPH_MP4_CONFIG* joseph_mp4_config, unsigned char *nVideoBuffer, int nVideoRet)
- {
- int nVideoSize = 0;
- char nVideoNum[32] = {0};
- memset(nVideoNum, 0, 32);
- sprintf(nVideoNum, "%s%d", JOSEPH_H264_LOCALTION, nVideoRet);
- joseph_mp4_config->fpInVideo = fopen(nVideoNum, "rb");
- memset(nVideoBuffer, 0, 1048576);
- nVideoSize = fread(nVideoBuffer, 1, 1048576, joseph_mp4_config->fpInVideo);
- fclose(joseph_mp4_config->fpInVideo);
- return nVideoSize;
- }
- /***********************************************************mp4 encode***********************************************/
- JOSEPH_MP4_CONFIG* InitMp4Encoder(JOSEPH_ACC_CONFIG* joseph_aac_config)
- {
- JOSEPH_MP4_CONFIG *joseph_mp4_config = NULL;
- joseph_mp4_config =(JOSEPH_MP4_CONFIG *)malloc(sizeof(JOSEPH_MP4_CONFIG));
- joseph_mp4_config->m_vFrameDur = 0;
- joseph_mp4_config->video = MP4_INVALID_TRACK_ID;
- joseph_mp4_config->audio = MP4_INVALID_TRACK_ID;
- joseph_mp4_config->hFile = NULL;
- joseph_mp4_config->timeScale = 90000;
- joseph_mp4_config->fps = 25;
- joseph_mp4_config->width = 640;
- joseph_mp4_config->height = 480;
- /*file handle*/
- joseph_mp4_config->hFile = MP4Create(JOSEPH_MP4_FILE, 0);
- if(joseph_mp4_config->hFile == MP4_INVALID_FILE_HANDLE)
- {
- printf("open file fialed.\n");
- return NULL;
- }
- MP4SetTimeScale(joseph_mp4_config->hFile, joseph_mp4_config->timeScale); //timeScale
- /*audio track*/
- joseph_mp4_config->audio = MP4AddAudioTrack(joseph_mp4_config->hFile, joseph_aac_config->nSampleRate, \
- joseph_aac_config->nInputSamples, MP4_MPEG4_AUDIO_TYPE);
- if(joseph_mp4_config->audio == MP4_INVALID_TRACK_ID)
- {
- printf("add audio track failed.\n");
- return NULL;
- }
- MP4SetAudioProfileLevel(joseph_mp4_config->hFile, 0x2);
- unsigned char aacConfig[2] = {0x15, 0x88}; // 0001 0101 1000 1000
- MP4SetTrackESConfiguration(joseph_mp4_config->hFile, joseph_mp4_config->audio, aacConfig, 2);
- return joseph_mp4_config;
- }
- //------------------------------------------------------------------------------------------------- Mp4Encode说明
- // 【h264编码出的NALU规律】
- // 第一帧 SPS【0 0 0 1 0x67】 PPS【0 0 0 1 0x68】 SEI【0 0 0 1 0x6】 IDR【0 0 0 1 0x65】
- // p帧 P【0 0 0 1 0x61】
- // I帧 SPS【0 0 0 1 0x67】 PPS【0 0 0 1 0x68】 IDR【0 0 0 1 0x65】
- // 【mp4v2封装函数MP4WriteSample】
- // 此函数接收I/P nalu,该nalu需要用4字节的数据大小头替换原有的起始头,并且数据大小为big-endian格式
- //-------------------------------------------------------------------------------------------------
- int Mp4VEncode(JOSEPH_MP4_CONFIG* joseph_mp4_config, unsigned char* naluData, int naluSize)
- {
- int index = -1;
- if(naluData[0]==0 && naluData[1]==0 && naluData[2]==0 && naluData[3]==1 && naluData[4]==0x67)
- {
- index = NALU_SPS;
- printf("%s[%d]====NALU_SPS\n",__FUNCTION__,__LINE__);
- }
- if(index!=NALU_SPS && joseph_mp4_config->video == MP4_INVALID_TRACK_ID)
- {
- return -1;
- }
- if(naluData[0]==0 && naluData[1]==0 && naluData[2]==0 && naluData[3]==1 && naluData[4]==0x68)
- {
- index = NALU_PPS;
- printf("%s[%d]====NALU_PPS\n",__FUNCTION__,__LINE__);
- }
- if(naluData[0]==0 && naluData[1]==0 && naluData[2]==0 && naluData[3]==1 && naluData[4]==0x65)
- {
- index = NALU_I;
- printf("%s[%d]====NALU_I\n",__FUNCTION__,__LINE__);
- }
- if(naluData[0]==0 && naluData[1]==0 && naluData[2]==0 && naluData[3]==1 && naluData[4]==0x61)
- {
- index = NALU_P;
- printf("%s[%d]====NALU_P\n",__FUNCTION__,__LINE__);
- }
- if(naluData[0]==0 && naluData[1]==0 && naluData[2]==0 && naluData[3]==1 && naluData[4]==0x6)
- {
- index = NALU_SET;
- printf("%s[%d]====NALU_SET\n",__FUNCTION__,__LINE__);
- }
- switch(index)
- {
- case NALU_SPS:
- if(joseph_mp4_config->video == MP4_INVALID_TRACK_ID)
- {
- joseph_mp4_config->video = MP4AddH264VideoTrack
- (joseph_mp4_config->hFile,
- joseph_mp4_config->timeScale, //timeScale
- (joseph_mp4_config->timeScale / joseph_mp4_config->fps), //sampleDuration timeScale/fps
- joseph_mp4_config->width, // width
- joseph_mp4_config->height, // height
- naluData[5], // sps[1] AVCProfileIndication
- naluData[6], // sps[2] profile_compat
- naluData[7], // sps[3] AVCLevelIndication
- 3); // 4 bytes length before each NAL unit
- if(joseph_mp4_config->video == MP4_INVALID_TRACK_ID)
- {
- printf("add video track failed.\n");
- return -1;
- }
- //MP4SetVideoProfileLevel(joseph_mp4_config->hFile, 1); // Simple Profile @ Level 3 mp4编码标准
- MP4SetVideoProfileLevel(joseph_mp4_config->hFile, 0x7F); // Simple Profile @ Level 3
- }
- MP4AddH264SequenceParameterSet(joseph_mp4_config->hFile, joseph_mp4_config->video, naluData+4, naluSize-4);
- break;
- case NALU_PPS:
- MP4AddH264PictureParameterSet(joseph_mp4_config->hFile, joseph_mp4_config->video, naluData+4, naluSize-4);
- break;
- case NALU_SET:
- MP4AddH264PictureParameterSet(joseph_mp4_config->hFile, joseph_mp4_config->video, naluData+4, naluSize-4);
- break;
- case NALU_I:
- {
- unsigned char* IFrameData = (unsigned char*)malloc((naluSize)* sizeof(unsigned char));
- IFrameData[0] = (naluSize-4) >>24;
- IFrameData[1] = (naluSize-4) >>16;
- IFrameData[2] = (naluSize-4) >>8;
- IFrameData[3] = (naluSize-4) &0xff;
- memcpy(IFrameData+4, naluData+4, naluSize-4);
- //if(!MP4WriteSample(joseph_mp4_config->hFile, joseph_mp4_config->video, IFrameData, naluSize+1, m_vFrameDur/8000*90000, 0, 1))
- if(!MP4WriteSample(joseph_mp4_config->hFile, joseph_mp4_config->video, IFrameData, naluSize, MP4_INVALID_DURATION, 0, 1))
- {
- return -1;
- }
- //joseph_mp4_config->m_vFrameDur = 0;
- free(IFrameData);
- IFrameData = NULL;
- break;
- }
- case NALU_P:
- {
- naluData[0] = (naluSize-4) >>24;
- naluData[1] = (naluSize-4) >>16;
- naluData[2] = (naluSize-4) >>8;
- naluData[3] = (naluSize-4) &0xff;
- //if(!MP4WriteSample(joseph_mp4_config->hFile, joseph_mp4_config->video, naluData, naluSize, m_vFrameDur/8000*90000, 0, 1))
- if(!MP4WriteSample(joseph_mp4_config->hFile, joseph_mp4_config->video, naluData, naluSize, MP4_INVALID_DURATION, 0, 1))
- {
- return -1;
- }
- //joseph_mp4_config->m_vFrameDur = 0;
- break;
- }
- default:
- break;
- }
- return 0;
- }
- int Mp4AEncode(JOSEPH_MP4_CONFIG* joseph_mp4_config, unsigned char* aacData, int aacSize)
- {
- if(joseph_mp4_config->video == MP4_INVALID_TRACK_ID)
- {
- return -1;
- }
- MP4WriteSample(joseph_mp4_config->hFile, joseph_mp4_config->audio, aacData, aacSize , MP4_INVALID_DURATION, 0, 1);
- //joseph_mp4_config->m_vFrameDur += 1024;
- return 0;
- }
- void CloseMp4Encoder(JOSEPH_MP4_CONFIG* joseph_mp4_config)
- {
- if(joseph_mp4_config->hFile)
- {
- MP4Close(joseph_mp4_config->hFile, 0);
- joseph_mp4_config->hFile = NULL;
- }
- return ;
- }
- /*************************************************aac encode******************************************/
- JOSEPH_ACC_CONFIG* InitAccEncoder(void)
- {
- JOSEPH_ACC_CONFIG* joseph_aac_config = NULL;
- faacEncConfigurationPtr pConfiguration;
- int nRet = 0;
- int nPCMBufferSize = 0;
- joseph_aac_config = (JOSEPH_ACC_CONFIG*)malloc(sizeof(JOSEPH_ACC_CONFIG));
- joseph_aac_config->nSampleRate = 8000;
- joseph_aac_config->nChannels = 1;
- joseph_aac_config->nPCMBitSize = 16;
- joseph_aac_config->nInputSamples = 0;
- joseph_aac_config->nMaxOutputBytes = 0;
- joseph_aac_config->fpIn = fopen(JOSEPH_G711A_LOCATION, "rb");
- //open FAAC engine
- joseph_aac_config->hEncoder = faacEncOpen(joseph_aac_config->nSampleRate, joseph_aac_config->nChannels, \
- &joseph_aac_config->nInputSamples, &joseph_aac_config->nMaxOutputBytes);
- if(joseph_aac_config->hEncoder == NULL)
- {
- printf("failed to call faacEncOpen()\n");
- return NULL;
- }
- nPCMBufferSize = (joseph_aac_config->nInputSamples*(joseph_aac_config->nPCMBitSize/8));
- joseph_aac_config->pbPCMBuffer=(unsigned char*)malloc(nPCMBufferSize*sizeof(unsigned char));
- memset(joseph_aac_config->pbPCMBuffer, 0, nPCMBufferSize);
- joseph_aac_config->pbAACBuffer=(unsigned char*)malloc(joseph_aac_config->nMaxOutputBytes*sizeof(unsigned char));
- memset(joseph_aac_config->pbAACBuffer, 0, joseph_aac_config->nMaxOutputBytes);
- //GET current encoding configuration
- pConfiguration = faacEncGetCurrentConfiguration(joseph_aac_config->hEncoder);
- #if 1
- pConfiguration->inputFormat = FAAC_INPUT_16BIT;
- pConfiguration->outputFormat = 0;
- pConfiguration->aacObjectType = LOW;
- #else
- pConfiguration->inputFormat = FAAC_INPUT_16BIT;
- /*0 - raw; 1 - ADTS*/
- pConfiguration->outputFormat = 0;
- pConfiguration->useTns = 0;
- pConfiguration->allowMidside = 1;
- pConfiguration->shortctl = SHORTCTL_NORMAL;
- pConfiguration->aacObjectType = LOW;
- pConfiguration->mpegVersion = MPEG2;
- #endif
- //set encoding configuretion
- nRet = faacEncSetConfiguration(joseph_aac_config->hEncoder, pConfiguration);
- return joseph_aac_config;
- }
- void CloseAccEncoder(JOSEPH_ACC_CONFIG* joseph_aac_config)
- {
- if(joseph_aac_config->hEncoder)
- {
- faacEncClose(joseph_aac_config->hEncoder);
- joseph_aac_config->hEncoder = NULL;
- }
- return ;
- }
- /*********************************************main**************************************************/
- int main(int argc, char* argv[])
- {
- JOSEPH_ACC_CONFIG *joseph_aac_config = NULL;
- JOSEPH_MP4_CONFIG *joseph_mp4_config = NULL;
- int nRet = 0;
- int nTmp = 0;
- int nCount = 0;
- int nStatus = 0;
- int PCMSize = 320;
- int nPCMRead = 0;
- int nVideoRet = 0;
- int gBytesRead = 0;
- int nVideoSize = 0;
- int nPCMBufferSize = 0;
- unsigned char nVideoBuffer[1048576] = {0};
- unsigned char *pPCM = NULL;
- unsigned char *pbG711ABuffer = NULL;
- unsigned char *pbPCMTmpBuffer = NULL;
- pbG711ABuffer = (unsigned char *)malloc(164 *sizeof(unsigned char));
- memset(pbG711ABuffer, 0, 164);
- pbPCMTmpBuffer = (unsigned char *)malloc(PCMSize *sizeof(unsigned char));
- memset(pbPCMTmpBuffer, 0, PCMSize);
- /*init aac */
- if((joseph_aac_config=InitAccEncoder()) == NULL)
- {
- printf("init aac failed\n");
- return -1;
- }
- /*init mp4*/
- if((joseph_mp4_config=InitMp4Encoder(joseph_aac_config)) == NULL)
- {
- printf("init mp4 failed\n");
- return -1;
- }
- nPCMBufferSize = (joseph_aac_config->nInputSamples*(joseph_aac_config->nPCMBitSize/8));
- /*write video audio frame*/
- while(((gBytesRead = fread(pbG711ABuffer, 1, 164, joseph_aac_config->fpIn)) >0) && (nVideoRet < 312))
- {
- nStatus = 0;
- /*hisi audio the first 4 bytes is error*/
- pPCM = pbG711ABuffer + 4;
- memset(pbPCMTmpBuffer, 0, PCMSize);
- memset(joseph_aac_config->pbAACBuffer, 0, joseph_aac_config->nMaxOutputBytes);
- /*g711 to pcm*/
- if((nPCMRead = g711a_decode(pbPCMTmpBuffer, &PCMSize, pPCM, gBytesRead-4)) < 0)
- {
- printf(" G711A -> PCM fail\n");
- break;
- }
- /*pcm to aac*/
- if((nPCMBufferSize - nCount) < nPCMRead)
- {
- nStatus = 1;
- memcpy((joseph_aac_config->pbPCMBuffer + nCount), pbPCMTmpBuffer, (nPCMBufferSize - nCount));
- joseph_aac_config->nInputSamples = (nPCMBufferSize / (joseph_aac_config->nPCMBitSize / 8));
- nRet = faacEncEncode(joseph_aac_config->hEncoder, (int*) (joseph_aac_config->pbPCMBuffer), \
- joseph_aac_config->nInputSamples, joseph_aac_config->pbAACBuffer, joseph_aac_config->nMaxOutputBytes);
- #if 1
- /*get video frame*/
- nVideoSize = joseph_get_g711a_frame(joseph_mp4_config, nVideoBuffer, nVideoRet);
- if(nVideoSize < 0)
- {
- printf("read g711a frame failed\n");
- break;
- }
- nVideoRet++;
- /*write video frame to mp4*/
- if((Mp4VEncode(joseph_mp4_config, nVideoBuffer, nVideoSize)) < 0)
- {
- printf("write video frame failed\n");
- break;
- }
- #endif
- #if 1
- /*write audio frame to mp4*/
- if((Mp4AEncode(joseph_mp4_config, joseph_aac_config->pbAACBuffer, nRet)) < 0)
- {
- printf("write audio frame failed\n");
- break;
- }
- #endif
- /*Treatment of redundant frames*/
- nTmp = (nPCMRead - (nPCMBufferSize - nCount));
- memset(joseph_aac_config->pbPCMBuffer, 0, nPCMBufferSize);
- memcpy(joseph_aac_config->pbPCMBuffer, (pbPCMTmpBuffer + (nPCMBufferSize-nCount)), nTmp);
- nCount = 0;
- nCount += nTmp;
- }
- if( nStatus == 0)
- {
- memcpy(joseph_aac_config->pbPCMBuffer + nCount, pbPCMTmpBuffer, nPCMRead);
- nCount += nPCMRead;
- }
- if(nPCMRead < 320)
- {
- joseph_aac_config->nInputSamples = (nCount / (joseph_aac_config->nPCMBitSize / 8));
- nRet = faacEncEncode(joseph_aac_config->hEncoder, (int*) (joseph_aac_config->pbPCMBuffer), \
- joseph_aac_config->nInputSamples, joseph_aac_config->pbAACBuffer, joseph_aac_config->nMaxOutputBytes);
- #if 1
- /*get video frame*/
- nVideoSize = joseph_get_g711a_frame(joseph_mp4_config, nVideoBuffer, nVideoRet);
- if(nVideoSize < 0)
- {
- printf("read g711a frame failed\n");
- break;
- }
- nVideoRet++;
- /*write video frame to mp4*/
- if((Mp4VEncode(joseph_mp4_config, nVideoBuffer, nVideoSize)) < 0)
- {
- printf("write video frame failed\n");
- break;
- }
- #endif
- #if 1
- /*write audio frame to mp4*/
- if((Mp4AEncode(joseph_mp4_config, joseph_aac_config->pbAACBuffer, nRet)) < 0)
- {
- printf("write audio frame failed\n");
- break;
- }
- #endif
- }
- memset(pbG711ABuffer, 0, 164);
- }
- /*Close FAAC engine*/
- CloseAccEncoder(joseph_aac_config);
- CloseMp4Encoder(joseph_mp4_config);
- /*close file fp*/
- fclose(joseph_aac_config->fpIn);
- /*free the source of malloc*/
- //audio part
- free(joseph_aac_config->pbPCMBuffer);
- joseph_aac_config->pbPCMBuffer = NULL;
- free(joseph_aac_config->pbAACBuffer);
- joseph_aac_config->pbAACBuffer = NULL;
- free(pbG711ABuffer);
- pbG711ABuffer = NULL;
- free(pbPCMTmpBuffer);
- pbPCMTmpBuffer = NULL;
- free(joseph_aac_config);
- joseph_aac_config = NULL;
- //video part
- free(joseph_mp4_config);
- joseph_mp4_config = NULL;
- printf("%s:[%d] The sys changover succeed !\n",__FUNCTION__,__LINE__);
- return 0;
- }</span>
时间: 2024-09-18 02:42:29