ffmpeg_wrapper.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2014, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  *
00030  * ffmpeg_wrapper.h
00031  *
00032  *  Created on: Oct 30, 2012
00033  *      Author: jkammerl
00034  */
00035 
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include <vector>
00040 
00041 #include <boost/cstdint.hpp>
00042 #include <boost/thread.hpp>
00043 #include <boost/date_time/posix_time/posix_time.hpp>
00044 #include <boost/date_time/posix_time/posix_time_io.hpp>
00045 
00046 #include "server_configuration.h"
00047 
00048 #ifndef FFMPEG_WRAPPER_HEADER_INCLUDED
00049 #define FFMPEG_WRAPPER_HEADER_INCLUDED
00050 
00051 #ifdef HAVE_AV_CONFIG_H
00052 #undef HAVE_AV_CONFIG_H
00053 #endif
00054 
00055 #define CODER_BUF_SIZE 50000
00056 
00057 
00058 namespace ros_http_video_streamer
00059 {
00060 
00061 // ffmpeg forward declarations
00062 class AVCodec;
00063 class AVOutputFormat;
00064 class AVCodecContext;
00065 class AVFormatContext;
00066 class AVPicture;
00067 class AVStream;
00068 class AVFrame;
00069 
00070 
00071 class FFMPEG_Wrapper
00072 {
00073 
00074 public:
00075 
00076   FFMPEG_Wrapper();
00077   ~FFMPEG_Wrapper();
00078 
00079   // initialize ffmpeg coding
00080   int  init(int input_width,
00081             int input_height,
00082             const ServerConfiguration& config);
00083 
00084   // shutdown ffmpeg coding
00085   void shutdown();
00086 
00087   // encode bgr8 encoded frame
00088   void encode_bgr_frame(uint8_t *bgr_data, std::vector<uint8_t>& encoded_frame);
00089   // encode rgb8 encoded frame
00090   void encode_rgb_frame(uint8_t *rgb_data, std::vector<uint8_t>& encoded_frame);
00091 
00092   // encode bgr8 encoded frame
00093   void encode_mono8_frame(uint8_t *gray8_data, std::vector<uint8_t>& encoded_frame);
00094   // encode bgr16 encoded frame
00095   void encode_mono16_frame(uint8_t *gray16_data, std::vector<uint8_t>& encoded_frame);
00096 
00097   // retrieve header
00098   void get_header(std::vector<uint8_t>& header);
00099   // retrieve trailer
00100   void get_trailer(std::vector<uint8_t>& trailer);
00101 
00102 private:
00103   // templated encoding method
00104   template<int CODING_FORMAT>
00105     void encode_frame(uint8_t *image_data, std::vector<uint8_t>& encoded_frame);
00106 
00107   //int ff_lockmgr(void **mutex, enum AVLockOp op);
00108 
00109   // mutex to protect encoding queue
00110   boost::mutex frame_mutex_;
00111   boost::mutex codec_mutex_;
00112 
00113   // ffmpeg contexts 
00114   AVCodec *ffmpeg_codec_;
00115   AVOutputFormat *ffmpeg_output_format_;
00116   AVCodecContext *ffmpeg_codec_context_;
00117   AVFormatContext *ffmpeg_format_context_;
00118   AVPicture* ffmpeg_src_picture_;
00119   AVPicture* ffmpeg_dst_picture_;
00120   AVStream *ffmpeg_video_st_;
00121   AVFrame *ffmpeg_frame_;
00122   double ffmpeg_video_pts_;
00123   struct SwsContext *ffmpeg_sws_ctx_;
00124 
00125   // Server and codec configuration
00126   ServerConfiguration config_;
00127 
00128   // image dimensions
00129   int input_width_;
00130   int input_height_;
00131   int output_width_;
00132   int output_height_;
00133 
00134   boost::posix_time::ptime time_started_;
00135 
00136   bool init_;
00137 
00138 };
00139 
00140 } // ros_http_video_streamer
00141 
00142 #endif
00143 


ros_web_video
Author(s): Julius Kammer
autogenerated on Fri Aug 28 2015 12:38:36