Ignore:
Timestamp:
02/12/08 16:01:56 (17 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Cosy/videodev
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Cosy/videodev/MVideo.cc

    r8859 r8861  
    4545#include <sys/ioctl.h> // ioctl
    4646
     47#include <TEnv.h>
    4748#include <TString.h>
    4849
     
    5051#include "MLogManip.h"
    5152
     53#undef DEBUG
     54
    5255using namespace std;
    5356
    5457//ClassImp(MVideo);
    5558
     59MVideoCtrl::MVideoCtrl(const v4l2_queryctrl &ctrl)
     60{
     61    fId      = ctrl.id;
     62    fName    = (const char*)ctrl.name;
     63    fMinimum = ctrl.minimum;
     64    fMaximum = ctrl.maximum;
     65    fStep    = ctrl.step;
     66    fDefault = ctrl.default_value;
     67}
     68
    5669// -------------------------------------------------------------
    5770//
     
    6174{
    6275    Reset();
     76
     77    fControls.SetOwner();
    6378}
    6479
     
    7287    memset(&fChannel, 0, sizeof(fChannel));
    7388    memset(&fBuffer,  0, sizeof(fBuffer));
     89    memset(&fAbil,    0, sizeof(fAbil));
    7490
    7591    fFileDesc = -1;
    7692    fMapBuffer = 0;
    7793    fChannel.channel = -1;
     94    fAbil.tuner = -1;
     95
     96    fControls.Delete();
    7897}
    7998
     
    137156// -------------------------------------------------------------
    138157//
     158// Read the video standard
     159//
     160Bool_t MVideo::GetVideoStandard()
     161{
     162    return Ioctl(VIDIOC_G_STD, &fVideoStandard)==-1;
     163}
     164
     165// -------------------------------------------------------------
     166//
     167// Read the abilities of the tuner
     168//
     169Bool_t MVideo::GetTunerAbilities()
     170{
     171    fAbil.tuner = 0; // FIXME?
     172    return Ioctl(VIDIOCGTUNER, &fAbil)!=-1;
     173}
     174
     175// -------------------------------------------------------------
     176//
     177// Enumerate (get) all controls from the device and store them
     178// as MVideoCtrl in fControls, starting with the id given as
     179// argument.
     180//
     181Bool_t MVideo::EnumerateControls(UInt_t id)
     182{
     183    struct v4l2_queryctrl qctrl;
     184    qctrl.id = id;
     185
     186    while (1)
     187    {
     188        if (Ioctl (VIDIOC_QUERYCTRL, &qctrl)==-1)
     189            break;
     190
     191        if (qctrl.maximum<=qctrl.minimum)
     192            continue;
     193
     194        fControls.Add(new MVideoCtrl(qctrl));
     195
     196        qctrl.id++;
     197    }
     198
     199    return kTRUE;
     200}
     201
     202// -------------------------------------------------------------
     203//
     204// Enumerate (get) all basic and private controls from the
     205// device and store them as MVideoCtrl in fControls.
     206//
     207Bool_t MVideo::EnumerateControls()
     208{
     209    if (!EnumerateControls(V4L2_CID_BASE))
     210        return kFALSE;
     211    if (!EnumerateControls(V4L2_CID_PRIVATE_BASE))
     212        return kFALSE;
     213
     214    return kTRUE;
     215}
     216
     217// -------------------------------------------------------------
     218//
     219// Reset a given control to it's default value as defined
     220// by the device.
     221//
     222Bool_t MVideo::ResetControl(MVideoCtrl &vctrl) const
     223{
     224    return WriteControl(vctrl, vctrl.fDefault);
     225}
     226
     227// -------------------------------------------------------------
     228//
     229// Reset all enumereated device controls to their default.
     230// The default is defined by the device iteself.
     231//
     232Bool_t MVideo::ResetControls() const
     233{
     234    Bool_t rc = kTRUE;
     235
     236    TIter Next(&fControls);
     237    MVideoCtrl *ctrl = 0;
     238    while ((ctrl=((MVideoCtrl*)Next())))
     239        if (!ResetControl(*ctrl))
     240        {
     241            gLog << err << "ERROR - Could not reset " << ctrl->fName << "." << endl;
     242            rc = kFALSE;
     243        }
     244
     245    return rc;
     246}
     247
     248// -------------------------------------------------------------
     249//
     250//  Read the value of the given control from the device
     251// and store it back into the given MVideoCtrl.
     252//
     253Bool_t MVideo::ReadControl(MVideoCtrl &vctrl) const
     254{
     255    struct v4l2_control ctrl = { vctrl.fId, 0 };
     256    if (Ioctl(VIDIOC_G_CTRL, &ctrl)==-1)
     257        return kFALSE;
     258
     259    vctrl.fValue = ctrl.value;
     260
     261    return kTRUE;
     262}
     263
     264// -------------------------------------------------------------
     265//
     266// Write the given value into the given control of the device.
     267// On success the value is stored in the given MVideoCtrl.
     268//
     269Bool_t MVideo::WriteControl(MVideoCtrl &vctrl, Int_t val) const
     270{
     271    if (val<vctrl.fMinimum)
     272    {
     273        gLog << err << "ERROR - Value of " << val << " below minimum of " << vctrl.fMinimum << " for " << vctrl.fName << endl;
     274        return kFALSE;
     275    }
     276
     277    if (val>vctrl.fMaximum)
     278    {
     279        gLog << err << "ERROR - Value of " << val << " above maximum of " << vctrl.fMaximum << " for " << vctrl.fName << endl;
     280        return kFALSE;
     281    }
     282
     283    struct v4l2_control ctrl = { vctrl.fId, val };
     284    if (Ioctl(VIDIOC_S_CTRL, &ctrl)==-1)
     285        return kFALSE;
     286
     287    vctrl.fValue = val;
     288
     289    return kTRUE;
     290}
     291
     292// -------------------------------------------------------------
     293//
     294// Set all controls from a TEnv. Note that all whitespaces
     295// and colons in the control names (as defined by the name of
     296// the MVideoCtrls stored in fControls) are replaced by
     297// underscores.
     298//
     299Bool_t MVideo::SetControls(TEnv &env) const
     300{
     301    Bool_t rc = kTRUE;
     302
     303    TIter Next(&fControls);
     304    TObject *o = 0;
     305    while ((o=Next()))
     306    {
     307        if (!env.Defined(o->GetName()))
     308            continue;
     309
     310        TString str = env.GetValue(o->GetName(), "");
     311        str = str.Strip(TString::kBoth);
     312        str.ReplaceAll(" ", "_");
     313        str.ReplaceAll(":", "_");
     314        if (str.IsNull())
     315            continue;
     316
     317        MVideoCtrl &ctrl = *static_cast<MVideoCtrl*>(o);
     318
     319        const Int_t val = str=="default" || str=="def" ?
     320            ctrl.fDefault : env.GetValue(o->GetName(), 0);
     321
     322        if (!WriteControl(ctrl, val))
     323            rc = kFALSE;
     324    }
     325
     326    return rc;
     327}
     328
     329
     330// -------------------------------------------------------------
     331//
    139332// Open channel ch of the device
    140333//
     
    184377    }
    185378
     379    gLog << warn << "Setting video standard to PAL-N." << endl;
     380
     381    fVideoStandard = V4L2_STD_PAL_N;
     382    if (Ioctl(VIDIOC_S_STD, &fVideoStandard)==-1)
     383    {
     384        gLog << err << "ERROR - Could not set video standard to PAL-N." << endl;
     385        return kFALSE;
     386    }
     387
     388    if (!EnumerateControls())
     389    {
     390        gLog << err << "ERROR - Could not enumerate controls." << endl;
     391        return kFALSE;
     392    }
     393
     394    if (!ResetControls())
     395    {
     396        gLog << err << "ERROR - Could not reset controls to default." << endl;
     397        return kFALSE;
     398    }
     399
    186400    if (!GetCapabilities())
    187401    {
     
    198412        return kFALSE;
    199413    }
    200 
     414/*
     415    if (HasTuner())
     416    {
     417        if (!GetTunerAbilities())
     418        {
     419            gLog << err << "ERROR - Couldn't get tuner abilities." << endl;
     420            return kFALSE;
     421        }
     422    }
     423 */
    201424    // get mmap grab buffer info
    202425    if (Ioctl(VIDIOCGMBUF, &fBuffer)==-1)
     
    207430
    208431    // map file (device) into memory
    209     fMapBuffer = (char*)mmap(0, fBuffer.size, PROT_READ|PROT_WRITE, MAP_SHARED, fFileDesc, 0);
     432    fMapBuffer = (unsigned char*)mmap(0, fBuffer.size, PROT_READ|PROT_WRITE, MAP_SHARED, fFileDesc, 0);
    210433    if (fMapBuffer == (void*)-1)
    211434    {
     
    258481Bool_t MVideo::CaptureStart(unsigned int frame) const
    259482{
     483    frame %= fBuffer.frames;
     484
    260485    struct video_mmap gb =
    261486    {
    262         frame%fBuffer.frames,            // frame
     487        frame,                           // frame
    263488        fCaps.maxheight, fCaps.maxwidth, // height, width
    264         VIDEO_PALETTE_RGB24              // palette
     489        VIDEO_PALETTE_RGB24             // palette
    265490    };
     491
     492#ifdef DEBUG
     493    gLog << dbg << "CapturStart(" << frame << ")" << endl;
     494#endif
    266495
    267496    //
     
    273502//    if (errno == EAGAIN)
    274503    gLog << err;
    275     gLog << "ERROR - Couldn't start capturing frame " << frame << " - unable to sync." << endl;
     504    gLog << "ERROR - Couldn't start capturing frame " << frame << "." << endl;
    276505    gLog << "        Maybe your card doesn't support VIDEO_PALETTE_RGB24." << endl;
    277506
     
    283512// Wait until hardware has finished capture into framebuffer frame
    284513//
    285 Int_t MVideo::CaptureWait(unsigned int frame, char **ptr) const
     514Int_t MVideo::CaptureWait(unsigned int frame, unsigned char **ptr) const
    286515{
    287516    frame %= fBuffer.frames;
    288517
    289     *ptr = NULL;
     518    if (ptr)
     519        *ptr = NULL;
    290520
    291521    const int SYNC_TIMEOUT = 1;
     522
     523#ifdef DEBUG
     524    gLog << dbg << "CaptureWait(" << frame << ")" << endl;
     525#endif
    292526
    293527    alarm(SYNC_TIMEOUT);
     
    302536    if (rc==-1)
    303537    {
    304         gLog << err << "ERROR - Waiting for captured frame failed." << endl;
    305         return kFALSE;
    306     }
    307 
    308     *ptr = fMapBuffer+fBuffer.offsets[frame];
     538        gLog << err << "ERROR - Waiting for " << frame << " frame failed." << endl;
     539        return kFALSE;
     540    }
     541
     542    if (ptr)
     543        *ptr = fMapBuffer+fBuffer.offsets[frame];
    309544
    310545    return kTRUE;
     
    315550// Change the channel of a priviously opened device
    316551//
    317 Bool_t MVideo::SetChannel(Int_t chan)
     552Int_t MVideo::SetChannel(Int_t chan)
    318553{
    319554    if (fChannel.channel==chan)
    320         return kTRUE;
     555        return kSKIP;
    321556
    322557    if (chan<0 || chan>=fCaps.channels)
     
    360595// -------------------------------------------------------------
    361596//
     597// Has a tuner
     598//
     599Bool_t MVideo::HasTuner() const
     600{
     601    return fCaps.type&VID_TYPE_TUNER;
     602}
     603
     604// -------------------------------------------------------------
     605//
    362606// Returns the number of frame buffers which can be used
    363607//
     
    394638    if (CanCapture())
    395639        rc += " capture";
    396     if (type&VID_TYPE_TUNER)
     640    if (HasTuner())
    397641        rc += " tuner";
    398642    if (type&VID_TYPE_TELETEXT)
     
    415659}
    416660
     661TString MVideo::GetTunerFlags(Int_t flags) const
     662{
     663    TString rc;
     664    if (flags&VIDEO_TUNER_PAL)
     665        rc += " PAL";
     666    if (flags&VIDEO_TUNER_NTSC)
     667        rc += " NTSC";
     668    if (flags&VIDEO_TUNER_SECAM)
     669        rc += " SECAM";
     670    if (flags&VIDEO_TUNER_LOW)
     671        rc += " kHz";
     672    if (flags&VIDEO_TUNER_NORM)
     673        rc += " CanSetNorm";
     674    if (flags&VIDEO_TUNER_STEREO_ON)
     675        rc += " StereoOn";
     676    return rc;
     677}
     678
     679TString MVideo::GetTunerMode(Int_t mode) const
     680{
     681    switch (mode)
     682    {
     683    case VIDEO_MODE_PAL:
     684        return "PAL";
     685    case VIDEO_MODE_NTSC:
     686        return "NTSC";
     687    case VIDEO_MODE_SECAM:
     688        return "SECAM";
     689    case VIDEO_MODE_AUTO:
     690        return "AUTO";
     691    }
     692    return "undefined";
     693}
     694
    417695// -------------------------------------------------------------
    418696//
     
    426704    if (flags&VIDEO_VC_AUDIO)
    427705        rc += " audio";
     706//    if (flags&VIDEO_VC_NORM)
     707//        rc += " normsetting";
    428708    return rc;
    429709}
     
    438718        return "TV";
    439719    if (type&VIDEO_TYPE_CAMERA)
    440         return "camera";
     720        return "Camera";
    441721    return "unknown";
    442722}
     
    448728TString MVideo::GetPalette(Int_t pal) const
    449729{
    450     if (pal&VIDEO_PALETTE_GREY)
    451         return "Linear intensity grey scale";
    452     if (pal&VIDEO_PALETTE_HI240)
    453         return "BT848 8-bit color cube";
    454     if (pal&VIDEO_PALETTE_RGB565)
    455         return "RGB565 packed into 16-bit words";
    456     if (pal&VIDEO_PALETTE_RGB555)
    457         return "RGB555 packed into 16-bit words, top bit undefined";
    458     if (pal&VIDEO_PALETTE_RGB24)
    459         return "RGB888 packed into 24-bit words";
    460     if (pal&VIDEO_PALETTE_RGB32)
    461         return "RGB888 packed into the low three bytes of 32-bit words. Top bits undefined.";
    462     if (pal&VIDEO_PALETTE_YUV422)
    463         return "Video style YUV422 - 8-bit packed, 4-bit Y, 2-bits U, 2-bits V";
    464     if (pal&VIDEO_PALETTE_YUYV)
    465         return "YUYV";
    466     if (pal&VIDEO_PALETTE_UYVY)
    467         return "UYVY";
    468     if (pal&VIDEO_PALETTE_YUV420)
    469         return "YUV420";
    470     if (pal&VIDEO_PALETTE_YUV411)
    471         return "YUV411";
    472     if (pal&VIDEO_PALETTE_RAW)
    473         return "Raw capture (Bt848)";
    474     if (pal&VIDEO_PALETTE_YUV422P)
    475         return "YUV 4:2:2 planar";
    476     if (pal&VIDEO_PALETTE_YUV411P)
    477         return "YUV 4:1:1 planar";
    478 
     730    switch (pal)
     731    {
     732    case VIDEO_PALETTE_GREY:
     733        return "VIDEO_PALETTE_GREY: Linear intensity grey scale";
     734    case VIDEO_PALETTE_HI240:
     735        return "VIDEO_PALETTE_HI240: BT848 8-bit color cube";
     736    case VIDEO_PALETTE_RGB565:
     737        return "VIDEO_PALETTE_RGB565: RGB565 packed into 16-bit words";
     738    case VIDEO_PALETTE_RGB555:
     739        return "VIDEO_PALETTE_RGB555: RGB555 packed into 16-bit words, top bit undefined";
     740    case VIDEO_PALETTE_RGB24:
     741        return "VIDEO_PALETTE_RGB24: RGB888 packed into 24-bit words";
     742    case VIDEO_PALETTE_RGB32:
     743        return "VIDEO_PALETTE_RGB32: RGB888 packed into the low three bytes of 32-bit words. Top bits undefined.";
     744    case VIDEO_PALETTE_YUV422:
     745        return "VIDEO_PALETTE_YUV422: Video style YUV422 - 8-bit packed, 4-bit Y, 2-bits U, 2-bits V";
     746    case VIDEO_PALETTE_YUYV:
     747        return "VIDEO_PALETTE_YUYV: YUYV";
     748    case VIDEO_PALETTE_UYVY:
     749        return "VIDEO_PALETTE_UYVY: UYVY";
     750    case VIDEO_PALETTE_YUV420:
     751        return "VIDEO_PALETTE_YUV420: YUV420";
     752    case VIDEO_PALETTE_YUV411:
     753        return "VIDEO_PALETTE_YUV411: YUV411";
     754    case VIDEO_PALETTE_RAW:
     755        return "VIDEO_PALETTE_RAW: Raw capture (Bt848)";
     756    case VIDEO_PALETTE_YUV422P:
     757        return "VIDEO_PALETTE_YUV422P: YUV 4:2:2 planar";
     758    case VIDEO_PALETTE_YUV411P:
     759        return "VIDEO_PALETTE_YUV411P: YUV 4:1:1 planar";
     760    }
    479761    return "unknown";
    480762}
     
    505787    {
    506788        gLog  << " - Channel:    " << fChannel.channel << " (" << fChannel.name << ")" << endl;
    507         gLog  << " - IsA:        " << GetChannelType(fChannel.type) << " with " << GetChannelFlags(fChannel.flags) << endl;
     789        gLog  << " - IsA:        " << GetChannelType(fChannel.type) << " with " << GetChannelFlags(fChannel.flags) << " (" << fChannel.flags << ")" << endl;
    508790        //if (fChannel.flags&VIDEO_VC_NORM)
    509791        gLog  << " - Norm:       " << fChannel.norm << endl;
    510792        gLog  << endl;
    511793    }
     794
     795    if (fAbil.tuner>=0)
     796    {
     797        gLog << " - Tuner:           " << fAbil.tuner << endl;
     798        gLog << " - Name:            " << fAbil.name << endl;
     799        gLog << " - Tuner Range:     " << fAbil.rangelow << " - " << fAbil.rangehigh << endl;
     800        gLog << " - Tuner flags:    " << GetTunerFlags(fAbil.flags) << " (" << fAbil.flags << ")" << endl;
     801        gLog << " - Tuner mode:      " << GetTunerMode(fAbil.mode) << " (" << fAbil.mode << ")" <<endl;
     802        gLog << " - Signal Strength: " << fAbil.signal << endl;
     803    }
     804
    512805    gLog  << " - Brightness: " << fProp.brightness << endl;
    513806    gLog  << " - Hue:        " << fProp.hue << endl;
     
    524817        gLog  << " 0x" << fBuffer.offsets[i];
    525818    gLog  << dec << endl;
     819
     820    gLog << "Controls:" << endl;
     821    fControls.Print();
    526822}
    527823
  • trunk/MagicSoft/Cosy/videodev/MVideo.h

    r8845 r8861  
    88#ifndef __CINT__
    99#ifndef __LINUX_VIDEODEV_H
    10 #include "videodev.h" // video4linux
     10#include <linux/videodev.h>  // video4linux
     11#endif
     12#ifndef __LINUX_VIDEODEV2_H
     13#include <linux/videodev2.h> // video4linux2
    1114#endif
    1215#endif
     16
     17struct v4l2_queryctrl;
     18class TEnv;
     19
     20class MVideoCtrl : public TObject
     21{
     22    friend class MVideo;
     23private:
     24    UInt_t  fId;
     25    //enum v4l2_ctrl_type  type;
     26    TString fName;
     27    Int_t   fMinimum;
     28    Int_t   fMaximum;
     29    Int_t   fStep;
     30    Int_t   fDefault;
     31    UInt_t  fFlags;
     32
     33    UInt_t  fValue;
     34
     35public:
     36    MVideoCtrl(const v4l2_queryctrl &ctrl);
     37    const char *GetName() const { return fName; }
     38    const char *GetTitle() const { return Form("Range=[%d;%d] Step=%d Def=%d", fMinimum, fMaximum, fStep, fDefault); }
     39
     40    //ClassDef(MVideoCtrl, 0) // Helper class to enumare device controls
     41};
    1342
    1443class MVideo
     
    1948    int fFileDesc; // File descriptor
    2049
    21     char *fMapBuffer;
     50    unsigned char *fMapBuffer;
    2251
    2352protected:
     
    2655    struct video_mbuf       fBuffer;    // Buffer information
    2756    struct video_picture    fProp;      // Picture properties
     57    struct video_tuner      fAbil;      // Tuner abilities
     58
     59    ULong64_t fVideoStandard;
     60
     61    TList fControls;
    2862
    2963private:
     
    3266    void Reset();
    3367
     68    Bool_t EnumerateControls(UInt_t id);
     69    Bool_t EnumerateControls();
    3470    Bool_t GetCapabilities();
    3571    Bool_t GetProperties();
     72    Bool_t GetTunerAbilities();
     73    Bool_t GetVideoStandard();
    3674    Bool_t Init(Int_t channel);
    3775
     
    4078    TString GetChannelFlags(Int_t flags) const;
    4179    TString GetChannelType(Int_t type) const;
     80    TString GetTunerFlags(Int_t type) const;
     81    TString GetTunerMode(Int_t type) const;
    4282    TString GetPalette(Int_t pal) const;
    4383
     
    4989    Bool_t IsOpen() const { return fFileDesc>0 && fMapBuffer; }
    5090    Bool_t CanCapture() const;
     91    Bool_t HasTuner() const;
    5192    Int_t  GetNumBuffers() const;
    5293
     
    5899    Int_t  Close();
    59100
    60     Bool_t SetChannel(Int_t chan);
     101    Int_t SetChannel(Int_t chan);
     102    Bool_t ReadControl(MVideoCtrl &vctrl) const;
     103    Bool_t WriteControl(MVideoCtrl &vctrl, Int_t val) const;
     104    Bool_t SetControls(TEnv &env) const;
     105    Bool_t ResetControl(MVideoCtrl &vctrl) const;
     106    Bool_t ResetControls() const;
    61107
    62108    // Image capture
    63109    Bool_t CaptureStart(unsigned int frame) const;
    64     Int_t  CaptureWait(unsigned int frame, char **ptr) const;
     110    Int_t  CaptureWait(unsigned int frame, unsigned char **ptr=0) const;
    65111
    66112    // Support
Note: See TracChangeset for help on using the changeset viewer.