Wednesday 30 January 2013

The [not so] EasyCap for Linux

An EasyCap is a USB device that connects a composite video source (like an "analogue" cctv camera) to a computer.


The name "EasyCap" is not really a product name, because there are so many companies making copies of the original device. In fact one company (ClimaxDigital) got so fed up with the number of bad copies in circulation, that it change its branding to EzCAP, and more recently, dropped the name completely.


So you don't really know what you are getting when you order one of these. Its only when you connect it to your Linux computer and type "lsusb" that you discover what is inside.

But last week, when I noticed Amazon were selling a 4 channel EasyCap for only £7.38, I took a gamble and ordered one.

I already have two ClimaxDigital EzCAP modules which I've been using successfully on Linux for a few years to capture bird box video. However, I like the idea of a 4 channel unit as this may simplify my existing setup.

Once connected to my laptop, lsusb revealed this module uses the "Somagic" chip. After a bit of searching on the net, I found a bunch of crazy guys are working on software for this family of devices.

A bit of background

As I'm using Lubuntu, I initially installed the deb packages for this device from the repository. But these packages do not support the newer driver included with this module. So having picked up some more information along the way, I uninstalled the debs and started again.

The basic approach is to download the current source files, then substitute some of the more recent files that are compatible with the newer driver. Once this is done, we can make a version compatible with this 4 channel device.

The Process

Using a Windows computer (...yeah, I know...) insert the disk that came with the EasyCap. In the Drivers folder, run the setup.exe to unpack the drivers. Now you should be able to locate and copy the driver from the XP folder: SmiUsbGrabber3F.sys

The "3F" indicates the version. Versions earlier than 3E are covered by the deb packages, but for 3F we need more recent source files.

Copy the driver to your Linux computer.
Install dependencies and suggested packages via the terminal:-
 sudo apt-get install libusb-1.0-0 libgcrypt11 mplayer usbutils

 sudo apt-get install build-essential libgcrypt11-dev libusb-1.0-0-dev

Then we need to get the source files, so in terminal:-
sudo wget https://easycap-somagic-linux.googlecode.com/files/somagic-easycap_1.0.tar.gz
...and:-
sudo wget https://easycap-somagic-linux.googlecode.com/files/somagic-easycap-tools_1.0.tar.gz

Now unzip the TAR archives, which will create 2 new directories ("user" and "somagic-extract-firmware"):-
sudo tar xvf somagic-easycap_1.0.tar.gz
...and:-
sudo tar xvf somagic-easycap-tools_1.0.tar.gz

Next step is to get a new version of: somagic-extract-firmware.c
...which we can copy and paste into a new blank file from here:-
http://code.google.com/p/easycap-somagic-linux/source/browse/tools/somagic-extract-firmware/somagic-extract-firmware.c?r=b4e1adcd7535d2487e5bbdaa54686f6fa087ddc1

Open the Lubuntu file manager (pcmanfm) and select Tools > Open current folder as root
Now change permissions on the somagic-extract-firmware folder (either make yourself the owner or give anyone full permission.... Hey! I don't care)

Open your somagic-extract-firmware directory and replace the old version of somatic-extract-firmware.c with this new version.

Now open a terminal in this folder (or cd into it) and run:-
make
...then
sudo make install

Still in terminal, cd to wherever you left the driver (SmiUsbGrabber3F.sys) then:-
sudo somagic-extract-firmware SmiUsbGrabber3F.sys
...and you should get a response like this:-
   Firmware written to /lib/firmware/somagic_firmware.bin  {file size = 6.5kB}

Now look for the newly created "user" directory (e.g. /home/{username}/user) and change permissions as before.

Get new somagic-int file from here:-
http://code.google.com/p/easycap-somagic-linux/source/browse/user/somagic-init.c?r=306a7981a53dfbd62bb579090cfcd81fefb928a6
..and overwrite the existing file which will be in the "user" directory.

Get new somagic-capture file:-
http://code.google.com/p/easycap-somagic-linux/source/browse/user/somagic-capture.c?r=ef1195b61fa36dc7f93bde82c3f4fadde5157574
..and overwrite the existing file which will also be in the "user" directory.

In terminal, cd to the "user" directory, then:-
make
sudo make install


Now connect the EasyCap device to your computer USB port, and in terminal:-
sudo somagic-init
...and now:-
lsusb
...which should show your device as 1c88:003f

Now connect a camera to input #2 and try this:-
sudo somagic-capture --iso-transfers=20 | mplayer -vf yadif,screenshot -demuxer rawvideo -rawvideo "pal:format=uyvy:fps=25" -aspect 4:3 -

Unfortunately I get an image like this:-


...but you may be lucky.

I've tried this on a laptop and a desktop (both running Lubuntu 12.10) and used 2 different cameras. Changes to the command line (including video standards PAL & NTSC, and also iso-transfers ) so far have not shown any improvement.

Just to make sure there is not a problem with the device, I've run this on Windows 7:-


...nice!



Edit 4th February 2015:-


In the comments below, a couple of people have taken the time to suggest solutions.

rat Mole
somagic-capture --sync=1 | mplayer -vf yadif,screenshot -demuxer rawvideo -rawvideo "pal:format=uyvy:fps=25" -aspect 4:3 -

Philip Jonker3
NTSC:-
sudo somagic-capture -c --iso-transfers 100 --ntsc --sync=1 | mplayer -nocache -vf yadif -demuxer rawvideo -rawvideo "ntsc:format=uyvy:fps=30" -aspect 4:3 -
 
PAL:-
sudo somagic-capture -c --iso-transfers 100 --pal --sync=1 | mplayer -nocache -vf yadif -demuxer rawvideo -rawvideo "pal:format=uyvy:fps=25" -aspect 4:3 -

It looks like the key is the --sync=1 option.

Many thanks.

Now that the Raspberry Pi 2 has been released (which can be run on Debian or Ubuntu) I plan to buy one, dust off both my Somagic and ClimaxDigital modules, and give it a go...
...I've just got a couple of other projects ahead of this in the queue!

9 comments:

  1. I have 4ch somagic and have same problem in Linux.

    ReplyDelete
  2. How to get /dev/videoX with easycap ?

    ReplyDelete
    Replies
    1. Hi Hakan, its a long time since I looked at this (wow!...nearly 2 years) but I think somagic-capture is just the userland application. To get a /dev/videoX you need to create a proper device driver. Take a look at this thread: https://groups.google.com/forum/#!topic/easycap-somagic-linux/yDIGkySfLNk

      If this doesn't answer your question, try asking on that forum. The guys there are very helpful, but also very teckie. So be persistent if you don't understand what they are talking about.

      Delete
    2. ...just noticed that this is also mentioned on this page: https://code.google.com/p/easycap-somagic-linux/wiki/GettingStarted

      ...see the second "Note"

      I hope this helps.

      Delete
  3. This worked for me for NTSC:
    sudo somagic-capture -c --iso-transfers 100 --ntsc --sync=1 | mplayer -nocache -vf yadif -demuxer rawvideo -rawvideo "ntsc:format=uyvy:fps=30" -aspect 4:3 -

    And this for PAL:
    sudo somagic-capture -c --iso-transfers 100 --pal --sync=1 | mplayer -nocache -vf yadif -demuxer rawvideo -rawvideo "pal:format=uyvy:fps=25" -aspect 4:3 -

    ReplyDelete
    Replies
    1. Many thanks to you and raT Mole. I've now edited the post and added your suggestion.

      Delete
  4. After days of killing with Raspberry pi B+, easycap somagic and cctv camera, I finally install driver successfully - "1c88:003c Somagic, Inc" in lsusb. I have http://www.elro.eu/en/products/cat/security/camera-surveillance/cameras/color-security-camera
    this camera. On raspberry I run last jessie.
    When I try to capture with command:
    sudo somagic-capture -c --sync=1 | mplayer -nocache -vf yadif -demuxer rawvideo -rawvideo "ntsc:format=uyvy:fps=10" -aspect 4:3 -
    , mplayer open and just close, and in terminal i get:

    1 get descriptor returned -7, bytes:
    MPlayer2 2.0-728-g2c378c7-4+b1 (C) 2000-2012 MPlayer Team
    Cannot open file '/home/pi/.mplayer/input.conf': No such file or directory
    Failed to open /home/pi/.mplayer/input.conf.
    Cannot open file '/etc/mplayer/input.conf': No such file or directory
    Failed to open /etc/mplayer/input.conf.

    Playing -.
    Reading from stdin...
    Failed to set active alternate setting for interface (after set_configuration): Connection timed out
    Detected file format: rawvideo
    Load subtitles in .
    Failed to open VDPAU backend libvdpau_nvidia.so: cannot open shared object file: No such file or directory
    [vdpau] Error when calling vdp_device_create_x11: 1
    [VO_XV] It seems there is no Xvideo support for your video card available.
    [VO_XV] Run 'xvinfo' to verify its Xv support and read
    [VO_XV] DOCS/HTML/en/video.html#xv!
    [VO_XV] See 'mplayer -vo help' for other (non-xv) video out drivers.
    [VO_XV] Try -vo x11.
    [gl] GLX version older than 1.3.
    [gl] no GLX support present
    [ass] auto-open
    Opening video filter: [yadif]
    VIDEO: 720x480 10.000 fps 55296.0 kbps (6912.0 kB/s)
    Could not find matching colorspace - retrying with -vf scale...
    Opening video filter: [scale]
    Aspect ratio is 1.33:1 - scaling to correct movie aspect.
    [swscaler @ 0xb5a5bae0]using unscaled uyvy422 -> yuv420p special converter
    VO: [x11] 720x480 => 720x540 Planar YV12
    [swscaler @ 0xb5a5bae0]No accelerated colorspace conversion found from yuv420p to rgb565le.
    Selected video codec: RAW UYVY [raw]
    Audio: no sound
    Starting playback...
    V: 0.0 0/ 0 ??% ??% ??,?% 0 0


    Exiting... (End of file)


    I tried similar parameters, vlc, ffmp, ... cant get capture :(

    ReplyDelete
    Replies
    1. Oh.. and when ls /dev/video* command returnno such file or directory. Is it possible that EasyCAP has right drivers, but raspi cant recognized camera on EasyCAP?

      Sorry I am noob.

      Delete
  5. For me, on **Ubuntu 14.04**, I solve the problem/issue of image flickered/flickering by setting the output to NTSC instead of PAL on mplayer/vlc.

    mplayer:
    sudo somagic-capture --cvbs-input=1 --ntsc | mplayer -vf yadif,screenshot -demuxer rawvideo -rawvideo "ntsc:format=uyvy:fps=30" -aspect 4:3 -

    VLC:
    sudo somagic-capture --cvbs-input=1 --ntsc | vlc --demux rawvid --rawvid-fps 30 --rawvid-width 720 --rawvid-height 480 --rawvid-chroma=UYVY -

    The "--sync=1" method does not works at all, always get the error message:
    "somagic-capture: Failed to submit request #84 for transfer: Cannot allocate memory"

    ReplyDelete