-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathMediaEncoderTest.cpp
More file actions
211 lines (200 loc) · 7.31 KB
/
MediaEncoderTest.cpp
File metadata and controls
211 lines (200 loc) · 7.31 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
#include <iostream>
#include <string>
#include "MediaParser.h"
#include "MediaReader.h"
#include "MediaEncoder.h"
#include "Logger.h"
using namespace std;
using namespace Logger;
using namespace MediaCore;
int main(int argc, const char* argv[])
{
if (argc < 3)
{
Log(Error) << "Wrong arguments!" << endl;
return -1;
}
// GetMediaReaderLogger()->SetShowLevels(DEBUG);
GetDefaultLogger()->SetShowLevels(DEBUG);
MediaEncoder::GetLogger()->SetShowLevels(DEBUG);
#if 1
string codecHint = "h264";
vector<MediaEncoder::Description> encoderDescList;
MediaEncoder::FindEncoder(codecHint, encoderDescList);
Log(DEBUG) << "Results for searching codec hint '" << codecHint << "':" << endl;
if (encoderDescList.empty())
Log(DEBUG) << "NO ENCODER IS FOUND." << endl;
else
{
for (int i = 0; i < encoderDescList.size(); i++)
{
auto& encdesc = encoderDescList[i];
Log(DEBUG) << "[" << i << "] " << encdesc << endl;
}
}
#endif
string vidEncCodec = "h264_vaapi";
uint32_t outWidth{3840}, outHeight{2160};
Ratio outFrameRate = { 60, 1 };
uint64_t outVidBitRate = 30*1000*1000;
string audEncCodec = "aac";
uint32_t outAudChannels = 2;
uint32_t outSampleRate = 48000;
uint64_t outAudBitRate = 128*1000;
int64_t maxEncodeDuration = 5*1000;
vector<MediaEncoder::Option> extraOpts = {
{ "profile", Value("high") },
{ "aspect", Value(Ratio(1,1)) },
{ "colorspace", Value(1) },
{ "color_trc", Value(1) },
{ "color_primaries", Value(1) },
};
bool videoOnly{false}, audioOnly{false};
MediaParser::Holder hParser = MediaParser::CreateInstance();
if (!hParser->Open(argv[1]))
{
Log(Error) << "FAILED to open MediaParser by file '" << argv[1] << "'! Error is '" << hParser->GetError() << "'." << endl;
return -1;
}
MediaInfo::Holder hInfo = hParser->GetMediaInfo();
MediaReader::Holder hVidReader, hAudReader;
string audioPcmFormat;
uint32_t audioBlockAlign{0};
if (hParser->GetBestVideoStreamIndex() >= 0 && !audioOnly)
{
hVidReader = MediaReader::CreateVideoInstance();
if (!hVidReader->Open(hParser))
{
Log(Error) << "FAILED to open video MediaReader! Error is '" << hVidReader->GetError() << "'." << endl;
return -2;
}
if (!hVidReader->ConfigVideoReader(outWidth, outHeight))
{
Log(Error) << "FAILED to configure video MediaReader! Error is '" << hVidReader->GetError() << "'." << endl;
return -3;
}
hVidReader->Start();
}
if (hParser->GetBestAudioStreamIndex() >= 0 && !videoOnly)
{
hAudReader = MediaReader::CreateInstance();
if (!hAudReader->Open(hParser))
{
Log(Error) << "FAILED to open audio MediaReader! Error is '" << hAudReader->GetError() << "'." << endl;
return -4;
}
if (!hAudReader->ConfigAudioReader(outAudChannels, outSampleRate, "flt"))
{
Log(Error) << "FAILED to configure audio MediaReader! Error is '" << hAudReader->GetError() << "'." << endl;
return -5;
}
audioPcmFormat = hAudReader->GetAudioOutPcmFormat();
audioBlockAlign = hAudReader->GetAudioOutFrameSize();
hAudReader->Start();
}
auto hEncoder = MediaEncoder::CreateInstance();
if (!hEncoder->Open(argv[2]))
{
Log(Error) << "FAILED to open MediaEncoder by '" << argv[2] << "'! Error is '" << hEncoder->GetError() << "'." << endl;
return -6;
}
string vidEncImgFormat;
if (hVidReader && !hEncoder->ConfigureVideoStream(vidEncCodec, vidEncImgFormat, outWidth, outHeight, outFrameRate, outVidBitRate, &extraOpts))
{
Log(Error) << "FAILED to configure video encoder! Error is '" << hEncoder->GetError() << "'." << endl;
return -7;
}
string audEncSmpFormat;
if (hAudReader && !hEncoder->ConfigureAudioStream(audEncCodec, audEncSmpFormat, outAudChannels, outSampleRate, outAudBitRate))
{
Log(Error) << "FAILED to configure audio encoder! Error is '" << hEncoder->GetError() << "'." << endl;
return -8;
}
hEncoder->Start();
bool vidInputEof = hVidReader ? false : true;
bool audInputEof = hAudReader ? false : true;
int64_t audpos = 0, vidpos = 0;
uint32_t vidFrameCount = 0;
ImGui::ImMat vmat, amat;
while (!vidInputEof || !audInputEof)
{
if ((!vidInputEof && vidpos <= audpos) || audInputEof)
{
bool eof;
vidpos = (int64_t)((double)vidFrameCount*outFrameRate.den/outFrameRate.num*1000);
auto hVf = hVidReader->ReadVideoFrame(vidpos, eof);
if (hVf && !eof && !hVf->GetMat(vmat))
vmat.release();
if (vmat.empty() && !eof)
{
Log(Error) << "FAILED to read video frame! Error is '" << hVidReader->GetError() << "'." << endl;
break;
}
vidFrameCount++;
if (maxEncodeDuration > 0 && vidpos >= maxEncodeDuration)
eof = true;
if (!eof)
{
vmat.time_stamp = (double)vidpos/1000;
bool consumed = false;
if (!hEncoder->EncodeVideoFrame(vmat, consumed))
{
Log(Error) << "FAILED to encode video frame! Error is '" << hEncoder->GetError() << "'." << endl;
break;
}
}
else
{
vmat.release();
bool consumed = false;
if (!hEncoder->EncodeVideoFrame(vmat, consumed))
{
Log(Error) << "FAILED to encode video EOF! Error is '" << hEncoder->GetError() << "'." << endl;
break;
}
vidInputEof = true;
}
}
else
{
bool eof;
uint32_t readSamples = 0;
if (!hAudReader->ReadAudioSamples(amat, readSamples, eof) && !eof)
{
Log(Error) << "FAILED to read audio samples! Error is '" << hAudReader->GetError() << "'." << endl;
break;
}
audpos = amat.time_stamp*1000;
if (maxEncodeDuration > 0 && audpos >= maxEncodeDuration)
eof = true;
if (!eof)
{
bool consumed = false;
if (!hEncoder->EncodeAudioSamples(amat, consumed))
{
Log(Error) << "FAILED to encode audio samples! Error is '" << hEncoder->GetError() << "'." << endl;
break;
}
}
else
{
amat.release();
bool consumed = false;
if (!hEncoder->EncodeAudioSamples(amat, consumed))
{
Log(Error) << "FAILED to encode audio EOF! Error is '" << hEncoder->GetError() << "'." << endl;
break;
}
audInputEof = true;
}
}
}
hEncoder->FinishEncoding();
hEncoder->Close();
hEncoder = nullptr;
hVidReader = nullptr;
hAudReader = nullptr;
hInfo = nullptr;
hParser = nullptr;
return 0;
}