1 #ifndef USB_CAM_CONTROLLERS_DECODING_CONTROLLERS 2 #define USB_CAM_CONTROLLERS_DECODING_CONTROLLERS 12 #include <sensor_msgs/Image.h> 18 #include <libavcodec/avcodec.h> 19 #include <libavutil/imgutils.h> 20 #include <libswscale/swscale.h> 25 template < AVCodecID CodecId, AVPixelFormat DstFormat, const std::
string *DstEncoding >
36 avcodec_register_all();
37 av_log_set_level(AV_LOG_FATAL);
40 AVCodec *
const decoder(avcodec_find_decoder(CodecId));
49 ROS_ERROR_STREAM(
"Cannot allocate a decoder context (codec id: " << CodecId <<
")");
54 if (avcodec_open2(
decoder_ctx_.get(), decoder, NULL) < 0) {
72 av_init_packet(&packet);
77 while (packet.size > 0) {
81 const int len(avcodec_decode_video2(
decoder_ctx_.get(), frame.get(), &got_frame, &packet));
90 const sensor_msgs::ImagePtr out(
new sensor_msgs::Image());
91 const int data_size(av_image_get_buffer_size(DstFormat, frame->width, frame->height, 1));
93 out->height = frame->height;
94 out->width = frame->width;
95 out->encoding = *DstEncoding;
96 out->step = data_size / frame->height;
97 out->data.resize(data_size);
103 frame->width, frame->height,
106 frame->width, frame->height, DstFormat,
108 SWS_FAST_BILINEAR, NULL, NULL, NULL),
110 int stride(out->step);
111 uint8_t *dst(&out->data[0]);
112 sws_scale(convert_ctx.get(),
114 frame->data, frame->linesize, 0, frame->height,
135 av_frame_free(&frame);
141 avcodec_free_context(&ctx);
148 case AV_PIX_FMT_YUVJ420P:
149 return AV_PIX_FMT_YUV420P;
150 case AV_PIX_FMT_YUVJ411P:
151 return AV_PIX_FMT_YUV411P;
152 case AV_PIX_FMT_YUVJ422P:
153 return AV_PIX_FMT_YUV422P;
154 case AV_PIX_FMT_YUVJ440P:
155 return AV_PIX_FMT_YUV440P;
156 case AV_PIX_FMT_YUVJ444P:
157 return AV_PIX_FMT_YUV444P;
ros::Time getStamp() const
static AVPixelFormat toUndeprecated(const AVPixelFormat format)
const Byte * getStartAs() const
boost::shared_ptr< AVCodecContext > decoder_ctx_
std::size_t getLength() const
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
DecodingController< AV_CODEC_ID_MJPEG, AV_PIX_FMT_BGR24,&sensor_msgs::image_encodings::BGR8 > MjpegController
DecodingController< AV_CODEC_ID_H264, AV_PIX_FMT_BGR24,&sensor_msgs::image_encodings::BGR8 > H264Controller
static void deleteAVCodecContext(AVCodecContext *ctx)
virtual ~DecodingController()
void publish(const sensor_msgs::Image &message) const
virtual void startingImpl(const ros::Time &time)
virtual void stoppingImpl(const ros::Time &time)
virtual bool initImpl(usb_cam_hardware_interface::PacketInterface *hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh)
image_transport::Publisher publisher_
#define ROS_ERROR_STREAM(args)
static void deleteAVFrame(AVFrame *frame)
usb_cam_hardware_interface::PacketHandle packet_iface_
virtual void updateImpl(const ros::Time &time, const ros::Duration &period)