-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathVideoEncoder.cpp
More file actions
175 lines (147 loc) · 5.5 KB
/
VideoEncoder.cpp
File metadata and controls
175 lines (147 loc) · 5.5 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
#include "VideoEncoder.hpp"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
}
#include <stdexcept>
#include <cstdlib>
namespace avtranscoder
{
VideoEncoder::VideoEncoder(const std::string& videoCodecName)
: _codec(eCodecTypeEncoder, videoCodecName)
{
}
VideoEncoder::~VideoEncoder()
{
}
void VideoEncoder::setupVideoEncoder(const VideoFrameDesc& frameDesc, const ProfileLoader::Profile& profile)
{
if(!profile.empty())
{
LOG_INFO("Setup video encoder with:\n" << profile)
}
// set width, height, pixel format, fps
_codec.setImageParameters(frameDesc);
// setup encoder
setupEncoder(profile);
}
void VideoEncoder::setupEncoder(const ProfileLoader::Profile& profile)
{
// check the given profile
const bool isValid = ProfileLoader::checkVideoProfile(profile);
if(!isValid && !profile.empty())
{
const std::string msg("Invalid video profile to setup encoder.");
LOG_ERROR(msg)
throw std::runtime_error(msg);
}
// set threads before any other options
if(profile.count(constants::avProfileThreads))
_codec.getOption(constants::avProfileThreads).setString(profile.at(constants::avProfileThreads));
else
_codec.getOption(constants::avProfileThreads).setInt(_codec.getAVCodecContext().thread_count);
// set encoder options
for(ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it)
{
if((*it).first == constants::avProfileIdentificator || (*it).first == constants::avProfileIdentificatorHuman ||
(*it).first == constants::avProfileType || (*it).first == constants::avProfileCodec ||
(*it).first == constants::avProfileWidth || (*it).first == constants::avProfileHeight ||
(*it).first == constants::avProfilePixelFormat || (*it).first == constants::avProfileFrameRate ||
(*it).first == constants::avProfileThreads)
continue;
try
{
Option& encodeOption = _codec.getOption((*it).first);
encodeOption.setString((*it).second);
}
catch(std::exception& e)
{
}
}
// open encoder
int encoderFlags = 0;
if(profile.count(constants::avProfileProcessStat))
{
LOG_INFO("SetUp video encoder to compute statistics during process")
#ifdef AV_CODEC_FLAG_PSNR
encoderFlags |= AV_CODEC_FLAG_PSNR;
#else
encoderFlags |= CODEC_FLAG_PSNR;
#endif
}
_codec.getAVCodecContext().flags |= encoderFlags;
_codec.openCodec();
// after open encoder, set specific encoder options
for(ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it)
{
if((*it).first == constants::avProfileIdentificator || (*it).first == constants::avProfileIdentificatorHuman ||
(*it).first == constants::avProfileType || (*it).first == constants::avProfileCodec ||
(*it).first == constants::avProfileWidth || (*it).first == constants::avProfileHeight ||
(*it).first == constants::avProfilePixelFormat || (*it).first == constants::avProfileFrameRate ||
(*it).first == constants::avProfileThreads)
continue;
try
{
Option& encodeOption = _codec.getOption((*it).first);
encodeOption.setString((*it).second);
}
catch(std::exception& e)
{
LOG_WARN("VideoEncoder - can't set option " << (*it).first << " to " << (*it).second << ": " << e.what())
}
}
}
bool VideoEncoder::encodeFrame(const IFrame& sourceFrame, CodedData& codedFrame)
{
AVPacket& packet = codedFrame.getAVPacket();
const AVFrame& srcAvFrame = sourceFrame.getAVFrame();
if(srcAvFrame.pts != (int)AV_NOPTS_VALUE)
{
packet.pts = srcAvFrame.pts;
}
if(srcAvFrame.key_frame)
{
packet.flags |= AV_PKT_FLAG_KEY;
}
return encode(&srcAvFrame, packet);
}
bool VideoEncoder::encodeFrame(CodedData& codedFrame)
{
return encode(NULL, codedFrame.getAVPacket());
}
bool VideoEncoder::encode(const AVFrame* decodedData, AVPacket& encodedData)
{
// Be sure that data of AVPacket is NULL so that the encoder will allocate it
encodedData.data = NULL;
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
#if LIBAVCODEC_VERSION_MAJOR > 58
int ret = avcodec_send_frame(&avCodecContext, decodedData);
if (ret != 0 && ret != AVERROR_EOF)
throw std::runtime_error("Error sending video frame to encoder: " + getDescriptionFromErrorCode(ret));
ret = avcodec_receive_packet(&avCodecContext, &encodedData);
if (ret == 0)
return true;
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return false;
throw std::runtime_error("Error receiving video frame from encoder: " + getDescriptionFromErrorCode(ret));
#elif LIBAVCODEC_VERSION_MAJOR > 53
int gotPacket = 0;
const int ret = avcodec_encode_video2(&avCodecContext, &encodedData, decodedData, &gotPacket);
if(ret != 0)
{
throw std::runtime_error("Encode video frame error: avcodec encode video frame - " +
getDescriptionFromErrorCode(ret));
}
return gotPacket == 1;
#else
const int ret = avcodec_encode_video(&avCodecContext, encodedData.data, encodedData.size, decodedData);
if(ret < 0)
{
throw std::runtime_error("Encode video frame error: avcodec encode video frame - " +
getDescriptionFromErrorCode(ret));
}
return true;
#endif
}
}