ES package stream PS

https://blog.csdn.net/qq_24977505/article/details/95193041

 

A recent national standard cascade, tinker with a simple ps flow package, do share notes.

#include <stdint.h>
#include <string>
#include <memory.h>

#define H264_ID        0x1b
#define H265_ID        0x24
#define MPEG_ID      0x10
#define SVACV_ID    0x80

#define G711_ID        0x90
#define SVACA_ID    0x9b

uint8_t PS_HEAD const [] = {
    / * Head the PS * /
    0x00, 0x00, 0x01, 0xBA,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, / * timestamp * /
    0x01, 0x47, 0xb3, 
    0xF8
};

uint8_t SYS_MAP_HEAD const [] = {
    / * PS_SYS header * /
    0x00, 0x00, 0x01, 0xbb, 
    0x00, 0x0C, / * SYS header length, excluding himself, 6 * 3 + stream number * /
    0x80, 0xA3, 0xD9, / * rate * /
    0x04, 0xE1, / * number of audio streams, video streams identification number 1 plus 3 * /
    0xFF, / ** /
    0xB9, 0xE0, 0x00, 0xB8, 0xC0, 0x40, / * flow information, b9 video , b8 audio * /
    / * header PS_MAP * /
    0x00, 0x00, 0x01, 0xbc, 
    0x00, 0x12, / * length PSM * /
    0x04, 0xFF, / ** /
    0x00, 0x00, 0x00, 0x08, / 2-way fixed * flow * /
    0x1b, 0xE0, 0x00, 0x00, / * video, the first byte (0x1b), more different coding changes to a different video stream package, see the beginning of the macro definition * /
    0x90, 0xc0, 0x00, 0x00, / * audio, video with * /
    0x00, 0x00, 0x00, 0x00 / * the CRC 4B, being not set * /
};

const uint8_t PES_HEAD [] = {
    / * PS_PES header * /
    0x00, 0x00, 0x01, 0xE0, 
    0x00, 0x00, / * PES length * /
    0x80, 0xC0, / * additional information * /
    0x0A, / * additional information length * /
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 / * pts * and PDS /
};

void SetHeaderTimeStamp(uint8_t *dest, uint64_t pts)
{
    uint8_t *scr_buf = dest + 4;
    scr_buf[0] = 0x40 | (((uint8_t)(pts >> 30) & 0x07) << 3) | 0x04 | ((uint8_t)(pts >> 28) & 0x03);
    scr_buf[1] = (uint8_t)((pts >> 20) & 0xff);
    scr_buf[2] = (((uint8_t)(pts >> 15) & 0x1f) << 3) | 0x04 | ((uint8_t)(pts >> 13) & 0x03);
    scr_buf[3] = (uint8_t)((pts >> 5) & 0xff);
    scr_buf[4] = (((uint8_t)pts & 0x1f) << 3) | 0x04;
    scr_buf[5] = 1;
}

// 设置PES头中的PTS和DTS字段
void SetPESTimeStamp(uint8_t *buff, uint64_t ts)
{
    buff += 9;
    // PTS
    buff[0] = (uint8_t)(((ts >> 30) & 0x07) << 1) | 0x30 | 0x01;
    buff[1] = (uint8_t)((ts >> 22) & 0xff);
    buff[2] = (uint8_t)(((ts >> 15) & 0xff) << 1) | 0x01;
    buff[3] = (uint8_t)((ts >> 7) & 0xff);
    buff[4] = (uint8_t)((ts & 0xff) << 1) | 0x01;
    // DTS
    buff[5] = (uint8_t)(((ts >> 30) & 0x07) << 1) | 0x10 | 0x01;
    buff[6] = (uint8_t)((ts >> 22) & 0xff);
    buff[7] = (uint8_t)(((ts >> 15) & 0xff) << 1) | 0x01;
    buff[8] = (uint8_t)((ts >> 7) & 0xff);
    buff[9] = (uint8_t)((ts & 0xff) << 1) | 0x01;
}

int GetSinglePESHeader(uint8_t *header, uint64_t mtime, uint16_t farmLen)
{
    farmLen += 13;
    memcpy(header, PES_HEAD, sizeof(PES_HEAD));
    *(header+4) = (uint8_t)(farmLen>>8);
    *(header+5) = (uint8_t)farmLen;

    SetPESTimeStamp(header, mtime);
    return sizeof(PES_HEAD);
}

int GetPSHeader(uint8_t *header, uint64_t mtime, uint16_t farmLen, int streamType, int farmType)
{
    if(streamType == 2)        //语音包
    {
        GetSinglePESHeader(header, mtime, farmLen);
        *(header+3) = 0xc0;
        return sizeof(PES_HEAD);
    }
    else if(farmType == 1)    //I帧
    {
        memcpy(header, PS_HEAD, sizeof(PS_HEAD));
        SetHeaderTimeStamp(header, mtime);
        header += sizeof(PS_HEAD);

        memcpy(header, SYS_MAP_HEAD, sizeof(SYS_MAP_HEAD));
        header += sizeof(SYS_MAP_HEAD);

        GetSinglePESHeader(header, mtime, farmLen);
        return sizeof(PS_HEAD) + sizeof(SYS_MAP_HEAD) + sizeof(PES_HEAD);
    }
    else
    {
        memcpy(header, PS_HEAD, sizeof(PS_HEAD));
        SetHeaderTimeStamp(header, mtime);
        header += sizeof(PS_HEAD);

        GetSinglePESHeader(header, mtime, farmLen);
        return sizeof(PS_HEAD) + sizeof(PES_HEAD);
    }
}

unsigned char PSFrameBuffer [10 * 1024 * 1024]; // convert ps after the frame buffer
int TransPSFrame (pFrame char *, int nFrameLength, nIFrameFlag int, int nStreamType, u_int nTimeStamp)
{
    (!! pFrame || nFrameLength) IF
        return 0;

    // Each pes up to 65400 data
    int PesLenth nFrameLength => 65400 65400:? NFrameLength;    
    // first need to have a pes ps head and other unwanted audio directly packaged pes (00 00 01 C0)
    int psHeadLen = GetPSHeader (PSFrameBuffer , nTimeStamp, PesLenth, nStreamType, nIFrameFlag);
    the memcpy (PSFrameBuffer + psHeadLen, pFrame, PesLenth);
    int = psHeadLen psSize + PesLenth;
    int = PesLenth POD;

    nFrameLength -= PesLenth;
    while (nFrameLength > 0)
    {
        PesLenth = nFrameLength > 65400 ? 65400 : nFrameLength;
        psHeadLen = GetSinglePESHeader(PSFrameBuffer + psSize, nTimeStamp, PesLenth);

        memcpy (PSFrameBuffer + psSize + psHeadLen, pFrame + subject, PesLenth);
        psSize + = (+ PesLenth psHeadLen);    
        below + = PesLenth;
        nFrameLength - = PesLenth;
    }

    //static FILE *fp = fopen("my.ps", "wb");
    //if(fp)
    //    fwrite(PSFrameBuffer, 1, psSize, fp);

    return psSize;
}

TransPSFrame function used in the test, ps frame header and the application data should be written directly obtained rtp packet omitted memcpy process.

SYS_MAP_HEAD data is according to GB standard hard-coded stream ps, if you want to modify data encapsulation format.

Modify SYS_MAP_HEAD [30] and the package 34 and h265 svac svac video and audio, and see note at the beginning of the macro definitions herein

 

Additional various codes corresponding to the encoding counter (SYS_MAP_HEAD [30] and the value [34]):

    PSMUX_ST_RESERVED                   = 0x00,
    PSMUX_ST_VIDEO_MPEG1                = 0x01,
    PSMUX_ST_VIDEO_MPEG2                = 0x02,
    PSMUX_ST_AUDIO_MPEG1                = 0x03,
    PSMUX_ST_AUDIO_MPEG2                = 0x04,
    PSMUX_ST_PRIVATE_SECTIONS           = 0x05,
    PSMUX_ST_PRIVATE_DATA               = 0x06,
    PSMUX_ST_MHEG                       = 0x07,
    PSMUX_ST_DSMCC                      = 0x08,
    PSMUX_ST_H222_1                     = 0x09,
    PSMUX_ST_AUDIO_AAC                  = 0x0f,
    PSMUX_ST_VIDEO_MPEG4                = 0x10,
    PSMUX_ST_VIDEO_H264                 = 0x1b,
    PSMUX_ST_VIDEO_H265                 = 0x24,
    = 0x80 PSMUX_ST_PS_VIDEO_SVAC,
    PSMUX_ST_PS_AUDIO_AC3 = 0x81,
    PSMUX_ST_PS_AUDIO_DTS = 0x8A,
    PSMUX_ST_PS_AUDIO_LPCM = 0x8b,
    PSMUX_ST_PS_AUDIO_G711A = 0x90,
    PSMUX_ST_PS_AUDIO_G711U = 0x91,
    PSMUX_ST_PS_AUDIO_G722_1 = 0x92,
    PSMUX_ST_PS_AUDIO_G723_1 = 0x93,
    PSMUX_ST_PS_AUDIO_G729 = 0x99,
    PSMUX_ST_PS_AUDIO_SVAC = 0x9B,
    PSMUX_ST_PS_DVD_SUBPICTURE = 0xFF,
    // not standard defined below which defined
    PSMUX_ST_VIDEO_DIRAC = 0xD1
----------------
Disclaimer: This article is the original article CSDN bloggers "Heworld_guo", and follow CC 4.0 BY-SA copyright agreements, please attach a reprint the original source link and this statement.
Original link: https: //blog.csdn.net/qq_24977505/article/details/95193041

Guess you like

Origin www.cnblogs.com/mingzhang/p/12565762.html
ps
ps