User avatar
HermannSW
Posts: 1913
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Why is post raw "yuv to .h264" compression 1/3rd size of .h264?

Tue Jan 07, 2020 11:29 am

I recently realized that you can use raspivid to generate raw output (yuv and rgb) concurrently with h264 output.

So I used this command to create tst.h264 and /dev/shm/tst.i420 in parallel from same recording:

Code: Select all

$ raspivid -md 7 -w 640 -h 480 -rf yuv -r /dev/shm/tst.i420 -o tst.h264
GraphvizFiddle share link
Image


I used i420toh264 tool, that is based on "/opt/vc/src/hello_pi/hello_encode/"
https://github.com/Hermann-SW2/userland ... i420toh264

to create .h264 file from /dev/shm/tst.i420 as well:

Code: Select all

$ userland/host_applications/linux/apps/hello_pi/i420toh264/i420toh264 tst.h264b 640 480 < /dev/shm/tst.i420
I cannot see differences in the generated .h264 videos.
Why is the file GPU compressed to .h264 in postprocessing only 1/3rd of size of tst.h264?

Code: Select all

[email protected]:~ $ ls -l tst.h264*
-rw-r--r-- 1 pi pi 2688539 Jan  7 12:11 tst.h264
-rw-r--r-- 1 pi pi  827084 Jan  7 12:13 tst.h264b
[email protected]:~ $ 
Attachments
raspivid.yuv+h264.png
raspivid.yuv+h264.png
raspivid.yuv+h264.png (19.68 KiB) Viewed 290 times
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 25054
Joined: Sat Jul 30, 2011 7:41 pm

Re: Why is post raw "yuv to .h264" compression 1/3rd size of .h264?

Tue Jan 07, 2020 11:44 am

Are you using the same bit rate in both encodes?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I own the world’s worst thesaurus. Not only is it awful, it’s awful."

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7933
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Why is post raw "yuv to .h264" compression 1/3rd size of .h264?

Tue Jan 07, 2020 11:55 am

I suspect you've messed up the timestamp and framerate information, so bitrate control will be doing very different things.

I'm assuming OV5647, so mode 7 is a minimum of 60fps. The video_encode component in raspivid will therefore be seeing timestamps for frames at 16ms intervals. It's also defaulted to a bitrate of 17Mbit/s.

Your app is telling video_encode that it is a 30fps stream, not providing any timestamps (nTimeStamp field in OMX_BUFFERHEADERTYPE), and requesting a bitrate of 1Mbit/s.

Ask for different encoding parameters and you'll get very different files.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
HermannSW
Posts: 1913
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Why is post raw "yuv to .h264" compression 1/3rd size of .h264?

Tue Jan 07, 2020 6:08 pm

Thanks for the responses.
I did use a v2 camera.
And the analysis wrt framerate and bitrate was correct.
I did change i420toh264 framerate to 60 and and bitrate to 17,000,000 and recompiled.
Since the Pi was rebooted /dev/shm/tst.i420 from noon was gone.
So I repeated the experiment, and this time the generated .h264 files are roughly the same size.
Finally I kept bitrate and changed framerate back to 30, and got same .h264 size as well:

Code: Select all

[email protected]:~ $ ls -l tst.h264*
-rw-r--r-- 1 pi pi 3529520 Jan  7 18:57 tst.h264
-rw-r--r-- 1 pi pi 3539499 Jan  7 19:00 tst.h264c
-rw-r--r-- 1 pi pi 3555870 Jan  7 19:05 tst.h264d
[email protected]:~ $ 
So it seems that bitrate alone is the factor for .h264 size when converting from .i420 to .h264.
Interesting that both noon videos looked "the same" to me despite the big differences in bitrate and .h264 size.
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7933
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Why is post raw "yuv to .h264" compression 1/3rd size of .h264?

Tue Jan 07, 2020 6:35 pm

HermannSW wrote:
Tue Jan 07, 2020 6:08 pm
Interesting that both noon videos looked "the same" to me despite the big differences in bitrate and .h264 size.
1Mbit/s should be sufficient for VGA, probably even 512kbit/s.

17Mbit/s is the default as the default resolution is 1080p30.
Somewhere around 8Mbit/s would be recommended for 720p30.
Scale down accordingly.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
HermannSW
Posts: 1913
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Why is post raw "yuv to .h264" compression 1/3rd size of .h264?

Tue Jan 07, 2020 8:41 pm

Thank you again
6by9 wrote:
Tue Jan 07, 2020 11:55 am
I'm assuming OV5647, so mode 7 is a minimum of 60fps.
I just recorded a 640x480 v2 camera video with default 5s length and used "ffmpeg -i tst.h264 frame.%04.pnm" to get the frames, and I got 196 frames. According to doc camera.md minimum/default for imx219 v2 camera mode 7 is 40fps, max without limitations is 120fps. I typically get 150fps without frameskips and absolute max is 200fps with limitations.
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/github_repo_i420toh264
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

Return to “Camera board”