/* video4linux interface wrapper for kwintv Copyright (C) 1998,1999 Moritz Wenk (wenk@mathematik.uni-kl.de) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* $Id: v4lxif.cpp,v 1.37 2008/01/25 22:56:33 kabi Exp $ */ #include "v4lxif.h" #include #include #include #include #include #include #include #include #include //#include #include #include #include #ifndef BTTV_VERSION #define BTTV_VERSION _IOR('v', BASE_VIDIOCPRIVATE+6, int) #endif #include #ifndef X_DISPLAY_MISSING #include #include #include "qt_visual.h" #ifdef HAVE_LIBXXF86DGA #include #include #include // Support deprecated XFree86 symbol names #ifndef XDGA_MINOR_VERSION #define XDGA_MINOR_VERSION XF86DGA_MINOR_VERSION #endif #ifndef XDGA_MAJOR_VERSION #define XDGA_MAJOR_VERSION XF86DGA_MAJOR_VERSION #endif #endif /* HAVE_LIBXXF86DGA */ #endif /* X_DISPLAY_MISSING */ /* PAL NTSC SECAM */ static const int maxwidth[] = { 768, 640, 768 }; static const int maxheight[] = { 576, 480, 576 }; static const int one = 1; static const int zero = 0; static const struct STRTAB norms[] = { { 0, "PAL" }, { 1, "NTSC" }, { 2, "SECAM" }, { 3, "AUTO" }, { -1, NULL } }; static const struct STRTAB ifname[] = { { 0, "no interface" }, { 1, "Video4Linux" }, { 2, "Video4Linux 2" }, { -1, NULL }, }; static const struct STRSTRTAB { int nr; const char* str; const char* description; } device_cap[] = { { VID_TYPE_CAPTURE, "capture","Can capture to memory" }, { VID_TYPE_TUNER, "tuner","Has a tuner of some form" }, { VID_TYPE_TELETEXT, "teletext","Has teletext capability" }, { VID_TYPE_OVERLAY, "overlay", "Can overlay its image onto the frame buffer" }, { VID_TYPE_CHROMAKEY, "chromakey", "Overlay is Chromakeyed" }, { VID_TYPE_CLIPPING, "clipping", "Overlay clipping is supported" }, { VID_TYPE_FRAMERAM, "frameram","Overlay overwrites frame buffer memory" }, { VID_TYPE_SCALES, "scales", "The hardware supports image scaling" }, { VID_TYPE_MONOCHROME, "monochrome", "Image capture is grey scale only" }, { VID_TYPE_SUBCAPTURE, "subcapture", "Capture can be of only part of the image" }, { -1, NULL, NULL } }; static const char* unknown = "???"; static const char* audiodescr[] = { "autodetection", "mono", "stereo", unknown, "language 1", unknown, unknown, unknown, "language 2" }; static const char *device_pal[] = { "-", "grey", "hi240", "rgb16", "rgb24", "rgb32", "rgb15", "yuv422", "yuyv", "uyvy", "yuv420", "yuv411", "raw", "yuv422p", "yuv411p", NULL }; //#define v4lDEBUG #define VIF_FATAL(a) avml(AVML_WARN, a) #define VIF_WARN(a) avml(AVML_WARN, a) static const unsigned short format2palette[] = { 0, /* unused */ VIDEO_PALETTE_HI240, /* RGB8 */ VIDEO_PALETTE_GREY, /* GRAY8 */ #if __BYTE_ORDER == __BIG_ENDIAN 0, 0, VIDEO_PALETTE_RGB555, /* RGB15_BE */ VIDEO_PALETTE_RGB565, /* RGB16_BE */ 0, 0, VIDEO_PALETTE_RGB24, /* RGB24 */ VIDEO_PALETTE_RGB32, /* RGB32 */ #else // little endian (intel) VIDEO_PALETTE_RGB555, /* RGB15_LE */ VIDEO_PALETTE_RGB565, /* RGB16_LE */ 0, 0, VIDEO_PALETTE_RGB24, /* BGR24 */ VIDEO_PALETTE_RGB32, /* BGR32 */ 0, 0, #endif }; #define VIDEO_RGB08 1 /* bt848 dithered */ #define VIDEO_GRAY 2 #define VIDEO_RGB15_LE 3 /* 15 bpp little endian */ #define VIDEO_RGB16_LE 4 /* 16 bpp little endian */ #define VIDEO_RGB15_BE 5 /* 15 bpp big endian */ #define VIDEO_RGB16_BE 6 /* 16 bpp big endian */ #define VIDEO_BGR24 7 /* bgrbgrbgrbgr (LE) */ #define VIDEO_BGR32 8 /* bgr-bgr-bgr- (LE) */ #define VIDEO_RGB24 9 /* rgbrgbrgbrgb (BE)*/ #define VIDEO_RGB32 10 /* -rgb-rgb-rgb (BE)*/ #define VIDEO_LUT2 11 /* lookup-table 2 byte depth */ #define VIDEO_LUT4 12 /* lookup-table 4 byte depth */ #define VIDEO_YUYV 13 /* YUV 4:2:2 */ #define VIDEO_YUV420 14 /* YUV 4:2:0 */ static const unsigned int format2depth[] = { 0, /* unused */ 8, /* RGB8 */ 8, /* GRAY8 */ 16, /* RGB15 LE */ 16, /* RGB16 LE */ 16, /* RGB15 BE */ 16, /* RGB16 BE */ 24, /* BGR24 */ 32, /* BGR32 */ 24, /* RGB24 */ 32, /* RGB32 */ 16, /* LUT2 */ 32, /* LUT4 */ 16, /* YUV422 */ 12, /* YUV420 */ }; static const char* format_desc[] = { "", "8 bit PseudoColor (dithering)", "8 bit StaticGray", "15 bit TrueColor (LE)", "16 bit TrueColor (LE)", "15 bit TrueColor (BE)", "16 bit TrueColor (BE)", "24 bit TrueColor (LE: bgr)", "32 bit TrueColor (LE: bgr-)", "24 bit TrueColor (BE: rgb)", "32 bit TrueColor (BE: -rgb)", "16 bit TrueColor (lut)", "32 bit TrueColor (lut)", "16 bit YUV 4:2:2", "12 bit YUV 4:2:0", "12 bit YUV 4:1:1" }; //#define v4lDEBUG static bool vbishown = false; int avm_debug_level = 0; //============================================================================== v4lxif::v4lxif(const char* _device, const char* _vbidev, v4lxif_version _ifv) : AvmOutputLog("v4lxif"), ifv(_ifv), m_sDevice(_device), m_sDeviceVBI(_vbidev) { // validate the name of device const char* p = _device; while (*p) { if (((*p>0) && (*p<32)) || (*p<0)) { AVMOUT(AVML_WARN, "Invalid video device filename %s suspected", _device); AVMOUT(AVML_WARN, "Setting to default value ( /dev/video )"); m_sDevice = "/dev/video"; break; } p++; } AVMOUT(AVML_DEBUG, "Using interface: %s", ifname[ifv].str); } v4lxif::~v4lxif() { AVMOUT(AVML_DEBUG, "Close device"); if (::close(m_iDevV4L) == -1) AVMOUT(AVML_WARNNO, "Error closing the v4lx device %s", m_sDevice); } int v4lxif::init() { AVMOUT(AVML_DEBUG, "Open video4linux device %s", m_sDevice); m_iDevV4L = ::open(m_sDevice, O_RDWR); if (m_iDevV4L < 0) { AVMOUT((AVML_ERROR | AVML_STRERROR), "Opening device %s", m_sDevice); return -1; } int tmp; int driver = ioctl(m_iDevV4L, BTTV_VERSION, &tmp); if (driver >= 0) { AVMOUT(AVML_INFO, "BTTV driver version %d.%d.%d detected", driver>>16, (driver & 0xff00) >>8, driver & 0xff); } else AVMOUT((AVML_WARN | AVML_STRERROR), "Querying BTTV driver version: %d", driver); if ((driver<0) || ((driver>>16)>0) || (((driver&0xff00)>>8)>=8)) /** Versions of bttv before 0.8.x behave strangely when program opens /dev/video and /dev/vbi simultaneously. We assume that non-bttv video4windows devices do it correctly ( does it work at all with non-bttv hardware? ) **/ { m_iDevVBI = ::open(m_sDeviceVBI, O_RDONLY); if (m_iDevVBI < 0) { AVMOUT(AVML_WARNNO, "Failed to open %s device", m_sDeviceVBI); AVMOUT(AVML_WARN, "Closed captioning won't be available"); } } else { m_iDevVBI = -1; if (!vbishown) AVMOUT(AVML_INFO, "Broken VBI implementation; disabling closed captioning"); vbishown = true; } AVMOUT(AVML_DEBUG, "end constructor"); return 0; } int v4lxif::readvbi(void* buf, unsigned int cnt) { if (m_iDevVBI < 0) return -1; Locker locker(m_Mutex); int r = ::read(m_iDevVBI, buf, cnt); return r; } //============================================================================== v4l1baseif::v4l1baseif( int fd ) { m_iDevV4L=fd; } v4l1baseif::v4l1baseif(const char* mem, const char * _device, int _bpp, int _palette ) : v4lxif(_device, "/dev/vbi", v4lxif::v4l1), vchan(0), vaudio(0), grabbermem(0), m_iBpp(_bpp), m_iPalette(_palette), _state(1) { setOutputMode("v4l1baseif"); } v4l1baseif::~v4l1baseif() { AVMOUT(AVML_DEBUG, "destructor"); // mute all if(vaudio) { for (int i = 0; i < vcap.audios; i++) if (vaudio[i].flags & VIDEO_AUDIO_MUTABLE) { vaudio[i].flags |= VIDEO_AUDIO_MUTE; if (ioctl(m_iDevV4L, VIDIOCSAUDIO, &vaudio[i]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_S_AUDIO"); } smute= true; } #if 1 if (ggrab > gsync) { if (ioctl(m_iDevV4L,VIDIOCSYNC,0) == -1) AVMOUT(AVML_WARNNO, "VIDIOCSYNC"); else gsync++; } #endif // capture off if (ioctl(m_iDevV4L, VIDIOCCAPTURE, &zero) == -1) AVMOUT(AVML_WARNNO, "VIDIOCCAPTURE"); if (grabbermem) munmap(grabbermem,msize); if (vchan) free(vchan); if (vaudio) free(vaudio); } int v4l1baseif::init() { if (v4lxif::init() < 0) return -1; setupok = true; // capture off if (ioctl(m_iDevV4L, VIDIOCCAPTURE, &zero) == -1) { if (errno != EINVAL && errno !=EBUSY) { AVMOUT(AVML_ERRORNO, "VIDIOCCAPTURE"); return -1; } } AVMOUT(AVML_DEBUG, "Before capabilities"); // get capabilities if (ioctl(m_iDevV4L, VIDIOCGCAP, &vcap) == -1) { AVMOUT(AVML_ERRORNO, "VIDIOC_G_CAP"); return -1; } // get input sources (channels) if (vcap.channels) vchan = (struct video_channel*)calloc(vcap.channels, sizeof(struct video_channel)); // get audios if (vcap.audios) vaudio = (struct video_audio*)calloc(vcap.audios, sizeof(struct video_audio)); AVMOUT(AVML_DEBUG, "Grabber name: %s", vcap.name); for (unsigned i = 0; device_cap[i].str != NULL; i++) if (vcap.type & (1 << i)) AVMOUT(AVML_DEBUG, "\t %s: %s", device_cap[i].str, device_cap[i].description); AVMOUT(AVML_DEBUG, "Range of tv size : %dx%d => %dx%d", vcap.minwidth, vcap.minheight, vcap.maxwidth, vcap.maxheight); AVMOUT(AVML_DEBUG, "Input channels: %d", vcap.channels); // get information about each channel for (int i = 0; i < vcap.channels; i++) { vchan[i].channel = i; if (ioctl(m_iDevV4L,VIDIOCGCHAN,&vchan[i]) == -1) { AVMOUT(AVML_ERRORNO, "VIDIOC_G_CHAN"); return -1; } } achan= 0; for (int i = 0; i < vcap.channels; i++) { AVMOUT(AVML_DEBUG, "\t [%d] %s: %s [%d], %s%s%s%s", i + 1, vchan[i].name, (vchan[i].flags & VIDEO_VC_TUNER) ? "has tuner" : "no tuner", vchan[i].tuners, (vchan[i].flags & VIDEO_VC_AUDIO) ? "audio, " : "", "",//(vchan[i].flags & VIDEO_VC_NORM) ? "norm, " : "", (vchan[i].type & VIDEO_TYPE_TV) ? "tv" : "", (vchan[i].type & VIDEO_TYPE_CAMERA) ? "camera" : ""); } AVMOUT(AVML_DEBUG, "Set input source"); // set v4l to the first available input source, also set first norm vchan[0].norm= 0; if (ioctl(m_iDevV4L, VIDIOCSCHAN, &vchan[0]) == -1) { AVMOUT(AVML_WARNNO, "You need a newer bttv version (>= 0.5.14)!"); //return -1; } // get information for each audio for (int i = 0; i < vcap.audios; i++) { vaudio[i].audio = i; if (ioctl(m_iDevV4L, VIDIOCGAUDIO, &vaudio[i]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_AUDIO"); } aaudio= 0; // mute them all for (int i = 0; i < vcap.audios; i++) if (vaudio[i].flags & VIDEO_AUDIO_MUTABLE) { vaudio[i].flags |= VIDEO_AUDIO_MUTE; if (ioctl(m_iDevV4L,VIDIOCSAUDIO,&vaudio[i]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_S_AUDIO"); } smute = true; AVMOUT(AVML_DEBUG, "Input audios: %d",vcap.audios); for (int i = 0; i < vcap.audios; i++) { AVMOUT(AVML_DEBUG, "\t [%d] %s: %s%s vol=%d, bass=%d, ter=%d",i+1,vaudio[i].name, (vaudio[i].flags & VIDEO_AUDIO_MUTABLE)?"mutable":"not mutable", (vaudio[i].flags & VIDEO_AUDIO_MUTE)?"[unmuted]":"[muted]", (vaudio[i].flags & VIDEO_AUDIO_VOLUME)?vaudio[i].volume:-1, (vaudio[i].flags & VIDEO_AUDIO_BASS)?vaudio[i].bass:-1, (vaudio[i].flags & VIDEO_AUDIO_TREBLE)?vaudio[i].treble:-1); } AVMOUT(AVML_DEBUG, "check tuner"); // check tuner ( are there any tv cards with more than one ??? ) for (int i = 0; i < vcap.channels; i++) { if ( vchan[i].tuners > 1 ) { AVMOUT(AVML_WARN, "Found more than one [%d] tuner for source channel %s", vchan[i].tuners, vchan[i].name); AVMOUT(AVML_WARN, "Only the first tuner of channel %s will be supported!", vchan[i].name); AVMOUT(AVML_WARN, "This warning can be ignored..."); } } atuner= 0; memset(&vtuner,0,sizeof(struct video_tuner)); // vtuner.tuner=atuner; if (vcap.type & VID_TYPE_TUNER) { if (ioctl(m_iDevV4L, VIDIOCGTUNER, &vtuner) == -1) { AVMOUT(AVML_ERRORNO, "VIDIOC_G_TUNER"); return -1; } #ifdef v4lDEBUG debug("v4l1: tuner: %s %lu-%lu",vtuner.name,vtuner.rangelow,vtuner.rangehigh); debug("v4l1: tuner sees stereo: %s",vtuner.flags & VIDEO_TUNER_STEREO_ON?"yes":"no"); //if ( vtuner.flags & VIDEO_TUNER_NORM ) { debug("v4l1: tuner supports modes (active: %s): ",norms[vtuner.mode].str); for (int i = 0; norms[i].str != NULL; i++) { if (vtuner.flags & (1<= vcap.channels)) { AVMOUT(AVML_WARN, "No such channel %d!", source); return; } if (ioctl(m_iDevV4L, VIDIOCSCHAN, &vchan[source]) == -1) { AVMOUT(AVML_WARNNO, "VIDIOC_S_CHAN in ::setChannel"); return; } achan = source; if (source < vcap.audios) aaudio = source; } void v4l1baseif::setChannelName(const char* name) { int source = 0; while (source < vcap.channels) { //printf("name %s\n", capChannelName(source)); if (strcasecmp(capChannelName(source), name) == 0) break; source++; } if (source >= vcap.channels) return; AVMOUT(AVML_DEBUG, "setChannel %d", source); if (ioctl(m_iDevV4L, VIDIOCSCHAN, &vchan[source]) == -1) { AVMOUT(AVML_WARNNO, "VIDIOC_S_CHAN in ::setChannel"); return; } achan = source; if(source= vcap.audios) { AVMOUT(AVML_WARN, "No such audio channel %d!", aaudio); return; } if ( vaudio[aaudio].flags & VIDEO_AUDIO_MUTABLE ) { if (ioctl(m_iDevV4L,VIDIOCGAUDIO,&vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_AUDIO"); if ( on ) vaudio[aaudio].flags |= VIDEO_AUDIO_MUTE; else vaudio[aaudio].flags &= ~VIDEO_AUDIO_MUTE; if (ioctl(m_iDevV4L,VIDIOCSAUDIO,&vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_S_AUDIO"); smute= on; } AVMOUT(AVML_DEBUG, "setAudioMute [%d] %s", aaudio, smute ? "muted" : "unmuted"); } void v4l1baseif::setAudioVolume(int vol) { if (!vaudio || aaudio>=vcap.audios) { AVMOUT(AVML_WARN, "No such audio channel %d!", aaudio); return; } #ifdef PREREAD if (ioctl(m_iDevV4L, VIDIOCGAUDIO, &vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_AUDIO setVolume"); #endif vaudio[aaudio].volume = vol; if (ioctl(m_iDevV4L, VIDIOCSAUDIO, &vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_S_AUDIO setVolume"); } void v4l1baseif::setAudioMode(int mode) { if (!vaudio || aaudio >= vcap.audios) { AVMOUT(AVML_WARN, "setAudioMode() No such audio channel %d!", aaudio); return; } #ifdef PREREAD if (ioctl(m_iDevV4L,VIDIOCGAUDIO,&vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "setAudioMode() VIDIOC_G_AUDIO"); #endif AVMOUT(AVML_DEBUG, "setAudioMode=%d [%s]", mode, audiodescr[mode]); vaudio[aaudio].mode = mode; if (ioctl(m_iDevV4L,VIDIOCSAUDIO,&vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_S_AUDIO setAudioMode"); } void v4l1baseif::setPalette(int pal) { vpic.palette = VIDEO_RGB15_LE; if (ioctl(m_iDevV4L, VIDIOCSPICT, &vpic) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_S_PICT setPalette"); } bool v4l1baseif::getAudioMute() { if (ioctl(m_iDevV4L,VIDIOCGAUDIO,&vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_AUDIO getAudioMute"); AVMOUT(AVML_DEBUG, "getAudioMute %s, %d", (vaudio[aaudio].flags & VIDEO_AUDIO_MUTE)?"true":"false", vaudio[aaudio].flags); return (vaudio[aaudio].flags & VIDEO_AUDIO_MUTE); } int v4l1baseif::getAudioVolume() { if (ioctl(m_iDevV4L,VIDIOCGAUDIO,&vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_AUDIO in ::getVolume"); return vaudio[aaudio].volume; } int v4l1baseif::getAudioMode() { if (ioctl(m_iDevV4L,VIDIOCGAUDIO,&vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_AUDIO in ::getAudioMode"); AVMOUT(AVML_DEBUG, "getAudioMode=%d", vaudio[aaudio].mode); return vaudio[aaudio].mode; } unsigned long v4l1baseif::getTunerSignal() { vtuner.tuner= atuner; if (ioctl(m_iDevV4L, VIDIOCGTUNER, &vtuner) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_TUNER in ::getTunerSignal"); return vtuner.signal; } int v4l1baseif::getTunerMode() { vtuner.tuner = atuner; if (ioctl(m_iDevV4L, VIDIOCGTUNER, &vtuner) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_TUNER in ::getTunerMode"); return vtuner.mode; } int v4l1baseif::getTunerFlags() { vtuner.tuner = atuner; if (ioctl(m_iDevV4L, VIDIOCGTUNER, &vtuner) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_TUNER in ::getTunerFlags"); return vtuner.flags; } bool v4l1baseif::capAudioVolume() { if (ioctl(m_iDevV4L,VIDIOCGAUDIO,&vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_AUDIO in ::capAudioVolume"); return ( vaudio[aaudio].flags & VIDEO_AUDIO_VOLUME ); } bool v4l1baseif::capAudioMutable() { if (ioctl(m_iDevV4L,VIDIOCGAUDIO,&vaudio[aaudio]) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_AUDIO in ::capAudioMutable"); return vaudio[aaudio].flags & VIDEO_AUDIO_MUTABLE; } bool v4l1baseif::capTunerStereo() { vtuner.tuner= atuner; if (ioctl(m_iDevV4L, VIDIOCGTUNER, &vtuner) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_TUNER in ::getTunerMode"); return vtuner.flags & VIDEO_TUNER_STEREO_ON; } bool v4l1baseif::capTunerNorm() { vtuner.tuner = atuner; if (ioctl(m_iDevV4L, VIDIOCGTUNER, &vtuner) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_TUNER in ::getTunerMode"); return vtuner.flags & VIDEO_TUNER_NORM; } void v4l1baseif::capCapSize( unsigned int *minw, unsigned int *minh, unsigned int *maxw, unsigned int *maxh ) { #if 0 *minw= vcap.minwidth; *minh= vcap.minheight; *maxw= vcap.maxwidth; *maxh= vcap.maxheight; #else *minw= vcap.minwidth; *minh= vcap.minheight; *maxw= maxwidth[vchan[achan].norm]; *maxh= maxheight[vchan[achan].norm]; #endif #ifdef v4lDEBUG debug("v4l1: minw %d, maxw %d, minh %d, maxh %d in ::capCapSize",*minw,*maxw,*minh,*maxh); #endif } int v4l1baseif::addCapAClip( int x1, int y1, unsigned int x2, unsigned int y2, int xadj, int yadj ) { // return 0; if ( vcap.type & VID_TYPE_CLIPPING ) { #ifdef v4lDEBUG debug("v4l1: addCapAClip: [%d] %dx%d+%d+%d",nrofclips,x2-x1,y2-y1,x1-xadj,y1-yadj); #endif if (cliprecs[nrofclips].x != x1 - xadj || cliprecs[nrofclips].y != y1 - yadj || cliprecs[nrofclips].width != (int)x2 - x1 || cliprecs[nrofclips].height != (int)y2 - y1 ) clipTabChanged= true; cliprecs[nrofclips].x = x1 - xadj; cliprecs[nrofclips].y = y1 - yadj; cliprecs[nrofclips].width = x2 - x1; cliprecs[nrofclips].height = y2 - y1; nrofclips++; } return nrofclips; } int v4l1baseif::setCapAClip( int x, int y, unsigned int width, unsigned int height ) { // return 0; x &= ~1; y &= ~1; width &= ~1; height &= ~1; #if 0 if (ioctl(m_iDevV4L, VIDIOCGWIN, &vwin) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_WIN in ::setCapAClip"); #endif AVMOUT(AVML_DEBUG, "setCapAClip: flags %d, chromakey %d", vwin.flags, vwin.chromakey); Locker locker(m_Mutex); if (vcap.type & VID_TYPE_CHROMAKEY) vwin.chromakey = 0; /* XXX */ vwin.flags = 0; if (vcap.type & VID_TYPE_CLIPPING) { vwin.clips = cliprecs; vwin.clipcount = nrofclips; } vwin.x = x; vwin.y = y; vwin.height = height; vwin.width = width; AVMOUT(AVML_DEBUG, "setCapAClip new values: %d, %d, %d, %d", x, y, width, height); return nrofclips; } int v4l1baseif::applyCapAClip(int n) { // save old values int ox = vwin.x; int oy = vwin.y; int ow = vwin.width; int oh = vwin.height; AVMOUT(AVML_DEBUG, "applyCapAClip given %d, %d, %d, %d [%d]", ox, oy, ow, oh, nrofclips); vwin.clipcount = n; Locker locker(m_Mutex); if (ioctl(m_iDevV4L, VIDIOCSWIN, &vwin) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_S_WIN"); #if 1 #if 0 if (ioctl(m_iDevV4L, VIDIOCGWIN, &vwin) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_WIN"); if ( vwin.x != x ) vwin.x = x; if ( vwin.y != y ) vwin.y = y; if ( vwin.height != height ) vwin.height = height; if ( vwin.width != width ) vwin.width = width; #else // set old values vwin.x = ox; vwin.y = oy; vwin.height = oh; vwin.width = ow; #endif #endif #if 0 vpic.palette = x11_format; #ifdef v4lDEBUG //debug("v4l1: palette=%s",PALETTE(vpic.palette)); #endif if (ioctl(m_iDevV4L, VIDIOCSPICT, &vpic) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_S_PICT"); if (ioctl(m_iDevV4L, VIDIOCGPICT, &vpic) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_PICT"); #endif return n; } void v4l1baseif::getCapAClip(int* x, int* y, unsigned int* width, unsigned int* height) { #if 0 if (ioctl(m_iDevV4L, VIDIOCGWIN, &vwin) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_G_WIN in ::getCapAClip"); #endif *x = vwin.x; *y = vwin.y; *width = vwin.width; *height = vwin.height; AVMOUT(AVML_DEBUG, "getCapAClip: %d, %d, %d, %d",*x,*y,*width,*height); } bool v4l1baseif::grabSetParams( int width, int height, int palette ) { AVMOUT(AVML_DEBUG, "grabSetParams(%d, %d, %d)", width, height, palette); vgrab[0].width= width; vgrab[0].height= height; vgrab[0].frame=0; vgrab[0].format=palette; memcpy(vgrab + 1, vgrab, sizeof(struct video_mmap)); vgrab[1].frame=1; grabCapture( true ); return true; } char * v4l1baseif::grabCapture(bool single) { // even frame: 0 // odd frame: 1 //printf("v4lxif::grabCapture(%s)\n", single ? "true" : "false"); //printf("v4lxif::grabCapture %d %d %d\n", ggrab, gsync, geven); char * rmem; if (!single && (ggrab == gsync)) if (!grabOne( geven ? 0 : 1 )) return NULL; if (!grabOne( geven ? 1 : 0 )) return NULL; if (ggrab > gsync+1 ) { grabWait( geven ? 0 : 1 ); rmem = (grabbermem + vmbuf.offsets[ geven ? 0 : 1 ]); } else { grabWait( geven ? 1 : 0 ); rmem = (grabbermem + vmbuf.offsets[ geven ? 1 : 0 ]); } geven = !geven; return rmem; } bool v4l1baseif::grabOne( int frame ) { if (ioctl(m_iDevV4L, VIDIOCMCAPTURE, &(vgrab[frame])) == -1) { if (errno == EAGAIN) { AVMOUT(AVML_WARN, "Grabber chip can't sync"); return false; } AVMOUT(AVML_WARNNO, "VIDIOCMCAPTURE"); } ggrab++; return true; } void v4l1baseif::grabWait( int frame ) { if (ioctl(m_iDevV4L,VIDIOCSYNC,&(vgrab[frame].frame)) == -1) AVMOUT(AVML_WARNNO, "VIDIOCSYNC (frame %d)", frame); else gsync++; } v4l1if::v4l1if(const char* mem, const char * _device, int _bpp, int _palette) : v4l1baseif(mem, _device, _bpp, _palette) { setOutputMode("v4l1if"); AVMOUT(AVML_DEBUG, "constructor"); } int v4l1if::init() { if (v4l1baseif::init() < 0) return -1; int visual_class; #ifndef X_DISPLAY_MISSING const char* xdname = XDisplayName(0); Display* disp= XOpenDisplay(xdname); Visual *myvisual; #ifdef v4lDEBUG List_visuals(disp); #endif // get the visual int display_bits= find_visual(disp,NULL,&myvisual,&visual_class); // do some checks for color depth, size and buffer pos // first, guess the color depth of the screen int n, pixmap_bytes = 0; XPixmapFormatValues *pf = XListPixmapFormats(disp, &n); int planes = DefaultDepth(disp, DefaultScreen(disp)); for (int i = 0; i < n; i++) { if (pf[i].depth == planes) pixmap_bytes = pf[i].bits_per_pixel / 8; } /* guess physical screen format */ bool be = (ImageByteOrder(disp) == MSBFirst); switch (pixmap_bytes) { case 1: x11_format = VIDEO_RGB08; break; case 2: x11_format = (display_bits==15) ? ((be) ? VIDEO_RGB15_BE : VIDEO_RGB15_LE) : ((be) ? VIDEO_RGB16_BE : VIDEO_RGB16_LE) ; break; case 3: x11_format = (be) ? VIDEO_RGB24 : VIDEO_BGR24; break; case 4: x11_format = (be) ? VIDEO_RGB32 : VIDEO_BGR32; break; default: AVMOUT(AVML_WARN, "Unknown color depth found: %d", pixmap_bytes); } #ifdef v4lDEBUG AVMOUT(AVML_INFO, "Hope physical screen format is <%s>", format_desc[x11_format]); #endif #endif /* X_DISPLAY_MISSING */ if ( m_iPalette > 0 && m_iPalette < 11 ) { x11_format = m_iPalette; } vpic.palette = (x11_format < sizeof(format2palette)/sizeof(unsigned short)) ? format2palette[x11_format] : 0; if ( vpic.palette == 0 ) { AVMOUT(AVML_WARN, "Unsupported overlay video format <%d-%s>", x11_format,format_desc[x11_format]); vpic.palette = VIDEO_RGB08; } if (ioctl(m_iDevV4L, VIDIOCSPICT, &vpic) == -1) { AVMOUT(AVML_ERRORNO, "VIDIOC_S_PIC"); return -1; } // reread palette if (ioctl(m_iDevV4L, VIDIOCGPICT, &vpic) == -1) { AVMOUT(AVML_ERRORNO, "VIDIOC_G_PICT"); return -1; } AVMOUT(AVML_DEBUG, "Palette is %d", vpic.palette); AVMOUT(AVML_DEBUG, "test dga"); #ifdef HAVE_LIBXXF86DGA // do some strange things with dga int major, minor, width, bank, ram; int flags; void *base = NULL; bool have_dga= false; #ifndef XDGA_MAJOR_VERSION #define XDGA_MAJOR_VERSION XF86DGA_MAJOR_VERSION #endif #ifndef XDGA_MINOR_VERSION #define XDGA_MINOR_VERSION XF86DGA_MINOR_VERSION #endif if (XF86DGAQueryExtension(disp, &major, &minor)) { XF86DGAQueryDirectVideo(disp,XDefaultScreen(disp),&flags); if (flags & XF86DGADirectPresent) { have_dga = true; XF86DGAQueryVersion(disp,&major,&minor); if ((major != XDGA_MAJOR_VERSION) || (minor != XDGA_MINOR_VERSION)) { AVMOUT(AVML_HINT, "X-Server DGA extension version mismatch, disabled"); AVMOUT(AVML_HINT, "server version %d.%d != included version %d.%d", major, minor, XDGA_MAJOR_VERSION, XDGA_MINOR_VERSION); have_dga = false; } else { XF86DGAGetVideoLL(disp, DefaultScreen(disp), (CAST_DGALL int*)&base, &width, &bank, &ram); if (!base) AVMOUT(AVML_WARN, "Can not allocate frame buffer base!"); } } } if (have_dga) { AVMOUT(AVML_DEBUG, "have dga"); // check framebuffer if (((unsigned long)vbuf.base & 0xfffff000) != ((unsigned long)base & 0xfffff000) ) { AVMOUT(AVML_HINT, "Video4Linux and DGA disagree about the framebuffer base"); AVMOUT(AVML_HINT, "Video4Linux: %p, dga: %p!", vbuf.base, base); AVMOUT(AVML_HINT, "You probably want to insmod the bttv module with " "\"vidmem=0x%03lx\"", (unsigned long)base >> 20); setupok=false; } if (m_iBpp == 0 && m_iPalette == 0) { // check color depth if ((unsigned int)((vbuf.depth+7)&0xf8) != format2depth[x11_format]) { AVMOUT(AVML_HINT, "Video4Linux and DGA disagree about the color depth"); AVMOUT(AVML_HINT, "Video4Linux: %d, DGA: %d.", (vbuf.depth + 7) & 0xf8, format2depth[x11_format]); AVMOUT(AVML_HINT, "Is kv4lsetup installed correctly?"); setupok=false; } } else AVMOUT(AVML_DEBUG, "Forced bpp %d, palette %d", m_iBpp, m_iPalette); } // have_dga #endif /* HAVE_LIBXXF86DGA */ return 0; } void v4l1if::setPalette(int pal) { x11_format = ((unsigned int)pal < sizeof(format2palette)/sizeof(unsigned short))?format2palette[pal]:0; vpic.palette= x11_format; if (vpic.palette == 0) AVMOUT(AVML_WARN, "Unsupported overlay video format <%s>", format_desc[x11_format]); if (ioctl(m_iDevV4L, VIDIOCSPICT, &vpic) == -1) AVMOUT(AVML_WARNNO, "VIDIOC_S_PICT"); }