mextril
Posts: 3
Joined: Tue May 07, 2019 9:45 am

v4l2h264enc bitrate cap setting

Fri May 10, 2019 3:32 am

The bitrate setting is not effect on gstreamer v4l2h264enc:
For video_bitrate=2500000

Code: Select all

gst-launch-1.0  -e videotestsrc ! 'video/x-raw,width=854,height=480,framerate=30/1,format=NV12,colorimetry=bt709' !  v4l2h264enc output-io-mode=4  extra-controls="encode,frame_level_rate_control_enable=1,h264_profile=0,h264_level=14,video_bitrate=2500000;"  ! h264parse ! matroskamux ! filesink location=2M.mkv
mediainfo

Code: Select all

General
Unique ID                                : 132578896357871515593100049166535812264 (0x63BDC4431AEC87B306CF9387C0F4E4A8)
Complete name                            : 2M.mkv
Format                                   : Matroska
Format version                           : Version 2
File size                                : 77.1 MiB
Duration                                 : 2 min 3 s
Overall bit rate mode                    : Constant
Overall bit rate                         : 5 247 kb/s
Encoded date                             : UTC 2019-05-10 03:15:25
Writing application                      : GStreamer Matroska muxer
Writing library                          : GStreamer matroskamux version 1.16.0

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : [email protected]
Format settings, CABAC                   : No
Format settings, ReFrames                : 1 frame
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 2 min 3 s
Bit rate mode                            : Constant
Bit rate                                 : 5 143 kb/s
Nominal bit rate                         : 2 500 kb/s
Width                                    : 854 pixels
Height                                   : 480 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 30.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.418
Stream size                              : 75.5 MiB (98%)
Title                                    : Video
Language                                 : English
Default                                  : Yes
Forced                                   : No

For video_bitrate=25000

Code: Select all

gst-launch-1.0  -e videotestsrc ! 'video/x-raw,width=854,height=480,framerate=30/1,format=NV12,colorimetry=bt709' !  v4l2h264enc output-io-mode=4  extra-controls="encode,frame_level_rate_control_enable=1,h264_profile=0,h264_level=14,video_bitrate=25000;"  ! h264parse ! matroskamux ! filesink location=25K.mkv
Mediainfo:

Code: Select all

General
Unique ID                                : 134223853421404118431904813444353817276 (0x64FA92E83238DBAFF6B6D57CEDD0FABC)
Complete name                            : 25K.mkv
Format                                   : Matroska
Format version                           : Version 2
File size                                : 94.3 MiB
Duration                                 : 2 min 30 s
Overall bit rate mode                    : Constant
Overall bit rate                         : 5 252 kb/s
Encoded date                             : UTC 2019-05-10 03:14:24
Writing application                      : GStreamer Matroska muxer
Writing library                          : GStreamer matroskamux version 1.16.0

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : [email protected]
Format settings, CABAC                   : No
Format settings, ReFrames                : 1 frame
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 2 min 30 s
Bit rate mode                            : Constant
Bit rate                                 : 5 147 kb/s
Nominal bit rate                         : 25.0 kb/s
Width                                    : 854 pixels
Height                                   : 480 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 30.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.419
Stream size                              : 92.4 MiB (98%)
Title                                    : Video
Language                                 : English
Default                                  : Yes
Forced                                   : No

Both setting, producing Bit rate around 5000 kb/s
How to set proper bitrate in gstreamer v4l2h264enc ?

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

Re: v4l2h264enc bitrate cap setting

Fri May 10, 2019 10:00 am

The V4L2 codecs are a very recent addition, so thank you for trying to use them, and reporting this issue. The decode path has had more testing than encode.

I'll agree that something does seem to be off when setting the bitrate.
Can I question where you got your list of parameters for extra-controls from though? They get mapped to the V4L2 parameters, and the supported parameters can be retrieved via "v4l2-ctl --list-ctrls-menu" (add "-d 11" in this case for the encoder)

Code: Select all

[email protected]:~ $ v4l2-ctl -d 11 --list-ctrls-menu

Codec Controls

             video_bitrate_mode 0x009909ce (menu)   : min=0 max=1 default=0 value=0 flags=update
				0: Variable Bitrate
				1: Constant Bitrate
                  video_bitrate 0x009909cf (int)    : min=25000 max=25000000 step=25000 default=10000000 value=10000000
         repeat_sequence_header 0x009909e2 (bool)   : default=0 value=0
            h264_i_frame_period 0x00990a66 (int)    : min=0 max=2147483647 step=1 default=60 value=60
                     h264_level 0x00990a67 (menu)   : min=0 max=13 default=11 value=11
				0: 1
				1: 1b
				2: 1.1
				3: 1.2
				4: 1.3
				5: 2
				6: 2.1
				7: 2.2
				8: 3
				9: 3.1
				10: 3.2
				11: 4
				12: 4.1
				13: 4.2
                   h264_profile 0x00990a6b (menu)   : min=0 max=4 default=4 value=4
				0: Baseline
				1: Constrained Baseline
				2: Main
				4: High
No parameter "frame_level_rate_control_enable". I'm not sure where this one has come from.
h264_level has a range of 0-13, so 14 (level 5.0?) is invalid.
Why the semi-colon on the end? That may be my misunderstanding of GStreamer boxed parameters (the "encode" tag at the start confused me for a while before I remembered that boxed params need that funny tag).

I think I know where I've tripped up. The underlying codec can either do rate control based on being told the frame rate, or based on frame timestamps. The frame rate isn't plumbed through, so it's relying on timestamps, however V4L2 provides nsecs and it wants usecs - guess who forgot to convert! Rate control therefore thinks it has oodles of bits available and cranks the compression right down.
Plumbing the frame rate through would be useful but isn't straightforward in V4L2 (they use frame intervals to be annoying). Fixing the timestamps should be trivial, but there's the minor complication of 64bit division within the kernel to handle (it's not working at the moment!).

Give me a little bit of time to poke around.
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.

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

Re: v4l2h264enc bitrate cap setting

Fri May 10, 2019 1:25 pm

Doh, I needed to reread the syntax for do_div.

Hopefully both paths are fixed with https://github.com/raspberrypi/linux/pull/2961. I certainly seem to be getting the bitrate that I was am asking for as long as there is sufficient complexity to the scene (854x480 doesn't at 20Mbit/s, but does at 1920x1088).
S_PARM and G_PARM mean nothing for decode, so are disabled.
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.

mextril
Posts: 3
Joined: Tue May 07, 2019 9:45 am

Re: v4l2h264enc bitrate cap setting

Sat May 11, 2019 6:57 am

Thanks !
Is there a way to test without re-build the kernel?

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

Re: v4l2h264enc bitrate cap setting

Sat May 11, 2019 7:54 am

mextril wrote:
Sat May 11, 2019 6:57 am
Thanks !
Is there a way to test without re-build the kernel?
Sorry, no. There's likely to be an rpi-update release within a few days, so hopefully it'll be merged before and in that.
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.

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

Re: v4l2h264enc bitrate cap setting

Fri May 24, 2019 8:44 am

I've just rebased the PR and nudged those responsible for merging it.
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.

Return to “Graphics, sound and multimedia”