4 * \brief Grab form camere, transfer to the MPEG and transmitt to the LAN
6 * This application handle USB camere.
10 * \author (last to touch it) $Author: zial (Ales Zikmund)$
12 * \version $Revision: 1.0 $
14 * \date $Date: 2008/12/12 5:45:20 $
16 * Contact: zikmua1@fel.cvut.cz
18 * Created on: Wed Dec 13 18:39:37 2008
25 * \brief This is the main file where application run.
33 #include <ffmpeg/avcodec.h>
64 * Function make frame from videodevice and send to.
71 void video_encode_alloc(const char *filename,uint8_t* in_buffer);
74 * Encode global buffer from picture .
76 void video_encode_mpeg();
81 void video_encode_free();
86 * \param count Count of byte
87 * \param strings String after run sequnce of aplicationss
88 * \return 1 if all good
90 int main(int argc, char *argv[])
95 const char *videodev ="/dev/video0";
96 const char *filename = "pokus.mpg";
101 for (i = 1; i < argc; i++){
102 /* skip bad arguments */
103 if (argv[i] == NULL || *argv[i] == 0 || *argv[i] != '-'){
106 if (strcmp (argv[i], "-f") == 0){
108 printf ("No parameter specified with -f, aborting.\n");
111 max_frame = atoi(argv[i + 1]);
113 if (strcmp (argv[i], "-d") == 0){
115 printf ("No parameter specified with -d, aborting.\n");
118 videodev = strdup (argv[i + 1]);
120 if (strcmp (argv[i], "-h") == 0) {
121 printf ("usage: grab_mpeg [-h -f] \n");
122 printf ("-h print this help \n");
123 printf ("-d /dev/videoX use videoX device\n");
124 printf ("-f count of frame \n");
129 printf("Video device %s frames %d\n",videodev,max_frame);
131 int grabmethod = 1; //or 0
133 printf("Open %s\n",videodev);
135 /* make room for init data */
136 myvidIn.videodevice = NULL;
137 myvidIn.cameraname = NULL;
138 myvidIn.bridge = NULL;
139 myvidIn.videodevice = (char *) realloc (myvidIn.videodevice, 16);
140 myvidIn.cameraname = (char *) realloc (myvidIn.cameraname, 32);
141 myvidIn.bridge = (char *) realloc (myvidIn.bridge, 9);
142 myvidIn.grabMethod = grabmethod; // 1 mmap 0 read
143 snprintf (myvidIn.videodevice, 12, "%s", videodev);
144 printf ("video device %s\n", myvidIn.videodevice);
145 retval=init_v4l (&myvidIn);
146 printf("Device was been initialized %d \n",retval);
148 video_encode_alloc(filename,myvidIn.pixTmp);
154 free (myvidIn.videodevice);
155 free (myvidIn.cameraname);
156 free (myvidIn.bridge);
157 close_v4l (&myvidIn);
172 myvidIn.hdrwidth = 384;
173 myvidIn.hdrheight = 288;
174 myvidIn.formatIn = VIDEO_PALETTE_YUV420P;
175 /* input data maybe a jpeg one */
176 setPalette (&myvidIn);
186 if(cnt==max_frame){run=0;}
187 printf("Frame %d\n",cnt);
191 void video_encode_alloc(const char *filename,uint8_t* in_buffer){
193 /* must be called before using avcodec lib */
196 /* register all the codecs */
197 avcodec_register_all();
199 //printf("Video encoding\n");
200 /* find the mpeg1 video encoder */
201 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
203 fprintf(stderr, "codec not found\n");
207 c= avcodec_alloc_context();
208 picture= avcodec_alloc_frame();
209 /* put sample parameters */
210 c->bit_rate = 1000000;
211 /* resolution must be a multiple of two */
214 /* frames per second */
215 c->time_base= (AVRational){1,60};
216 c->gop_size = 10; /* emit one intra frame every ten frames */
218 c->pix_fmt = PIX_FMT_YUV420P;
221 if (avcodec_open(c, codec) < 0) {
222 fprintf(stderr, "could not open codec\n");
226 f = fopen(filename, "wb");
228 fprintf(stderr, "could not open %s\n", filename);
232 /* alloc image and output buffer */
233 //buffer pro zakodovany obrazek mpegem
234 outbuf_size = 100000;
235 outbuf = malloc(outbuf_size);
237 size = c->width * c->height;
239 picture->data[0] = in_buffer;
240 picture->data[1] = picture->data[0] + size;
241 picture->data[2] = picture->data[1] + size / 4;
242 picture->linesize[0] = c->width;
243 picture->linesize[1] = c->width / 2;
244 picture->linesize[2] = c->width / 2;
247 void video_encode_mpeg(){
249 /* encode the image */
250 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
251 //printf("encoding frame %3d (size=%5d)\n", i, out_size);
252 fwrite(outbuf, 1, out_size, f);
256 void video_encode_free(){
258 /* get the delayed frames */
259 for(; out_size; i++) {
261 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
262 printf("write frame %3d (size=%5d)\n", i, out_size);
263 fwrite(outbuf, 1, out_size, f);
266 /* add sequence end code to have a real mpeg file */
272 fwrite(outbuf, 1, 4, f);
282 printf("video codev was been freed\n");