-
Notifications
You must be signed in to change notification settings - Fork 0
/
H264_Decoder.cpp
190 lines (144 loc) · 3.51 KB
/
H264_Decoder.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
#include "H264_Decoder.h"
H264_Decoder::H264_Decoder(h264_decoder_callback frameCallback, void* user)
:codec(NULL)
,codec_context(NULL)
,parser(NULL)
,fp(NULL)
,frame(0)
,cb_frame(frameCallback)
,cb_user(user)
,frame_timeout(0)
,frame_delay(0)
{
avcodec_register_all();
}
H264_Decoder::~H264_Decoder() {
if(parser) {
av_parser_close(parser);
parser = NULL;
}
if(codec_context) {
avcodec_close(codec_context);
av_free(codec_context);
codec_context = NULL;
}
if(picture) {
av_free(picture);
picture = NULL;
}
if(fp) {
fclose(fp);
fp = NULL;
}
cb_frame = NULL;
cb_user = NULL;
frame = 0;
frame_timeout = 0;
}
bool H264_Decoder::load(std::string filepath, float fps) {
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if(!codec) {
printf("Error: cannot find the h264 codec: %s\n", filepath.c_str());
return false;
}
codec_context = avcodec_alloc_context3(codec);
if(codec->capabilities & CODEC_CAP_TRUNCATED) {
codec_context->flags |= CODEC_FLAG_TRUNCATED;
}
if(avcodec_open2(codec_context, codec, NULL) < 0) {
printf("Error: could not open codec.\n");
return false;
}
fp = fopen(filepath.c_str(), "rb");
if(!fp) {
printf("Error: cannot open: %s\n", filepath.c_str());
return false;
}
picture = av_frame_alloc();
parser = av_parser_init(AV_CODEC_ID_H264);
if(!parser) {
printf("Erorr: cannot create H264 parser.\n");
return false;
}
if(fps > 0.0001f) {
frame_delay = (1.0f/fps) * 1000ull * 1000ull * 1000ull;
frame_timeout = rx_hrtime() + frame_delay;
}
// kickoff reading...
readBuffer();
return true;
}
bool H264_Decoder::readFrame() {
uint64_t now = rx_hrtime();
if(now < frame_timeout) {
return false;
}
bool needs_more = false;
while(!update(needs_more)) {
if(needs_more) {
readBuffer();
}
}
// it may take some 'reads' before we can set the fps
if(frame_timeout == 0 && frame_delay == 0) {
double fps = av_q2d(codec_context->time_base);
if(fps > 0.0) {
frame_delay = fps * 1000ull * 1000ull * 1000ull;
}
}
if(frame_delay > 0) {
frame_timeout = rx_hrtime() + frame_delay;
}
return true;
}
void H264_Decoder::decodeFrame(uint8_t* data, int size) {
AVPacket pkt;
int got_picture = 0;
int len = 0;
av_init_packet(&pkt);
pkt.data = data;
pkt.size = size;
len = avcodec_decode_video2(codec_context, picture, &got_picture, &pkt);
if(len < 0) {
printf("Error while decoding a frame.\n");
}
if(got_picture == 0) {
return;
}
++frame;
if(cb_frame) {
cb_frame(picture, &pkt, cb_user);
}
}
int H264_Decoder::readBuffer() {
int bytes_read = (int)fread(inbuf, 1, H264_INBUF_SIZE, fp);
if(bytes_read) {
std::copy(inbuf, inbuf + bytes_read, std::back_inserter(buffer));
}
return bytes_read;
}
bool H264_Decoder::update(bool& needsMoreBytes) {
needsMoreBytes = false;
if(!fp) {
printf("Cannot update .. file not opened...\n");
return false;
}
if(buffer.size() == 0) {
needsMoreBytes = true;
return false;
}
uint8_t* data = NULL;
int size = 0;
int len = av_parser_parse2(parser, codec_context, &data, &size,
&buffer[0], buffer.size(), 0, 0, AV_NOPTS_VALUE);
if(size == 0 && len >= 0) {
needsMoreBytes = true;
return false;
}
if(len) {
decodeFrame(&buffer[0], size);
buffer.erase(buffer.begin(), buffer.begin() + len);
return true;
}
return false;
}