Skip to content

Instantly share code, notes, and snippets.

@vvuk
Created December 29, 2022 01:12
Show Gist options
  • Save vvuk/134985ffee280f7dc3521fc2d3d23563 to your computer and use it in GitHub Desktop.
Save vvuk/134985ffee280f7dc3521fc2d3d23563 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <stropts.h>
#include <sgimc.h>
#include <sgivideo.h>
#define WIDTH 640
#define HEIGHT 480
#define FPS 30
int main(int argc, char **argv)
{
int fd;
sgimc_video_mode_req_t req;
sgimc_video_mode_t mode;
sgimc_memc_set_wd_t wd;
sgimc_memc_set_timing_t timing;
int i, j;
unsigned char *frame_buf;
int frame_size;
FILE *fp;
// Open the video device
fd = open("/dev/video", O_RDWR);
if (fd < 0) {
perror("Error opening video device");
return 1;
}
// Set the video mode
req.format = SGIMC_VIDEO_FORMAT_RGB;
req.size.width = WIDTH;
req.size.height = HEIGHT;
req.fps = FPS;
if (ioctl(fd, SGIMC_VIDEO_SET_MODE, &req) < 0) {
perror("Error setting video mode");
return 1;
}
// Get the current video mode
if (ioctl(fd, SGIMC_VIDEO_GET_MODE, &mode) < 0) {
perror("Error getting video mode");
return 1;
}
// Set the write delay
wd.enable = 1;
wd.value = 4;
if (ioctl(fd, SGIMC_MEMC_SET_WD, &wd) < 0) {
perror("Error setting write delay");
return 1;
}
// Set the memory controller timing
timing.trcd = 2;
timing.trp = 2;
timing.twr = 2;
timing.refresh = 64;
if (ioctl(fd, SGIMC_MEMC_SET_TIMING, &timing) < 0) {
perror("Error setting memory controller timing");
return 1;
}
// Allocate a buffer for the frame
frame_size = mode.size.width * mode.size.height * 3;
frame_buf = (unsigned char *)malloc(frame_size);
if (!frame_buf) {
perror("Error allocating frame buffer");
return 1;
}
// Open the output file
fp = fopen("output.mpg", "w");
if (!fp) {
perror("Error opening output file");
return 1;
}
// Start the video capture
if (ioctl(fd, SGIMC_VIDEO_START_CAPTURE, 0) < 0) {
perror("Error starting video capture");
return 1;
}
// Record the video
for (i = 0; i < 100; i++) {
// Get a frame from the video capture device
if (read(fd, frame_buf, frame_size) < 0) {
perror("Error reading frame");
return 1;
}
// Write the frame to the output file
if (fwrite(frame_buf, 1, frame_size, fp) < frame_size) {
perror("Error writing frame to output file");
return 1;
}
}
// Stop the video capture
if (ioctl(fd, SGIMC_VIDEO_STOP_CAPTURE, 0) < 0) {
perror("Error stopping video capture");
return 1;
}
// Close the output file
fclose(fp);
// Free the frame buffer
free(frame_buf);
// Close the video device
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment