H3D API  2.4.1
FFmpegDecoder.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB, PolyDimensions
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
24 // Original file contributed by PolyDimensions.
25 //
29 //
31 
32 #ifndef __FFMPEGDECODER_H__
33 #define __FFMPEGDECODER_H__
34 
35 #include <H3D/FieldTemplates.h>
36 #include <H3D/SFTime.h>
37 #include <time.h>
38 
39 #ifdef HAVE_FFMPEG
40 
41 extern "C" {
42 #define __STDC_CONSTANT_MACROS
43 
44 #ifdef _STDINT_H
45 
46 #undef _STDINT_H
47 
48 #endif
49 #include <avcodec.h>
50 #include <avformat.h>
51 #include <swscale.h>
52 }
53 
55 
56 namespace H3D {
62  class H3DAPI_API FFmpegDecoder : public H3DVideoClipDecoderNode {
64  class FFmpegEventHandler: public AutoUpdate< SFTime > {
65  virtual void update();
66  };
67 
68  public:
70  FFmpegDecoder();
71 
73  ~FFmpegDecoder();
74 
76  virtual bool testClip( const string &url );
77 
79  virtual bool loadClip( const string &url );
80 
82  virtual void startPlaying();
83 
86  virtual void stopPlaying();
87 
89  virtual void pausePlaying();
90 
93  virtual void setLooping( bool /*v*/ ) {
94 
95  // Quick fix - for some reason the incorrect value of v gets passed
96  looping = true;
97 
98 /*
99  if (v == true)
100  Console(LogLevel::Error) << "setLooping(true)" << endl;
101  if (v == false)
102  Console(LogLevel::Error) << "setLooping(false)" << endl;
103  looping = v;
104 */
105  }
106 
108  virtual H3DTime getPosition();
109 
111  virtual void setPosition( H3DTime pos );
112 
117  virtual bool setRate( double r ) {
118  rate = r;
119  return 1;
120  }
121 
124  virtual H3DTime getDuration() {
125  return duration;
126  }
127 
129  virtual bool haveNewFrame() { return have_new_frame; }
130 
132  virtual void getNewFrame( unsigned char *buffer );
133 
135  virtual unsigned int getFrameWidth() { return frame_width; }
136 
138  virtual unsigned int getFrameHeight() { return frame_height; }
139 
141  virtual unsigned int getFrameBitsPerPixel() { return frame_bpp; }
142 
144  virtual unsigned int getFrameSize() {
145  return frame_height * frame_width * (frame_bpp / 8);
146  }
147 
149  virtual bool theTimeIsRight();
150 
152  virtual Image::PixelType getFramePixelType() { return Image::RGB; }
153 
156  return Image::UNSIGNED;
157  }
158 
161 
164 
167  static bool supportsFileType( const string &url );
168 
169  protected:
170 
171  uint8_t *temp_buffer;
172 
173  struct SwsContext *imgConvertCtx;
174 
175  AVFormatContext *pFormatCtx;
176 
177  AVCodecContext *pCodecCtx;
178 
179  AVCodecContext *aCodecCtx;
180 
181  AVCodec *pCodec;
182 
183  AVCodec *aCodec;
184 
185  AVFrame *pFrame;
186 
187  AVFrame *pFrameRGB;
188 
189  unsigned char *data;
190 
191  AVPacket packet;
192 
193  double pts;
194 
195  int frameFinished;
196 
197  int videoStream;
198 
199  int audioStream;
200 
201  int numBytes;
202 
203  long current_frame;
204 
205  float video_fps;
206 
207  unsigned long current_time;
208 
209  unsigned long start_time;
210 
213  void cleanupFFmpeg( void );
214 
216  unsigned int frame_width;
217 
219  unsigned int frame_height;
220 
222  unsigned int frame_bpp;
223 
225  unsigned int data_size;
226 
230 
232  double rate;
233 
235  bool looping;
236 
239 
242 
243  };
244 
245 }
246 #endif // HAVE_FFMPEG
247 #endif // __FFMPEGDECODER_H__
Contains different templates to modify field behaviour.
Header file for H3DVideoClipDecoderNode, the abstract base class for all nodes the decodes video clip...
Contains the SFTime field class.
This node uses FFmpeg to decode video clips.
Definition: FFmpegDecoder.h:62
static DecoderRegistration reader_registration
Register this node to the H3DVideoClipDecoderNodes available.
Definition: FFmpegDecoder.h:163
virtual unsigned int getFrameBitsPerPixel()
Get the number of bits per pixel in the current frame.
Definition: FFmpegDecoder.h:141
virtual bool setRate(double r)
Set the playback rate.
Definition: FFmpegDecoder.h:117
bool just_a_test
For proper functioning of testClip()
Definition: FFmpegDecoder.h:241
unsigned int frame_height
The height in pixels of the frame.
Definition: FFmpegDecoder.h:219
unsigned int frame_bpp
Bits per pixel of the frame.
Definition: FFmpegDecoder.h:222
virtual H3DTime getDuration()
Returns the duration in seconds at normal play rate of the currently loaded video clip.
Definition: FFmpegDecoder.h:124
unsigned int frame_width
The width in pixels of the frame.
Definition: FFmpegDecoder.h:216
virtual Image::PixelType getFramePixelType()
The pixel type of the current frame.
Definition: FFmpegDecoder.h:152
virtual bool haveNewFrame()
Returns true when a new frame is available.
Definition: FFmpegDecoder.h:129
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: FFmpegDecoder.h:160
virtual unsigned int getFrameWidth()
The width in pixels of the current frame.
Definition: FFmpegDecoder.h:135
bool have_new_frame
Set to true when a new frame is available, reset to false when getNewFrame is called.
Definition: FFmpegDecoder.h:229
double rate
The speed at which to play the clip. 1 is normal speed, 0.5 half.
Definition: FFmpegDecoder.h:232
virtual unsigned int getFrameSize()
The size in bytes of the current frame.
Definition: FFmpegDecoder.h:144
bool looping
True if the video should loop when it reaches the end.
Definition: FFmpegDecoder.h:235
unsigned int data_size
The size in bytes of the data.
Definition: FFmpegDecoder.h:225
virtual unsigned int getFrameHeight()
The height in pixels of the current frame.
Definition: FFmpegDecoder.h:138
virtual Image::PixelComponentType getFramePixelComponentType()
The pixel component type of the current frame.
Definition: FFmpegDecoder.h:155
virtual void setLooping(bool)
Set whether the clip should loop and start from the start again when the end has been reached.
Definition: FFmpegDecoder.h:93
H3DTime duration
The duration of the current clip in seconds.
Definition: FFmpegDecoder.h:238
H3DVideoClipDecoderNode is a virtual base class for classes decoding video clips making new frames av...
Definition: H3DVideoClipDecoderNode.h:43
double H3DTime
H3D API namespace.
Definition: Anchor.h:38
The AutoUpdate field is a template to force the BaseField to update itself as soon as an event is rec...
Definition: FieldTemplates.h:130
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194
Class used to register a class to the registered file readers.
Definition: H3DVideoClipDecoderNode.h:60