YARP
Yet Another Robot Platform
FfmpegGrabber.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef FfmpegGrabber_INC
11 #define FfmpegGrabber_INC
12 
13 #include "avpreamble.h"
14 
15 extern "C" {
16 #include <libavcodec/avcodec.h>
17 #include <libavformat/avformat.h>
18 }
19 
20 /*
21  * A YARP frame grabber device driver using ffmpeg to implement
22  * image capture from AVI files.
23  */
24 
26 #include <yarp/dev/DeviceDriver.h>
27 
28 #include "ffmpeg_api.h"
29 
42 {
43 public:
44 
46  system_resource(nullptr),
47  formatParamsVideo(nullptr),
48  formatParamsAudio(nullptr),
49  pFormatCtx(nullptr),
50  pFormatCtx2(nullptr),
51  pAudioFormatCtx(nullptr),
52  active(false),
53  startTime(0),
54  _hasAudio(false),
55  _hasVideo(false),
56  needRateControl(false),
57  shouldLoop(true),
58  pace(1),
59  imageSync(false),
60  m_w(0),
61  m_h(0),
62  m_channels(0),
63  m_rate(0),
64  m_capture(nullptr)
65  {
66  memset(&packet,0,sizeof(packet));
67  }
68 
69  bool open(yarp::os::Searchable & config) override;
70 
71  bool close() override;
72 
74 
75  bool getSound(yarp::sig::Sound& sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override;
76 
77  int height() const override { return m_h; }
78 
79  int width() const override { return m_w; }
80 
82  yarp::sig::Sound& sound) override;
83 
84 
85  bool hasAudio() override {
86  return _hasAudio;
87  }
88 
89  bool hasVideo() override {
90  return _hasVideo;
91  }
92 
93  bool startRecording() override {
94  return true;
95  }
96 
97  bool stopRecording() override {
98  return true;
99  }
100 
102  return false;
103  }
104 
106  return false;
107  }
108 
109  bool resetRecordingAudioBuffer() override {
110  return false;
111  }
112 
113 protected:
115 
116  AVDictionary* formatParamsVideo;
117  AVDictionary* formatParamsAudio;
118  AVFormatContext *pFormatCtx;
119  AVFormatContext *pFormatCtx2;
120  AVFormatContext *pAudioFormatCtx;
121  AVPacket packet;
122  bool active;
123  double startTime;
127  double pace;
128  bool imageSync;
129 
131  std::string m_uri;
132 
134  int m_w;
136  int m_h;
137 
139  int m_rate;
140 
142  void * m_capture;
143 
144  bool openFirewire(yarp::os::Searchable & config,
145  AVFormatContext **ppFormatCtx);
146 
147  bool openV4L(yarp::os::Searchable & config,
148  AVFormatContext **ppFormatCtx,
149  AVFormatContext **ppFormatCtx2);
150 
151  bool openFile(AVFormatContext **ppFormatCtx,
152  const char *fname);
153 };
154 
155 #endif
FfmpegGrabber::getSound
bool getSound(yarp::sig::Sound &sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override
Get a sound from a device.
Definition: FfmpegGrabber.cpp:647
FfmpegGrabber::_hasAudio
bool _hasAudio
Definition: FfmpegGrabber.h:124
FfmpegGrabber::needRateControl
bool needRateControl
Definition: FfmpegGrabber.h:125
ffmpeg_api.h
FfmpegGrabber::hasVideo
bool hasVideo() override
Definition: FfmpegGrabber.h:89
FfmpegGrabber::active
bool active
Definition: FfmpegGrabber.h:122
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
FfmpegGrabber::pFormatCtx2
AVFormatContext * pFormatCtx2
Definition: FfmpegGrabber.h:119
FfmpegGrabber::m_w
int m_w
Width of the images a grabber produces.
Definition: FfmpegGrabber.h:134
FfmpegGrabber::width
int width() const override
Return the width of each frame.
Definition: FfmpegGrabber.h:79
FfmpegGrabber::shouldLoop
bool shouldLoop
Definition: FfmpegGrabber.h:126
yarp::dev::AudioBufferSize
Definition: AudioBufferSize.h:26
FfmpegGrabber::startRecording
bool startRecording() override
Start the recording.
Definition: FfmpegGrabber.h:93
FfmpegGrabber::openV4L
bool openV4L(yarp::os::Searchable &config, AVFormatContext **ppFormatCtx, AVFormatContext **ppFormatCtx2)
Definition: FfmpegGrabber.cpp:327
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
FfmpegGrabber::FfmpegGrabber
FfmpegGrabber()
Definition: FfmpegGrabber.h:45
FfmpegGrabber
ffmpeg_grabber: An image frame grabber device using ffmpeg to capture images from AVI files.
Definition: FfmpegGrabber.h:42
FfmpegGrabber::_hasVideo
bool _hasVideo
Definition: FfmpegGrabber.h:124
FfmpegGrabber::imageSync
bool imageSync
Definition: FfmpegGrabber.h:128
FfmpegGrabber::getImage
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Get an rgb image from the frame grabber, if required demosaicking/color reconstruction is applied.
Definition: FfmpegGrabber.cpp:638
FfmpegGrabber::formatParamsAudio
AVDictionary * formatParamsAudio
Definition: FfmpegGrabber.h:117
FfmpegGrabber::m_channels
int m_channels
Definition: FfmpegGrabber.h:138
FfmpegGrabber::openFile
bool openFile(AVFormatContext **ppFormatCtx, const char *fname)
Definition: FfmpegGrabber.cpp:451
FfmpegGrabber::m_capture
void * m_capture
Opaque ffmpeg structure for image capture.
Definition: FfmpegGrabber.h:142
FfmpegGrabber::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: FfmpegGrabber.cpp:459
yarp::sig::ImageOf< yarp::sig::PixelRgb >
yarp::dev::IAudioGrabberSound
Read a YARP-format sound block from a device.
Definition: IAudioGrabberSound.h:26
AudioVisualInterfaces.h
FfmpegGrabber::packet
AVPacket packet
Definition: FfmpegGrabber.h:121
yarp::dev::IFrameGrabberImage
Read a YARP-format image from a device.
Definition: FrameGrabberInterfaces.h:241
FfmpegGrabber::resetRecordingAudioBuffer
bool resetRecordingAudioBuffer() override
Definition: FfmpegGrabber.h:109
FfmpegGrabber::system_resource
void * system_resource
Definition: FfmpegGrabber.h:114
yarp::dev::IAudioVisualGrabber
Read a YARP-format image and sound from a device.
Definition: IAudioVisualGrabber.h:27
FfmpegGrabber::pFormatCtx
AVFormatContext * pFormatCtx
Definition: FfmpegGrabber.h:118
FfmpegGrabber::stopRecording
bool stopRecording() override
Stop the recording.
Definition: FfmpegGrabber.h:97
FfmpegGrabber::getAudioVisual
virtual bool getAudioVisual(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image, yarp::sig::Sound &sound) override
Get an image and sound.
Definition: FfmpegGrabber.cpp:657
FfmpegGrabber::getRecordingAudioBufferCurrentSize
bool getRecordingAudioBufferCurrentSize(yarp::dev::AudioBufferSize &) override
Definition: FfmpegGrabber.h:105
FfmpegGrabber::openFirewire
bool openFirewire(yarp::os::Searchable &config, AVFormatContext **ppFormatCtx)
Definition: FfmpegGrabber.cpp:435
FfmpegGrabber::m_uri
std::string m_uri
Uri of the images a grabber produces.
Definition: FfmpegGrabber.h:131
FfmpegGrabber::height
int height() const override
Return the height of each frame.
Definition: FfmpegGrabber.h:77
FfmpegGrabber::getRecordingAudioBufferMaxSize
bool getRecordingAudioBufferMaxSize(yarp::dev::AudioBufferSize &) override
Definition: FfmpegGrabber.h:101
FfmpegGrabber::formatParamsVideo
AVDictionary * formatParamsVideo
Definition: FfmpegGrabber.h:116
FfmpegGrabber::hasAudio
bool hasAudio() override
Definition: FfmpegGrabber.h:85
yarp::dev::IAudioVisualStream
Definition: IAudioVisualStream.h:27
FfmpegGrabber::m_h
int m_h
Height of the images a grabber produces.
Definition: FfmpegGrabber.h:136
FfmpegGrabber::startTime
double startTime
Definition: FfmpegGrabber.h:123
avpreamble.h
FfmpegGrabber::pace
double pace
Definition: FfmpegGrabber.h:127
yarp::sig::Sound
Class for storing sounds.
Definition: Sound.h:28
FfmpegGrabber::close
bool close() override
Close the DeviceDriver.
Definition: FfmpegGrabber.cpp:607
FfmpegGrabber::m_rate
int m_rate
Definition: FfmpegGrabber.h:139
FfmpegGrabber::pAudioFormatCtx
AVFormatContext * pAudioFormatCtx
Definition: FfmpegGrabber.h:120
DeviceDriver.h