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
24 #include "../hvirtual_config.h"
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 #ifdef HAVE_VIDEO4LINUX2
46 #include <linux/videodev2.h>
48 #include <sys/ioctl.h>
53 DeviceV4L2BufferQ(int qsize)
56 bfr_lock = new Mutex("DeviceV4L2BufferQ::bfr_lock");
57 buffers = new int[limit=qsize];
67 int DeviceV4L2BufferQ::q(int bfr)
70 bfr_lock->lock("DeviceV4L2Buffer::q");
72 if( in1 >= limit ) in1 = 0;
84 int DeviceV4L2BufferQ::dq(Condition *time_lock, int time_out)
87 bfr_lock->lock(" DeviceV4L2Buffer::dq 0");
92 if( time_lock->timed_lock(time_out, "DeviceV4L2Buffer::dq 1") )
93 printf("DeviceV4L2Buffer::wait time_out\n");
94 bfr_lock->lock(" DeviceV4L2Buffer::dq 2");
98 result = buffers[out];
100 if( out1 >= limit ) out1 = 0;
107 int DeviceV4L2BufferQ::dq()
110 bfr_lock->lock(" DeviceV4L2Buffer::dq");
113 result = buffers[out];
115 if( out1 >= limit ) out1 = 0;
124 DeviceV4L2Base::DeviceV4L2Base()
127 v4l2_lock = new Mutex("DeviceV4L2Input::v4l2_lock", 0);
128 qbfrs_lock = new Mutex("DeviceV4L2Base::qbfrs_lock");
129 video_lock = new Condition(0, "DeviceV4L2Base::video_lock", 1);
143 DeviceV4L2Base::~DeviceV4L2Base()
153 DeviceV4L2Put::DeviceV4L2Put(DeviceV4L2Base *v4l2)
158 putq = new DeviceV4L2BufferQ(256);
159 buffer_lock = new Condition(0, "DeviceV4L2Put::buffer_lock");
162 DeviceV4L2Put::~DeviceV4L2Put()
164 if(Thread::running())
167 buffer_lock->unlock();
176 void DeviceV4L2Put::run()
180 buffer_lock->lock("DeviceV4L2Put::run");
182 int bfr = putq->dq();
183 if( bfr < 0 ) continue;
184 struct v4l2_buffer arg;
185 memset(&arg, 0, sizeof(arg));
186 arg.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
188 arg.memory = V4L2_MEMORY_MMAP;
190 Thread::enable_cancel();
191 v4l2->qbfrs_lock->lock("DeviceV4L2Put::run");
192 // This locks up if there's no signal.
193 if( v4l2->vioctl(VIDIOC_QBUF, &arg) < 0 )
194 perror("DeviceV4L2Put::run 1 VIDIOC_QBUF");
197 v4l2->qbfrs_lock->unlock();
198 Thread::disable_cancel();
205 int DeviceV4L2Base::get_sources()
207 v4l2_lock->lock("DeviceV4L2Base::get_sources");
208 VideoDevice *video_device = v4l2_device();
209 if( !video_device ) return 1;
210 const char *dev_path = video_device->in_config->get_path();
211 int vfd = open(dev_path, O_RDWR+O_CLOEXEC);
214 perror("DeviceV4L2Base::get_sources open failed");
215 printf("DeviceV4L2Base::get_sources path: %s\n", dev_path);
221 for(int i = 0; i < 20; ++i)
223 struct v4l2_input arg;
224 memset(&arg, 0, sizeof(arg));
227 if(ioctl(vfd, VIDIOC_ENUMINPUT, &arg) < 0) break;
228 Channel *channel = video_device->new_input_source((char*)arg.name);
229 channel->device_index = i;
230 channel->tuner = arg.type == V4L2_INPUT_TYPE_TUNER ? arg.tuner : -1;
233 // Get the picture controls
234 for(int i = V4L2_CID_BASE; i < V4L2_CID_LASTP1; i++)
236 struct v4l2_queryctrl arg;
237 memset(&arg, 0, sizeof(arg));
239 // This returns errors for unsupported controls which is what we want.
240 if(!ioctl(vfd, VIDIOC_QUERYCTRL, &arg))
242 // Test if control exists already
243 if(!video_device->picture->get_item((const char*)arg.name, arg.id))
246 PictureItem *item = video_device->picture->new_item((const char*)arg.name);
247 item->device_id = arg.id;
248 item->min = arg.minimum;
249 item->max = arg.maximum;
250 item->step = arg.step;
251 item->default_ = arg.default_value;
252 item->type = arg.type;
253 item->value = arg.default_value;
258 // Load defaults for picture controls
259 video_device->picture->load_defaults();
266 int DeviceV4L2Base::vioctl(unsigned long int req, void *arg)
268 int result = ioctl(dev_fd, req, arg);
272 int DeviceV4L2Base::v4l2_open(int color_model)
274 VideoDevice *video_device = v4l2_device();
275 if( !video_device ) return 1;
277 struct v4l2_capability cap;
278 memset(&cap, 0, sizeof(cap));
279 if(vioctl(VIDIOC_QUERYCAP, &cap))
280 perror("DeviceV4L2Base::v4l2_open VIDIOC_QUERYCAP");
282 // printf("DeviceV4L2Base::v4l2_open dev_fd=%d driver=%s card=%s bus_info=%s version=%d\n",
283 // dev_fd(), cap.driver, cap.card, cap.bus_info, cap.version);
285 // if( (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) ) printf("VIDEO_CAPTURE ");
286 // if( (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) ) printf("VIDEO_OUTPUT ");
287 // if( (cap.capabilities & V4L2_CAP_VIDEO_OVERLAY) ) printf("VIDEO_OVERLAY ");
288 // if( (cap.capabilities & V4L2_CAP_VBI_CAPTURE) ) printf("VBI_CAPTURE ");
289 // if( (cap.capabilities & V4L2_CAP_VBI_OUTPUT) ) printf("VBI_OUTPUT ");
290 // if( (cap.capabilities & V4L2_CAP_RDS_CAPTURE) ) printf("RDS_CAPTURE ");
291 // if( (cap.capabilities & V4L2_CAP_TUNER) ) printf("TUNER ");
292 // if( (cap.capabilities & V4L2_CAP_AUDIO) ) printf("AUDIO ");
293 // if( (cap.capabilities & V4L2_CAP_RADIO) ) printf("RADIO ");
294 // if( (cap.capabilities & V4L2_CAP_READWRITE) ) printf("READWRITE ");
295 // if( (cap.capabilities & V4L2_CAP_ASYNCIO) ) printf("ASYNCIO ");
296 // if( (cap.capabilities & V4L2_CAP_STREAMING) ) printf("STREAMING ");
299 int config_width = video_device->in_config->w;
300 int config_height = video_device->in_config->h;
301 int config_area = config_width * config_height;
302 int best_width = config_width;
303 int best_height = config_height;
304 unsigned int best_format = 0;
306 int best_color_model = -1;
308 int driver = video_device->in_config->driver;
310 for( int i=0; i<20; ++i ) {
311 struct v4l2_fmtdesc fmt;
312 memset(&fmt,0,sizeof(fmt));
314 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
315 if( vioctl(VIDIOC_ENUM_FMT,&fmt) != 0 ) break;
316 printf("DeviceV4L2Base::v4l2_open");
317 // printf("format=\"%s\";",&fmt.description[0]);
318 printf(" pixels=\"%4.4s\"; res=\"",(char*)&fmt.pixelformat);
322 switch(fmt.pixelformat)
324 case V4L2_PIX_FMT_UYVY: cmodel = BC_UVY422; merit = 4; break;
325 case V4L2_PIX_FMT_YUYV: cmodel = BC_YUV422; merit = 4; break;
326 case V4L2_PIX_FMT_Y41P: cmodel = BC_YUV411P; merit = 1; break;
327 case V4L2_PIX_FMT_YVU420: cmodel = BC_YUV420P; merit = 3; break;
328 case V4L2_PIX_FMT_YUV422P: cmodel = BC_YUV422P; merit = 4; break;
329 case V4L2_PIX_FMT_RGB24: cmodel = BC_RGB888; merit = 6; break;
330 case V4L2_PIX_FMT_MJPEG:
331 cmodel = BC_COMPRESSED;
332 merit = driver == VIDEO4LINUX2JPEG ? 7 : 0;
334 case V4L2_PIX_FMT_MPEG:
336 merit = driver == VIDEO4LINUX2MPEG ? 7 : 2;
339 if( cmodel >= 0 && merit > best_merit )
342 best_format = fmt.pixelformat;
343 best_color_model = cmodel;
346 for( int n=0; n<20; ++n ) {
347 struct v4l2_frmsizeenum fsz;
348 memset(&fsz,0,sizeof(fsz));
350 fsz.pixel_format = fmt.pixelformat;
351 if( vioctl(VIDIOC_ENUM_FRAMESIZES,&fsz) != 0 ) break;
352 int w = fsz.discrete.width, h = fsz.discrete.height;
353 if( n > 0 ) printf(" ");
356 if( area > config_area ) continue;
357 int diff0 = area - best_area;
358 if( diff0 < 0 ) diff0 = -diff0;
359 int diff1 = area - config_area;
360 if( diff1 < 0 ) diff1 = -diff1;
361 if( diff1 < diff0 ) {
372 printf("DeviceV4L2Base::v4l2_open cant determine best_format\n");
375 case VIDEO4LINUX2JPEG:
376 best_format = V4L2_PIX_FMT_MJPEG;
378 case VIDEO4LINUX2MPEG:
379 best_format = V4L2_PIX_FMT_MPEG;
382 best_color_model = color_model;
383 best_format = cmodel_to_device(color_model);
386 printf(_("DeviceV4L2Base::v4l2_open "
387 " attempting format %4.4s\n"), (char *)&best_format);
389 if(driver == VIDEO4LINUX2JPEG && best_format != V4L2_PIX_FMT_MJPEG)
391 printf(_("DeviceV4L2Base::v4l2_open jpeg driver"
392 " and best_format not mjpeg (%4.4s)\n"), (char *)&best_format);
395 if(driver == VIDEO4LINUX2MPEG && best_format != V4L2_PIX_FMT_MPEG)
397 printf(_("DeviceV4L2Base::v4l2_open mpeg driver"
398 " and best_format not mpeg (%4.4s)\n"),(char *)&best_format);
401 if(config_width != best_width || config_height != best_height)
403 printf(_("DeviceV4L2Base::v4l2_open config geom %dx%d != %dx%d best_geom\n"),
404 config_width, config_height, best_width, best_height);
408 struct v4l2_streamparm v4l2_parm;
409 memset(&v4l2_parm, 0, sizeof(v4l2_parm));
410 v4l2_parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
411 if(vioctl(VIDIOC_G_PARM, &v4l2_parm) < 0)
412 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_PARM");
413 if(v4l2_parm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME)
415 v4l2_parm.parm.capture.capturemode |= V4L2_CAP_TIMEPERFRAME;
416 v4l2_parm.parm.capture.timeperframe.numerator = 1;
417 v4l2_parm.parm.capture.timeperframe.denominator =
418 (unsigned long)((float)1 /
419 video_device->frame_rate * 10000000);
420 if(vioctl(VIDIOC_S_PARM, &v4l2_parm) < 0)
421 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_PARM");
423 if(vioctl(VIDIOC_G_PARM, &v4l2_parm) < 0)
424 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_PARM");
427 // Set up data format
428 struct v4l2_format v4l2_params;
429 memset(&v4l2_params, 0, sizeof(v4l2_params));
430 v4l2_params.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
431 if(vioctl(VIDIOC_G_FMT, &v4l2_params) < 0)
432 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_FMT 1");
433 v4l2_params.fmt.pix.width = best_width;
434 v4l2_params.fmt.pix.height = best_height;
435 v4l2_params.fmt.pix.pixelformat = best_format;
436 if(vioctl(VIDIOC_S_FMT, &v4l2_params) < 0)
437 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_FMT");
438 memset(&v4l2_params, 0, sizeof(v4l2_params));
439 v4l2_params.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
440 if(vioctl(VIDIOC_G_FMT, &v4l2_params) < 0)
441 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_FMT 2");
442 if( v4l2_params.fmt.pix.pixelformat != best_format )
444 printf("DeviceV4L2Base::v4l2_open set format %4.4s != %4.4s best_format\n",
445 (char*)&v4l2_params.fmt.pix.pixelformat, (char*)&best_format);
448 if( (int)v4l2_params.fmt.pix.width != best_width ||
449 (int)v4l2_params.fmt.pix.height != best_height )
451 printf("DeviceV4L2Base::v4l2_open set geom %dx%d != %dx%d best_geom\n",
452 v4l2_params.fmt.pix.width, v4l2_params.fmt.pix.height,
453 best_width, best_height);
457 // Set picture controls. This driver requires the values to be set once to default
458 // values and then again to different values before it takes up the values.
459 // Unfortunately VIDIOC_S_CTRL resets the audio to mono in 2.6.7.
460 PictureConfig *picture = video_device->picture;
461 for(int i = 0; i < picture->controls.total; i++)
463 struct v4l2_queryctrl arg;
464 memset(&arg, 0, sizeof(arg));
465 PictureItem *item = picture->controls.values[i];
466 arg.id = item->device_id;
467 if(!vioctl(VIDIOC_QUERYCTRL, &arg))
469 struct v4l2_control ctrl_arg;
470 memset(&ctrl_arg, 0, sizeof(ctrl_arg));
471 ctrl_arg.id = item->device_id;
473 if(vioctl(VIDIOC_S_CTRL, &ctrl_arg) < 0)
474 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL");
478 printf("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL 1 %s/id %08x failed\n",
479 item->name, item->device_id);
484 for(int i = 0; i < picture->controls.total; i++)
486 struct v4l2_queryctrl arg;
487 memset(&arg, 0, sizeof(arg));
488 PictureItem *item = picture->controls.values[i];
489 arg.id = item->device_id;
491 if(!vioctl(VIDIOC_QUERYCTRL, &arg))
493 struct v4l2_control ctrl_arg;
494 memset(&ctrl_arg, 0, sizeof(ctrl_arg));
495 ctrl_arg.id = item->device_id;
496 ctrl_arg.value = item->value;
497 if(vioctl(VIDIOC_S_CTRL, &ctrl_arg) < 0)
498 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL");
502 printf("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL 2 %s/id %08x failed\n",
503 item->name, item->device_id);
509 Channel *video_channel = video_device->channel;
510 ArrayList<Channel*> *inputs = video_device->get_inputs();
511 int input = video_channel->input;
512 device_channel = input >= 0 && input < inputs->total ?
513 inputs->values[input] : 0;
519 device_channel = inputs->values[0];
520 printf("DeviceV4L2Base::v4l2_open user channel not found. Using %s\n",
521 device_channel->device_name);
525 printf("DeviceV4L2Base::v4l2_open channel \"%s\" not found.\n",
526 video_channel->title);
530 // Translate input to API structures
531 input = device_channel ? device_channel->device_index : 0;
532 if(vioctl(VIDIOC_S_INPUT, &input) < 0)
533 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_INPUT");
536 v4l2_std_id std_id = 0;
537 switch(video_channel->norm)
539 case NTSC: std_id = V4L2_STD_NTSC; break;
540 case PAL: std_id = V4L2_STD_PAL; break;
541 case SECAM: std_id = V4L2_STD_SECAM; break;
542 default: std_id = V4L2_STD_NTSC_M; break;
544 if(vioctl(VIDIOC_S_STD, &std_id))
545 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_STD");
547 int dev_tuner = device_channel ? device_channel->tuner : -1;
548 if( (cap.capabilities & V4L2_CAP_TUNER) && dev_tuner >= 0 )
550 struct v4l2_tuner tuner;
551 memset(&tuner, 0, sizeof(tuner));
552 tuner.index = dev_tuner;
553 if(!vioctl(VIDIOC_G_TUNER, &tuner))
555 // printf("DeviceV4L2Base::v4l2_open audmode=%d rxsubchans=%d\n",
556 // tuner.audmode, tuner.rxsubchans);
557 tuner.index = dev_tuner;
558 tuner.type = V4L2_TUNER_ANALOG_TV;
559 tuner.audmode = V4L2_TUNER_MODE_STEREO;
560 tuner.rxsubchans = V4L2_TUNER_SUB_STEREO;
561 if(vioctl(VIDIOC_S_TUNER, &tuner) < 0)
562 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_TUNER");
564 struct v4l2_frequency frequency;
565 memset(&frequency, 0, sizeof(frequency));
566 frequency.tuner = video_channel->tuner;
567 frequency.type = V4L2_TUNER_ANALOG_TV;
568 int table = video_channel->freqtable;
569 int entry = video_channel->entry;
570 frequency.frequency = (int)(chanlists[table].list[entry].freq * 0.016);
571 // printf("DeviceV4L2Base::v4l2_open tuner=%d freq=%d norm=%d\n",
572 // video_channel->tuner, frequency.frequency, video_channel->norm);
573 if(vioctl(VIDIOC_S_FREQUENCY, &frequency) < 0)
574 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_FREQUENCY");
577 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_TUNER");
581 if(color_model == BC_COMPRESSED)
583 struct v4l2_jpegcompression jpeg_arg;
584 memset(&jpeg_arg, 0, sizeof(jpeg_arg));
585 if(vioctl(VIDIOC_G_JPEGCOMP, &jpeg_arg) < 0)
586 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_JPEGCOMP");
587 jpeg_arg.quality = video_device->quality / 2;
588 if(vioctl(VIDIOC_S_JPEGCOMP, &jpeg_arg) < 0)
589 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_JPEGCOMP");
592 this->color_model = best_color_model;
596 int DeviceV4L2Base::v4l2_status()
599 Channel *channel = device_channel;
604 else if( channel->tuner >= 0 )
606 struct v4l2_tuner tuner;
607 memset(&tuner,0,sizeof(tuner));
608 tuner.index = channel->tuner;
609 if( !vioctl(VIDIOC_G_TUNER, &tuner) )
611 dev_status = tuner.signal ? 1 : 0;
615 perror("DeviceV4L2Base::v4l2_status VIDIOC_S_TUNER");
619 struct v4l2_input arg;
620 memset(&arg, 0, sizeof(arg));
621 arg.index = channel->device_index;
622 if( !vioctl(VIDIOC_ENUMINPUT, &arg) )
624 dev_status = (arg.status &
625 (V4L2_IN_ST_NO_POWER | V4L2_IN_ST_NO_SIGNAL)) ? 0 : 1;
629 perror("DeviceV4L2Base::v4l2_status VIDIOC_ENUMINPUT");
634 void DeviceV4L2Base::dev_close()
643 int DeviceV4L2Base::dev_open()
647 VideoDevice *video_device = v4l2_device();
650 printf("DeviceV4L2Base::dev_open: video_device not available\n");
654 const char *dev_path = video_device->in_config->get_path();
655 dev_fd = open(dev_path, O_RDWR);
658 perror("DeviceV4L2Base::dev_open: open_failed");
659 printf("DeviceV4L2Base::dev_open: dev_path %s\n", dev_path);
662 video_device->set_cloexec_flag(dev_fd, 1);
668 int DeviceV4L2Base::open_dev(int color_model)
670 v4l2_lock->lock("DeviceV4L2Base::open_dev");
676 result = v4l2_open(color_model);
678 result = start_dev();
683 getq = new DeviceV4L2BufferQ(total_buffers+1);
684 put_thread = new DeviceV4L2Put(this);
690 printf("DeviceV4L2Base::open_dev failed\n");
694 printf("DeviceV4L2Base::open_dev: adaptor open failed\n");
704 void DeviceV4L2Base::close_dev()
706 v4l2_lock->lock("DeviceV4L2Base::close_dev");
709 if(Thread::running())
729 int DeviceV4L2Base::status_dev()
731 v4l2_lock->lock("DeviceV4L2Base::status_dev");
732 int result = dev_fd >= 0 ? v4l2_status() : 1;
737 unsigned int DeviceV4L2Base::cmodel_to_device(int color_model)
741 case BC_COMPRESSED: return V4L2_PIX_FMT_MJPEG;
742 case BC_YUV422: return V4L2_PIX_FMT_YUYV;
743 case BC_UVY422: return V4L2_PIX_FMT_UYVY;
744 case BC_YUV411P: return V4L2_PIX_FMT_Y41P;
745 case BC_YUV420P: return V4L2_PIX_FMT_YVU420;
746 case BC_YUV422P: return V4L2_PIX_FMT_YUV422P;
747 case BC_RGB888: return V4L2_PIX_FMT_RGB24;
752 DeviceV4L2VFrame::DeviceV4L2VFrame(int dev_fd, unsigned long ofs, unsigned long sz)
754 dev_data = (unsigned char*)mmap(NULL, dev_size=sz,
755 PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, ofs);
756 if(dev_data == MAP_FAILED) {
757 perror("DeviceV4L2VFrame::DeviceV4L2VFrame: mmap");
763 DeviceV4L2VFrame::~DeviceV4L2VFrame()
765 munmap(dev_data, dev_size);
768 int DeviceV4L2Base::start_dev()
770 VideoDevice *video_device = v4l2_device();
771 if( !video_device ) return 1;
773 // Errors here are fatal.
774 int req_buffers = video_device->in_config->capture_length;
775 req_buffers = MAX(req_buffers, 2);
776 req_buffers = MIN(req_buffers, 0xff);
778 struct v4l2_requestbuffers requestbuffers;
779 memset(&requestbuffers, 0, sizeof(requestbuffers));
780 //printf("DeviceV4L2Base::start_dev requested %d buffers\n", req_buffers);
781 requestbuffers.count = req_buffers;
782 requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
783 requestbuffers.memory = V4L2_MEMORY_MMAP;
784 if(vioctl(VIDIOC_REQBUFS, &requestbuffers) < 0)
786 perror("DeviceV4L2Base::start_dev VIDIOC_REQBUFS");
789 int bfr_count = requestbuffers.count;
790 //printf("DeviceV4L2Base::start_dev got %d buffers\n", bfr_count);
791 device_buffers = new DeviceV4L2VFrame *[bfr_count];
792 memset(device_buffers, 0, bfr_count*sizeof(device_buffers[0]));
794 //printf("DeviceV4L2Base::start_dev color_model=%d\n", color_model);
795 int iwidth = video_device->in_config->w;
796 int iheight = video_device->in_config->h;
797 int y_offset = 0, line_size = -1;
798 int u_offset = 0, v_offset = 0;
800 if(color_model != BC_COMPRESSED)
805 u_offset = iwidth * iheight;
806 v_offset = u_offset + u_offset / 2;
810 // In 2.6.7, the v and u are inverted for 420 but not 422
811 v_offset = iwidth * iheight;
812 u_offset = v_offset + v_offset / 4;
818 for(int i = 0; i < bfr_count; i++)
820 struct v4l2_buffer buffer;
821 memset(&buffer, 0, sizeof(buffer));
822 buffer.type = requestbuffers.type;
824 if(vioctl(VIDIOC_QUERYBUF, &buffer) < 0)
826 perror("DeviceV4L2Base::start_dev VIDIOC_QUERYBUF");
830 DeviceV4L2VFrame *dframe =
831 new DeviceV4L2VFrame(dev_fd, buffer.m.offset, buffer.length);
832 unsigned char *data = dframe->get_dev_data();
833 if( !data ) continue;
834 device_buffers[total_buffers++] = dframe;
835 if(color_model != BC_COMPRESSED)
836 dframe->reallocate(data, 0, y_offset, u_offset, v_offset,
837 iwidth, iheight, color_model, line_size);
839 dframe->set_compressed_memory(data, 0, 0, dframe->get_dev_size());
843 for(int i = 0; i < total_buffers; i++)
845 struct v4l2_buffer buffer;
846 memset(&buffer, 0, sizeof(buffer));
847 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
848 buffer.memory = V4L2_MEMORY_MMAP;
851 if(vioctl(VIDIOC_QBUF, &buffer) < 0)
853 perror("DeviceV4L2Base::start_dev VIDIOC_QBUF");
859 int streamon_arg = V4L2_BUF_TYPE_VIDEO_CAPTURE;
860 if(vioctl(VIDIOC_STREAMON, &streamon_arg) < 0)
862 perror("DeviceV4L2Base::start_dev VIDIOC_STREAMON");
870 int DeviceV4L2Base::stop_dev()
874 int streamoff_arg = V4L2_BUF_TYPE_VIDEO_CAPTURE;
875 if(vioctl(VIDIOC_STREAMOFF, &streamoff_arg) < 0)
876 perror("DeviceV4L2Base::stop_stream_capture VIDIOC_STREAMOFF");
880 for( int i = 0; i < total_buffers; i++ )
882 delete device_buffers[i];
885 struct v4l2_requestbuffers requestbuffers;
886 memset(&requestbuffers, 0, sizeof(requestbuffers));
887 requestbuffers.count = 0;
888 requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
889 requestbuffers.memory = V4L2_MEMORY_MMAP;
890 vioctl(VIDIOC_REQBUFS, &requestbuffers);
892 delete [] device_buffers;
899 void DeviceV4L2Base::run()
901 Thread::disable_cancel();
902 int min_buffers = total_buffers / 2;
903 if( min_buffers > 3 ) min_buffers = 3;
905 // Read buffers continuously
910 while( !done && (!qbfrs || (!retry && qbfrs < min_buffers)) )
912 Thread::enable_cancel();
914 Thread::disable_cancel();
915 ++retry; qbfrs = q_bfrs;
918 struct v4l2_buffer buffer;
919 memset(&buffer, 0, sizeof(buffer));
920 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
921 buffer.memory = V4L2_MEMORY_MMAP;
923 // The driver returns the first buffer not queued, so only one buffer
924 // can be unqueued at a time.
925 Thread::enable_cancel();
926 qbfrs_lock->lock("DeviceV4L2Base::run");
927 int result = vioctl(VIDIOC_DQBUF, &buffer);
928 if( !result ) --q_bfrs;
929 qbfrs_lock->unlock();
930 Thread::disable_cancel();
933 perror("DeviceV4L2Base::run VIDIOC_DQBUF");
934 Thread::enable_cancel();
936 Thread::disable_cancel();
941 int bfr = buffer.index;
942 // Set output frame data size/time, queue video
943 VFrame *frame = device_buffers[bfr];
944 if(color_model == BC_COMPRESSED)
945 frame->set_compressed_size(buffer.bytesused);
946 struct timeval *tv = &buffer.timestamp;
947 double bfr_time = tv->tv_sec + tv->tv_usec / 1000000.;
948 frame->set_timestamp(bfr_time);
949 if( !getq->q(bfr) ) video_lock->unlock();