@@ -29,12 +29,7 @@ LibavStreamer::~LibavStreamer()
2929 avcodec_close (codec_context_);
3030 if (frame_)
3131 {
32- #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
33- av_free (frame_);
34- frame_ = NULL ;
35- #else
3632 av_frame_free (&frame_);
37- #endif
3833 }
3934 if (io_buffer_)
4035 delete io_buffer_;
@@ -157,11 +152,8 @@ void LibavStreamer::initialize(const cv::Mat &img)
157152 }
158153
159154 // Allocate frame buffers
160- #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
161- frame_ = avcodec_alloc_frame ();
162- #else
163155 frame_ = av_frame_alloc ();
164- # endif
156+
165157 av_image_alloc (frame_->data , frame_->linesize , output_width_, output_height_,
166158 codec_context_->pix_fmt , 1 );
167159
@@ -207,22 +199,12 @@ void LibavStreamer::sendImage(const cv::Mat &img, const rclcpp::Time &time)
207199 first_image_timestamp_ = time;
208200 }
209201 std::vector<uint8_t > encoded_frame;
210- #if (LIBAVUTIL_VERSION_MAJOR < 53)
211- PixelFormat input_coding_format = PIX_FMT_BGR24;
212- #else
202+
213203 AVPixelFormat input_coding_format = AV_PIX_FMT_BGR24;
214- #endif
215204
216- #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
217- AVPicture *raw_frame = new AVPicture;
218- avpicture_fill (raw_frame, img.data , input_coding_format, output_width_, output_height_);
219- #else
220205 AVFrame *raw_frame = av_frame_alloc ();
221206 av_image_fill_arrays (raw_frame->data , raw_frame->linesize ,
222207 img.data , input_coding_format, output_width_, output_height_, 1 );
223- #endif
224-
225-
226208
227209 // Convert from opencv to libav
228210 if (!sws_context_)
@@ -241,29 +223,11 @@ void LibavStreamer::sendImage(const cv::Mat &img, const rclcpp::Time &time)
241223 (const uint8_t * const *)raw_frame->data , raw_frame->linesize , 0 ,
242224 output_height_, frame_->data , frame_->linesize );
243225
244- #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
245- delete raw_frame;
246- #else
247226 av_frame_free (&raw_frame);
248- #endif
249227
250228 // Encode the frame
251229 AVPacket* pkt = av_packet_alloc ();
252- int got_packet;
253-
254- #if (LIBAVCODEC_VERSION_MAJOR < 54)
255- int buf_size = 6 * output_width_ * output_height_;
256- pkt.data = (uint8_t *)av_malloc (buf_size);
257- pkt.size = avcodec_encode_video (codec_context_, pkt.data , buf_size, frame_);
258- got_packet = pkt.size > 0 ;
259- #elif (LIBAVCODEC_VERSION_MAJOR < 57)
260- pkt.data = NULL ; // packet data will be allocated by the encoder
261- pkt.size = 0 ;
262- if (avcodec_encode_video2 (codec_context_, &pkt, frame_, &got_packet) < 0 )
263- {
264- throw std::runtime_error (" Error encoding video frame" );
265- }
266- #else
230+
267231 if (avcodec_send_frame (codec_context_, frame_) < 0 )
268232 {
269233 throw std::runtime_error (" Error encoding video frame" );
@@ -272,10 +236,8 @@ void LibavStreamer::sendImage(const cv::Mat &img, const rclcpp::Time &time)
272236 {
273237 throw std::runtime_error (" Error retrieving encoded packet" );
274238 }
275- got_packet = pkt->size > 0 ;
276- #endif
277239
278- if (got_packet )
240+ if (pkt-> size > 0 )
279241 {
280242 std::size_t size;
281243 uint8_t *output_buf;
@@ -301,15 +263,8 @@ void LibavStreamer::sendImage(const cv::Mat &img, const rclcpp::Time &time)
301263 {
302264 encoded_frame.clear ();
303265 }
304- #if LIBAVCODEC_VERSION_INT < 54
305- av_free (pkt.data );
306- #endif
307266
308- #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
309- av_free_packet (&pkt);
310- #else
311267 av_packet_unref (pkt);
312- #endif
313268
314269 connection_->write_and_clear (encoded_frame);
315270}
0 commit comments