forked from mikrosimage/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoEncoder.cpp
More file actions
190 lines (162 loc) · 5.3 KB
/
Copy pathVideoEncoder.cpp
File metadata and controls
190 lines (162 loc) · 5.3 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
#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 )
, _frame( NULL )
{
#if LIBAVCODEC_VERSION_MAJOR > 54
_frame = av_frame_alloc();
#else
_frame = avcodec_alloc_frame();
#endif
}
VideoEncoder::~VideoEncoder()
{
#if LIBAVCODEC_VERSION_MAJOR > 54
av_frame_free( &_frame );
#else
#if LIBAVCODEC_VERSION_MAJOR > 53
avcodec_free_frame( &_frame );
#else
av_free( _frame );
#endif
#endif
}
void VideoEncoder::setup()
{
_codec.open();
}
bool VideoEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
{
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
// Set default frame parameters
#if LIBAVCODEC_VERSION_MAJOR > 54
av_frame_unref( _frame );
#else
avcodec_get_frame_defaults( _frame );
#endif
const VideoFrame& sourceImageFrame = static_cast<const VideoFrame&>( sourceFrame );
_frame->width = avCodecContext.width;
_frame->height = avCodecContext.height;
_frame->format = avCodecContext.pix_fmt;
int bufferSize = avpicture_fill( (AVPicture*)_frame, const_cast< unsigned char * >( sourceImageFrame.getData() ), avCodecContext.pix_fmt, avCodecContext.width, avCodecContext.height );
if( bufferSize < 0 )
{
throw std::runtime_error( "Encode video frame error: buffer size < 0 - " + getDescriptionFromErrorCode( bufferSize ) );
}
AVPacket& packet = codedFrame.getAVPacket();
packet.stream_index = 0;
if( ( avCodecContext.coded_frame ) &&
( avCodecContext.coded_frame->pts != (int)AV_NOPTS_VALUE ) )
{
packet.pts = avCodecContext.coded_frame->pts;
}
if( avCodecContext.coded_frame &&
avCodecContext.coded_frame->key_frame )
{
packet.flags |= AV_PKT_FLAG_KEY;
}
#if LIBAVCODEC_VERSION_MAJOR > 53
int gotPacket = 0;
int ret = avcodec_encode_video2( &avCodecContext, &packet, _frame, &gotPacket );
if( ret != 0 && gotPacket == 0 )
{
throw std::runtime_error( "Encode video frame error: avcodec encode video frame - " + getDescriptionFromErrorCode( ret ) );
}
#else
int ret = avcodec_encode_video( &avCodecContext, packet.data, packet.size, _frame );
if( ret < 0 )
{
throw std::runtime_error( "Encode video frame error: avcodec encode video frame - " + getDescriptionFromErrorCode( ret ) );
}
#endif
#if LIBAVCODEC_VERSION_MAJOR > 53
return ret == 0 && gotPacket == 1;
#endif
return ret == 0;
}
bool VideoEncoder::encodeFrame( Frame& codedFrame )
{
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
AVPacket& packet = codedFrame.getAVPacket();
packet.stream_index = 0;
#if LIBAVCODEC_VERSION_MAJOR > 53
int gotPacket = 0;
int ret = avcodec_encode_video2( &avCodecContext, &packet, NULL, &gotPacket );
if( ret != 0 && gotPacket == 0 )
{
throw std::runtime_error( "Encode video frame error: avcodec encode last video frame - " + getDescriptionFromErrorCode( ret ) );
}
return ret == 0 && gotPacket == 1;
#else
int ret = avcodec_encode_video( &avCodecContext, packet.data, packet.size, NULL );
if( ret < 0 )
{
throw std::runtime_error( "Encode video frame error: avcodec encode last video frame - " + getDescriptionFromErrorCode( ret ) );
}
return ret == 0;
#endif
}
void VideoEncoder::setProfile( const ProfileLoader::Profile& profile, const avtranscoder::VideoFrameDesc& frameDesc )
{
// set width, height, pixel format, fps
_codec.setImageParameters( frameDesc );
// 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 ).setString( "auto" );
// 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 )
{}
}
setup();
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() )
}
}
}
}