-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathcustomEncoder.cpp
More file actions
208 lines (179 loc) · 6.17 KB
/
Copy pathcustomEncoder.cpp
File metadata and controls
208 lines (179 loc) · 6.17 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
#include <AvTranscoder/transcoder/Transcoder.hpp>
#include <AvTranscoder/file/OutputFile.hpp>
#include <AvTranscoder/progress/ConsoleProgress.hpp>
#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdlib>
void parseConfigFile(const std::string& configFilename, avtranscoder::Transcoder& transcoder)
{
std::ifstream configFile(configFilename.c_str(), std::ifstream::in);
std::string line;
while(std::getline(configFile, line))
{
std::istringstream is_line(line);
std::string filename;
if(std::getline(is_line, filename, '='))
{
std::string streamId;
if(std::getline(is_line, streamId, ':'))
{
std::string transcodeProfile;
std::getline(is_line, transcodeProfile);
std::stringstream ss(streamId);
size_t streamIndex = 0;
char separator = 'x';
std::vector<size_t> channelIndexArray;
ss >> streamIndex;
ss >> separator;
if(separator == '.')
{
int subStreamIndex = -1;
ss >> subStreamIndex;
channelIndexArray.push_back(subStreamIndex);
}
// generated stream
if(!filename.length())
transcoder.addGenerateStream(transcodeProfile);
else
{
avtranscoder::InputStreamDesc inputDesc(filename, streamIndex, channelIndexArray);
transcoder.addStream(inputDesc, transcodeProfile);
}
}
}
}
configFile.close();
}
class AvExport CustomCodec
: public avtranscoder::ICodec
{
public:
CustomCodec()
: avtranscoder::ICodec(avtranscoder::eCodecTypeEncoder, AV_CODEC_ID_PCM_S24LE)
{
}
void openCodec(){}
void closeCodec(){}
std::string getCodecName() const { return "Custom Encoder"; };
AVCodecID getCodecId() const { return AV_CODEC_ID_PCM_S24LE; }
avtranscoder::ECodecType getCodecType() const { return avtranscoder::eCodecTypeEncoder; }
int getLatency() const { return 0; }
avtranscoder::OptionArray getOptions() {
std::vector<avtranscoder::Option> options;
return options;
}
};
class AvExport CustomEncoder
: public avtranscoder::IEncoder
{
public:
CustomEncoder()
: _codec()
{}
/**
* @brief Setup the encoder
* @param profile: set encoder parameters from the given profile
* @note Open the encoder.
*/
void setupEncoder(const avtranscoder::ProfileLoader::Profile& profile = avtranscoder::ProfileLoader::Profile()) {
return;
};
/**
* @brief Encode a new frame, and get coded frame
* @param sourceFrame: frame that needs to be encoded
* @param codedFrame: output encoded coded data (first frames can be delayed)
* @return status of encoding
* @throw runtime_error if the encoded process failed.
*/
bool encodeFrame(const avtranscoder::IFrame& sourceFrame, avtranscoder::CodedData& codedFrame) {
codedFrame.assign(5760, 0);
return true;
};
/**
* @brief Get the frames remaining into the encoder
* @param codedFrame: output encoded data
* @return status of encoding
* @throw runtime_error if the encoded process failed.
*/
bool encodeFrame(avtranscoder::CodedData& codedFrame) {
return false;
};
/**
* @brief Get codec used for encoding.
* @return a reference to the codec
*/
avtranscoder::ICodec& getCodec() {
return _codec;
};
private:
CustomCodec _codec;
};
int main(int argc, char** argv)
{
std::string help;
help += "Usage\n";
help += "\tavprocessor INPUT_FILE_NAME OUTPUT_FILE_NAME [--verbose] [--logFile] [--help]\n";
help += "Command line options\n";
help += "\t--verbose: set log level to AV_LOG_DEBUG\n";
help += "\t--logFile: put log in 'avtranscoder.log' file\n";
help += "\t--help: display this help\n";
// Preload FFmpeg context
avtranscoder::preloadCodecsAndFormats();
avtranscoder::Logger::setLogLevel(AV_LOG_QUIET);
// List command line arguments
std::vector<std::string> arguments;
for(int argument = 1; argument < argc; ++argument)
{
arguments.push_back(argv[argument]);
}
for(size_t argument = 0; argument < arguments.size(); ++argument)
{
if(arguments.at(argument) == "--help")
{
std::cout << help << std::endl;
return 0;
}
else if(arguments.at(argument) == "--verbose")
{
avtranscoder::Logger::setLogLevel(AV_LOG_DEBUG);
}
else if(arguments.at(argument) == "--logFile")
{
avtranscoder::Logger::logInFile();
}
}
// Check required arguments
if(argc < 3)
{
std::cout << "avprocessor can rewrap or transcode inputs to create an output media file." << std::endl;
std::cout << "Use option --help to display help" << std::endl;
return (-1);
}
try
{
std::string output_format = "s24le";
avtranscoder::OutputFile outputFile(argv[2], output_format);
avtranscoder::Transcoder transcoder(outputFile);
transcoder.setProcessMethod(avtranscoder::eProcessMethodBasedOnStream);
CustomEncoder* customEncoder = new CustomEncoder;
avtranscoder::InputStreamDesc inputDescLeft(argv[1], 1, 0);
avtranscoder::InputStreamDesc inputDescRight(argv[1], 2, 0);
std::vector<avtranscoder::InputStreamDesc> inputDescriptors;
inputDescriptors.push_back(avtranscoder::InputStreamDesc(argv[1], 1, 0));
inputDescriptors.push_back(avtranscoder::InputStreamDesc(argv[1], 2, 0));
transcoder.addStream(inputDescriptors, customEncoder);
avtranscoder::ConsoleProgress progress;
transcoder.process(progress);
}
catch(std::exception& e)
{
std::cerr << "ERROR: during process, an error occured: " << e.what() << std::endl;
}
catch(...)
{
std::cerr << "ERROR: during process, an unknown error occured" << std::endl;
}
}