@@ -109,8 +109,6 @@ bool VideoEncoder::encodeFrame(const Frame& sourceFrame, CodedData& codedFrame)
109
109
AVCodecContext& avCodecContext = _codec.getAVCodecContext ();
110
110
111
111
AVPacket& packet = codedFrame.getAVPacket ();
112
- packet.stream_index = 0 ;
113
-
114
112
if ((avCodecContext.coded_frame ) && (avCodecContext.coded_frame ->pts != (int )AV_NOPTS_VALUE))
115
113
{
116
114
packet.pts = avCodecContext.coded_frame ->pts ;
@@ -121,53 +119,38 @@ bool VideoEncoder::encodeFrame(const Frame& sourceFrame, CodedData& codedFrame)
121
119
packet.flags |= AV_PKT_FLAG_KEY;
122
120
}
123
121
124
- #if LIBAVCODEC_VERSION_MAJOR > 53
125
- int gotPacket = 0 ;
126
- int ret = avcodec_encode_video2 (&avCodecContext, &packet, &sourceFrame.getAVFrame (), &gotPacket);
127
- if (ret != 0 && gotPacket == 0 )
128
- {
129
- throw std::runtime_error (" Encode video frame error: avcodec encode video frame - " +
130
- getDescriptionFromErrorCode (ret));
131
- }
132
- #else
133
- int ret = avcodec_encode_video (&avCodecContext, packet.data , packet.size , &sourceFrame.getAVFrame ());
134
- if (ret < 0 )
135
- {
136
- throw std::runtime_error (" Encode video frame error: avcodec encode video frame - " +
137
- getDescriptionFromErrorCode (ret));
138
- }
139
- #endif
140
-
141
- #if LIBAVCODEC_VERSION_MAJOR > 53
142
- return ret == 0 && gotPacket == 1 ;
143
- #endif
144
- return ret == 0 ;
122
+ return encode (&sourceFrame.getAVFrame (), packet);
145
123
}
146
124
147
125
bool VideoEncoder::encodeFrame (CodedData& codedFrame)
148
126
{
149
- AVCodecContext& avCodecContext = _codec.getAVCodecContext ();
127
+ return encode (NULL , codedFrame.getAVPacket ());
128
+ }
150
129
151
- AVPacket& packet = codedFrame.getAVPacket ();
152
- packet.stream_index = 0 ;
130
+ bool VideoEncoder::encode (const AVFrame* decodedData, AVPacket& encodedData)
131
+ {
132
+ // Be sure that data of AVPacket is NULL so that the encoder will allocate it
133
+ encodedData.data = NULL ;
153
134
135
+ AVCodecContext& avCodecContext = _codec.getAVCodecContext ();
154
136
#if LIBAVCODEC_VERSION_MAJOR > 53
155
137
int gotPacket = 0 ;
156
- int ret = avcodec_encode_video2 (&avCodecContext, &packet, NULL , &gotPacket);
157
- if (ret != 0 && gotPacket == 0 )
138
+ const int ret = avcodec_encode_video2 (&avCodecContext, &encodedData, decodedData , &gotPacket);
139
+ if (ret != 0 )
158
140
{
159
- throw std::runtime_error (" Encode video frame error: avcodec encode last video frame - " +
141
+ throw std::runtime_error (" Encode video frame error: avcodec encode video frame - " +
160
142
getDescriptionFromErrorCode (ret));
161
143
}
162
- return ret == 0 && gotPacket == 1 ;
144
+ return gotPacket == 1 ;
163
145
#else
164
- int ret = avcodec_encode_video (&avCodecContext, packet .data , packet .size , NULL );
146
+ const int ret = avcodec_encode_video (&avCodecContext, encodedData .data , encodedData .size , decodedData );
165
147
if (ret < 0 )
166
148
{
167
- throw std::runtime_error (" Encode video frame error: avcodec encode last video frame - " +
149
+ throw std::runtime_error (" Encode video frame error: avcodec encode video frame - " +
168
150
getDescriptionFromErrorCode (ret));
169
151
}
170
- return ret == 0 ;
152
+ return true ;
171
153
#endif
172
154
}
155
+
173
156
}
0 commit comments