1 #ifndef CODEC_IMAGE_TRANSPORT_DECODING_SUBSCRIBERS_HPP 2 #define CODEC_IMAGE_TRANSPORT_DECODING_SUBSCRIBERS_HPP 11 #include <sensor_msgs/CompressedImage.h> 12 #include <sensor_msgs/Image.h> 15 #include <boost/mpl/string.hpp> 16 #include <boost/shared_ptr.hpp> 19 #include <libavcodec/avcodec.h> 20 #include <libavutil/imgutils.h> 21 #include <libswscale/swscale.h> 26 template < AVCodecID CodecID,
typename TransportName >
34 virtual std::string
getTransportName()
const {
return boost::mpl::c_str< TransportName >::value; }
38 uint32_t queue_size,
const Callback &callback,
43 avcodec_register_all();
44 av_log_set_level(AV_LOG_FATAL);
47 AVCodec *
const decoder(avcodec_find_decoder(CodecID));
59 if (avcodec_open2(
decoder_ctx_.get(), decoder, NULL) < 0) {
71 av_init_packet(&packet);
72 packet.size = message->data.size();
73 packet.data =
const_cast< uint8_t *
>(&message->data[0]);
75 while (packet.size > 0) {
85 const int len(avcodec_decode_video2(
decoder_ctx_.get(), frame.get(), &got_frame, &packet));
94 const sensor_msgs::ImagePtr out(
new sensor_msgs::Image());
95 out->header = message->header;
96 out->height = frame->height;
97 out->width = frame->width;
99 out->step = 3 * frame->width;
100 out->data.resize(3 * frame->width * frame->height);
106 frame->width, frame->height,
109 frame->width, frame->height, AV_PIX_FMT_BGR24,
111 SWS_FAST_BILINEAR, NULL, NULL, NULL),
113 int stride(out->step);
114 uint8_t *dst(&out->data[0]);
115 sws_scale(convert_ctx.get(),
117 frame->data, frame->linesize, 0, frame->height,
134 av_frame_free(&frame);
140 avcodec_free_context(&ctx);
147 case AV_PIX_FMT_YUVJ420P:
148 return AV_PIX_FMT_YUV420P;
149 case AV_PIX_FMT_YUVJ411P:
150 return AV_PIX_FMT_YUV411P;
151 case AV_PIX_FMT_YUVJ422P:
152 return AV_PIX_FMT_YUV422P;
153 case AV_PIX_FMT_YUVJ440P:
154 return AV_PIX_FMT_YUV440P;
155 case AV_PIX_FMT_YUVJ444P:
156 return AV_PIX_FMT_YUV444P;
static void deleteAVCodecContext(AVCodecContext *ctx)
static void deleteAVFrame(AVFrame *frame)
DecodingSubscriber< AV_CODEC_ID_H264, boost::mpl::string< 'h', '2', '6', '4' > > H264Subscriber
boost::function< void(const sensor_msgs::ImageConstPtr &)> Callback
virtual void subscribeImpl(ros::NodeHandle &nh, const std::string &base_topic, uint32_t queue_size, const Callback &callback, const ros::VoidPtr &tracked_object, const image_transport::TransportHints &transport_hints)
virtual ~DecodingSubscriber()
virtual void internalCallback(const sensor_msgs::CompressedImage::ConstPtr &message, const Callback &user_cb)
static AVPixelFormat toUndeprecated(const AVPixelFormat format)
const ros::NodeHandle & nh() const
image_transport::SimpleSubscriberPlugin< sensor_msgs::CompressedImage > Base
virtual std::string getTransportName() const
boost::shared_ptr< AVCodecContext > decoder_ctx_
virtual void subscribeImpl(ros::NodeHandle &nh, const std::string &base_topic, uint32_t queue_size, const Callback &callback, const ros::VoidPtr &tracked_object, const TransportHints &transport_hints)