MxPEG to H.264

Get a Raspberry Pi, or one of the very cheap clones. Then install FFMpeg and an RTMP server with RTSP capability (EvoStream, Wowza).

Make sure the RTMP server is operational.

Ask FFMpeg to convert from MxPEG to H.264 and broadcast to the RTMP server, by entering this command (on one line)

ffmpeg 
  -f mxg 
  -i http://[user]:[pass]@[camera-host]/cgi-bin/faststream.jpg?stream=mxpeg 
  -codec:v libx264 
  -b:v 500k 
  -maxrate 500k 
  -bufsize 1000k 
  -vf scale=-1:480 
  -threads 0 
  -an 
  -f flv [rtmp address]

If you are using EvoStream, you might have entered something like this for the RTMP address:

rtmp://[ip of evostream]/live/mobotix

If that is the case, you can add a generic RTSP camera to your VMS

rtsp://[ip of evostream]/mobotix

The MxPEG stream will now be converted to H.264 and recorded as such. You’ll miss out on the advantages of MxPEG, but sometimes there’s no other way around the issue.

Advertisements

Thoughts on Low Light Cameras

For security applications, I don’t rely on a cameras low light performance to provide great footage. I recently installed a camera in my basement, and naturally I ran a few tests to ensure that I would get good footage if anyone were to break in. After some experimentation, I would get either

  • Black frames, no motion detected
  • Grainy, noisy footage triggering motion recording all night
  • Ghosts moving around

The grainy images were caused by the AGC. It simply multiplies the pixel values by some factor, this amplifies everything – including noise. Such imagery does not compress well – so the frames take up a lot of space in the DB, and may falsely trigger motion detection. Cranking up the compression to solve the size problem is a bad solution, as it will simply make the video completely useless. I suppose that some motion detection algorithms can take the noise into consideration, and provide a more robust detection, but you may not have that option on your equipment.

Black frames is dues to the shuttertime being too fast, and AGC being turned off. On the other hand, if I lower the shutter speed, I get blurry people (ghosts) unless the burglar is standing still, facing the camera, for some duration (Which I don’t want to rely upon).

A word of caution

Often you’ll see demonstrations of AGC, where you have a fairly dark image on one side, and a much better image on the other, usually accompanied by some verbiage about how Automatic Gain Control works its “magic”. Notice that in many cases the subject in the scene is stationary. The problem here is that you don’t know the settings for the reference frame. It might be that the shutter speed is set to 1/5th of a second.  The problem is that a 1/5th of second shutter speed is way too slow for most security applications – leading to motion blur.
Ghost

During installation of the cameras, there are too common pitfalls

  1. Leave shutter and gain settings to “Auto”
  2. Manually set the shutter speed with someone standing (still) in front of the camera

#1 will almost certainly cause problems in low light conditions, as the camera turns the shutter waaay down, leading to adverse ghosting. #2 is better, but your pal should be moving around, and you should look at single frames from the video when determining the quality. This applies to live demos too : always make sure that the camera is capturing a scene with some motion, and request an exported still frame, to make sure you can make out the features properly.

A low tech solution

What I prefer, is for visible light to turn on, when something is amiss. This alerts my neighbours, and hopefully it causes the burglar to abort his mission. I also have quite a few PIR (Passive InfraRed) sensors in my house. They detect motion like a champ, in total darkness (they are – in a sense – a 1 pixel FLIR camera), and they don’t even trigger when our cat is on the prowl.

So, if the PIR sensors trigger, I turn on the light. Hopefully, that scares away the burglar. And since the light is on, I get great shots, without worrying about AGC or buying an expensive camera.

The cheapest DIY PIR sensors are around $10 bucks, you’ll then need some additional gear to wire it all together, but if you are nerdy enough to read this blog, I am pretty sure you already have some wiring in the house to control the lighting too.

So – it’s useless – right?

Well, it depends on the application. I am trying to protect my belongings and my house from intruders, that’s the primary goal. I’d prefer if the cameras never, ever recorded a single frame. But there are many other types of applications where low light cameras come in handy. If you can’t use visible light, and the camera is good enough that it can save you from using an infrared source then that might be your ROI. All else being equal, I’d certainly chose a camera with great low light capabilities over one that is worse, but rarely are things equal. The camera is more expensive, perhaps it has lower resolution and so on.

Finally a word on 3D noise reduction

Traditionally noise reduction was done by looking at adjacent pixels. Photoshop has a filter called “despeckle” which will remove some of the noise in a frame. It does so by looking at other pixels in the frame, and so we get 2 dimensions (vertical and horizontal). By looking at frames in the past, we can add a 3rd dimension – time – to the mix (hence 3D). If the camera is stationary, the algorithm tries to determine if a change of pixel value between frames, is caused by true change, or because of noise. Depending on a threshold, the change is either discarded as noise, or kept as a true change. You might also hear terms such as spatiotemporal, meaning “space and time” – basically, another way of expressing the same as 3D noise reduction.

 

Analog vs IP Video

In the good old days, you had to have a guy walk in front of an automobile, ringing a bell to warn pedestrians that a car was coming (at walking pace), it was unreliable, difficult to operate, and one false move and it became an instant death trap. Horses, on the other hand, were easy to replace, they all fit in the old barn, and they all ate pretty much the same food.

The car analogy is not to say that IP is the car of today compared to a horse; the analogy is that when cars first came about, it took a long time before they became as homogeneous as they are today. Back then it was very difficult to see cars as a viable alternative to the horse. The point I was trying to make is that we are busy designing a better car, while others are convinced that Horse 2.0 is the way to go.

So is IP really better than Analog?

A DVR is analog – right?

Well… Let’s define the terms a little, to avoid the semantic confusion. In this discussion, “analog” refers to the transmission mechanism from the camera to the recording device. “analog” means that the camera sends an analog, uncompressed NTSC/PAL signal directly to the recording device. Whereas the an IP camera captures the image, compresses it and sends it via an IP network to the recording device.

The recording device may be labelled “DVR” or “NVR”, but in most cases the internal components are roughly the same. A DVR usually comes with a framegrabber card preinstalled that allows the recorder to capture the analog video and store it in digital form on a storage medium (is this starting to sound like lawyer-speak?). Likewise, an NVR may be retrofitted with a framegrabber card too, and thus the DVR and NVR becomes almost indistinguishable. Therefore, the discussion is not about DVR vs NVR, but rather analog vs. digital transmission of video.

Cabling
If you already have cabling in place, or if the placement of the cameras is such that you can’t cluster the cameras, then the cabling part of it is equal. But if you can do clustering, it is extremely cheap to do with IP cameras. Also, as Mark Schweitzer pointed out in the LinkedIn forum, IP comes with a built-in upstream channel, so if you ever need to replace a fixed camera with a PTZ, you do not need any additional cabling. Wireless transmission can also be achieved and you can monitor a camera (or a cluster of cameras) via the internet. As far as I can tell, HDcctv cannot piggyback off cat5, but need new HD-SDI cabling (I’m sure Todd Rockoff can clearify on the cabling requirements). I don’t know if SDTI is commonly used in HDcctv installations either.

Image Quality
Analog comes in a few flavors; the most common is NTSC/PAL(and their variations). NTSC has a resolution of 486 lines which in many cases is too low to identify faces unless they are very close to the camera. IP allows you to pick cameras that fit the purpose; cheap, low res cameras for overview, more expensive HD cameras for details and so on. If you so desire, you can easily replace one camera with another of higher or lower resolution. HDcctv seems to allow 1080P (2MP) as the maximum resolution. I think the increased flexibility of IP makes a winner overall.

Live viewing
For those who look at video, live, and respond to it, low latency and fluidity (high framerate) is important. High resolution less so. The reason is that you do not need crisp video to see that someone is fighting on the street, it is only when you go back to investigate and later go to court that the high resolution is real important (identification of license plates etc). Some IP cameras allow you to run 2 streams at the same time. One that gets recorded and one which is called up on demand when you want to view live video. Naturally, you are constrained by the bandwidth available to you as well as the ping-time. If you are cabling like you would an analog installation, you would have no problems with latency or quality at all.

On the other hand, if your IP camera does not provide dual streaming capabilities, then what you record is is what you get to see live. This means that if you run a low framerate and high compression then your live view will reflect that. On the other hand, you can always replace the camera with a better one that supports dual streams.

Playback
Even if the transmission of analog video is lossless the recorder will compress the video using the same compression technology as the IP camera. Any compression artifact introduced by the IP camera will also be introduced by the recorder as it compresses the video. However, while the IP solution may provide playback video in substantially higher resolution than the live video feed (which has an emphasis on framerate and latency), the analog solution can never provide higher quality than the live feed. What you see live is the highest “quality” that you will ever see.

Again, apples to apples, if IP is deployed using analog cabling conventions, you can certainly get the same live/playback quality as an analog system (e.g. VGA/30fps). Furthermore, your recorded video may be of much better fidelity than your live video (e.g. 1080p/10fps). This makes it possible to identify faces and license plates on the recorded video as you are conducting an investigation. It must be stressed that this is not always possible to achieve, bandwidth constraints are always a factor that must be taken into consideration. You should never expect to get 5MP, high quality, 30 fps recordings over a dial-up internet connection.

Scalability
IP cameras is like a BYOB party. Everyone brings their own beer, so you just need to consider how many people will physically fit in your pad. IP cameras already do the compression for you, so the recorder simply needs to pipe the data to the hard-drives. In an analog system, the recording device is handing out the beer as people arrive. The host might have enough beer for a 16 people, but once you are out of beer the party stops. Furthermore, some IP cameras will allow you to do motion detection, or even video analytics directly on the camera (this technology is still in its infancy though). Obviously, a system based on analog transmission can also scale, by adding more/stronger/faster recorders. In terms of scalability I think IP eeks out the advantage, but not by much.

Conclusion
I think the prudent investment is not in more barns for horses or investing heavily in horse 2.0 (which requires special pavement to achieve the speed of a car).

With that said – Happy Holidays folks

HD vs PTZ

I have to agree with the Luddites. Analog PTZ is far superior to IP MP Video. Especially if you need to really zoom in on tiny details, and you have a manned system…

…But that’s like judging a fish by it’s ability to climb trees (as Einstein supposedly said once).

Let’s flip it around, and ask how well an analog PTZ camera can look in two directions at once. Let’s ask if analog PTZ can do a tour at 90 degrees per second, 24-7-365 without breaking. Let’s try and do analog PTZ in Houston, from an office in New York on a shoestring. Let’s ask if we can change our minds and zoom in on a different area after the video was recorded.

Now, if an IP based optomechanical PTZ camera was given the same budget as the cost of wiring an analog one, then you would not be able to tell them apart at all. And I am guessing you don’t really need to spend the full budget to get equal performance – you can probably get good (perhaps not as good) performance a lot cheaper.

But what if you really wanted to replace a mechanical PTZ with a MP camera?

I guess a lot of installation were getting mechanical PTZ’s in the past because there was no other choice. Now there is. It’s fairly cheap to install 3 fixed cameras vs. 1 PTZ (simply because you don’t need to pull 3 cables all the way back to the recorder – you can pop in a POE switch and cluster the 3 cameras). If you then put in 3 decent cameras you are golden. You even get to see things from 3 vantage points – something PTZ will never do. Even if a vandal breaks one, you still have 2 others that are recording.

It is true that right now, the cost of 2 additional camera licenses are a burden, but I think that cost will come down dramatically over the next 24 months.

But a mechanical PTZ camera is really equivalent to a Gigapixel camera. If you do the maximum zoom level, and do a full pan-tilt of the area you get a huge resolution. If you were monitoring highways it would make sense to have an optical PTZ at the intersections which would allow you to zoom in much more than the MP would ever let you.

Another disadvantage to HD cameras are that they take up a lot of space and they require a lot of processing power to decode. This is mostly an issue for the client side developer (as we need to decode the frames to show them to you 😉 ), but an issue nonetheless. Some people will just compress the crap out of those feeds, but that totally negates the purpose. You might as well use a lower resolution camera then. Sometimes the framerate gets lowered to the point where you might as well be looking at a slideshow – but that might just be good enough for the user.

So I think Todd Rockoff is correct. HD and PTZ are complimentary.

Motion Detection on the Edge

When we design a surveillance system, we need to carefully consider how we allocate resources and distribute workloads. When you add a camera to an NVR, the most common use is to reduce the camera to a fairly dumb “video transmitter” and then let the server do the heavy lifting.

But even if the server is much, much more powerful than your humble IP camera, it is usually taxed with a lot of work. One of the tasks the server routinely carries out is to do what some folks call “motion detection”. The term is usually misleading as the NVR is not really detecting motion at all. It is detecting “changes in the frame”, which could be noise, light, and transition from color to B/W etc. not related to what we understand as “motion” at all. Analytics engines look at differences too, but they are truly looking for “motion” and not JUST changes.

Looking for changes is usually “good enough”, and does not need to be any more than that. And if looking for “change” is what you need, then you really should let your camera do the work and free up the NVR to do more important things.

The reason we initially decided to analyze the frames for changes was really motivated by storage problems. A common HDD in those days was 200-300 MB, the 640×480 frames were considered “high resolution” and the format was always MJPEG. Naturally, the Axis 200+ could not deliver these crisp HD feeds at anywhere near 30 FPS. 3-5 FPS was usually all you could get. But storing this massive amount of data became a problem, so we decided to discard frames that were almost identical.

Naturally, as time passed we got higher resolutions and higher framerates, we were suddenly able to do MPEG4 encoding on a consumer device – in real time!!! MPEG4 and H.264 actually looks at two successive frames in much the same way we do on the NVR. The codec simply “throws away” the redundant information just as we do. Except the codec is throwing away just the parts of the frame that is similar to the previous one, while preserving only the changes – a much, much better way of doing things.

For the codec to figure out what to throw away, it must look at two successive frames. If they are very similar, it can throw away a lot, if they are very different it needs to send almost all the pixels. On top of that H.264 does a lot of other things before the video is sent across the network. These involve among other things – discrete cosine transformation, quantization and Huffman encoding.

It does not seem like a far stretch that the codec implementation could provide a number that tells the camera how much 2 frames are alike. And in a primitive way it actually does – if the frame is large in terms of bytes, then we can deduce that the frames are very different, if the frames are small, then they are very similar. Naturally this is too crude and would not work on CBR feeds, and there is no windowing etc.

Nor does it seem totally unreasonable that the codec implementation could give the “difference parameter” for each macroblock (a small 8×8 pixel block). It is important to understand that the codec already is doing the computation, we are just asking to get to peek at the result. Furthermore, the codec is also working on the crisp uncompressed frames that have the highest level of fidelity, and no information has been thrown away.

In naive implementations like the one I describe here, there is not a lot to be gained from working on the raw frames in the camera, but ask any analytics vendor if they would prefer to work on the video BEFORE or AFTER compression and the answer will uniformly be the same : BEFORE compression. So while the benefit is not huge, it is not completely without merit.

To do the detection on the NVR, the NVR will have to completely reverse the process: Take the Huffman symbols, and expand them into imaginary coefficients, go from frequency to the spatial domain, and only then can you start to think about examining the frames. You can then make all sorts of tricks – perhaps you only look at every N pixel, perhaps you don’t look at every frame, perhaps you get a lot of noise from too heavy compression, perhaps you don’t. Every single trick lowers the “quality” of the detection. Perhaps the client doesn’t care, even with severe degradation of the quality, and that’s fine by me. I am focused on improving and providing better, more efficient solutions and offering them to the ones who appreciate such things.

The point is this – spending a lot of resources decoding a H.264 stream, to get information that could have been gathered almost for free in the camera, is not my idea of efficient allocation of the resources. It is like rejecting a free apple, only to ride 30 miles to the store to buy the same, exact apple, only now it is slightly bruised from transporting it to the store – AND it takes a lot of effort to unwrap the apple.

In time, an NVR will not need to do much, in fact, I expect an NVR to be very similar to a NAS. Cheap, easy to replace, and very scalable. This will require that the cameras become a little more advanced, but my experience tells me that progress doesn’t just stop. We were amazed by 640 x 480 at 4 fps when I started, and just as we laugh at that today, we will laugh at NVR side change detection 10 years from now.

I suspect that a lot of cameras do not have the fine grained control over the encoding process that is needed here. I assume that they are picking off-the-shelf H.264 encoders or reference designs offered by the chip manufacturers. For such cameras, there might not be a simple way to do on-board processing, and doing so may jeopardize the performance of the camera – for those, you will have to spring for the expensive PC’s.

Start preparing for the change 🙂