Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpFFMPEG.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Class that manages the FFMPEG library.
32  *
33  * Authors:
34  * Nicolas Melchior
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
44 #ifndef vpFFMPEG_H
45 #define vpFFMPEG_H
46 
47 #include <visp3/io/vpImageIo.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <vector>
51 
52 #ifdef VISP_HAVE_FFMPEG
53 
54 // Fix for the following compilation error:
55 // libavutil/common.h:154: error: UINT64_C was not declared in this scope
56 // libavutil/common.h is no more autosufficient for C++ program because
57 // stdint.h defines UINT64_C only for C program and not C++ program
58 #ifdef __cplusplus
59 # ifndef __STDC_CONSTANT_MACROS
60 # define __STDC_CONSTANT_MACROS
61 # endif
62 # ifdef _STDINT_H
63 # undef _STDINT_H
64 # endif
65 // On OS X
66 # ifdef _STDINT_H_
67 # undef _STDINT_H_
68 # endif
69 # include <stdint.h>
70 
71 # ifndef INT64_C
72 # define INT64_C(c) (c ## LL)
73 # endif
74 # ifndef UINT64_C
75 # define UINT64_C(c) (c ## ULL)
76 # endif
77 #endif
78 // end fix
79 
80 
81 extern "C"
82 {
83 #include <libavcodec/avcodec.h> // requested for CodecID enum
84 //#include <avformat.h>
85 //#include <swscale.h>
86 }
87 
88 struct AVFormatContext;
89 struct AVCodecContext;
90 struct AVCodec;
91 struct AVFrame;
92 struct AVFrame;
93 struct AVFrame;
94 struct AVPacket;
95 struct SwsContext;
96 
145 class VISP_EXPORT vpFFMPEG
146 {
147  public:
148  typedef enum
149  {
152  }vpFFMPEGColorType;
153 
154  private:
156  int width, height;
158  unsigned long frameNumber;
160  AVFormatContext *pFormatCtx;
161  AVCodecContext *pCodecCtx;
162  AVCodec *pCodec;
163  AVFrame *pFrame;
164  AVFrame *pFrameRGB;
165  AVFrame *pFrameGRAY;
166  AVPacket *packet;
167  SwsContext *img_convert_ctx ;
168  unsigned int videoStream;
169  int numBytes ;
170  uint8_t * buffer ;
171  std::vector<int64_t> index;
173  bool streamWasOpen;
175  bool streamWasInitialized;
177  vpFFMPEGColorType color_type;
178 
180  FILE *f;
182  uint8_t *outbuf, *picture_buf;
184  int outbuf_size;
186  int out_size;
188  unsigned int bit_rate;
190  bool encoderWasOpened;
191  double framerate_stream; // input stream
192  int framerate_encoder; // output stream
193 
194  public:
195  vpFFMPEG();
196  ~vpFFMPEG();
197 
198  bool acquire(vpImage<vpRGBa> &I);
199  bool acquire(vpImage<unsigned char> &I);
200 
201  void closeStream();
202 
203  bool endWrite();
204 
205  bool getFrame(vpImage<vpRGBa> &I, unsigned int frameNumber);
206  bool getFrame(vpImage<unsigned char> &I, unsigned int frameNumber);
212  inline unsigned long getFrameNumber() const {return frameNumber;}
219  inline double getFramerate() const {
220  if (streamWasOpen)
221  return framerate_stream;
222  throw vpException(vpException::ioError, "Video stream not opened.");
223  }
229  inline int getHeight() const {return height;}
235  inline int getWidth() const {return width;}
236 
237  bool initStream();
238 
239 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,51,110) // libavcodec 54.51.100
240  bool openEncoder(const char *filename, unsigned int width, unsigned int height, CodecID codec = CODEC_ID_MPEG1VIDEO);
241 #else
242  bool openEncoder(const char *filename, unsigned int width, unsigned int height, AVCodecID codec = AV_CODEC_ID_MPEG1VIDEO);
243 #endif
244  bool openStream(const char *filename,vpFFMPEGColorType color_type);
245 
246  bool saveFrame(vpImage<vpRGBa> &I);
247  bool saveFrame(vpImage<unsigned char> &I);
253  inline void setBitRate(const unsigned int rate) {this->bit_rate = rate;}
259  inline void setFramerate(const int framerate) {framerate_encoder = framerate;}
260 
261  private:
262  void copyBitmap(vpImage<vpRGBa> &I);
263  void copyBitmap(vpImage<unsigned char> &I);
264  void writeBitmap(vpImage<vpRGBa> &I);
265  void writeBitmap(vpImage<unsigned char> &I);
266 };
267 #endif
268 #endif
void setBitRate(const unsigned int rate)
Definition: vpFFMPEG.h:253
double getFramerate() const
Definition: vpFFMPEG.h:219
error that can be emited by ViSP classes.
Definition: vpException.h:73
unsigned long getFrameNumber() const
Definition: vpFFMPEG.h:212
int getHeight() const
Definition: vpFFMPEG.h:229
This class interfaces the FFmpeg library to enable video stream reading or writing.
Definition: vpFFMPEG.h:145
int getWidth() const
Definition: vpFFMPEG.h:235
void setFramerate(const int framerate)
Definition: vpFFMPEG.h:259