Skip to content

Commit

Permalink
Adding v4l2 and video interface. In progress
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Lovegrove <[email protected]>
  • Loading branch information
stevenlovegrove committed Apr 10, 2011
1 parent e3f8978 commit 6b2a927
Show file tree
Hide file tree
Showing 8 changed files with 953 additions and 53 deletions.
22 changes: 22 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2011 Steven Lovegrove and Richard Newcombe

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
151 changes: 122 additions & 29 deletions examples/SimpleVideo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,140 @@
**/

#include <pangolin/pangolin.h>
#include <pangolin/videosource.h>
#include <pangolin/firewire.h>
#include <pangolin/v4l.h>

using namespace pangolin;

int main( int /*argc*/, char* argv[] )
int firewire_sample()
{
// Setup Firewire Camera
FirewireVideo video(0,DC1394_VIDEO_MODE_640x480_RGB8,DC1394_FRAMERATE_30,DC1394_ISO_SPEED_400,50);
const unsigned w = video.Width();
const unsigned h = video.Height();

// Create Glut window
pangolin::CreateGlutWindowAndBind("Main",w,h);
// Setup Firewire Camera
FirewireVideo video(0,DC1394_VIDEO_MODE_640x480_RGB8,DC1394_FRAMERATE_30,DC1394_ISO_SPEED_400,50);
const unsigned w = video.Width();
const unsigned h = video.Height();

// Create viewport for video with fixed aspect
View& vVideo = Display("Video").SetAspect((float)w/h);
// Create Glut window
pangolin::CreateGlutWindowAndBind("Main",w,h);

// OpenGl Texture for video frame
GlTexture texVideo(w,h,GL_RGBA8);
// Create viewport for video with fixed aspect
View& vVideo = Display("Video").SetAspect((float)w/h);

for(int frame=0; !pangolin::ShouldQuit(); ++frame)
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// OpenGl Texture for video frame
GlTexture texVideo(w,h,GL_RGBA8);

// Get newest frame from camera and upload to GPU as texture
for(int frame=0; !pangolin::ShouldQuit(); ++frame)
{
FirewireFrame frame = video.GetNewest(true);
texVideo.Upload(frame.Image(),GL_RGB,GL_UNSIGNED_BYTE);
video.PutFrame(frame);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

// Get newest frame from camera and upload to GPU as texture
{
FirewireFrame frame = video.GetNewest(true);
texVideo.Upload(frame.Image(),GL_RGB,GL_UNSIGNED_BYTE);
video.PutFrame(frame);
}

// Activate video viewport and render texture
vVideo.Activate();
texVideo.RenderToViewportFlipY();

// Swap back buffer with front
glutSwapBuffers();

// Process window events via GLUT
glutMainLoopEvent();
}

// Activate video viewport and render texture
vVideo.Activate();
texVideo.RenderToViewportFlipY();
return 0;
}

// borrowed tempararily and altered from libfreenect
// https://github.com/OpenKinect/libfreenect/blob/master/src/cameras.c
#define CLAMP(x) if (x < 0) {x = 0;} if (x > 255) {x = 255;}
static void convert_yuyv_to_rgb(uint8_t *raw_buf, uint8_t *proc_buf)
{
int x, y;
for(y = 0; y < 480; ++y) {
for(x = 0; x < 640; x+=2) {
int i = (640 * y + x);
int y1 = raw_buf[2*i];
int u = raw_buf[2*i+1];
int y2 = raw_buf[2*i+2];
int v = raw_buf[2*i+3];
int r1 = (y1-16)*1164/1000 + (v-128)*1596/1000;
int g1 = (y1-16)*1164/1000 - (v-128)*813/1000 - (u-128)*391/1000;
int b1 = (y1-16)*1164/1000 + (u-128)*2018/1000;
int r2 = (y2-16)*1164/1000 + (v-128)*1596/1000;
int g2 = (y2-16)*1164/1000 - (v-128)*813/1000 - (u-128)*391/1000;
int b2 = (y2-16)*1164/1000 + (u-128)*2018/1000;
CLAMP(r1);
CLAMP(g1);
CLAMP(b1);
CLAMP(r2);
CLAMP(g2);
CLAMP(b2);
proc_buf[3*i] =r1;
proc_buf[3*i+1]=g1;
proc_buf[3*i+2]=b1;
proc_buf[3*i+3]=r2;
proc_buf[3*i+4]=g2;
proc_buf[3*i+5]=b2;
}
}
}
#undef CLAMP

// !V4L interface subject to change dramatically
int v4l_sample()
{
// Setup Firewire Camera
V4lVideo video("/dev/video0");
// FirewireVideo video(0,DC1394_VIDEO_MODE_640x480_RGB8,DC1394_FRAMERATE_30,DC1394_ISO_SPEED_400,50);
const unsigned w = video.Width();
const unsigned h = video.Height();

// Create Glut window
pangolin::CreateGlutWindowAndBind("Main",w,h);

// Create viewport for video with fixed aspect
View& vVideo = Display("Video").SetAspect((float)w/h);

// OpenGl Texture for video frame
GlTexture texVideo(w,h,GL_RGBA8);

for(int frame=0; !pangolin::ShouldQuit(); ++frame)
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

// Get newest frame from camera and upload to GPU as texture
// {
// FirewireFrame frame = video.GetNewest(true);
// texVideo.Upload(frame.Image(),GL_RGB,GL_UNSIGNED_BYTE);
// video.PutFrame(frame);
// }
{
static unsigned char yuv[640*480*3];
static unsigned char rgb[640*480*3];
video.GrabNext(yuv,true);
convert_yuyv_to_rgb(yuv,rgb);
texVideo.Upload(rgb,GL_RGB,GL_UNSIGNED_BYTE);
}

// Swap back buffer with front
glutSwapBuffers();
// Activate video viewport and render texture
vVideo.Activate();
texVideo.RenderToViewportFlipY();

// Process window events via GLUT
glutMainLoopEvent();
}
// Swap back buffer with front
glutSwapBuffers();

return 0;
// Process window events via GLUT
glutMainLoopEvent();
}

return 0;
}

int main( int /*argc*/, char* argv[] )
{
// firewire_sample();
v4l_sample();
}
2 changes: 1 addition & 1 deletion pangolin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SET(
widgets.h widgets.cpp
plotter.h plotter.cpp
gl.h glcuda.h cg.h
videosource.h videosource.cpp
video.h firewire.h firewire.cpp v4l.h v4l.cpp
)

#######################################################
Expand Down
2 changes: 1 addition & 1 deletion pangolin/videosource.cpp → pangolin/firewire.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "videosource.h"
#include "firewire.h"
#ifdef HAVE_DC1394

#include <stdio.h>
Expand Down
39 changes: 17 additions & 22 deletions pangolin/videosource.h → pangolin/firewire.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef PANGOLIN_VIDEOSOURCE_H
#define PANGOLIN_VIDEOSOURCE_H
#ifndef PANGOLIN_FIREWIRE_H
#define PANGOLIN_FIREWIRE_H

#include "pangolin.h"
#include "video.h"

#ifdef HAVE_DC1394

#include <dc1394/dc1394.h>
Expand All @@ -40,14 +42,6 @@
namespace pangolin
{

struct VideoException : std::exception
{
VideoException(std::string str) : desc(str) {}
~VideoException() throw() {}
const char* what() const throw() { return desc.c_str(); }
std::string desc;
};

class FirewireFrame
{
friend class FirewireVideo;
Expand All @@ -68,7 +62,7 @@ struct Guid
uint64_t guid;
};

class FirewireVideo
class FirewireVideo : public VideoSource
{
public:
FirewireVideo(
Expand All @@ -89,21 +83,22 @@ class FirewireVideo

~FirewireVideo();

int Width() const { return width; }
int Height() const { return height; }
//! Implement VideoSource::Width()
unsigned Width() const { return width; }

//! Implement VideoSource::Height()
unsigned Height() const { return height; }

//! Implement VideoSource::Start()
void Start();

//! Implement VideoSource::Stop()
void Stop();

//! Copy the next frame from the camera to image.
//! Optionally wait for a frame if one isn't ready
//! Returns true iff image was copied
//! Implement VideoSource::GrabNext()
bool GrabNext( unsigned char* image, bool wait = true );

//! Copy the newest frame from the camera to image
//! discarding all older frames.
//! Optionally wait for a frame if one isn't ready
//! Returns true iff image was copied
//! Implement VideoSource::GrabNewest()
bool GrabNewest( unsigned char* image, bool wait = true );

//! Return object containing reference to image data within
Expand Down Expand Up @@ -134,7 +129,7 @@ class FirewireVideo

bool running;
dc1394camera_t *camera;
unsigned int width, height;
unsigned width, height;
//dc1394featureset_t features;
dc1394_t * d;
dc1394camera_list_t * list;
Expand All @@ -145,4 +140,4 @@ class FirewireVideo


#endif // HAVE_DC1394
#endif // PANGOLIN_VIDEOSOURCE_H
#endif // PANGOLIN_FIREWIRE_H
Loading

0 comments on commit 6b2a927

Please sign in to comment.