4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_VIDEO4LINUX2
27 #include "chantables.h"
29 #include "condition.h"
30 #include "devicev4l2base.h"
33 #include "preferences.h"
34 #include "recordconfig.h"
35 #include "videodevice.h"
45 #include <linux/videodev2.h>
46 #include <sys/ioctl.h>
51 DeviceV4L2BufferQ(int qsize)
54 bfr_lock = new Mutex("DeviceV4L2BufferQ::bfr_lock");
55 buffers = new int[limit=qsize];
65 int DeviceV4L2BufferQ::q(int bfr)
68 bfr_lock->lock("DeviceV4L2Buffer::q");
70 if( in1 >= limit ) in1 = 0;
82 int DeviceV4L2BufferQ::dq(Condition *time_lock, int time_out)
85 bfr_lock->lock(" DeviceV4L2Buffer::dq 0");
90 if( time_lock->timed_lock(time_out, "DeviceV4L2Buffer::dq 1") )
91 printf("DeviceV4L2Buffer::wait time_out\n");
92 bfr_lock->lock(" DeviceV4L2Buffer::dq 2");
96 result = buffers[out];
98 if( out1 >= limit ) out1 = 0;
105 int DeviceV4L2BufferQ::dq()
108 bfr_lock->lock(" DeviceV4L2Buffer::dq");
111 result = buffers[out];
113 if( out1 >= limit ) out1 = 0;
122 DeviceV4L2Base::DeviceV4L2Base()
125 v4l2_lock = new Mutex("DeviceV4L2Input::v4l2_lock", 0);
126 qbfrs_lock = new Mutex("DeviceV4L2Base::qbfrs_lock");
127 video_lock = new Condition(0, "DeviceV4L2Base::video_lock", 1);
141 DeviceV4L2Base::~DeviceV4L2Base()
151 DeviceV4L2Put::DeviceV4L2Put(DeviceV4L2Base *v4l2)
156 putq = new DeviceV4L2BufferQ(256);
157 buffer_lock = new Condition(0, "DeviceV4L2Put::buffer_lock");
160 DeviceV4L2Put::~DeviceV4L2Put()
162 if(Thread::running())
165 buffer_lock->unlock();
174 void DeviceV4L2Put::run()
178 buffer_lock->lock("DeviceV4L2Put::run");
180 int bfr = putq->dq();
181 if( bfr < 0 ) continue;
182 struct v4l2_buffer arg;
183 memset(&arg, 0, sizeof(arg));
184 arg.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
186 arg.memory = V4L2_MEMORY_MMAP;
188 Thread::enable_cancel();
189 v4l2->qbfrs_lock->lock("DeviceV4L2Put::run");
190 // This locks up if there's no signal.
191 if( v4l2->vioctl(VIDIOC_QBUF, &arg) < 0 )
192 perror("DeviceV4L2Put::run 1 VIDIOC_QBUF");
195 v4l2->qbfrs_lock->unlock();
196 Thread::disable_cancel();
203 int DeviceV4L2Base::get_sources()
205 v4l2_lock->lock("DeviceV4L2Base::get_sources");
206 VideoDevice *video_device = v4l2_device();
207 if( !video_device ) return 1;
208 const char *dev_path = video_device->in_config->get_path();
209 int vfd = open(dev_path, O_RDWR+O_CLOEXEC);
212 perror("DeviceV4L2Base::get_sources open failed");
213 printf("DeviceV4L2Base::get_sources path: %s\n", dev_path);
219 for(int i = 0; i < 20; ++i)
221 struct v4l2_input arg;
222 memset(&arg, 0, sizeof(arg));
225 if(ioctl(vfd, VIDIOC_ENUMINPUT, &arg) < 0) break;
226 Channel *channel = video_device->new_input_source((char*)arg.name);
227 channel->device_index = i;
228 channel->tuner = arg.type == V4L2_INPUT_TYPE_TUNER ? arg.tuner : -1;
231 // Get the picture controls
232 for(int i = V4L2_CID_BASE; i < V4L2_CID_LASTP1; i++)
234 struct v4l2_queryctrl arg;
235 memset(&arg, 0, sizeof(arg));
237 // This returns errors for unsupported controls which is what we want.
238 if(!ioctl(vfd, VIDIOC_QUERYCTRL, &arg))
240 // Test if control exists already
241 if(!video_device->picture->get_item((const char*)arg.name, arg.id))
244 PictureItem *item = video_device->picture->new_item((const char*)arg.name);
245 item->device_id = arg.id;
246 item->min = arg.minimum;
247 item->max = arg.maximum;
248 item->step = arg.step;
249 item->default_ = arg.default_value;
250 item->type = arg.type;
251 item->value = arg.default_value;
256 // Load defaults for picture controls
257 video_device->picture->load_defaults();
264 int DeviceV4L2Base::vioctl(unsigned long int req, void *arg)
266 int result = ioctl(dev_fd, req, arg);
270 int DeviceV4L2Base::v4l2_open(int color_model)
272 VideoDevice *video_device = v4l2_device();
273 if( !video_device ) return 1;
275 struct v4l2_capability cap;
276 memset(&cap, 0, sizeof(cap));
277 if(vioctl(VIDIOC_QUERYCAP, &cap))
278 perror("DeviceV4L2Base::v4l2_open VIDIOC_QUERYCAP");
280 // printf("DeviceV4L2Base::v4l2_open dev_fd=%d driver=%s card=%s bus_info=%s version=%d\n",
281 // dev_fd(), cap.driver, cap.card, cap.bus_info, cap.version);
283 // if( (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) ) printf("VIDEO_CAPTURE ");
284 // if( (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) ) printf("VIDEO_OUTPUT ");
285 // if( (cap.capabilities & V4L2_CAP_VIDEO_OVERLAY) ) printf("VIDEO_OVERLAY ");
286 // if( (cap.capabilities & V4L2_CAP_VBI_CAPTURE) ) printf("VBI_CAPTURE ");
287 // if( (cap.capabilities & V4L2_CAP_VBI_OUTPUT) ) printf("VBI_OUTPUT ");
288 // if( (cap.capabilities & V4L2_CAP_RDS_CAPTURE) ) printf("RDS_CAPTURE ");
289 // if( (cap.capabilities & V4L2_CAP_TUNER) ) printf("TUNER ");
290 // if( (cap.capabilities & V4L2_CAP_AUDIO) ) printf("AUDIO ");
291 // if( (cap.capabilities & V4L2_CAP_RADIO) ) printf("RADIO ");
292 // if( (cap.capabilities & V4L2_CAP_READWRITE) ) printf("READWRITE ");
293 // if( (cap.capabilities & V4L2_CAP_ASYNCIO) ) printf("ASYNCIO ");
294 // if( (cap.capabilities & V4L2_CAP_STREAMING) ) printf("STREAMING ");
297 int config_width = video_device->in_config->w;
298 int config_height = video_device->in_config->h;
299 int config_area = config_width * config_height;
300 int best_width = config_width;
301 int best_height = config_height;
302 unsigned int best_format = 0;
304 int best_color_model = -1;
306 int driver = video_device->in_config->driver;
308 for( int i=0; i<20; ++i ) {
309 struct v4l2_fmtdesc fmt;
310 memset(&fmt,0,sizeof(fmt));
312 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
313 if( vioctl(VIDIOC_ENUM_FMT,&fmt) != 0 ) break;
314 printf("DeviceV4L2Base::v4l2_open");
315 // printf("format=\"%s\";",&fmt.description[0]);
316 printf(" pixels=\"%4.4s\"; res=\"",(char*)&fmt.pixelformat);
320 switch(fmt.pixelformat)
322 case V4L2_PIX_FMT_UYVY: cmodel = BC_UVY422; merit = 4; break;
323 case V4L2_PIX_FMT_YUYV: cmodel = BC_YUV422; merit = 4; break;
324 case V4L2_PIX_FMT_Y41P: cmodel = BC_YUV411P; merit = 1; break;
325 case V4L2_PIX_FMT_YVU420: cmodel = BC_YUV420P; merit = 3; break;
326 case V4L2_PIX_FMT_YUV422P: cmodel = BC_YUV422P; merit = 4; break;
327 case V4L2_PIX_FMT_RGB24: cmodel = BC_RGB888; merit = 6; break;
328 case V4L2_PIX_FMT_MJPEG:
329 cmodel = BC_COMPRESSED;
330 merit = driver == VIDEO4LINUX2JPEG ? 7 : 0;
332 case V4L2_PIX_FMT_MPEG:
334 merit = driver == VIDEO4LINUX2MPEG ? 7 : 2;
337 if( cmodel >= 0 && merit > best_merit )
340 best_format = fmt.pixelformat;
341 best_color_model = cmodel;
344 for( int n=0; n<20; ++n ) {
345 struct v4l2_frmsizeenum fsz;
346 memset(&fsz,0,sizeof(fsz));
348 fsz.pixel_format = fmt.pixelformat;
349 if( vioctl(VIDIOC_ENUM_FRAMESIZES,&fsz) != 0 ) break;
350 int w = fsz.discrete.width, h = fsz.discrete.height;
351 if( n > 0 ) printf(" ");
354 if( area > config_area ) continue;
355 int diff0 = area - best_area;
356 if( diff0 < 0 ) diff0 = -diff0;
357 int diff1 = area - config_area;
358 if( diff1 < 0 ) diff1 = -diff1;
359 if( diff1 < diff0 ) {
370 printf("DeviceV4L2Base::v4l2_open cant determine best_format\n");
373 case VIDEO4LINUX2JPEG:
374 best_format = V4L2_PIX_FMT_MJPEG;
376 case VIDEO4LINUX2MPEG:
377 best_format = V4L2_PIX_FMT_MPEG;
380 best_color_model = color_model;
381 best_format = cmodel_to_device(color_model);
384 printf("DeviceV4L2Base::v4l2_open ");
385 printf(_(" attempting format %4.4s\n"),
386 (char *)&best_format);
388 if(driver == VIDEO4LINUX2JPEG && best_format != V4L2_PIX_FMT_MJPEG)
390 printf("DeviceV4L2Base::v4l2_open ");
391 printf(_("jpeg driver and best_format not mjpeg (%4.4s)\n"),
392 (char *)&best_format);
395 if(driver == VIDEO4LINUX2MPEG && best_format != V4L2_PIX_FMT_MPEG)
397 printf("DeviceV4L2Base::v4l2_open ");
398 printf(_("mpeg driver and best_format not mpeg (%4.4s)\n"),
399 (char *)&best_format);
402 if(config_width != best_width || config_height != best_height)
404 printf("DeviceV4L2Base::v4l2_open ");
405 printf(_("config geom %dx%d != %dx%d best_geom\n"),
406 config_width, config_height, best_width, best_height);
410 struct v4l2_streamparm v4l2_parm;
411 memset(&v4l2_parm, 0, sizeof(v4l2_parm));
412 v4l2_parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
413 if(vioctl(VIDIOC_G_PARM, &v4l2_parm) < 0)
414 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_PARM");
415 if(v4l2_parm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME)
417 v4l2_parm.parm.capture.capturemode |= V4L2_CAP_TIMEPERFRAME;
418 v4l2_parm.parm.capture.timeperframe.numerator = 1;
419 v4l2_parm.parm.capture.timeperframe.denominator =
420 (unsigned long)((float)1 /
421 video_device->frame_rate * 10000000);
422 if(vioctl(VIDIOC_S_PARM, &v4l2_parm) < 0)
423 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_PARM");
425 if(vioctl(VIDIOC_G_PARM, &v4l2_parm) < 0)
426 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_PARM");
429 // Set up data format
430 struct v4l2_format v4l2_params;
431 memset(&v4l2_params, 0, sizeof(v4l2_params));
432 v4l2_params.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
433 if(vioctl(VIDIOC_G_FMT, &v4l2_params) < 0)
434 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_FMT 1");
435 v4l2_params.fmt.pix.width = best_width;
436 v4l2_params.fmt.pix.height = best_height;
437 v4l2_params.fmt.pix.pixelformat = best_format;
438 if(vioctl(VIDIOC_S_FMT, &v4l2_params) < 0)
439 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_FMT");
440 memset(&v4l2_params, 0, sizeof(v4l2_params));
441 v4l2_params.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
442 if(vioctl(VIDIOC_G_FMT, &v4l2_params) < 0)
443 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_FMT 2");
444 if( v4l2_params.fmt.pix.pixelformat != best_format )
446 printf("DeviceV4L2Base::v4l2_open set format %4.4s != %4.4s best_format\n",
447 (char*)&v4l2_params.fmt.pix.pixelformat, (char*)&best_format);
450 if( (int)v4l2_params.fmt.pix.width != best_width ||
451 (int)v4l2_params.fmt.pix.height != best_height )
453 printf("DeviceV4L2Base::v4l2_open set geom %dx%d != %dx%d best_geom\n",
454 v4l2_params.fmt.pix.width, v4l2_params.fmt.pix.height,
455 best_width, best_height);
459 // Set picture controls. This driver requires the values to be set once to default
460 // values and then again to different values before it takes up the values.
461 // Unfortunately VIDIOC_S_CTRL resets the audio to mono in 2.6.7.
462 PictureConfig *picture = video_device->picture;
463 for(int i = 0; i < picture->controls.total; i++)
465 struct v4l2_queryctrl arg;
466 memset(&arg, 0, sizeof(arg));
467 PictureItem *item = picture->controls.values[i];
468 arg.id = item->device_id;
469 if(!vioctl(VIDIOC_QUERYCTRL, &arg))
471 struct v4l2_control ctrl_arg;
472 memset(&ctrl_arg, 0, sizeof(ctrl_arg));
473 ctrl_arg.id = item->device_id;
475 if(vioctl(VIDIOC_S_CTRL, &ctrl_arg) < 0)
476 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL");
480 printf("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL 1 %s/id %08x failed\n",
481 item->name, item->device_id);
486 for(int i = 0; i < picture->controls.total; i++)
488 struct v4l2_queryctrl arg;
489 memset(&arg, 0, sizeof(arg));
490 PictureItem *item = picture->controls.values[i];
491 arg.id = item->device_id;
493 if(!vioctl(VIDIOC_QUERYCTRL, &arg))
495 struct v4l2_control ctrl_arg;
496 memset(&ctrl_arg, 0, sizeof(ctrl_arg));
497 ctrl_arg.id = item->device_id;
498 ctrl_arg.value = item->value;
499 if(vioctl(VIDIOC_S_CTRL, &ctrl_arg) < 0)
500 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL");
504 printf("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL 2 %s/id %08x failed\n",
505 item->name, item->device_id);
511 Channel *video_channel = video_device->channel;
512 ArrayList<Channel*> *inputs = video_device->get_inputs();
513 int input = video_channel->input;
514 device_channel = input >= 0 && input < inputs->total ?
515 inputs->values[input] : 0;
521 device_channel = inputs->values[0];
522 printf("DeviceV4L2Base::v4l2_open user channel not found. Using %s\n",
523 device_channel->device_name);
527 printf("DeviceV4L2Base::v4l2_open channel \"%s\" not found.\n",
528 video_channel->title);
532 // Translate input to API structures
533 input = device_channel ? device_channel->device_index : 0;
534 if(vioctl(VIDIOC_S_INPUT, &input) < 0)
535 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_INPUT");
538 v4l2_std_id std_id = 0;
539 switch(video_channel->norm)
541 case NTSC: std_id = V4L2_STD_NTSC; break;
542 case PAL: std_id = V4L2_STD_PAL; break;
543 case SECAM: std_id = V4L2_STD_SECAM; break;
544 default: std_id = V4L2_STD_NTSC_M; break;
546 if(vioctl(VIDIOC_S_STD, &std_id))
547 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_STD");
549 int dev_tuner = device_channel ? device_channel->tuner : -1;
550 if( (cap.capabilities & V4L2_CAP_TUNER) && dev_tuner >= 0 )
552 struct v4l2_tuner tuner;
553 memset(&tuner, 0, sizeof(tuner));
554 tuner.index = dev_tuner;
555 if(!vioctl(VIDIOC_G_TUNER, &tuner))
557 // printf("DeviceV4L2Base::v4l2_open audmode=%d rxsubchans=%d\n",
558 // tuner.audmode, tuner.rxsubchans);
559 tuner.index = dev_tuner;
560 tuner.type = V4L2_TUNER_ANALOG_TV;
561 tuner.audmode = V4L2_TUNER_MODE_STEREO;
562 tuner.rxsubchans = V4L2_TUNER_SUB_STEREO;
563 if(vioctl(VIDIOC_S_TUNER, &tuner) < 0)
564 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_TUNER");
566 struct v4l2_frequency frequency;
567 memset(&frequency, 0, sizeof(frequency));
568 frequency.tuner = video_channel->tuner;
569 frequency.type = V4L2_TUNER_ANALOG_TV;
570 int table = video_channel->freqtable;
571 int entry = video_channel->entry;
572 frequency.frequency = (int)(chanlists[table].list[entry].freq * 0.016);
573 // printf("DeviceV4L2Base::v4l2_open tuner=%d freq=%d norm=%d\n",
574 // video_channel->tuner, frequency.frequency, video_channel->norm);
575 if(vioctl(VIDIOC_S_FREQUENCY, &frequency) < 0)
576 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_FREQUENCY");
579 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_TUNER");
583 if(color_model == BC_COMPRESSED)
585 struct v4l2_jpegcompression jpeg_arg;
586 memset(&jpeg_arg, 0, sizeof(jpeg_arg));
587 if(vioctl(VIDIOC_G_JPEGCOMP, &jpeg_arg) < 0)
588 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_JPEGCOMP");
589 jpeg_arg.quality = video_device->quality / 2;
590 if(vioctl(VIDIOC_S_JPEGCOMP, &jpeg_arg) < 0)
591 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_JPEGCOMP");
594 this->color_model = best_color_model;
598 int DeviceV4L2Base::v4l2_status()
601 Channel *channel = device_channel;
606 else if( channel->tuner >= 0 )
608 struct v4l2_tuner tuner;
609 memset(&tuner,0,sizeof(tuner));
610 tuner.index = channel->tuner;
611 if( !vioctl(VIDIOC_G_TUNER, &tuner) )
613 dev_status = tuner.signal ? 1 : 0;
617 perror("DeviceV4L2Base::v4l2_status VIDIOC_S_TUNER");
621 struct v4l2_input arg;
622 memset(&arg, 0, sizeof(arg));
623 arg.index = channel->device_index;
624 if( !vioctl(VIDIOC_ENUMINPUT, &arg) )
626 dev_status = (arg.status &
627 (V4L2_IN_ST_NO_POWER | V4L2_IN_ST_NO_SIGNAL)) ? 0 : 1;
631 perror("DeviceV4L2Base::v4l2_status VIDIOC_ENUMINPUT");
636 void DeviceV4L2Base::dev_close()
645 int DeviceV4L2Base::dev_open()
649 VideoDevice *video_device = v4l2_device();
652 printf("DeviceV4L2Base::dev_open: video_device not available\n");
656 const char *dev_path = video_device->in_config->get_path();
657 dev_fd = open(dev_path, O_RDWR);
660 perror("DeviceV4L2Base::dev_open: open_failed");
661 printf("DeviceV4L2Base::dev_open: dev_path %s\n", dev_path);
664 video_device->set_cloexec_flag(dev_fd, 1);
670 int DeviceV4L2Base::open_dev(int color_model)
672 v4l2_lock->lock("DeviceV4L2Base::open_dev");
678 result = v4l2_open(color_model);
680 result = start_dev();
685 getq = new DeviceV4L2BufferQ(total_buffers+1);
686 put_thread = new DeviceV4L2Put(this);
692 printf("DeviceV4L2Base::open_dev failed\n");
696 printf("DeviceV4L2Base::open_dev: adaptor open failed\n");
706 void DeviceV4L2Base::close_dev()
708 v4l2_lock->lock("DeviceV4L2Base::close_dev");
711 if(Thread::running())
731 int DeviceV4L2Base::status_dev()
733 v4l2_lock->lock("DeviceV4L2Base::status_dev");
734 int result = dev_fd >= 0 ? v4l2_status() : 1;
739 unsigned int DeviceV4L2Base::cmodel_to_device(int color_model)
743 case BC_COMPRESSED: return V4L2_PIX_FMT_MJPEG;
744 case BC_YUV422: return V4L2_PIX_FMT_YUYV;
745 case BC_UVY422: return V4L2_PIX_FMT_UYVY;
746 case BC_YUV411P: return V4L2_PIX_FMT_Y41P;
747 case BC_YUV420P: return V4L2_PIX_FMT_YVU420;
748 case BC_YUV422P: return V4L2_PIX_FMT_YUV422P;
749 case BC_RGB888: return V4L2_PIX_FMT_RGB24;
754 DeviceV4L2VFrame::DeviceV4L2VFrame(int dev_fd, unsigned long ofs, unsigned long sz)
756 dev_data = (unsigned char*)mmap(NULL, dev_size=sz,
757 PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, ofs);
758 if(dev_data == MAP_FAILED) {
759 perror("DeviceV4L2VFrame::DeviceV4L2VFrame: mmap");
765 DeviceV4L2VFrame::~DeviceV4L2VFrame()
767 munmap(dev_data, dev_size);
770 int DeviceV4L2Base::start_dev()
772 VideoDevice *video_device = v4l2_device();
773 if( !video_device ) return 1;
775 // Errors here are fatal.
776 int req_buffers = video_device->in_config->capture_length;
777 req_buffers = MAX(req_buffers, 2);
778 req_buffers = MIN(req_buffers, 0xff);
780 struct v4l2_requestbuffers requestbuffers;
781 memset(&requestbuffers, 0, sizeof(requestbuffers));
782 //printf("DeviceV4L2Base::start_dev requested %d buffers\n", req_buffers);
783 requestbuffers.count = req_buffers;
784 requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
785 requestbuffers.memory = V4L2_MEMORY_MMAP;
786 if(vioctl(VIDIOC_REQBUFS, &requestbuffers) < 0)
788 perror("DeviceV4L2Base::start_dev VIDIOC_REQBUFS");
791 int bfr_count = requestbuffers.count;
792 //printf("DeviceV4L2Base::start_dev got %d buffers\n", bfr_count);
793 device_buffers = new DeviceV4L2VFrame *[bfr_count];
794 memset(device_buffers, 0, bfr_count*sizeof(device_buffers[0]));
796 //printf("DeviceV4L2Base::start_dev color_model=%d\n", color_model);
797 int iwidth = video_device->in_config->w;
798 int iheight = video_device->in_config->h;
799 int y_offset = 0, line_size = -1;
800 int u_offset = 0, v_offset = 0;
802 if(color_model != BC_COMPRESSED)
807 u_offset = iwidth * iheight;
808 v_offset = u_offset + u_offset / 2;
812 // In 2.6.7, the v and u are inverted for 420 but not 422
813 v_offset = iwidth * iheight;
814 u_offset = v_offset + v_offset / 4;
820 for(int i = 0; i < bfr_count; i++)
822 struct v4l2_buffer buffer;
823 memset(&buffer, 0, sizeof(buffer));
824 buffer.type = requestbuffers.type;
826 if(vioctl(VIDIOC_QUERYBUF, &buffer) < 0)
828 perror("DeviceV4L2Base::start_dev VIDIOC_QUERYBUF");
832 DeviceV4L2VFrame *dframe =
833 new DeviceV4L2VFrame(dev_fd, buffer.m.offset, buffer.length);
834 unsigned char *data = dframe->get_dev_data();
835 if( !data ) continue;
836 device_buffers[total_buffers++] = dframe;
837 if(color_model != BC_COMPRESSED)
838 dframe->reallocate(data, 0, y_offset, u_offset, v_offset,
839 iwidth, iheight, color_model, line_size);
841 dframe->set_compressed_memory(data, 0, 0, dframe->get_dev_size());
845 for(int i = 0; i < total_buffers; i++)
847 struct v4l2_buffer buffer;
848 memset(&buffer, 0, sizeof(buffer));
849 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
850 buffer.memory = V4L2_MEMORY_MMAP;
853 if(vioctl(VIDIOC_QBUF, &buffer) < 0)
855 perror("DeviceV4L2Base::start_dev VIDIOC_QBUF");
861 int streamon_arg = V4L2_BUF_TYPE_VIDEO_CAPTURE;
862 if(vioctl(VIDIOC_STREAMON, &streamon_arg) < 0)
864 perror("DeviceV4L2Base::start_dev VIDIOC_STREAMON");
872 int DeviceV4L2Base::stop_dev()
876 int streamoff_arg = V4L2_BUF_TYPE_VIDEO_CAPTURE;
877 if(vioctl(VIDIOC_STREAMOFF, &streamoff_arg) < 0)
878 perror("DeviceV4L2Base::stop_stream_capture VIDIOC_STREAMOFF");
882 for( int i = 0; i < total_buffers; i++ )
884 delete device_buffers[i];
887 struct v4l2_requestbuffers requestbuffers;
888 memset(&requestbuffers, 0, sizeof(requestbuffers));
889 requestbuffers.count = 0;
890 requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
891 requestbuffers.memory = V4L2_MEMORY_MMAP;
892 vioctl(VIDIOC_REQBUFS, &requestbuffers);
894 delete [] device_buffers;
901 void DeviceV4L2Base::run()
903 Thread::disable_cancel();
904 int min_buffers = total_buffers / 2;
905 if( min_buffers > 3 ) min_buffers = 3;
907 // Read buffers continuously
912 while( !done && (!qbfrs || (!retry && qbfrs < min_buffers)) )
914 Thread::enable_cancel();
916 Thread::disable_cancel();
917 ++retry; qbfrs = q_bfrs;
920 struct v4l2_buffer buffer;
921 memset(&buffer, 0, sizeof(buffer));
922 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
923 buffer.memory = V4L2_MEMORY_MMAP;
925 // The driver returns the first buffer not queued, so only one buffer
926 // can be unqueued at a time.
927 Thread::enable_cancel();
928 qbfrs_lock->lock("DeviceV4L2Base::run");
929 int result = vioctl(VIDIOC_DQBUF, &buffer);
930 if( !result ) --q_bfrs;
931 qbfrs_lock->unlock();
932 Thread::disable_cancel();
935 perror("DeviceV4L2Base::run VIDIOC_DQBUF");
936 Thread::enable_cancel();
938 Thread::disable_cancel();
943 int bfr = buffer.index;
944 // Set output frame data size/time, queue video
945 VFrame *frame = device_buffers[bfr];
946 if(color_model == BC_COMPRESSED)
947 frame->set_compressed_size(buffer.bytesused);
948 struct timeval *tv = &buffer.timestamp;
949 double bfr_time = tv->tv_sec + tv->tv_usec / 1000000.;
950 frame->set_timestamp(bfr_time);
951 if( !getq->q(bfr) ) video_lock->unlock();