ViSP  2.6.2
vpVideoReader.cpp
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 - 2012 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  * Read videos and image sequences.
36  *
37  * Authors:
38  * Nicolas Melchior
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
48 #include <visp/vpDebug.h>
49 #include <visp/vpVideoReader.h>
50 
51 #include <iostream>
52 #include <fstream>
53 
58 {
59  imSequence = NULL;
60  #ifdef VISP_HAVE_FFMPEG
61  ffmpeg = NULL;
62  #endif
63  initFileName = false;
64  isOpen = false;
65  firstFrame = 0;
66  frameCount = 0;
67  lastFrame = 0;
68 }
69 
70 
75 {
76  if (imSequence != NULL)
77  {
78  delete imSequence;
79  }
80  #ifdef VISP_HAVE_FFMPEG
81  if (ffmpeg != NULL)
82  {
83  delete ffmpeg;
84  }
85  #endif
86 }
87 
88 
98 void vpVideoReader::setFileName(const char *filename)
99 {
100  if (filename == '\0')
101  {
102  vpERROR_TRACE("filename empty ") ;
103  throw (vpImageException(vpImageException::noFileNameError,"filename empty ")) ;
104  }
105 
106  strcpy(this->fileName,filename);
107 
108  formatType = getFormat(fileName);
109 
110  initFileName = true;
111 }
112 
113 
122 {
123  if (!initFileName)
124  {
125  vpERROR_TRACE("The generic filename has to be set");
126  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
127  }
128 
129  if (formatType == FORMAT_PGM ||
130  formatType == FORMAT_PPM ||
131  formatType == FORMAT_JPEG ||
132  formatType == FORMAT_PNG)
133  {
134  imSequence = new vpDiskGrabber;
135  imSequence->setGenericName(fileName);
136  imSequence->setImageNumber((int)firstFrame);
137  }
138  #ifdef VISP_HAVE_FFMPEG
139  else if (formatType == FORMAT_AVI ||
140  formatType == FORMAT_MPEG ||
141  formatType == FORMAT_MOV ||
142  formatType == FORMAT_OGV)
143  {
144  ffmpeg = new vpFFMPEG;
145  if(!ffmpeg->openStream(fileName, vpFFMPEG::COLORED))
146  throw (vpException(vpException::ioError ,"Could not open the video"));
147  ffmpeg->initStream();
148  }
149 
150  #else
151  else if (formatType == FORMAT_AVI ||
152  formatType == FORMAT_MPEG ||
153  formatType == FORMAT_MOV ||
154  formatType == FORMAT_OGV)
155  {
156  vpERROR_TRACE("To read video files the FFmpeg library has to be installed");
157  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
158  }
159  #endif
160  else if (formatType == FORMAT_UNKNOWN)
161  {
162  vpERROR_TRACE("The format of the file does not correpsond to a readable format.");
163  throw (vpException(vpException::fatalError ,"The format of the file does not correpsond to a readable format."));
164  }
165 
166  frameCount = firstFrame;
167  if(!getFrame(I,firstFrame))
168  {
169  vpERROR_TRACE("Could not read the first frame");
170  throw (vpException(vpException::ioError ,"Could not read the first frame"));
171  }
172  height = I.getHeight();
173  width = I.getWidth();
174 
175  isOpen = true;
176 
177  findLastFrameIndex();
178 }
179 
180 
189 {
190  if (!initFileName)
191  {
192  vpERROR_TRACE("The generic filename has to be set");
193  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
194  }
195 
196  if (formatType == FORMAT_PGM ||
197  formatType == FORMAT_PPM ||
198  formatType == FORMAT_JPEG ||
199  formatType == FORMAT_PNG)
200  {
201  imSequence = new vpDiskGrabber;
202  imSequence->setGenericName(fileName);
203  imSequence->setImageNumber((int)firstFrame);
204  }
205  #ifdef VISP_HAVE_FFMPEG
206  else if (formatType == FORMAT_AVI ||
207  formatType == FORMAT_MPEG ||
208  formatType == FORMAT_MOV ||
209  formatType == FORMAT_OGV)
210  {
211  ffmpeg = new vpFFMPEG;
212  if (!ffmpeg->openStream(fileName, vpFFMPEG::GRAY_SCALED))
213  throw (vpException(vpException::ioError ,"Could not open the video"));
214  ffmpeg->initStream();
215  }
216  #else
217  else if (formatType == FORMAT_AVI ||
218  formatType == FORMAT_MPEG ||
219  formatType == FORMAT_MOV ||
220  formatType == FORMAT_OGV)
221  {
222  vpERROR_TRACE("To read video files the FFmpeg library has to be installed");
223  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
224  }
225  #endif
226  else if (formatType == FORMAT_UNKNOWN)
227  {
228  vpERROR_TRACE("The format of the file does not correpsond to a readable format.");
229  throw (vpException(vpException::fatalError ,"The format of the file does not correpsond to a readable format."));
230  }
231 
232  frameCount = firstFrame;
233  if(!getFrame(I,firstFrame))
234  {
235  vpERROR_TRACE("Could not read the first frame");
236  throw (vpException(vpException::ioError ,"Could not read the first frame"));
237  }
238  height = I.getHeight();
239  width = I.getWidth();
240 
241  isOpen = true;
242 
243  findLastFrameIndex();
244 }
245 
246 
255 {
256  if (!isOpen)
257  {
258  vpERROR_TRACE("Use the open method before");
259  throw (vpException(vpException::notInitialized,"file not yet opened"));
260  }
261 
262  //getFrame(I,frameCount);
263  if (imSequence != NULL)
264  imSequence->acquire(I);
265  #ifdef VISP_HAVE_FFMPEG
266  else if (ffmpeg !=NULL)
267  ffmpeg->acquire(I);
268  #endif
269 
270  frameCount++;
271 }
272 
273 
282 {
283  if (!isOpen)
284  {
285  vpERROR_TRACE("Use the open method before");
286  throw (vpException(vpException::notInitialized,"file not yet opened"));
287  }
288 
289  //getFrame(I,frameCount);
290  if (imSequence != NULL)
291  imSequence->acquire(I);
292  #ifdef VISP_HAVE_FFMPEG
293  else if (ffmpeg != NULL)
294  ffmpeg->acquire(I);
295  #endif
296 
297  frameCount++;
298 }
299 
300 
314 {
315  if (imSequence != NULL)
316  {
317  try
318  {
319  imSequence->acquire(I, frame);
320  }
321  catch(...)
322  {
323  vpERROR_TRACE("Couldn't find the %u th frame", frame) ;
324  return false;
325  }
326  }
327  #ifdef VISP_HAVE_FFMPEG
328  else
329  {
330 
331  if(!ffmpeg->getFrame(I, (unsigned int)frame))
332  {
333  vpERROR_TRACE("Couldn't find the %ld th frame", frame) ;
334  return false;
335  }
336  }
337  #endif
338  return true;
339 }
340 
341 
355 {
356  if (imSequence != NULL)
357  {
358  try
359  {
360  imSequence->acquire(I, frame);
361  }
362  catch(...)
363  {
364  vpERROR_TRACE("Couldn't find the %u th frame", frame) ;
365  return false;
366  }
367  }
368  #ifdef VISP_HAVE_FFMPEG
369  else
370  {
371  if(!ffmpeg->getFrame(I, (unsigned int)frame))
372  {
373  vpERROR_TRACE("Couldn't find the %ld th frame", frame) ;
374  return false;
375  }
376  }
377  #endif
378  return true;
379 }
380 
381 
387 vpVideoReader::vpVideoFormatType
388 vpVideoReader::getFormat(const char *filename)
389 {
390  std::string sfilename(filename);
391 
392  std::string ext = vpVideoReader::getExtension(sfilename);
393 
394  if (ext.compare(".PGM") == 0)
395  return FORMAT_PGM;
396  else if (ext.compare(".pgm") == 0)
397  return FORMAT_PGM;
398  else if (ext.compare(".PPM") == 0)
399  return FORMAT_PPM;
400  else if (ext.compare(".ppm") == 0)
401  return FORMAT_PPM;
402  else if (ext.compare(".JPG") == 0)
403  return FORMAT_JPEG;
404  else if (ext.compare(".jpg") == 0)
405  return FORMAT_JPEG;
406  else if (ext.compare(".JPEG") == 0)
407  return FORMAT_JPEG;
408  else if (ext.compare(".jpeg") == 0)
409  return FORMAT_JPEG;
410  else if (ext.compare(".PNG") == 0)
411  return FORMAT_PNG;
412  else if (ext.compare(".png") == 0)
413  return FORMAT_PNG;
414  else if (ext.compare(".AVI") == 0)
415  return FORMAT_AVI;
416  else if (ext.compare(".avi") == 0)
417  return FORMAT_AVI;
418  else if (ext.compare(".MPEG") == 0)
419  return FORMAT_MPEG;
420  else if (ext.compare(".mpeg") == 0)
421  return FORMAT_MPEG;
422  else if (ext.compare(".MPG") == 0)
423  return FORMAT_MPEG;
424  else if (ext.compare(".mpg") == 0)
425  return FORMAT_MPEG;
426  else if (ext.compare(".MOV") == 0)
427  return FORMAT_MOV;
428  else if (ext.compare(".mov") == 0)
429  return FORMAT_MOV;
430  else if (ext.compare(".OGV") == 0)
431  return FORMAT_OGV;
432  else if (ext.compare(".ogv") == 0)
433  return FORMAT_OGV;
434  else
435  return FORMAT_UNKNOWN;
436 }
437 
438 // return the extension of the file including the dot
439 std::string vpVideoReader::getExtension(const std::string &filename)
440 {
441  // extract the extension
442  size_t dot = filename.find_last_of(".");
443  std::string ext = filename.substr(dot, filename.size()-1);
444  return ext;
445 }
446 
447 
451 void
452 vpVideoReader::findLastFrameIndex()
453 {
454  if (!isOpen)
455  {
456  vpERROR_TRACE("Use the open method before");
457  throw (vpException(vpException::notInitialized,"file not yet opened"));
458  }
459 
460  if (imSequence != NULL)
461  {
462  char name[FILENAME_MAX];
463  int image_number = firstFrame;
464  std::fstream file;
465  bool failed;
466  do
467  {
468  sprintf(name,fileName,image_number) ;
469  file.open(name, std::fstream::in);
470  failed = file.fail();
471  if (!failed) file.close();
472  image_number++;
473  }while(!failed);
474 
475  lastFrame = image_number - 2;
476  }
477 
478  #ifdef VISP_HAVE_FFMPEG
479  else if (ffmpeg != NULL)
480  lastFrame = (long)(ffmpeg->getFrameNumber() - 1);
481  #endif
482 }
unsigned int getWidth() const
Definition: vpImage.h:154
#define vpERROR_TRACE
Definition: vpDebug.h:379
bool getFrame(vpImage< vpRGBa > &I, unsigned int frameNumber)
Definition: vpFFMPEG.cpp:275
Error that can be emited by the vpImage class and its derivates.
unsigned long getFrameNumber() const
Definition: vpFFMPEG.h:224
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
unsigned int height
Number of rows in the image.
bool openStream(const char *filename, vpFFMPEGColorType color_type)
Definition: vpFFMPEG.cpp:101
void acquire(vpImage< vpRGBa > &I)
This class interfaces the FFmpeg library to enable the reading of video files.
Definition: vpFFMPEG.h:154
void setImageNumber(long number)
void setFileName(const char *filename)
Class to grab (ie. read) images from the disk.
bool acquire(vpImage< vpRGBa > &I)
Definition: vpFFMPEG.cpp:328
unsigned int getHeight() const
Definition: vpImage.h:145
void acquire(vpImage< unsigned char > &I)
bool initStream()
Definition: vpFFMPEG.cpp:217
unsigned int width
Number of columns in the image.
void setGenericName(const char *genericName)