|
From: Juan C. <elg...@ya...> - 2005-03-10 21:39:06
|
Hi,
I have a Sony VX2000 DV camera which supports firewire
connection. What I would like to do is capture frames
continuously from the firewire and save those frames
to my hard disk for image processing. After some
research, I found that to capture the raw DV data
libraw1394 library in linux has to be used and to
decode the data libdv should be used. I wrote a small
code for this purpose, however I am getting weird,
random colored images. I also looked at the examples
that come from libdv source files; however no luck at
all. Can somebody please look at my code and tell me
what I am doing wrong? I got stuck on this and I can't
start my research area. Thanks a lot for your
responses in advance. Here is my code:
#define BUFFER 1000
#define PACKET_MAX 4096
#include <iostream>
#include
"/home/elgitaro/libraw1394-1.1.0/src/raw1394.h"
#include <libdv/dv.h>
using namespace std;
u_int64_t listen_channels;
raw1394handle_t handle;
int num_of_cards;
struct raw1394_portinfo pinf[ 16 ];
quadlet_t buffer;
dv_decoder_t *mydecoder;
raw1394_iso_dma_recv_mode mode;
int pitches[3];
unsigned char *pixels[1];
char filename[1024];
void saveasppm(unsigned char**,dv_decoder_t);
unsigned char dv_buffer[144000];
unsigned char video_buffer[720 * 576 * 3];
int *isPAL = FALSE;
static enum raw1394_iso_disposition
iso_handler(raw1394handle_t handle, unsigned char
*data,
unsigned int length, unsigned char channel,
unsigned char tag, unsigned char sy, unsigned
int cycle,
unsigned int dropped)
{
if ( length > 16 )
{
memcpy(dv_buffer,data,120000);
unsigned char * p = ( unsigned char* ) & data[ 3 ];
if (p[ 3 ] & 0x80) { //Check if the signal is
PAL
cout << "Pal" << endl;
mydecoder->clamp_luma = FALSE;
mydecoder->clamp_chroma = FALSE;
dv_reconfigure(FALSE, FALSE);
memcpy(dv_buffer + 120000, data,
144000 - 120000);
}
if((dv_parse_header(mydecoder, dv_buffer)) < 0){
cout << "dv_parse_header failed" << endl;
return RAW1394_ISO_OK;
}
else
cout << "Parse Header successful" << endl;
pitches[0] = 720 * 3; //mydecoder->width * 3;
//dv_parse_packs (mydecoder, dv_buffer);
dv_decode_full_frame(mydecoder,
dv_buffer,
e_dv_color_rgb, /* RGB */
pixels,
pitches);
FILE *fp;
sprintf(filename, "outbuf.ppm");
fp = fopen(filename, "wb");
if(fp != NULL)
{
fprintf(fp, "P6\n%d
%d\n255\n",mydecoder->width,mydecoder->height);
fwrite(pixels[0], 1,
3*mydecoder->width*mydecoder->height, fp);
fclose(fp);
}
}
return RAW1394_ISO_OK;
}
int main()
{
pixels[0] = video_buffer;
handle = raw1394_new_handle();
if (handle==0)
cout << "No handle has been created" << endl;
else
cout << "Handle is: " << handle << endl;
//num_of_cards = raw1394_get_port_info( handle, pinf,
16 );
//cout << "Number of ports: " << num_of_cards <<
endl;
//cout << "Port info: " << pinf[0].name << " . " <<
pinf[0].nodes << endl;
if (raw1394_set_port(handle, 0) < 0)
{
cout << "Couldn't set port";
exit(1);
}
mode = RAW1394_DMA_DEFAULT;
raw1394_iso_multichannel_recv_init(handle,
iso_handler,
BUFFER, 2048, -1);
listen_channels = ~0ULL;
raw1394_iso_recv_set_channel_mask(handle,
listen_channels);
/* create dv decoder */
cout << "creating the dv decoder" << endl;
mydecoder = dv_decoder_new(FALSE, FALSE, FALSE);
if(mydecoder == NULL)
cout << "dv_decoder_new failed" << endl;
mydecoder->video->arg_monochrome = 0; /* color frames
*/
mydecoder->quality =
mydecoder->video->quality = DV_QUALITY_BEST; /*
color and fine scale */
if(raw1394_iso_recv_start(handle, -1, -1, 0)==0)
cout << "iso_recv_start success" << endl;
//while (raw1394_loop_iterate(handle) == 0);
//for (int i=0; i<5; i++)
raw1394_loop_iterate(handle);
dv_decoder_free(mydecoder);
raw1394_iso_shutdown(handle);
exit(1);
}
__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/
|