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 "quicktime.h"
35 #include "recordconfig.h"
36 #include "videodevice.h"
46 #ifdef HAVE_VIDEO4LINUX2
47 #include <linux/videodev2.h>
49 #include <sys/ioctl.h>
54 DeviceV4L2BufferQ(int qsize)
57 bfr_lock = new Mutex("DeviceV4L2BufferQ::bfr_lock");
58 buffers = new int[limit=qsize];
68 int DeviceV4L2BufferQ::q(int bfr)
71 bfr_lock->lock("DeviceV4L2Buffer::q");
73 if( in1 >= limit ) in1 = 0;
85 int DeviceV4L2BufferQ::dq(Condition *time_lock, int time_out)
88 bfr_lock->lock(" DeviceV4L2Buffer::dq 0");
93 if( time_lock->timed_lock(time_out, "DeviceV4L2Buffer::dq 1") )
94 printf("DeviceV4L2Buffer::wait time_out\n");
95 bfr_lock->lock(" DeviceV4L2Buffer::dq 2");
99 result = buffers[out];
101 if( out1 >= limit ) out1 = 0;
108 int DeviceV4L2BufferQ::dq()
111 bfr_lock->lock(" DeviceV4L2Buffer::dq");
114 result = buffers[out];
116 if( out1 >= limit ) out1 = 0;
125 DeviceV4L2Base::DeviceV4L2Base()
128 v4l2_lock = new Mutex("DeviceV4L2Input::v4l2_lock", 0);
129 qbfrs_lock = new Mutex("DeviceV4L2Base::qbfrs_lock");
130 video_lock = new Condition(0, "DeviceV4L2Base::video_lock", 1);
144 DeviceV4L2Base::~DeviceV4L2Base()
154 DeviceV4L2Put::DeviceV4L2Put(DeviceV4L2Base *v4l2)
159 putq = new DeviceV4L2BufferQ(256);
160 buffer_lock = new Condition(0, "DeviceV4L2Put::buffer_lock");
163 DeviceV4L2Put::~DeviceV4L2Put()
165 if(Thread::running())
168 buffer_lock->unlock();
177 void DeviceV4L2Put::run()
181 buffer_lock->lock("DeviceV4L2Put::run");
183 int bfr = putq->dq();
184 if( bfr < 0 ) continue;
185 struct v4l2_buffer arg;
186 memset(&arg, 0, sizeof(arg));
187 arg.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
189 arg.memory = V4L2_MEMORY_MMAP;
191 Thread::enable_cancel();
192 v4l2->qbfrs_lock->lock("DeviceV4L2Put::run");
193 // This locks up if there's no signal.
194 if( v4l2->vioctl(VIDIOC_QBUF, &arg) < 0 )
195 perror("DeviceV4L2Put::run 1 VIDIOC_QBUF");
198 v4l2->qbfrs_lock->unlock();
199 Thread::disable_cancel();
206 int DeviceV4L2Base::get_sources()
208 v4l2_lock->lock("DeviceV4L2Base::get_sources");
209 VideoDevice *video_device = v4l2_device();
210 if( !video_device ) return 1;
211 const char *dev_path = video_device->in_config->get_path();
212 int vfd = open(dev_path, O_RDWR+O_CLOEXEC);
215 perror("DeviceV4L2Base::get_sources open failed");
216 printf("DeviceV4L2Base::get_sources path: %s\n", dev_path);
222 for(int i = 0; i < 20; ++i)
224 struct v4l2_input arg;
225 memset(&arg, 0, sizeof(arg));
228 if(ioctl(vfd, VIDIOC_ENUMINPUT, &arg) < 0) break;
229 Channel *channel = video_device->new_input_source((char*)arg.name);
230 channel->device_index = i;
231 channel->tuner = arg.type == V4L2_INPUT_TYPE_TUNER ? arg.tuner : -1;
234 // Get the picture controls
235 for(int i = V4L2_CID_BASE; i < V4L2_CID_LASTP1; i++)
237 struct v4l2_queryctrl arg;
238 memset(&arg, 0, sizeof(arg));
240 // This returns errors for unsupported controls which is what we want.
241 if(!ioctl(vfd, VIDIOC_QUERYCTRL, &arg))
243 // Test if control exists already
244 if(!video_device->picture->get_item((const char*)arg.name, arg.id))
247 PictureItem *item = video_device->picture->new_item((const char*)arg.name);
248 item->device_id = arg.id;
249 item->min = arg.minimum;
250 item->max = arg.maximum;
251 item->step = arg.step;
252 item->default_ = arg.default_value;
253 item->type = arg.type;
254 item->value = arg.default_value;
259 // Load defaults for picture controls
260 video_device->picture->load_defaults();
267 int DeviceV4L2Base::vioctl(unsigned long int req, void *arg)
269 int result = ioctl(dev_fd, req, arg);
273 int DeviceV4L2Base::v4l2_open(int color_model)
275 VideoDevice *video_device = v4l2_device();
276 if( !video_device ) return 1;
278 struct v4l2_capability cap;
279 memset(&cap, 0, sizeof(cap));
280 if(vioctl(VIDIOC_QUERYCAP, &cap))
281 perror("DeviceV4L2Base::v4l2_open VIDIOC_QUERYCAP");
283 // printf("DeviceV4L2Base::v4l2_open dev_fd=%d driver=%s card=%s bus_info=%s version=%d\n",
284 // dev_fd(), cap.driver, cap.card, cap.bus_info, cap.version);
286 // if( (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) ) printf("VIDEO_CAPTURE ");
287 // if( (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) ) printf("VIDEO_OUTPUT ");
288 // if( (cap.capabilities & V4L2_CAP_VIDEO_OVERLAY) ) printf("VIDEO_OVERLAY ");
289 // if( (cap.capabilities & V4L2_CAP_VBI_CAPTURE) ) printf("VBI_CAPTURE ");
290 // if( (cap.capabilities & V4L2_CAP_VBI_OUTPUT) ) printf("VBI_OUTPUT ");
291 // if( (cap.capabilities & V4L2_CAP_RDS_CAPTURE) ) printf("RDS_CAPTURE ");
292 // if( (cap.capabilities & V4L2_CAP_TUNER) ) printf("TUNER ");
293 // if( (cap.capabilities & V4L2_CAP_AUDIO) ) printf("AUDIO ");
294 // if( (cap.capabilities & V4L2_CAP_RADIO) ) printf("RADIO ");
295 // if( (cap.capabilities & V4L2_CAP_READWRITE) ) printf("READWRITE ");
296 // if( (cap.capabilities & V4L2_CAP_ASYNCIO) ) printf("ASYNCIO ");
297 // if( (cap.capabilities & V4L2_CAP_STREAMING) ) printf("STREAMING ");
300 int config_width = video_device->in_config->w;
301 int config_height = video_device->in_config->h;
302 int config_area = config_width * config_height;
303 int best_width = config_width;
304 int best_height = config_height;
305 unsigned int best_format = 0;
307 //int best_color_model = -1;
309 int driver = video_device->in_config->driver;
311 for( int i=0; i<20; ++i ) {
312 struct v4l2_fmtdesc fmt;
313 memset(&fmt,0,sizeof(fmt));
315 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
316 if( vioctl(VIDIOC_ENUM_FMT,&fmt) != 0 ) break;
317 printf("DeviceV4L2Base::v4l2_open");
318 // printf("format=\"%s\";",&fmt.description[0]);
319 printf(" pixels=\"%4.4s\"; res=\"",(char*)&fmt.pixelformat);
323 switch(fmt.pixelformat)
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_format = cmodel_to_device(color_model);
385 printf("DeviceV4L2Base::v4l2_open "
386 " attempting format %4.4s\n", (char *)&best_format);
388 if(driver == VIDEO4LINUX2JPEG && best_format != V4L2_PIX_FMT_MJPEG)
390 printf("DeviceV4L2Base::v4l2_open jpeg driver"
391 " and best_format not mjpeg (%4.4s)\n", (char *)&best_format);
394 if(driver == VIDEO4LINUX2MPEG && best_format != V4L2_PIX_FMT_MPEG)
396 printf("DeviceV4L2Base::v4l2_open mpeg driver"
397 " and best_format not mpeg (%4.4s)\n",(char *)&best_format);
400 if(config_width != best_width || config_height != best_height)
402 printf("DeviceV4L2Base::v4l2_open config geom %dx%d != %dx%d best_geom\n",
403 config_width, config_height, best_width, best_height);
407 struct v4l2_streamparm v4l2_parm;
408 memset(&v4l2_parm, 0, sizeof(v4l2_parm));
409 v4l2_parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
410 if(vioctl(VIDIOC_G_PARM, &v4l2_parm) < 0)
411 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_PARM");
412 if(v4l2_parm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME)
414 v4l2_parm.parm.capture.capturemode |= V4L2_CAP_TIMEPERFRAME;
415 v4l2_parm.parm.capture.timeperframe.numerator = 1;
416 v4l2_parm.parm.capture.timeperframe.denominator =
417 (unsigned long)((float)1 /
418 video_device->frame_rate * 10000000);
419 if(vioctl(VIDIOC_S_PARM, &v4l2_parm) < 0)
420 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_PARM");
422 if(vioctl(VIDIOC_G_PARM, &v4l2_parm) < 0)
423 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_PARM");
426 // Set up data format
427 struct v4l2_format v4l2_params;
428 memset(&v4l2_params, 0, sizeof(v4l2_params));
429 v4l2_params.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
430 if(vioctl(VIDIOC_G_FMT, &v4l2_params) < 0)
431 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_FMT 1");
432 v4l2_params.fmt.pix.width = best_width;
433 v4l2_params.fmt.pix.height = best_height;
434 v4l2_params.fmt.pix.pixelformat = best_format;
435 if(vioctl(VIDIOC_S_FMT, &v4l2_params) < 0)
436 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_FMT");
437 memset(&v4l2_params, 0, sizeof(v4l2_params));
438 v4l2_params.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
439 if(vioctl(VIDIOC_G_FMT, &v4l2_params) < 0)
440 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_FMT 2");
441 if( v4l2_params.fmt.pix.pixelformat != best_format )
443 printf("DeviceV4L2Base::v4l2_open set format %4.4s != %4.4s best_format\n",
444 (char*)&v4l2_params.fmt.pix.pixelformat, (char*)&best_format);
447 if( (int)v4l2_params.fmt.pix.width != best_width ||
448 (int)v4l2_params.fmt.pix.height != best_height )
450 printf("DeviceV4L2Base::v4l2_open set geom %dx%d != %dx%d best_geom\n",
451 v4l2_params.fmt.pix.width, v4l2_params.fmt.pix.height,
452 best_width, best_height);
456 // Set picture controls. This driver requires the values to be set once to default
457 // values and then again to different values before it takes up the values.
458 // Unfortunately VIDIOC_S_CTRL resets the audio to mono in 2.6.7.
459 PictureConfig *picture = video_device->picture;
460 for(int i = 0; i < picture->controls.total; i++)
462 struct v4l2_queryctrl arg;
463 memset(&arg, 0, sizeof(arg));
464 PictureItem *item = picture->controls.values[i];
465 arg.id = item->device_id;
466 if(!vioctl(VIDIOC_QUERYCTRL, &arg))
468 struct v4l2_control ctrl_arg;
469 memset(&ctrl_arg, 0, sizeof(ctrl_arg));
470 ctrl_arg.id = item->device_id;
472 if(vioctl(VIDIOC_S_CTRL, &ctrl_arg) < 0)
473 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL");
477 printf("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL 1 %s/id %08x failed\n",
478 item->name, item->device_id);
483 for(int i = 0; i < picture->controls.total; i++)
485 struct v4l2_queryctrl arg;
486 memset(&arg, 0, sizeof(arg));
487 PictureItem *item = picture->controls.values[i];
488 arg.id = item->device_id;
490 if(!vioctl(VIDIOC_QUERYCTRL, &arg))
492 struct v4l2_control ctrl_arg;
493 memset(&ctrl_arg, 0, sizeof(ctrl_arg));
494 ctrl_arg.id = item->device_id;
495 ctrl_arg.value = item->value;
496 if(vioctl(VIDIOC_S_CTRL, &ctrl_arg) < 0)
497 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL");
501 printf("DeviceV4L2Base::v4l2_open VIDIOC_S_CTRL 2 %s/id %08x failed\n",
502 item->name, item->device_id);
508 Channel *video_channel = video_device->channel;
509 ArrayList<Channel*> *inputs = video_device->get_inputs();
510 int input = video_channel->input;
511 device_channel = input >= 0 && input < inputs->total ?
512 inputs->values[input] : 0;
518 device_channel = inputs->values[0];
519 printf("DeviceV4L2Base::v4l2_open user channel not found. Using %s\n",
520 device_channel->device_name);
524 printf("DeviceV4L2Base::v4l2_open channel \"%s\" not found.\n",
525 video_channel->title);
529 // Translate input to API structures
530 input = device_channel ? device_channel->device_index : 0;
531 if(vioctl(VIDIOC_S_INPUT, &input) < 0)
532 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_INPUT");
535 v4l2_std_id std_id = 0;
536 switch(video_channel->norm)
538 case NTSC: std_id = V4L2_STD_NTSC; break;
539 case PAL: std_id = V4L2_STD_PAL; break;
540 case SECAM: std_id = V4L2_STD_SECAM; break;
541 default: std_id = V4L2_STD_NTSC_M; break;
543 if(vioctl(VIDIOC_S_STD, &std_id))
544 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_STD");
546 int dev_tuner = device_channel ? device_channel->tuner : -1;
547 if( (cap.capabilities & V4L2_CAP_TUNER) && dev_tuner >= 0 )
549 struct v4l2_tuner tuner;
550 memset(&tuner, 0, sizeof(tuner));
551 tuner.index = dev_tuner;
552 if(!vioctl(VIDIOC_G_TUNER, &tuner) < 0)
554 // printf("DeviceV4L2Base::v4l2_open audmode=%d rxsubchans=%d\n",
555 // tuner.audmode, tuner.rxsubchans);
556 tuner.index = dev_tuner;
557 tuner.type = V4L2_TUNER_ANALOG_TV;
558 tuner.audmode = V4L2_TUNER_MODE_STEREO;
559 tuner.rxsubchans = V4L2_TUNER_SUB_STEREO;
560 if(vioctl(VIDIOC_S_TUNER, &tuner) < 0)
561 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_TUNER");
563 struct v4l2_frequency frequency;
564 memset(&frequency, 0, sizeof(frequency));
565 frequency.tuner = video_channel->tuner;
566 frequency.type = V4L2_TUNER_ANALOG_TV;
567 int table = video_channel->freqtable;
568 int entry = video_channel->entry;
569 frequency.frequency = (int)(chanlists[table].list[entry].freq * 0.016);
570 // printf("DeviceV4L2Base::v4l2_open tuner=%d freq=%d norm=%d\n",
571 // video_channel->tuner, frequency.frequency, video_channel->norm);
572 if(vioctl(VIDIOC_S_FREQUENCY, &frequency) < 0)
573 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_FREQUENCY");
576 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_TUNER");
580 if(color_model == BC_COMPRESSED)
582 struct v4l2_jpegcompression jpeg_arg;
583 memset(&jpeg_arg, 0, sizeof(jpeg_arg));
584 if(vioctl(VIDIOC_G_JPEGCOMP, &jpeg_arg) < 0)
585 perror("DeviceV4L2Base::v4l2_open VIDIOC_G_JPEGCOMP");
586 jpeg_arg.quality = video_device->quality / 2;
587 if(vioctl(VIDIOC_S_JPEGCOMP, &jpeg_arg) < 0)
588 perror("DeviceV4L2Base::v4l2_open VIDIOC_S_JPEGCOMP");
591 this->color_model = color_model;
595 int DeviceV4L2Base::v4l2_status()
598 Channel *channel = device_channel;
603 else if( channel->tuner >= 0 )
605 struct v4l2_tuner tuner;
606 memset(&tuner,0,sizeof(tuner));
607 tuner.index = channel->tuner;
608 if( !vioctl(VIDIOC_G_TUNER, &tuner) )
610 dev_status = tuner.signal ? 1 : 0;
614 perror("DeviceV4L2Base::v4l2_status VIDIOC_S_TUNER");
618 struct v4l2_input arg;
619 memset(&arg, 0, sizeof(arg));
620 arg.index = channel->device_index;
621 if( !vioctl(VIDIOC_ENUMINPUT, &arg) )
623 dev_status = (arg.status &
624 (V4L2_IN_ST_NO_POWER | V4L2_IN_ST_NO_SIGNAL)) ? 0 : 1;
628 perror("DeviceV4L2Base::v4l2_status VIDIOC_ENUMINPUT");
633 void DeviceV4L2Base::dev_close()
642 int DeviceV4L2Base::dev_open()
646 VideoDevice *video_device = v4l2_device();
649 printf("DeviceV4L2Base::dev_open: video_device not available\n");
653 const char *dev_path = video_device->in_config->get_path();
654 dev_fd = open(dev_path, O_RDWR);
657 perror("DeviceV4L2Base::dev_open: open_failed");
658 printf("DeviceV4L2Base::dev_open: dev_path %s\n", dev_path);
661 video_device->set_cloexec_flag(dev_fd, 1);
667 int DeviceV4L2Base::open_dev(int color_model)
669 v4l2_lock->lock("DeviceV4L2Base::open_dev");
675 result = v4l2_open(color_model);
677 result = start_dev();
682 getq = new DeviceV4L2BufferQ(total_buffers+1);
683 put_thread = new DeviceV4L2Put(this);
689 printf("DeviceV4L2Base::open_dev failed\n");
693 printf("DeviceV4L2Base::open_dev: adaptor open failed\n");
703 void DeviceV4L2Base::close_dev()
705 v4l2_lock->lock("DeviceV4L2Base::close_dev");
708 if(Thread::running())
728 int DeviceV4L2Base::status_dev()
730 v4l2_lock->lock("DeviceV4L2Base::status_dev");
731 int result = dev_fd >= 0 ? v4l2_status() : 1;
736 unsigned int DeviceV4L2Base::cmodel_to_device(int color_model)
740 case BC_COMPRESSED: return V4L2_PIX_FMT_MJPEG;
741 case BC_YUV422: return V4L2_PIX_FMT_YUYV;
742 case BC_YUV411P: return V4L2_PIX_FMT_Y41P;
743 case BC_YUV420P: return V4L2_PIX_FMT_YVU420;
744 case BC_YUV422P: return V4L2_PIX_FMT_YUV422P;
745 case BC_RGB888: return V4L2_PIX_FMT_RGB24;
750 DeviceV4L2VFrame::DeviceV4L2VFrame(int dev_fd, unsigned long ofs, unsigned long sz)
752 dev_data = (unsigned char*)mmap(NULL, dev_size=sz,
753 PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, ofs);
754 if(dev_data == MAP_FAILED) {
755 perror("DeviceV4L2VFrame::DeviceV4L2VFrame: mmap");
761 DeviceV4L2VFrame::~DeviceV4L2VFrame()
763 munmap(dev_data, dev_size);
766 int DeviceV4L2Base::start_dev()
768 VideoDevice *video_device = v4l2_device();
769 if( !video_device ) return 1;
771 // Errors here are fatal.
772 int req_buffers = video_device->in_config->capture_length;
773 req_buffers = MAX(req_buffers, 2);
774 req_buffers = MIN(req_buffers, 0xff);
776 struct v4l2_requestbuffers requestbuffers;
777 memset(&requestbuffers, 0, sizeof(requestbuffers));
778 //printf("DeviceV4L2Base::start_dev requested %d buffers\n", req_buffers);
779 requestbuffers.count = req_buffers;
780 requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
781 requestbuffers.memory = V4L2_MEMORY_MMAP;
782 if(vioctl(VIDIOC_REQBUFS, &requestbuffers) < 0)
784 perror("DeviceV4L2Base::start_dev VIDIOC_REQBUFS");
787 int bfr_count = requestbuffers.count;
788 //printf("DeviceV4L2Base::start_dev got %d buffers\n", bfr_count);
789 device_buffers = new DeviceV4L2VFrame *[bfr_count];
790 memset(device_buffers, 0, bfr_count*sizeof(device_buffers[0]));
792 //printf("DeviceV4L2Base::start_dev color_model=%d\n", color_model);
793 int iwidth = video_device->in_config->w;
794 int iheight = video_device->in_config->h;
795 int y_offset = 0, line_size = -1;
796 int u_offset = 0, v_offset = 0;
798 if(color_model != BC_COMPRESSED)
803 u_offset = iwidth * iheight;
804 v_offset = u_offset + u_offset / 2;
808 // In 2.6.7, the v and u are inverted for 420 but not 422
809 v_offset = iwidth * iheight;
810 u_offset = v_offset + v_offset / 4;
816 for(int i = 0; i < bfr_count; i++)
818 struct v4l2_buffer buffer;
819 memset(&buffer, 0, sizeof(buffer));
820 buffer.type = requestbuffers.type;
822 if(vioctl(VIDIOC_QUERYBUF, &buffer) < 0)
824 perror("DeviceV4L2Base::start_dev VIDIOC_QUERYBUF");
828 DeviceV4L2VFrame *dframe =
829 new DeviceV4L2VFrame(dev_fd, buffer.m.offset, buffer.length);
830 unsigned char *data = dframe->get_dev_data();
831 if( !data ) continue;
832 device_buffers[total_buffers++] = dframe;
833 if(color_model != BC_COMPRESSED)
834 dframe->reallocate(data, 0, y_offset, u_offset, v_offset,
835 iwidth, iheight, color_model, line_size);
837 dframe->set_compressed_memory(data, 0, 0, dframe->get_dev_size());
841 for(int i = 0; i < total_buffers; i++)
843 struct v4l2_buffer buffer;
844 memset(&buffer, 0, sizeof(buffer));
845 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
846 buffer.memory = V4L2_MEMORY_MMAP;
849 if(vioctl(VIDIOC_QBUF, &buffer) < 0)
851 perror("DeviceV4L2Base::start_dev VIDIOC_QBUF");
857 int streamon_arg = V4L2_BUF_TYPE_VIDEO_CAPTURE;
858 if(vioctl(VIDIOC_STREAMON, &streamon_arg) < 0)
860 perror("DeviceV4L2Base::start_dev VIDIOC_STREAMON");
868 int DeviceV4L2Base::stop_dev()
872 int streamoff_arg = V4L2_BUF_TYPE_VIDEO_CAPTURE;
873 if(vioctl(VIDIOC_STREAMOFF, &streamoff_arg) < 0)
874 perror("DeviceV4L2Base::stop_stream_capture VIDIOC_STREAMOFF");
878 for( int i = 0; i < total_buffers; i++ )
880 delete device_buffers[i];
883 struct v4l2_requestbuffers requestbuffers;
884 memset(&requestbuffers, 0, sizeof(requestbuffers));
885 requestbuffers.count = 0;
886 requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
887 requestbuffers.memory = V4L2_MEMORY_MMAP;
888 vioctl(VIDIOC_REQBUFS, &requestbuffers);
890 delete [] device_buffers;
897 void DeviceV4L2Base::run()
899 Thread::disable_cancel();
900 int min_buffers = total_buffers / 2;
901 if( min_buffers > 3 ) min_buffers = 3;
903 // Read buffers continuously
908 while( !done && (!qbfrs || (!retry && qbfrs < min_buffers)) )
910 Thread::enable_cancel();
912 Thread::disable_cancel();
913 ++retry; qbfrs = q_bfrs;
916 struct v4l2_buffer buffer;
917 memset(&buffer, 0, sizeof(buffer));
918 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
919 buffer.memory = V4L2_MEMORY_MMAP;
921 // The driver returns the first buffer not queued, so only one buffer
922 // can be unqueued at a time.
923 Thread::enable_cancel();
924 qbfrs_lock->lock("DeviceV4L2Base::run");
925 int result = vioctl(VIDIOC_DQBUF, &buffer);
926 if( !result ) --q_bfrs;
927 qbfrs_lock->unlock();
928 Thread::disable_cancel();
931 perror("DeviceV4L2Base::run VIDIOC_DQBUF");
932 Thread::enable_cancel();
934 Thread::disable_cancel();
939 int bfr = buffer.index;
940 // Set output frame data size/time, queue video
941 VFrame *frame = device_buffers[bfr];
942 if(color_model == BC_COMPRESSED)
943 frame->set_compressed_size(buffer.bytesused);
944 struct timeval *tv = &buffer.timestamp;
945 double bfr_time = tv->tv_sec + tv->tv_usec / 1000000.;
946 frame->set_timestamp(bfr_time);
947 if( !getq->q(bfr) ) video_lock->unlock();