ViSP  2.7.0
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 - 2013 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 
122 void vpVideoReader::setFileName(const std::string &filename)
123 {
124  setFileName(filename.c_str());
125 }
126 
135 {
136  if (!initFileName)
137  {
138  vpERROR_TRACE("The generic filename has to be set");
139  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
140  }
141 
142  if (formatType == FORMAT_PGM ||
143  formatType == FORMAT_PPM ||
144  formatType == FORMAT_JPEG ||
145  formatType == FORMAT_PNG)
146  {
147  imSequence = new vpDiskGrabber;
148  imSequence->setGenericName(fileName);
149  imSequence->setImageNumber((int)firstFrame);
150  }
151  #ifdef VISP_HAVE_FFMPEG
152  else if (formatType == FORMAT_AVI ||
153  formatType == FORMAT_MPEG ||
154  formatType == FORMAT_MOV ||
155  formatType == FORMAT_OGV)
156  {
157  ffmpeg = new vpFFMPEG;
158  if(!ffmpeg->openStream(fileName, vpFFMPEG::COLORED))
159  throw (vpException(vpException::ioError ,"Could not open the video"));
160  ffmpeg->initStream();
161  }
162 
163  #else
164  else if (formatType == FORMAT_AVI ||
165  formatType == FORMAT_MPEG ||
166  formatType == FORMAT_MOV ||
167  formatType == FORMAT_OGV)
168  {
169  vpERROR_TRACE("To read video files the FFmpeg library has to be installed");
170  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
171  }
172  #endif
173  else if (formatType == FORMAT_UNKNOWN)
174  {
175  vpERROR_TRACE("The format of the file does not correpsond to a readable format.");
176  throw (vpException(vpException::fatalError ,"The format of the file does not correpsond to a readable format."));
177  }
178 
179  frameCount = firstFrame;
180  if(!getFrame(I,firstFrame))
181  {
182  vpERROR_TRACE("Could not read the first frame");
183  throw (vpException(vpException::ioError ,"Could not read the first frame"));
184  }
185  height = I.getHeight();
186  width = I.getWidth();
187 
188  isOpen = true;
189 
190  findLastFrameIndex();
191 }
192 
193 
202 {
203  if (!initFileName)
204  {
205  vpERROR_TRACE("The generic filename has to be set");
206  throw (vpImageException(vpImageException::noFileNameError,"filename empty"));
207  }
208 
209  if (formatType == FORMAT_PGM ||
210  formatType == FORMAT_PPM ||
211  formatType == FORMAT_JPEG ||
212  formatType == FORMAT_PNG)
213  {
214  imSequence = new vpDiskGrabber;
215  imSequence->setGenericName(fileName);
216  imSequence->setImageNumber((int)firstFrame);
217  }
218  #ifdef VISP_HAVE_FFMPEG
219  else if (formatType == FORMAT_AVI ||
220  formatType == FORMAT_MPEG ||
221  formatType == FORMAT_MOV ||
222  formatType == FORMAT_OGV)
223  {
224  ffmpeg = new vpFFMPEG;
225  if (!ffmpeg->openStream(fileName, vpFFMPEG::GRAY_SCALED))
226  throw (vpException(vpException::ioError ,"Could not open the video"));
227  ffmpeg->initStream();
228  }
229  #else
230  else if (formatType == FORMAT_AVI ||
231  formatType == FORMAT_MPEG ||
232  formatType == FORMAT_MOV ||
233  formatType == FORMAT_OGV)
234  {
235  vpERROR_TRACE("To read video files the FFmpeg library has to be installed");
236  throw (vpException(vpException::fatalError ,"the FFmpeg library is required"));
237  }
238  #endif
239  else if (formatType == FORMAT_UNKNOWN)
240  {
241  vpERROR_TRACE("The format of the file does not correpsond to a readable format.");
242  throw (vpException(vpException::fatalError ,"The format of the file does not correpsond to a readable format."));
243  }
244 
245  frameCount = firstFrame;
246  if(!getFrame(I,firstFrame))
247  {
248  vpERROR_TRACE("Could not read the first frame");
249  throw (vpException(vpException::ioError ,"Could not read the first frame"));
250  }
251  height = I.getHeight();
252  width = I.getWidth();
253 
254  isOpen = true;
255 
256  findLastFrameIndex();
257 }
258 
259 
268 {
269  if (!isOpen)
270  {
271  vpERROR_TRACE("Use the open method before");
272  throw (vpException(vpException::notInitialized,"file not yet opened"));
273  }
274 
275  //getFrame(I,frameCount);
276  if (imSequence != NULL)
277  imSequence->acquire(I);
278  #ifdef VISP_HAVE_FFMPEG
279  else if (ffmpeg !=NULL)
280  ffmpeg->acquire(I);
281  #endif
282 
283  frameCount++;
284 }
285 
286 
295 {
296  if (!isOpen)
297  {
298  vpERROR_TRACE("Use the open method before");
299  throw (vpException(vpException::notInitialized,"file not yet opened"));
300  }
301 
302  //getFrame(I,frameCount);
303  if (imSequence != NULL)
304  imSequence->acquire(I);
305  #ifdef VISP_HAVE_FFMPEG
306  else if (ffmpeg != NULL)
307  ffmpeg->acquire(I);
308  #endif
309 
310  frameCount++;
311 }
312 
313 
327 {
328  if (imSequence != NULL)
329  {
330  try
331  {
332  imSequence->acquire(I, frame);
333  }
334  catch(...)
335  {
336  vpERROR_TRACE("Couldn't find the %u th frame", frame) ;
337  return false;
338  }
339  }
340  #ifdef VISP_HAVE_FFMPEG
341  else
342  {
343 
344  if(!ffmpeg->getFrame(I, (unsigned int)frame))
345  {
346  vpERROR_TRACE("Couldn't find the %ld th frame", frame) ;
347  return false;
348  }
349  }
350  #endif
351  return true;
352 }
353 
354 
368 {
369  if (imSequence != NULL)
370  {
371  try
372  {
373  imSequence->acquire(I, frame);
374  }
375  catch(...)
376  {
377  vpERROR_TRACE("Couldn't find the %u th frame", frame) ;
378  return false;
379  }
380  }
381  #ifdef VISP_HAVE_FFMPEG
382  else
383  {
384  if(!ffmpeg->getFrame(I, (unsigned int)frame))
385  {
386  vpERROR_TRACE("Couldn't find the %ld th frame", frame) ;
387  return false;
388  }
389  }
390  #endif
391  return true;
392 }
393 
394 
400 vpVideoReader::vpVideoFormatType
401 vpVideoReader::getFormat(const char *filename)
402 {
403  std::string sfilename(filename);
404 
405  std::string ext = vpVideoReader::getExtension(sfilename);
406 
407  if (ext.compare(".PGM") == 0)
408  return FORMAT_PGM;
409  else if (ext.compare(".pgm") == 0)
410  return FORMAT_PGM;
411  else if (ext.compare(".PPM") == 0)
412  return FORMAT_PPM;
413  else if (ext.compare(".ppm") == 0)
414  return FORMAT_PPM;
415  else if (ext.compare(".JPG") == 0)
416  return FORMAT_JPEG;
417  else if (ext.compare(".jpg") == 0)
418  return FORMAT_JPEG;
419  else if (ext.compare(".JPEG") == 0)
420  return FORMAT_JPEG;
421  else if (ext.compare(".jpeg") == 0)
422  return FORMAT_JPEG;
423  else if (ext.compare(".PNG") == 0)
424  return FORMAT_PNG;
425  else if (ext.compare(".png") == 0)
426  return FORMAT_PNG;
427  else if (ext.compare(".AVI") == 0)
428  return FORMAT_AVI;
429  else if (ext.compare(".avi") == 0)
430  return FORMAT_AVI;
431  else if (ext.compare(".MPEG") == 0)
432  return FORMAT_MPEG;
433  else if (ext.compare(".mpeg") == 0)
434  return FORMAT_MPEG;
435  else if (ext.compare(".MPG") == 0)
436  return FORMAT_MPEG;
437  else if (ext.compare(".mpg") == 0)
438  return FORMAT_MPEG;
439  else if (ext.compare(".MOV") == 0)
440  return FORMAT_MOV;
441  else if (ext.compare(".mov") == 0)
442  return FORMAT_MOV;
443  else if (ext.compare(".OGV") == 0)
444  return FORMAT_OGV;
445  else if (ext.compare(".ogv") == 0)
446  return FORMAT_OGV;
447  else
448  return FORMAT_UNKNOWN;
449 }
450 
451 // return the extension of the file including the dot
452 std::string vpVideoReader::getExtension(const std::string &filename)
453 {
454  // extract the extension
455  size_t dot = filename.find_last_of(".");
456  std::string ext = filename.substr(dot, filename.size()-1);
457  return ext;
458 }
459 
460 
464 void
465 vpVideoReader::findLastFrameIndex()
466 {
467  if (!isOpen)
468  {
469  vpERROR_TRACE("Use the open method before");
470  throw (vpException(vpException::notInitialized,"file not yet opened"));
471  }
472 
473  if (imSequence != NULL)
474  {
475  char name[FILENAME_MAX];
476  int image_number = firstFrame;
477  std::fstream file;
478  bool failed;
479  do
480  {
481  sprintf(name,fileName,image_number) ;
482  file.open(name, std::fstream::in);
483  failed = file.fail();
484  if (!failed) file.close();
485  image_number++;
486  }while(!failed);
487 
488  lastFrame = image_number - 2;
489  }
490 
491  #ifdef VISP_HAVE_FFMPEG
492  else if (ffmpeg != NULL)
493  lastFrame = (long)(ffmpeg->getFrameNumber() - 1);
494  #endif
495 }
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:277
error that can be emited by ViSP classes.
Definition: vpException.h:75
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:103
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:330
unsigned int getHeight() const
Definition: vpImage.h:145
void acquire(vpImage< unsigned char > &I)
bool initStream()
Definition: vpFFMPEG.cpp:219
unsigned int width
Number of columns in the image.
void setGenericName(const char *genericName)