Skip to content

H.264 flicker when using the V4L2 M2M encoder #4704

Closed

Description

@mdevaev

Describe the bug
I'm researching a new m2m interface to migrate our streaming software to it and found that the parameters that the encoder uses produce a flickering video for a variable bitrate. When using a constant bitrate, everything is fine, but I need a variable one because it allows me to achieve zero-latency encoding. There were two very important parameters in the old MAIL interface: MIN_QUANT and MAX_QUANT, by configuring which I could get rid of the mentioned artifact. But if the quantization from 16 to 32 is configured, then everything will be fine.

Unfortunately, the new API does not provide this and without them I get the following result
(Look at the area under the cursor in the terminal):
https://user-images.githubusercontent.com/416259/142009115-0f65da9d-306e-439f-a503-519cd56666b8.mp4

And how it should be (MIN_QUANT=16, MAX_QUANT=32):
https://user-images.githubusercontent.com/416259/142009452-bdcb8587-d94f-4f98-aed9-cf61d38d69c5.mp4

To reproduce
Use variable bitrate to encode H.264. In a contrasting black-and-white image, you will notice pulsating artifacts when an I-frame arrives. The full list of parameters is as follows:

V4L2_CID_MPEG_VIDEO_BITRATE = 5000000
V4L2_CID_MPEG_VIDEO_H264_I_PERIOD = 30
V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE
V4L2_MPEG_VIDEO_H264_LEVEL_4_0
V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER = 1
output FPS = 1/30

How to solve
To solve the problem, we just need the ability to throw MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT/MAX_QUANT into the kernel using V4L2_CID. The problem is that V4L2 does not seem to provide suitable CIDs for this. I suggest making a pair of specific CIDs with a large offset like this:

diff --git a/include/media/h264-ctrls.h b/include/media/h264-ctrls.h
index ec4799154..d1b045fde 100644
--- a/include/media/h264-ctrls.h
+++ b/include/media/h264-ctrls.h
@@ -37,6 +37,8 @@
 #define V4L2_CID_MPEG_VIDEO_H264_DECODE_MODE	(V4L2_CID_MPEG_BASE+1005)
 #define V4L2_CID_MPEG_VIDEO_H264_START_CODE	(V4L2_CID_MPEG_BASE+1006)
 #define V4L2_CID_MPEG_VIDEO_H264_PRED_WEIGHTS	(V4L2_CID_MPEG_BASE+1007)
+#define V4L2_CID_MPEG_VIDEO_H264_QUANT_MIN		(V4L2_CID_MPEG_BASE+9901)
+#define V4L2_CID_MPEG_VIDEO_H264_QUANT_MAX		(V4L2_CID_MPEG_BASE+9902)

I will make all the necessary changes and send you a patch, just tell me which solution you think is preferable. I really need these parameters.

Activity

  1. 6by9 commented on Nov 16, 2021

    @6by9
    Contributor

    V4L2 controls V4L2_CID_MPEG_VIDEO_H264_MAX_QP and V4L2_CID_MPEG_VIDEO_H264_MIN_QP already exist in the API.
    https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/ext-ctrls-codec.html

    /include/media/h264-ctrls.h is the wrong file to be looking at. As noted in the headers

    *
     * These are the H.264 state controls for use with stateless H.264
     * codec drivers.
     *
     * It turns out that these structs are not stable yet and will undergo
     * more changes. So keep them private until they are stable and ready to
     * become part of the official public API.
    

    The Pi H264 codec implements the stateful API, not the stateless (which is only defined for decoders at present).
    Files outside of /include/uapi are not considered part of the stable userspace API and will not be exported.

  2. mdevaev commented on Nov 16, 2021

    @mdevaev
    ContributorAuthor

    Thank you, got it. I will make a patch.

  3. mdevaev commented on Nov 17, 2021

    @mdevaev
    ContributorAuthor

    Done: #4705

  4. mdevaev commented on Nov 25, 2021

    @mdevaev
    ContributorAuthor

    Fixed by #4705

    Thank you!

  5. 271 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions