嵌入式 hi3518x平台h264+g711a封装mp4代码demo

这篇博客分享了一个基于hi3518x平台的H264和G711A音频封装到MP4的代码demo。提供的完整工程可以直接在x86或arm环境下编译运行,通过make命令生成可执行文件。
摘要由CSDN通过智能技术生成

先看代码吧,有代码有真相,具体代码的demo(下载demo的朋友请勿在网上上传我的demo,谢谢)下载连接为:

http://download.csdn.net/detail/skdkjxy/8071721

注:代码demo是一个完整的工程,直接进行make x86或者make arm 就可以到目录src下运行可执行文件了。

<span style="font-family:Courier New;font-size:12px;">
</span>
<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, 0x8
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
G.711A是一种音频编码标准,常用于音频的数字化和压缩,尤其是在电话通信中。它属于脉冲编码调制(PCM)技术的一部分,是国际电信联盟(ITU)推荐的几种标准之一。G.711A编码器在将模拟信号转换为数字信号的过程中使用了A律(A-Law)算法进行非线性压缩,以提高信号的动态范围。 解封装G.711A编码的音频数据通常包括以下步骤: 1. 接收G.711A编码的音频流。 2. 检查每个8位样本,因为G.711A音频数据是以8位样本的形式存在。 3. 应用A律解压缩算法将每个8位样本转换为13位的PCM数据。 4. 由于每个G.711A样本是8位,而解压缩后的数据是13位,因此需要对这些样本进行适当的位移和格式转换。 5. 完成上述步骤后,得到的PCM数据可以进一步用于数字信号处理、播放或其他用途。 以下是一个简化的伪代码示例,用于说明如何对G.711A编码的音频数据进行解封装: ```pseudo function G711ADecodeSample(sample) // A律解压缩算法 if sample < 0x80 // 对于0x00到0x7F之间的样本,进行A律扩展 // 这里省略了具体的A律解压算法实现细节 uncompressed_sample = A_Law_Expansion(sample) else // 对于0x80到0xFF之间的样本,进行符号扩展和位反转 // 这里省略了具体的符号扩展和位反转处理细节 uncompressed_sample = Sign_Extend_Invert_Bit(sample) return uncompressed_sample end function function G711ADecodeStream(stream) // 解封装整个G.711A编码的音频流 decoded_stream = [] for each sample in stream decoded_sample = G711ADecodeSample(sample) decoded_stream.append(decoded_sample) return decoded_stream end function ``` 这个示例中省略了A律解压缩的具体算法细节,因为实际的解压缩过程相对复杂,涉及到位操作和非线性映射。在实际应用中,通常会使用专门的音频处理库来处理这种转换。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值