-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain-test.cpp
More file actions
219 lines (185 loc) · 7.38 KB
/
main-test.cpp
File metadata and controls
219 lines (185 loc) · 7.38 KB
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
#include <string>
#include <thread>
#include <vector>
#include <cstring>
#include <mp4v2/mp4v2.h>
#include <netinet/in.h>
#include "fMP4.h"
FILE *fptr = nullptr;
class MP4Reader
{
public:
enum MP4ReadStatus
{
MP4_READ_OK,
MP4_READ_EOS,
MP4_READ_ERR
};
MP4Reader(const std::string &file_path)
: time_scale(9 * MP4_MSECS_TIME_SCALE)
, file_path(file_path)
, handle(MP4_INVALID_FILE_HANDLE)
, video_track_id(MP4_INVALID_TRACK_ID)
, next_video_sample_idx(1)
, video_sample(nullptr)
, video_timescale(0)
, video_sample_max_size(0)
, video_sample_number(0)
, video_duration(0)
, pSeqHeaders(nullptr)
, pSeqHeaderSize(nullptr)
, pPictHeaders(nullptr)
, pPictHeaderSize(nullptr)
{
handle = MP4Read(this->file_path.c_str());
video_track_id = MP4FindTrackId(handle, 0, MP4_VIDEO_TRACK_TYPE);
if (video_track_id != MP4_INVALID_TRACK_ID) {
video_timescale = MP4GetTrackTimeScale(handle, video_track_id);
video_sample_max_size = MP4GetTrackMaxSampleSize(handle, video_track_id) * 2;
video_duration = MP4GetTrackDuration(handle, video_track_id);
video_sample = new unsigned char[video_sample_max_size];
video_sample_number = MP4GetTrackNumberOfSamples(handle, video_track_id);
if (MP4GetTrackH264SeqPictHeaders(handle,
video_track_id,
&pSeqHeaders,
&pSeqHeaderSize,
&pPictHeaders,
&pPictHeaderSize))
{
printf("Get SPS(%d) and PPS(%d)\n", *pSeqHeaderSize, *pPictHeaderSize);
for(int i = 0; (pSeqHeaders[i] && pSeqHeaderSize[i]); i++) {
printf("SPS(%d): %02x %02x %02x %02x %02x\n", i,
pSeqHeaders[i][0], pSeqHeaders[i][1], pSeqHeaders[i][2],
pSeqHeaders[i][3], pSeqHeaders[i][4]);
}
for(int i = 0; (pPictHeaders[i] && pPictHeaderSize[i]); i++) {
printf("PPS(%d): %02x %02x %02x %02x %02x\n", i,
pPictHeaders[i][0], pPictHeaders[i][1], pPictHeaders[i][2],
pPictHeaders[i][3], pPictHeaders[i][4]);
}
}
}
}
~MP4Reader()
{
if (pSeqHeaders || pSeqHeaderSize || pPictHeaders || pPictHeaderSize) {
MP4FreeH264SeqPictHeaders(pSeqHeaders, pSeqHeaderSize, pPictHeaders, pPictHeaderSize);
}
if (handle != MP4_INVALID_FILE_HANDLE) MP4Close(handle);
if (video_sample) delete[] video_sample;
}
unsigned int GetVideoWidth() const
{
return MP4GetTrackVideoWidth(handle, video_track_id);
}
unsigned int GetVideoHeight() const
{
return MP4GetTrackVideoHeight(handle, video_track_id);
}
double GetVideoFps() const
{
return MP4GetTrackVideoFrameRate(handle, video_track_id);
}
unsigned int GetBitRate() const
{
return MP4GetTrackBitRate(handle, video_track_id);
}
MP4ReadStatus GetNextH264VideoSample(unsigned char **sample,
unsigned int &sample_size,
unsigned long long int &duration,
bool &is_key_frame)
{
if (next_video_sample_idx > video_sample_number) {
return MP4_READ_EOS;
}
unsigned int video_sample_offset = 0;
if(MP4GetSampleSync(handle, video_track_id, next_video_sample_idx)) {
/*
* If current sample has key frame, we need to put SPS/PPS in front of key frame.
*/
if (pSeqHeaders && pSeqHeaderSize) {
for(int i = 0; (pSeqHeaders[i] && pSeqHeaderSize[i]); i++) {
(*(unsigned int *)(video_sample + video_sample_offset)) = htonl(1);
video_sample_offset += 4;
memcpy(video_sample + video_sample_offset, pSeqHeaders[i], pSeqHeaderSize[i]);
video_sample_offset += pSeqHeaderSize[i];
}
}
if (pPictHeaders && pPictHeaderSize) {
for(int i = 0; (pPictHeaders[i] && pPictHeaderSize[i]); i++) {
(*(unsigned int *)(video_sample + video_sample_offset)) = htonl(1);
video_sample_offset += 4;
memcpy(video_sample + video_sample_offset, pPictHeaders[i], pPictHeaderSize[i]);
video_sample_offset += pPictHeaderSize[i];
}
}
}
MP4Duration mp4_duration = 0;
unsigned char *video_sample_start_addr = video_sample + video_sample_offset;
sample_size = video_sample_max_size - video_sample_offset;
if (!MP4ReadSample(handle, video_track_id, next_video_sample_idx,
&video_sample_start_addr, &sample_size,
NULL,
&mp4_duration,
NULL,
&is_key_frame)) {
printf("Fail to read video sample (%d)\n", next_video_sample_idx);
return MP4_READ_ERR;
}
// Convert AVC1 format to AnnexB
if (sample_size >= 4) {
unsigned int *p = (unsigned int *) video_sample_start_addr;
*p = htonl(1);
}
*sample = video_sample;
sample_size += video_sample_offset;
duration = (1000 * mp4_duration) / time_scale;
next_video_sample_idx++;
return MP4_READ_OK;
}
private:
const unsigned int time_scale;
std::string file_path;
MP4FileHandle handle;
MP4TrackId video_track_id;
unsigned int next_video_sample_idx;
unsigned char *video_sample;
unsigned int video_timescale;
unsigned int video_sample_max_size;
unsigned int video_sample_number;
unsigned long long int video_duration;
unsigned char **pSeqHeaders;
unsigned int *pSeqHeaderSize;
unsigned char **pPictHeaders;
unsigned int *pPictHeaderSize;
};
static int Write(unsigned char* buf, int buf_size)
{
static int i = 0;
printf("#%d Write: buf: %p(%02x%02x%02x%02x %c%c%c%c), size: %d\n", i++, buf, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf_size);
if (buf[4] == 'm' && buf[5] == 'f' && buf[6] == 'r' && buf[7] == 'a') {
return buf_size;
} else {
return fwrite(buf, 1, buf_size, fptr);
}
}
int main(int argc, char **argv)
{
if (argc < 3) {
printf("usage: %s input output\n", argv[0]);
return 1;
}
std::shared_ptr<MP4Reader> input = std::make_shared<MP4Reader>(argv[1]);
fptr = fopen(argv[2], "wb");
fMP4Writer fmp4_writer = fMP4_CreateWriter(&Write);
unsigned char *sample = nullptr;
unsigned int sample_size = 0;
unsigned long long int duration = 0;
bool is_key_frame = false;
while (input->GetNextH264VideoSample(&sample, sample_size, duration, is_key_frame) == MP4Reader::MP4_READ_OK) {
fMP4_WriteH264Sample(fmp4_writer, sample, sample_size, is_key_frame, duration);
}
fMP4_ReleaseWriter(fmp4_writer);
fclose(fptr);
return 0;
}