Page 1 of 1

Camera framerate 1% faster than requested

Posted: Thu Jul 10, 2014 10:59 am
by lwd8cmd
Hi!

I tried to record video with picamera using variable framerate (camera.framerate=0, exposure_mode='night') and detect real fps using motion vectors stream. For some reason calculated fps were higher than they should be. Here is a test script to find real framerate when camera.framerate is set to 90.

Code: Select all

import picamera
import picamera.array
import time

fps	= 30

class DetectMotion(picamera.array.PiMotionAnalysis):
	def analyse(self, a):# method is called for each frame
		self.i	+= 1
		if self.i > self.fpsUpdate:
			curTime	= time.time()
			self.fps	= float(self.fpsUpdate) / (curTime - self.lastTime)
			print(self.fps, self.i, curTime)
			self.i	= 0
			self.lastTime	= curTime
			self.fpsUpdate	= int(self.fps * 10)
		
	def setI(self, fps):
		self.i	= 0
		self.fpsUpdate	= fps * 10
		self.lastTime	= time.time()

with picamera.PiCamera() as camera:
	camera.resolution	= (80, 60)
	camera.framerate	= fps
	with DetectMotion(camera) as motionDetector:
		motionDetector.setI(fps)
		camera.start_recording('/dev/null', format='h264', motion_output=motionDetector)
		camera.wait_recording(300)
and screen output:

Code: Select all

(90.64054710017014, 901, 1404987424.683723)
(91.1498060304278, 907, 1404987434.623404)
(91.11016823524258, 912, 1404987444.622288)
(91.09151915592423, 912, 1404987454.623219)
(91.11739475477107, 911, 1404987464.610335)
(91.11531729942031, 912, 1404987474.608654)
(91.09084596834764, 912, 1404987484.609659)
(91.12406443833711, 911, 1404987494.596044)
Calculated fps is off more than 1%. Camera is able to record video faster than 90 fps? Same script with fps=30 gives real framerate 30.363121614197492, which is again 1% larger.

It's not a problem with picamera, because raspivid gives similar results:
raspivid -w 320 -h 240 -fps 90 -qp 40 -t 300000 -o test_fps90.h264
MP4Box -add test_fps90.h264 -fps 90 test_fps90.mp4
AVC-H264 import - frame size 320 x 240 at 90.000 FPS
AVC Import results: 27395 samples - Slices: 457 I 26938 P 0 B - 0 SEI - 457 IDR
Saving to /mnt/usbdrive/test_fps90.mp4: 0.500 secs Interleaving

Created video length is 05:04 although it should be 5 minutes. Number of I frames+P frames is 457+26938, fps=(457+26938)/300=91.317. Is camera clock running faster than raspi clock?

Re: Camera framerate 1% faster than requested

Posted: Thu Jul 10, 2014 11:09 am
by jamesh
lwd8cmd wrote:Hi!

I tried to record video with picamera using variable framerate (camera.framerate=0, exposure_mode='night') and detect real fps using motion vectors stream. For some reason calculated fps were higher than they should be. Here is a test script to find real framerate when camera.framerate is set to 90.

Code: Select all

import picamera
import picamera.array
import time

fps	= 30

class DetectMotion(picamera.array.PiMotionAnalysis):
	def analyse(self, a):# method is called for each frame
		self.i	+= 1
		if self.i > self.fpsUpdate:
			curTime	= time.time()
			self.fps	= float(self.fpsUpdate) / (curTime - self.lastTime)
			print(self.fps, self.i, curTime)
			self.i	= 0
			self.lastTime	= curTime
			self.fpsUpdate	= int(self.fps * 10)
		
	def setI(self, fps):
		self.i	= 0
		self.fpsUpdate	= fps * 10
		self.lastTime	= time.time()

with picamera.PiCamera() as camera:
	camera.resolution	= (80, 60)
	camera.framerate	= fps
	with DetectMotion(camera) as motionDetector:
		motionDetector.setI(fps)
		camera.start_recording('/dev/null', format='h264', motion_output=motionDetector)
		camera.wait_recording(300)
and screen output:

Code: Select all

(90.64054710017014, 901, 1404987424.683723)
(91.1498060304278, 907, 1404987434.623404)
(91.11016823524258, 912, 1404987444.622288)
(91.09151915592423, 912, 1404987454.623219)
(91.11739475477107, 911, 1404987464.610335)
(91.11531729942031, 912, 1404987474.608654)
(91.09084596834764, 912, 1404987484.609659)
(91.12406443833711, 911, 1404987494.596044)
Calculated fps is off more than 1%. Camera is able to record video faster than 90 fps? Same script with fps=30 gives real framerate 30.363121614197492, which is again 1% larger.

It's not a problem with picamera, because raspivid gives similar results:
raspivid -w 320 -h 240 -fps 90 -qp 40 -t 300000 -o test_fps90.h264
MP4Box -add test_fps90.h264 -fps 90 test_fps90.mp4
AVC-H264 import - frame size 320 x 240 at 90.000 FPS
AVC Import results: 27395 samples - Slices: 457 I 26938 P 0 B - 0 SEI - 457 IDR
Saving to /mnt/usbdrive/test_fps90.mp4: 0.500 secs Interleaving

Created video length is 05:04 although it should be 5 minutes. Number of I frames+P frames is 457+26938, fps=(457+26938)/300=91.317. Is camera clock running faster than raspi clock?
I think this might be partially down to the clock setting for the camera being defined to a slightly different value (a bit lower) to the incoming clock as supplied by the crystal on the board. It needs tweaking, but I haven't had time to look at it.

Re: Camera framerate 1% faster than requested

Posted: Thu Jul 10, 2014 11:40 am
by 6by9
The register settings from Ominvision were for a 24.8MHz clock. The oscillator fitted by Farnell and RS is 25MHz. Difference is 0.8%, so the sensor is framing 0.8% too fast, hence a slight difference in framerate.

Is 1% really that big a deal to you? It may be possible to tweak the sensor PLL settings to get exactly the desired pixel rate, but I wouldn't say it was the highest priority ever.

Re: Camera framerate 1% faster than requested

Posted: Thu Jul 10, 2014 11:51 am
by lwd8cmd
Getting slightly faster framerate is not a problem. I thought my code (calculating fps using motion vectors callback) was wrong and spent some time figuring out why it's giving values outside of the limits.