This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

DM8107 - H264 (v2.0) encoding bitrate control

Other Parts Discussed in Thread: DM8107

Hello.

I'd like to request some clarification regarding the h264 codec's rate control.  It's running on the DM8107 processor.

None of the rate control presets seem to have any effect on the output bitrate.  We are generating a high-complexity test video stream with another equipment, it should be encoded at around 5 Mbps, although the actual numbers don't really matter as far as I can tell.  There's two points we are interested in, having constant bitrate and/or a hard maximum bitrate.

For the first case I tried setting the rateControlPreset to LOWDELAY (also tried STORAGE) and the rcAlgo to 1 (CBR), however, they both behaved exactly the same as with rcALgo 0 (VBR).  I would adjust the targetBitrate and the output bitrate would follow up to about 5~6 Mbps, which is somewhat of a sweet-spot for the signal and then they'd just stay there, no matter if I raised the targetBitrate to 10 Mbps or more.

For the second case (although they both go together), I adjusted the maxBitrate parameter.  This didn't change anything in the output bitrate, no matter if I set it lower than, equal to or higher than the targetBitrate, even -1 as is the default.  When trying a higher maxBitrate, for instance 6 Mbps agains a target of 2 Mbps, the output remained at around 2.4 Mbps even if it was clearly crippling the quality, I'd expect it to use more than 2 if the scene is that complex, is that right?  Trying different combinations here didn't change anything, the output bitrate would ignore whatever value was assigned to the maxBitrate.

On another note, when using, for example, a targetBitrate of 2 Mbps we'd get an output of 2.4 Mbps.  A targetBitrate of 6 Mbps would give us an output of 6.9 Mbps.  There seems to be a 15~20% offset there, is this to be expected?  We need to be able to deterministically configure a maximum bitrate for our output stream.


Just in case I'm misunderstanding something or just plain configuring it wrong, here's a dump of the encoder's parameters in one of the runs (it's at the bottom of the question).  What I believe are the important parameters would be:

encodingPreset 3 # User Defined
rateControlPreset 2 # Storage
videnc2Params.maxBitRate 6000000
rateControlParams.rateControlParamsPreset 1
rateControlParams.rcAlgo 1
videnc2DynamicParams.targetBitRate 2000000

I'd appreciate any help in this matter.

Thank you.

H.264 Static params
=============================
videnc2Params.size 316
encodingPreset 3 # User Defined
rateControlPreset 2 # Storage
videnc2Params.maxHeight 1088
videnc2Params.maxWidth 1920
videnc2Params.dataEndianness 1
videnc2Params.maxInterFrameInterval 1
videnc2Params.maxBitRate 6000000
videnc2Params.minBitRate 0
videnc2Params.inputChromaFormat 9
videnc2Params.inputContentType 0
videnc2Params.operatingMode 1
videnc2Params.profile 100
videnc2Params.level 40
videnc2Params.inputDataMode 3
videnc2Params.outputDataMode 3
videnc2Params.numInputDataUnits 1
videnc2Params.numOutputDataUnits 1
=============================
rateControlParams.rateControlParamsPreset 1
rateControlParams.scalingMatrixPreset 1
rateControlParams.rcAlgo 1
rateControlParams.qpI 4294967295
rateControlParams.qpMaxI 36
rateControlParams.qpMinI 10
rateControlParams.qpP 4294967295
rateControlParams.qpMaxP 40
rateControlParams.qpMinP 10
rateControlParams.qpOffsetB 4
rateControlParams.qpMaxB 44
rateControlParams.qpMinB 10
rateControlParams.allowFrameSkip 0
rateControlParams.removeExpensiveCoeff 0
rateControlParams.chromaQPIndexOffset 0
rateControlParams.IPQualityFactor 0
rateControlParams.initialBufferLevel 4000000
rateControlParams.HRDBufferSize 4000000
rateControlParams.minPicSizeRatioI 0
rateControlParams.maxPicSizeRatioI 640
rateControlParams.minPicSizeRatioP 0
rateControlParams.maxPicSizeRatioP 0
rateControlParams.minPicSizeRatioB 0
rateControlParams.maxPicSizeRatioB 0
rateControlParams.enablePRC 1
rateControlParams.enablePartialFrameSkip 0
rateControlParams.discardSavedBits 1
rateControlParams.VBRDuration 8
rateControlParams.VBRsensitivity 0
rateControlParams.skipDistributionWindowLength 5
rateControlParams.numSkipInDistributionWindow 1
rateControlParams.enableHRDComplianceMode 1
rateControlParams.frameSkipThMulQ5 0
rateControlParams.vbvUseLevelThQ5 0
=============================
interCodingParams.interCodingPreset 1
interCodingParams.searchRangeHorP 144
interCodingParams.searchRangeVerP 32
interCodingParams.searchRangeHorB 144
interCodingParams.searchRangeVerB 16
interCodingParams.interCodingBias 1
interCodingParams.skipMVCodingBias 1
interCodingParams.minBlockSizeP 1
interCodingParams.minBlockSizeB 1
interCodingParams.meAlgoMode 0
=============================
intraCodingParams.intraCodingPreset 0
intraCodingParams.lumaIntra4x4Enable 0
intraCodingParams.lumaIntra8x8Enable 255
intraCodingParams.lumaIntra16x16Enable 15
intraCodingParams.chromaIntra8x8Enable 15
intraCodingParams.chromaComponentEnable 1
intraCodingParams.intraRefreshMethod 0
intraCodingParams.intraRefreshRate 0
intraCodingParams.gdrOverlapRowsBtwFrames 0
intraCodingParams.constrainedIntraPredEnable 0
=============================
nalUnitControlParams.naluControlPreset 1
nalUnitControlParams.naluPresentMaskStartOfSequence 9120
nalUnitControlParams.naluPresentMaskIDRPicture 9120
nalUnitControlParams.naluPresentMaskIntraPicture 8962
nalUnitControlParams.naluPresentMaskNonIntraPicture 514
nalUnitControlParams.naluPresentMaskEndOfSequence 3072
=============================
sliceCodingParams.sliceCodingPreset 0
sliceCodingParams.sliceMode 0
sliceCodingParams.sliceUnitSize 0
sliceCodingParams.sliceStartOffset[0] 0
sliceCodingParams.sliceStartOffset[1] 0
sliceCodingParams.streamFormat 0
=============================
loopFilterParams.loopfilterPreset 0
loopFilterParams.loopfilterDisableIDC 0
loopFilterParams.filterOffsetA 0
loopFilterParams.filterOffsetB 0
=============================
fmoCodingParams.fmoCodingPreset 0
fmoCodingParams.numSliceGroups 1
fmoCodingParams.sliceGroupMapType 4
fmoCodingParams.sliceGroupChangeDirectionFlag 0
fmoCodingParams.sliceGroupChangeRate 0
fmoCodingParams.sliceGroupChangeCycle 0
fmoCodingParams.sliceGroupParams[0] 0
fmoCodingParams.sliceGroupParams[1] 0
=============================
vuiCodingParams.vuiCodingPreset 1
vuiCodingParams.aspectRatioInfoPresentFlag 1
vuiCodingParams.aspectRatioIdc 0
vuiCodingParams.videoSignalTypePresentFlag 0
vuiCodingParams.videoFormat 2
vuiCodingParams.videoFullRangeFlag 0
vuiCodingParams.timingInfoPresentFlag 1
vuiCodingParams.hrdParamsPresentFlag 1
vuiCodingParams.numUnitsInTicks 1000
=============================
stereoInfoParams.stereoInfoPreset 0
stereoInfoParams.topFieldIsLeftViewFlag 1
stereoInfoParams.viewSelfContainedFlag 0
=============================
framePackingSEIParams.framePackingPreset 0
framePackingSEIParams.framePackingType 3
framePackingSEIParams.frame0PositionX 0
framePackingSEIParams.frame0PositionY 0
framePackingSEIParams.frame1PositionX 0
framePackingSEIParams.frame1PositionY 0
framePackingSEIParams.reservedByte 0
=============================
svcCodingParams.svcExtensionFlag 0
svcCodingParams.dependencyID 0
svcCodingParams.qualityID 0
svcCodingParams.enhancementProfileID 0
svcCodingParams.layerIndex 0
svcCodingParams.refLayerDQId 0
=============================
interlaceCodingType 4
bottomFieldIntra 0
gopStructure 0
entropyCodingMode 1
transformBlockSize 2
log2MaxFNumMinus4 10
picOrderCountType 0
enableWatermark 0
IDRFrameInterval 1
pConstantMemory 0
maxIntraFrameInterval 2147483640
debugTraceLevel 0
lastNFramesToLog 0
enableAnalyticinfo 0
enableGMVSei 0
constraintSetFlags 0
enableRCDO 0
enableLongTermRefFrame 0
LTRPPeriod 0
numTemporalLayer 1
referencePicMarking 1
=============================
=============================
videnc2DynamicParams.size 240
videnc2DynamicParams.inputHeight 720
videnc2DynamicParams.inputWidth 1280
videnc2DynamicParams.refFrameRate 30000
videnc2DynamicParams.targetFrameRate 60000
videnc2DynamicParams.targetBitRate 2000000
videnc2DynamicParams.intraFrameInterval 10
videnc2DynamicParams.generateHeader 0
videnc2DynamicParams.captureWidth 16384
videnc2DynamicParams.forceFrame 4294967295
videnc2DynamicParams.interFrameInterval 1
videnc2DynamicParams.mvAccuracy 2
videnc2DynamicParams.sampleAspectRatioHeight 1
videnc2DynamicParams.sampleAspectRatioWidth 1
videnc2DynamicParams.ignoreOutbufSizeFlag 1
videnc2DynamicParams.putDataFxn 0
videnc2DynamicParams.putDataHandle 0
videnc2DynamicParams.getDataFxn 0
videnc2DynamicParams.getDataHandle 0
videnc2DynamicParams.getBufferFxn 2638810325
videnc2DynamicParams.getBufferHandle 0
videnc2DynamicParams.lateAcquireArg 4294967295
=============================
rateControlParams.rateControlParamsPreset 1
rateControlParams.scalingMatrixPreset 1
rateControlParams.rcAlgo 1
rateControlParams.qpI 26
rateControlParams.qpMaxI 51
rateControlParams.qpMinI 5
rateControlParams.qpP 26
rateControlParams.qpMaxP 51
rateControlParams.qpMinP 5
rateControlParams.qpOffsetB 4
rateControlParams.qpMaxB 44
rateControlParams.qpMinB 10
rateControlParams.allowFrameSkip 0
rateControlParams.removeExpensiveCoeff 0
rateControlParams.chromaQPIndexOffset 0
rateControlParams.IPQualityFactor 0
rateControlParams.initialBufferLevel 4000000
rateControlParams.HRDBufferSize 4000000
rateControlParams.minPicSizeRatioI 0
rateControlParams.maxPicSizeRatioI 320
rateControlParams.minPicSizeRatioP 0
rateControlParams.maxPicSizeRatioP 0
rateControlParams.minPicSizeRatioB 0
rateControlParams.maxPicSizeRatioB 0
rateControlParams.enablePRC 1
rateControlParams.enablePartialFrameSkip 0
rateControlParams.discardSavedBits 1
rateControlParams.VBRDuration 8
rateControlParams.VBRsensitivity 0
rateControlParams.skipDistributionWindowLength 5
rateControlParams.numSkipInDistributionWindow 2
rateControlParams.enableHRDComplianceMode 1
rateControlParams.frameSkipThMulQ5 0
rateControlParams.vbvUseLevelThQ5 0
=============================
interCodingParams.interCodingPreset 1
interCodingParams.searchRangeHorP 144
interCodingParams.searchRangeVerP 32
interCodingParams.searchRangeHorB 144
interCodingParams.searchRangeVerB 16
interCodingParams.interCodingBias 1
interCodingParams.skipMVCodingBias 1
interCodingParams.minBlockSizeP 1
interCodingParams.minBlockSizeB 1
interCodingParams.meAlgoMode 0
=============================
intraCodingParams.intraCodingPreset 0
intraCodingParams.lumaIntra4x4Enable 0
intraCodingParams.lumaIntra8x8Enable 255
intraCodingParams.lumaIntra16x16Enable 15
intraCodingParams.chromaIntra8x8Enable 15
intraCodingParams.chromaComponentEnable 1
intraCodingParams.intraRefreshMethod 0
intraCodingParams.intraRefreshRate 0
intraCodingParams.gdrOverlapRowsBtwFrames 0
intraCodingParams.constrainedIntraPredEnable 0
=============================
sliceCodingParams.sliceCodingPreset 0
sliceCodingParams.sliceMode 0
sliceCodingParams.sliceUnitSize 0
sliceCodingParams.sliceStartOffset[0] 0
sliceCodingParams.sliceStartOffset[1] 0
sliceCodingParams.streamFormat 0
=============================
sliceGroupChangeCycle 0
searchCenter.x 32767
searchCenter.y 32767
enableStaticMBCount 0
enableROI 0
=============================

  • Hello,

    For CBR you could check this thread:

    e2e.ti.com/.../1246750

    Keep in mind that this is for EZSDK encode demo.
    You could also check the h264encoder user guide.
    Please notice that the encoder configuration setting may also depend upon the factors like resolution, complexity of the video, etc.

    For CBR should be set as :HRDBufferSize = nBitRate * 0.5 and initialBufferLevel = nBitRate * 0.5;

    Could you try to set bit 12 as well refer to the B.3 Control masks:
    // for the mask bits, please refer to codec user guide
    tStaticParam.videoStaticParams.h264EncStaticParams.nalUnitControlParams.naluControlPreset = IH264_NALU_CONTROL_USERDEFINED;
    tStaticParam.videoStaticParams.h264EncStaticParams.nalUnitControlParams.naluPresentMaskStartOfSequence |= 0x11A0;
    tStaticParam.videoStaticParams.h264EncStaticParams.nalUnitControlParams.naluPresentMaskIDRPicture |= 0x11A0;
    tStaticParam.videoStaticParams.h264EncStaticParams.nalUnitControlParams.naluPresentMaskIntraPicture |= 0x11A0;
    tStaticParam.videoStaticParams.h264EncStaticParams.nalUnitControlParams.naluPresentMaskNonIntraPicture |= 0x1002;
    tStaticParam.videoStaticParams.h264EncStaticParams.nalUnitControlParams.naluPresentMaskEndOfSequence |= 0x1C00;

    You could check this thread also:

    e2e.ti.com/.../432633

    BR
    Margarita
  • Hello Margarita,

    Thank you for the prompt response.

    I've been trying to follow the user guide in this regards but it seems to not go into much detail about bitrate control, other than specifying max > 1.5*target for HF-VBR.  Hadn't taken notice of the filler bit though.

    I've tried setting bit 12 on all 5 masks and also your snippet of code presented above.  I think you also mentioned in one of the links that it should *not* be set for the EndOfSequence mask, I've tried that as well.  HRDBufferSize and initial level have been set to half the target bitrate and maxBitrate equal to it.

    Unfortunately, we still can't get constant bitrate in the output, actually, the problem is that the target bitrate is not even reached.  I'm using a target bitrate intentionally higher than needed to see whether we can achieve CBR or not.  Currently the encoder is configured as follows:

    encodingPreset 3 # User Defined
    rateControlPreset 2 # Low delay
    rateControlParams.rateControlParamsPreset 1
    rateControlParams.rcAlgo 1
    videnc2Params.maxBitRate 8000000
    videnc2DynamicParams.targetBitRate 8000000
    rateControlParams.initialBufferLevel 400000
    rateControlParams.HRDBufferSize 400000

    naluControlPreset = IH264_NALU_CONTROL_USERDEFINED
    StartOfSequence  0x33A0
    IDRPicture       0x33A0
    IntraPicture     0x3302
    NonIntraPicture  0x1202
    EndOfSequence    0x1C00

    FrameSkip and partialFrameSkip are not allowed, but that shouldn't be a problem in this case, right?  Given that I'm getting a lower bitrate than desired.

    The output bitrate goes up to about 4~5 Mbps, no matter how much I raise the target bitrate.  If I then lower the encoding quality (increasing the GoP size and such) the output bitrate keeps going down, not to mention that during high complexity scenes it goes back up towards the target bitrate, clearly not the desired behavior for CBR.

    The video input is a 1280x720p60 video stream.  I am aware this cannot be done with interlaced content and B-frames encoding.

     

    Leaving that aside, I've got another question about VBR.  The user guide mentions HF-VBR and how to set the maxBitrate to enable it, however, the code defaults maxBitrate to -1.  How does the encoder behave in such case?  I've tried using VBR with target bitrate of 5 Mbps and both maxBitrate of 8 Mbps and -1:

    • With 8 Mbps, I seem to get an output bitrate around 5.9 Mbps with some spikes on more complex scenes, with a spike every now and then that goes a bit above 8.
    • With -1, the output also hovers around 5.9 Mbps but it never goes above it.  I don't quite understand this, why is the output about 20% higher than the target bitrate, it does seem to behave like an actual max limit though.

     

    I appreciate the help, thank you.

  • Hello,

    What is the software release that you are using here? Is it IPNC?

    What is the exact h264 encoder version?

    You could go through the guide to check is the maxbitrate is supported.

    BR
    Margarita

  • Hi Sebastian,

    Can you try the below configurations -
    rateControlPreset = IVIDEO_USER_DEFINED,
    rcAlgo =IH264_RATECONTROL_PRC_LOW_DELAY (1)
    maxBitRate = -1
    initialBufferLevel = HRDBufferSize = 0.5 * targetbitrate,
    For 60fps stream, targetframerate = 60000,

    Maxbitrate only used to enable/disable HDVBR. If maxbitrate > 1.5 targetbitrate, HFVBR is enable.
    else HFVBR disabled.

    In case of HFVBR, you will see lot of bitrate fluctuations & it will take time to settle the bitrate in case of much varations in the stream.

    To get better understanding can you share the bitstreams along with the parameters used to generate it for the case of bitrate not achieved. We can provide you the better feedback based on that.


    Regards
    Gajanan
  • Hello Gajanan,

    Are you saying that maxBitrate is only used to enable/disable HF-VBR?  In that case why is it an integer value and not just a bit or a boolean?  My question in particular about VBR is why it goes (in average) around 15~20% above the target bitrate?  This happens with both maxBitrate > 1.5*targetBitrate and maxBitrate = -1.

    In response to both you and Margarita, I'm afraid I'll be out of the office this week, so I'll have to get back to you regarding that test and the actual release full version string next Monday.  I apologize for that.

    Thank you.

  • Hi Sebastian,

                 The big variation in the bit-rate is input sequence dependent. In case of VBR, the ratecontrol will try to achieve the specified bit-rate within 2sec (60 frames in case of 30fps). So once you back, share the stream for better analysis. 

                  Max-bitrate is used for tunning the Rate-control for a special cases (This cant be disclosed). So from general case point of view, the is not so useful. And used to enable/disable HFVBR.

    Regards

    Gajanan

  • The encoder's version is IVAHD_001 build iD 02.00.06.00.

    Regarding the stream, I'm afraid I can't share it.  The reason is that I haven't been using any source in particular because the VBR behavior I'm enquiring about is always present,  in a video source, clip, broadcast signal, etc.  I think I might not have been clear enough about it, let me explain it again.

    I'm aware that the output bitrate variation is strongly dependent on the input source, this is not the problem.  Lets say you have a 1080p60 input source that encodes with a given set of parameters (frames, GoP, etc) at around 10 Mbps, taking into account variations and all.  Now, lower the target bitrate to a noticeable lower value, 5 Mbps for instance.  Given that the source hasn't changed and the codec parameters remain the same I'd expect the encoder to maintain 5 Mbps on its output (assuming the input variations never account to an encoded sequence which would require less than 5 Mbps), because we've clearly crippled the performance and quality by selecting a target bitrate quite lower than the "ideal".

    The problem is that I'm getting an output bitrate of about 5.9 Mbps instead of the target bitrate of 5, this is about 15~20% more.  We need to be able to set a maximum bitrate that assures us the output will never go above this value.  In the previous example the encoder seems to use the 5.9 Mbps as a maximum, this is what we are looking for except that the actual value is higher than the target bitrate and that makes it unpredictable.

    Setting the maxBitrate to -1 or >1.5*targetBitrate results in what was explained in the previous post.  We've been measuring the output bitrate over several seconds and even minutes, this should give the encoder enough time to settle into an average, if I'm not mistaken.

    How can we this be accomplished?

    Thank you,

    Sebastian.

  • Hi Sebastian,

    When you change the bitrate, along with it are you changing the initialBufferLevel & HRDBufferSize ??
    It should be always
    CBR ==> initialBufferLevel = HRDBufferSize = 0.5 * targetbitrate,
    VBR ==> initialBufferLevel = HRDBufferSize = 2 * targetbitrate,


    Regards
    Gajanan
  • Hello,

    Yes, I'm changing them accordingly.

    My biggest concern right now is being able to set a hard maximum bitrate value.  When I set maxBitrate to -1, I can't see why the output then goes, in average, about 15~20% above the target Bitrate.  This is with VBR and the HRD buffers set as you've explained.

    Thanks.

    Regards,
    Sebastian.

  • Hi Sabastian,

              Can you change your parameters to rateControlPreset = IVIDEO_USER_DEFINED(5).

    How are you measuring the bitrate overshoot. Gop based overshoot or for full sequence overshoot ??

    Regards

    Gajanan