-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhevc_parse.cpp
240 lines (215 loc) · 6.13 KB
/
hevc_parse.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <string>
#include "utils.h"
int hevc_parse(frame** fp, char* file_string)
{
frameA* chainA = NULL;
frameB* chainB = NULL;
frameA* currentFrameA=NULL;
frameB* currentFrameB=NULL;
std::string cmd;
int x,y,c,counter;
int frameCountA = 0;
int frameCountB = 0;
double tmpd=0.0;
int mbcount = 0;
int mbcount_check = -1;
char* line = new char[LINE_BUFFER_SIZE];
char* cptr;
FILE* pipe;
fprintf(stderr, "[LOG] STARTING ffmpeg.exe | ffprobe_mod.exe FOR ANALYSIS\n");
cmd = std::string("ffmpeg.exe -hide_banner -loglevel error -threads 1 -i \"")
+ std::string(file_string)
+ std::string("\" -map 0:V:0 -c:v copy -f hevc -bsf:v hevc_mp4toannexb pipe: | ffprobe_mod.exe -threads 1 -v quiet -show_frames -show_streams -show_entries frame=key_frame,pkt_pos,pkt_size,pict_type -i pipe:");
pipe = _popen(cmd.c_str(), "rb");
if (pipe == NULL)
{
fprintf(stderr, "[LOG] Fail opening pipe for ffprobe stdout - Exit Program");
exit(ERR_FFPROBE_PIPE_ANALYSIS);
}
x = 0;
while (readline(pipe, line, LINE_BUFFER_SIZE) >= 0)
{
cptr = line;
if (strstr(line, "ffprobe_mod: ") == line)
{
if (chainB == NULL)//first round
{
chainB = new frameB;
currentFrameB = chainB;
}
else
{
currentFrameB->next = new frameB;
currentFrameB = currentFrameB->next;
}
frameCountB++;
currentFrameB->marked = 0;
while (*cptr != ' ') cptr++; cptr++;
mbcount = (int) (sttoint(cptr));
while (*cptr != ' ') cptr++; cptr++;
currentFrameB->avg_qp = sttodouble(cptr);
while (*cptr != ' ') cptr++; cptr++;
currentFrameB->stdevQ_qp = sttodouble(cptr);
while (*cptr != ' ') cptr++; cptr++;
currentFrameB->minqp = (int) (sttoint(cptr));
while (*cptr != ' ') cptr++; cptr++;
currentFrameB->maxqp = (int) (sttoint(cptr));
if (mbcount_check == -1)
{
mbcount_check = mbcount;
fprintf(stderr, "[LOG] Macroblocks per frame: %d\n", mbcount);
}
else if (mbcount_check != mbcount)
{
delete[] line;
fprintf(stderr, "[LOG] Something went wrong while parsing (mbcount_check). Closing app.");
_pclose(pipe);
exit(ERR_MB_CHECK);
}
currentFrameB->avg_qp /= mbcount;
currentFrameB->stdevQ_qp /= mbcount;
currentFrameB->stdevQ_qp -= currentFrameB->avg_qp * currentFrameB->avg_qp;
}
else if (strstr(line, "[FRAME]") == line)
{
frameCountA++;
if (chainA == NULL)//first round
{
chainA = new frameA;
currentFrameA = chainA;
}
else
{
currentFrameA->next = new frameA;
currentFrameA = currentFrameA->next;
}
currentFrameA->type = '?';
currentFrameA->next = NULL;
if (frameCountA % 200 == 0)
fprintf(stderr, "[LOG] Proccessing FRAME %d\n", frameCountA);
}
else if (strstr(line, "key_frame=") == line)
{
while (*cptr != '=') cptr++;
cptr++;
if (*cptr == '1') currentFrameA->type = 'K';
}
else if (strstr(line, "pkt_pos=") == line)
{
while (*cptr != '=') cptr++;
cptr++;
currentFrameA->byte_position = sttoint(cptr);
}
else if (strstr(line, "pkt_size=") == line)
{
while (*cptr != '=') cptr++;
cptr++;
currentFrameA->size = (int) (sttoint(cptr));
}
else if (strstr(line, "pict_type=") == line)
{
if (currentFrameA->type != 'K')
{
while (*cptr != '=') cptr++;
cptr++;
currentFrameA->type = *cptr;
}
}
else if (strstr(line, "[STREAM]") == line)
{
x = 1;//END OK
break;
}
}
delete[] line;
if (x != 1)
{
fprintf(stderr, "[LOG] Something went wrong while parsing. Closing app.");
_pclose(pipe);
exit( ERR_STDOUT_END_INVALID);
}
//COMPARE ffprobe vs hevcdec
_pclose(pipe);
if (currentFrameA!=NULL) if (currentFrameA->type == 'B') fprintf(stderr, "[LOG] ** W A R N I N G ** Last frame is a B frame. Analysis could be wrong\n");
fprintf(stderr, "[LOG] Frames counted: %d --- Frames decoded: %d\n", frameCountA, frameCountB);
if (frameCountB < frameCountA)
{
fprintf(stderr, "[LOG] FAIL ( decoded < counted ) - Exit Program\n");
exit( ERR_FRAME_MISMATCH);
}
counter = frameCountB - frameCountA;
if (counter > 0) fprintf(stderr, "[LOG] Discarding %d decoded frames (initial parsing)\n", counter);
for (x = 0; x < counter; x++)
{
currentFrameB = chainB->next;//tmp
delete chainB;
chainB = currentFrameB;
}
counter = frameCountA;
fprintf(stderr, "[LOG] Total frames %d\n", counter);
//////////SORT DECODED FRAMES
fprintf(stderr, "[LOG] SORTING DECODED FRAMES\n");
int firstUnmarked = 0;
char Kc;
//arrange in array
frame* arrayA = new frame[counter];
frameB* arrayB = new frameB[counter];
*fp = arrayA;
for (x = 0; x < counter; x++)
{
arrayA[x].type = chainA->type;
arrayA[x].size = chainA->size;
arrayA[x].byte_position = chainA->byte_position;
arrayB[x].avg_qp = chainB->avg_qp;
arrayB[x].stdevQ_qp = chainB->stdevQ_qp;
arrayB[x].marked = 0;
arrayB[x].minqp = chainB->minqp;
arrayB[x].maxqp = chainB->maxqp;
currentFrameA = chainA->next;
currentFrameB = chainB->next;
delete chainA;
delete chainB;
chainA = currentFrameA;
chainB = currentFrameB;
}
//sort
for (x = 0; x < counter; x++)
{
c = firstUnmarked;
Kc = 0;
for (y = firstUnmarked; y < counter; y++)
{
if (arrayA[y].byte_position < arrayA[x].byte_position) c++;
if (arrayA[y].type == 'K') Kc++;
if (Kc == 2) break; //search max in following gop, up to last K included
}
arrayA[x].avg_qp = arrayB[c].avg_qp;
arrayA[x].stdevQ_qp = arrayB[c].stdevQ_qp;
arrayA[x].minqp = arrayB[c].minqp;
arrayA[x].maxqp = arrayB[c].maxqp;
if (arrayB[c].marked == 1)
{
fprintf(stderr, "[LOG] ERROR WHILE SORTING DECODED FRAMES - Exit program\n");
exit(ERR_ALREADY_MARKED);
}
arrayB[c].marked = 1;
for (y = firstUnmarked; y < counter; y++)
{
if (arrayB[y].marked == 0)
{
firstUnmarked = y;
break;
}
}
}
for (x = 0; x < counter; x++)
{
if (arrayB[x].marked == 0)
{
fprintf(stderr, "[LOG] ERROR AFTER SORTING DECODED FRAMES - Exit program\n");
exit(ERR_NOT_MARKED);
}
}
delete[] arrayB;
return counter;
}