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