1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
/* Read a yuv file directly and play with infinite loop
*
* Example:
* $ ./yuv420_infiniteloop /dev/video1 akiyo_qcif.yuv 176 144 30
* This will loop a yuv file named akiyo_qcif.yuv over video 1
*
* Modified by T. Xu <x.tongda@nyu.edu> from yuv4mpeg_to_v4l2 example,
* original Copyright (C) 2011 Eric C. Cooper <ecc@cmu.edu>
* Released under the GNU General Public License
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
char *prog;
struct yuv_setup {
char *device;
char *file_name;
int frame_width;
int frame_height;
int frame_bytes;
float fps;
};
void
fail(char *msg)
{
fprintf(stderr, "%s: %s\n", prog, msg);
exit(1);
}
struct yuv_setup
process_args(int argc, char **argv)
{
prog = argv[0];
struct yuv_setup setup;
if (argc != 6) {
fail("invalid argument");
} else {
setup.device = argv[1];
setup.file_name = argv[2];
setup.frame_width = atoi(argv[3]);
setup.frame_height = atoi(argv[4]);
setup.frame_bytes = 3 * setup.frame_height * setup.frame_width / 2;
setup.fps = atof(argv[5]);
}
return setup;
}
void
copy_frames(struct yuv_setup setup, int dev_fd)
{
FILE * yuv_file = fopen (setup.file_name,"rb");
if (yuv_file == NULL) {
fail("can not open yuv file");
}
char *frame = malloc(setup.frame_bytes);
if (frame == NULL) {
fail("cannot malloc frame");
}
while (1) {
int read_size = fread(frame, 1, setup.frame_bytes, yuv_file);
usleep(1.0f/setup.fps * 1000000.0f);
if (read_size == setup.frame_bytes) {
write(dev_fd, frame, setup.frame_bytes);
} else if (read_size == 0) {
fclose(yuv_file);
yuv_file = fopen (setup.file_name,"rb");
} else {
free(frame);
fail("invalid frame size or file ending");
}
}
free(frame);
}
int
open_video(struct yuv_setup setup)
{
struct v4l2_format v;
int dev_fd = open(setup.device, O_RDWR);
if (dev_fd == -1) {
fail("cannot open video device");
}
v.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
if (ioctl(dev_fd, VIDIOC_G_FMT, &v) == -1) {
fail("cannot setup video device");
}
v.fmt.pix.width = setup.frame_width;
v.fmt.pix.height = setup.frame_height;
v.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
v.fmt.pix.sizeimage = setup.frame_bytes;
v.fmt.pix.field = V4L2_FIELD_NONE;
if (ioctl(dev_fd, VIDIOC_S_FMT, &v) == -1) {
fail("cannot setup video device");
}
return dev_fd;
}
int
main(int argc, char **argv)
{
struct yuv_setup loc_setup = process_args(argc, argv);
int loc_dev = open_video(loc_setup);
copy_frames(loc_setup, loc_dev);
return 0;
}
|