Ambilight from any video source with a Raspberry (part 3)

A couple of months ago I purchased a HummingBoard-i2eX, a raspberry-like board with a more powerful i.mx6 chip. Since OpenELEC, Geebox and Raspbmc are actively supporting this board, I decided to try to use it as my main HTPC instead of my raspberry.

The main problem were the leds (I will talk about the other one in the next paragraph), Hyperion does not support boards different from the Raspberry Pi, so instead of messing with Hyperion source code, I decided to use the raspberry to just handle the leds. The nice thing of this setup is that you can still use the OpenELEC installed on it, so you do not have to buy another board just because you want the Ambilight while playing with your console or watching video from another device.

The parts list

As usual, let us start with the part list. I assume you already have a raspberry with working leds, if not, look at part one and part two of this series to build your own.

Beware of a couple of things. With the splitter in place, the CEC stopped working, so you cannot use your TV remote to pilot Kodi (ex XBMC and used by OpenELEC 5.0 rc3). I did not think about it when I purchased the parts, but after some searches, I found there are some that declare to support it, so keep it in mind when you buy your parts. For me is no big deal, because I have the infrared port of the hummingbird (look here on how to configure it) and actually I use my tablet or phone.

The second thing is the video capture, if you do some searches, you will find that not all chipsets are supported by the Linux kernel 3.11 and the one, suggested by many, is the Fushicai chip. Unfortunately, not all EasyCap use the same chipset, so you may buy a device that will not work. I purchased two different USB video grabbers (the other one was the August VGB100) and just the EasyCap works, so be careful when you buy yours.

The HDMI splitter, the video converter and the video grabber are needed to pass the signal to the Raspberry, while the HDMI switch is necessary only if you plan to use more than one video input. In my case, I use the input from the PS4 and the Hummingboard so, if you use your raspberry with OpenELEC and not just for controlling the leds, you can skip this purchase.

Here are the photos of the pieces.

 

WP_20141220_002 WP_20141220_005
WP_20141220_009 WP_20141220_006

WP_20141220_011

Check if the system correctly sees the video grabber

The first thing to do is to be sure that the system correctly see the video grabber. To do that, connect to OpenELEC (to learn how look at part 2) and type these two commands:

lsusb
ls /dev/video*

You should see your video grabber listed (in my case the Fushicai USBTV007) and then the video0 mounted in /dev directory

UsbList

Configuring Hyperion with OpenElec

Now that we have assembled all the pieces, we need to configure Hyperion to handle the video input.
The commands that I will show here are for OpenELEC, if you are using Raspbmc, you should use different commands (this page should help).

Hyperion support the v412 grabber, so we just need to activate it in our configuration file.

Open the hyperion.config.json file and add, at the end, the following lines of code:

/// Configuration for the embedded V4L2 grabber
///  * device          : V4L2 Device to use [default="/dev/video0"]
///  * input           : V4L2 input to use [default=0]
///  * standard        : Video standard (no-change/PAL/NTSC) [default="no-change"]
///  * width                : V4L2 width to set [default=-1]
///  * height               : V4L2 height to set [default=-1]
///  * frameDecimation      : Frame decimation factor [default=2]
///  * sizeDecimation       : Size decimation factor [default=8]
///  * priority             : Hyperion priority channel [default=800]
///  * mode                 : 3D mode to use 2D/3DSBS/3DTAB (note: no autodetection) [default="2D"]
///  * cropLeft             : Cropping from the left [default=0]
///  * cropRight            : Cropping from the right [default=0]
///  * cropTop              : Cropping from the top [default=0]
///  * cropBottom           : Cropping from the bottom [default=0]
///  * redSignalThreshold   : Signal threshold for the red channel between 0.0 and 1.0 [default=0.0]
///  * greenSignalThreshold : Signal threshold for the green channel between 0.0 and 1.0 [default=0.0]
///  * blueSignalThreshold  : Signal threshold for the blue channel between 0.0 and 1.0 [default=0.0]
"grabber-v4l2" :
{
    "device" : "/dev/video0",
    "input" : 0,
    "standard" : "no-change",
    "width" : -1,
    "height" : -1,
    "frameDecimation" : 2,
    "sizeDecimation" : 8,
    "priority" : 800,
    "mode" : "2D",
    "cropLeft" : 0,
    "cropRight" : 0,
    "cropTop" : 0,
    "cropBottom" : 0,
    "redSignalThreshold" : 0.0,
    "greenSignalThreshold" : 0.0,
    "blueSignalThreshold" : 0.0
}

Remember to put a comma on the line preceding this piece of code as in the picture:

Hyperion-v412 config

Reboot your Pi and we are ready to face the tricky part.

Setting the correct crop size and other params

The image coming from the video grabber will surely have some black border, what we need to tell Hyperion, is how much to crop from every border. Before we start, we need to choose a video frame or an image bright enough to easily distinguish the black border from the real image.

Let us start by stopping the Hyperion service or we will not be able to take the screenshots needed for our calibration. Just type:

killall hyperiond

Before executing the next command, move to a directory that you can access with your pc client, because Hyperion will save the screenshot in the correct directory.

I choose the screenshot directory because is already shared by OpenELEC so I do not have to manually copy the files.

Once you are in your preferred directory, copy and paste this command:

LD_LIBRARY_PATH=/storage/hyperion/bin /storage/hyperion/bin/hyperion-v4l2 /storage/.config/hyperion.config.json --crop-top 0 --crop-left 0 --crop-bottom 0 --crop-right 0 --size-decimator 8 --frame-decimator 2 --screenshot

You should end up with something like in the picture below:

Hhyperion screengrab

The result will we be like this

totalcmd_screen

The sizeDecimator specify the quality of the image, higher the number less the quality and, obviously, its size. The default value is eight and is acceptable. The image size is also very small (about 9kb).

Now that we have the image, we can adjust the crop params to get a clean image. For me, the final configuration is this one:

LD_LIBRARY_PATH=/storage/hyperion/bin /storage/hyperion/bin/hyperion-v4l2 /storage/.config/hyperion.config.json --crop-top 40 --crop-left 50 --crop-bottom 0 --crop-right 30 --size-decimator 8 --frame-decimator 2 --screenshot

screenshot

The image is small, but you get the idea of the result you should obtain.

Now we just need to update the configuration file with the values discovered and reboot the raspberry.

In case you want more precision, you can lower the sizeDecimator value, but remember that it will consume more CPU. In my test using a value of four I got a 27KB image and no noticeable differences. I cannot say if the OpenELEC playback is compromised somehow, because I use it only for the leds. It would be nice to hear from yours experiments.

Here is a video of Far Cry 4 from my PS4, I can say that playing with the leds on, is somehow more immersive.

If you need it, here you can download my complete configuration file.

Android application

As a plus, you can download the android app here or here, if you want to support the developer, and you can use the led as a mood lights.

If you have changed the path in the config file, as I wrote in part 2, you should also have all the effects available.