forked from sd007/SimpleSipClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pstoh264.cpp
140 lines (116 loc) · 4.64 KB
/
pstoh264.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "pstoh264.h"
#include <iostream>
bool PsToH264::m_firstMainFrame = false;
PsToH264::PsToH264(QObject *parent) : QObject(parent)
{
}
int PsToH264::GetH246FromPs(char *buffer, int length, char **h264Buffer, int *h264length)
{
int leftlength = 0;
char *NextPack = 0;
*h264Buffer = buffer;
*h264length = 0;
// 00 00 01 ba
if(ProgramStreamPackHeader(buffer, length, &NextPack, &leftlength)==0)
return 0;
char *PayloadData=NULL;
int PayloadDataLen=0;
while(leftlength >= sizeof(pack_start_code))
{
PayloadData=NULL;
PayloadDataLen=0;
if(/*m_firstMainFrame && */NextPack
&& NextPack[0]=='\x00'
&& NextPack[1]=='\x00'
&& NextPack[2]=='\x01'
&& NextPack[3]=='\xE0')
{
if(Pes(NextPack, leftlength, &NextPack, &leftlength, &PayloadData, &PayloadDataLen))
{
if(PayloadDataLen)
{
memcpy(buffer, PayloadData, PayloadDataLen);
buffer += PayloadDataLen;
*h264length += PayloadDataLen;
}
}
else
{
if(PayloadDataLen)
{
memcpy(buffer, PayloadData, PayloadDataLen);
buffer += PayloadDataLen;
*h264length += PayloadDataLen;
}
break;
}
}
else if(NextPack
&& NextPack[0]=='\x00'
&& NextPack[1]=='\x00'
&& NextPack[2]=='\x01'
&& NextPack[3]=='\xBB')
{
if(length < sizeof(pack_title_header)) break;
pack_title_header *title = (pack_title_header *)NextPack;
littel_endian_size titile_length;
titile_length.byte[0] = title->headerLength.byte[1];
titile_length.byte[1] = title->headerLength.byte[0];
NextPack += titile_length.length + sizeof(pack_title_header);
leftlength = leftlength - titile_length.length - sizeof(pack_title_header);
}else if(NextPack
&& NextPack[0]=='\x00'
&& NextPack[1]=='\x00'
&& NextPack[2]=='\x01'
&& NextPack[3]=='\xBC')
{
if(leftlength < sizeof(program_stream_map)) break;
program_stream_map* PSMPack = (program_stream_map*)NextPack;
littel_endian_size psm_length;
psm_length.byte[0] = PSMPack->PackLength.byte[1];
psm_length.byte[1] = PSMPack->PackLength.byte[0];
NextPack = NextPack + psm_length.length + sizeof(program_stream_map);
leftlength = leftlength - psm_length.length - sizeof(program_stream_map);
// m_firstMainFrame = true;
}
else
{
// printf("[%s]no konw %x %x %x %x\n", __FUNCTION__, NextPack[0], NextPack[1], NextPack[2], NextPack[3]);
break;
}
}
return *h264length;
}
int PsToH264::Pes(char *Pack, int length, char **NextPack, int *leftlength, char **PayloadData, int *PayloadDataLen)
{
*PayloadData = 0;
*PayloadDataLen = 0;
if(length < sizeof(program_stream_e)) return 0;
program_stream_e* PSEPack = (program_stream_e*)Pack;
littel_endian_size pse_length;
pse_length.byte[0] = PSEPack->PackLength.byte[1];
pse_length.byte[1] = PSEPack->PackLength.byte[0];
//2 字节表示长度
//1 字节固定值
*PayloadDataLen = pse_length.length - 2 - 1 - PSEPack->stuffing_length;
if(*PayloadDataLen > 0)
{
*PayloadData = Pack + sizeof(program_stream_e) + PSEPack->stuffing_length;
}
*leftlength = length - pse_length.length - sizeof(pack_start_code) - sizeof(littel_endian_size);
if(*leftlength <= 0) return 0;
*NextPack = Pack + sizeof(pack_start_code) + sizeof(littel_endian_size) + pse_length.length;
return *leftlength;
}
int PsToH264::ProgramStreamPackHeader(char *Pack, int length, char **NextPack, int *leftlength)
{
//printf("[%s]%x %x %x %x\n", __FUNCTION__, Pack[0], Pack[1], Pack[2], Pack[3]);
//通过 00 00 01 ba头的第14个字节的最后3位来确定头部填充了多少字节
program_stream_pack_header *PsHead = (program_stream_pack_header *)Pack;
unsigned char pack_stuffing_length = PsHead->stuffinglen & '\x07';
*leftlength = length - sizeof(program_stream_pack_header) - pack_stuffing_length;//减去头和填充的字节
*NextPack = Pack+sizeof(program_stream_pack_header) + pack_stuffing_length;
if(*leftlength<4) return 0;
//printf("[%s]2 %x %x %x %x\n", __FUNCTION__, (*NextPack)[0], (*NextPack)[1], (*NextPack)[2], (*NextPack)[3]);
return *leftlength;
}