iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FfmpegVideoReader.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies (voorhies@usc.edu)
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_VIDEOREADERS_FFMPEGVIDEOREADER_H
37 #define INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_VIDEOREADERS_FFMPEGVIDEOREADER_H
38 
39 #include <nrt/config.h>
40 
41 #ifdef NRT_HAVE_FFMPEG
42 
44 #include <fstream>
45 
46 class AVFormatContext;
47 class AVCodecContext;
48 class AVCodec;
49 class AVFrame;
50 class AVStream;
51 class SwsContext;
52 
53 namespace nrt
54 {
55  //! A VideoReader for decoding videos using the FFMpeg library
56  /*! This VideoReader is a wrapper around ffmpeg, and allows users to stream in all sorts of useful formats such as mpeg, avi,
57  mov, ogg, etc.
58 
59  \note { While FfmpegVideoReader officially supports the full VideoReaderBase API, you should be wary when calling frameNumber(),
60  frameRange(), and seek(). FFMpeg (and it's associated video formats) have no real concept of 'frames,' as everything is done based on
61  time. For example, a call to FfmpegVideoReader::seek() will internally try to convert the requested frame number into a timestamp
62  before telling FFMpeg to seek to that time. For most cases, users should not need to worry about this. However, if you require
63  _precise_ frame seeking, then you should try using the MgzJVideoReader and it's associated .mgzJ file format. Here is a specific list
64  of issues concerning FFMpeg's approximate time:
65 
66  \li frameNumber() will return the approximate current frame number, given the current timestamp of the video. Beware that consecutive
67  calls to frameNumber() may give non-consecutive frame numbers.
68 
69  \li seek() will try its best to seek to your requested frame number, but there will likely be some slop in the result.
70 
71  \li frameRange() will also try its best to determine the number of frames in the video, but this is just calculated from the bitrate,
72  reported framerate, number of bytes, etc. in the video. } */
73  class FfmpegVideoReader: public VideoReaderBase
74  {
75  public:
76  FfmpegVideoReader();
77 
78  ~FfmpegVideoReader();
79 
80  GenericImage const in();
81 
82  void open(std::string const& filename);
83 
84  void close();
85 
86  bool const seek(size_t const frameNumber);
87 
88  bool const ok() { return itsOk; }
89 
90  size_t const nextFrameNumber();
91 
92  FrameRange const frameRange();
93 
94  static std::string const id() { return "mpeg|mpg|mov|avi|vob|ogg"; }
95 
96  static std::string const description()
97  { return "An FFMpeg library adapter."; }
98 
99  private:
100  bool itsOk;
101 
102  std::string itsFilename;
103  AVFormatContext *itsFormatCtx;
104  AVCodecContext *itsCodecCtx;
105  AVCodec *itsCodec;
106  AVFrame *itsFrame;
107  SwsContext *itsConvertContext;
108  AVStream *itsVideoStream;
109  int itsVideoStreamIdx;
110 
111  void prepareNextFrame();
112  nrt::GenericImage itsNextFrame;
113  nrt::FrameRange itsFrameRange;
114  };
115 } // nrt
116 
117 #endif // INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_VIDEOREADERS_FFMPEGVIDEOREADER_H
118 #endif /* NRT_IMAGEPROC_IMAGESOURCE_MGZJIMAGESOURCE_H */