Page 1 of 1

Setting quality level and/or bitrate for MMAL_ENCODING_MJPEG

Posted: Tue Apr 22, 2014 1:00 pm
by abroun
Hi there,

I'm playing around with mmal, creating a video encoder component with encoding set to MMAL_ENCODING_MJPEG.

This seems to be working well, in that I get a series of JPEG images out the other end. My problem is that I can't see any way to set the quality level of the images, and have it stick.

I'm encoding images with a resolution of 640x480 at 30fps. If I set the bitrate of the encoder output port to something low like 100,000bps then I'll get frames encoded to a size of approximately 6400 bytes. Over time however, if I leave the camera running, the encoded size will creep up, and the quality of the encoded images will improve. Eventually, the size of the frames will level out at about 60-70,000bytes. Reading MMAL_PARAMETER_VIDEO_BIT_RATE whilst the encoder is running tells me that the bitrate is still 100,000.

I have tried setting MMAL_PARAMETER_VIDEO_ENCODE_FRAME_LIMIT_BITS to the same as bitrate with no success. Also, setting MMAL_PARAMETER_RATECONTROL to MMAL_VIDEO_RATECONTROL_CONSTANT seems to make no difference.

Can someone please tell me how I should be setting the quality level, and/or bitrate of the MJPEG encoder? If it's any help, I'm using the latest version of the firmware in Hexxeh's github repository (15/4/2014, e65b8c992ad0c2486e172aee1e2b1e98d623d782)

Thanks in advance.

Regards

Alan

Re: Setting quality level and/or bitrate for MMAL_ENCODING_M

Posted: Tue Apr 22, 2014 5:04 pm
by 6by9
MMAL_PARAMETER_VIDEO_BIT_RATE is the correct thing, or set the bitrate in the port format (that's probably better - I'd need to check if the MJPEG codec supports dynamic changes in bitrate). None of the other parameters you mention will have any effect for MJPEG.
The codec starts at a nominal q factor for the images, and then alters the value in a control loop to try and achieve the desired bit rate.

Are you really trying to run MJPEG 30fps VGA at 100kbits/s? That sounds very low to me (416 bytes per frame!), but it does sound like something is off if set to 100kbit/s and you're getting 60kB per frame.

Re: Setting quality level and/or bitrate for MMAL_ENCODING_M

Posted: Tue Apr 22, 2014 5:53 pm
by abroun
Thanks for the reply.

I'm not really aiming for a bitrate as low as 100kbits/s but it does demonstrate the problem I'm having fairly well. :)

You can find the source for the project I'm trying to use this in here. Basically, bits copied from raspivid to get me familiar with mmal, mashed together with a hacked version of the httpd server from mjpeg-streamer.

I've moved down to an image size of 320x240 for now as a very crude way of controlling image quality, but the basic behaviour is the same. If you run the program with the bitrate set to 0 (the default value) then the video encoder just seems to do its own thing, the result of printing the buffer length in the encoder buffer callback is

Code: Select all

Buffer size is 0
Buffer size is 5630
Buffer size is 7974
Buffer size is 9594
Buffer size is 10969
Buffer size is 12481
Buffer size is 14419
Buffer size is 15413
Buffer size is 16529
Buffer size is 17860
Buffer size is 18950
Buffer size is 20122
Buffer size is 21100
Buffer size is 22377
Buffer size is 23163
Buffer size is 23218
Buffer size is 23166
...
Size remains at about 23,000 bytes
If you set the bitrate to 100,000 then the behaviour is

Code: Select all

Buffer size is 0
Buffer size is 5650
Buffer size is 7946
Buffer size is 9607
Buffer size is 10971
Buffer size is 12457            # VVV Scroll down VVV
Buffer size is 14582
Buffer size is 15587
Buffer size is 16710
Buffer size is 17954
Buffer size is 18540
Buffer size is 20197
Buffer size is 21351
Buffer size is 22528
Buffer size is 23387
Buffer size is 23399
Buffer size is 22531
Buffer size is 21251
Buffer size is 20262
Buffer size is 18836
Buffer size is 17698
Buffer size is 16356
Buffer size is 15325
Buffer size is 14196
Buffer size is 12903
Buffer size is 11581
Buffer size is 10510
Buffer size is 9213
Buffer size is 8117
Buffer size is 6838
Buffer size is 5676
Buffer size is 4417
Buffer size is 3335
Buffer size is 2390
Buffer size is 2128
Buffer size is 2133
Buffer size is 2134
Buffer size is 2135
Buffer size is 2134
Buffer size is 2133
Buffer size is 2134
Buffer size is 2134
Buffer size is 2133
Buffer size is 2134
Buffer size is 2126
Buffer size is 2125
Buffer size is 2126
Buffer size is 2121
Buffer size is 2129
Buffer size is 2133
Buffer size is 2137
...
From here the buffer size will slowly (give it time) edge up to ~23,000 bytes
If you can offer any advice, or spot an error in my usage of MMAL then that would be very much appreciated.

Also, I did try setting MMAL_PARAMETER_VIDEO_BIT_RATE but it didn't seem to work. I was able to read it, and it just returns the bitrate set in the port format.