Media (Intel® Video Processing Library, Intel Media SDK)
Access community support with transcoding, decoding, and encoding in applications using media tools like Intel® oneAPI Video Processing Library and Intel® Media SDK
Announcements
The Intel Media SDK project is no longer active. For continued support and access to new features, Intel Media SDK users are encouraged to read the transition guide on upgrading from Intel® Media SDK to Intel® Video Processing Library (VPL), and to move to VPL as soon as possible.
For more information, see the VPL website.
3056 Discussions

how to packet QP into slice header, In CBR mode.when encoding with qsv h264 encoder.

weapon__lau
Beginner
2,878 Views

Hi All:    

  Now i am doing a project, using Intel QSV H264 encoder, the encoded data will be transmitted to webrtc, 

webrtc will change the bitrate according to the coding quality(and other factors).

but, In CBR mode, The h264 Per-slice QP (Quantizer Parameter) is always zero. QP in each macroblock.

webrtc can't read per-MB's QP. webrtc can only read QP in slice header.

I need to packet QP into slice header, In CBR mode. what should i do?

0 Kudos
1 Solution
Dmitry_E_Intel
Employee
2,878 Views

Hi,

Sorry for slow reply, I missed your update above. 

You can ignore info posted at https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md#example-3-external-brc-pseudo-code. In the first step, please instead just do something like:

    mfxExtCodingOption2 co2;

    co2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
    co2.Header.BufferSz = sizeof(mfxExtCodingOption2);
    co2.ExtBRC = MFX_CODINGOPTION_ON;

You can test it quickly by running sample_encode with "-extbrc:implicit" option:

https://github.com/Intel-Media-SDK/MediaSDK/blob/master/samples/sample_encode/src/sample_encode.cpp#L183

 

This ExtBRC feature means that BRC is done on SW level (not inside HW as it's done in regular cases). And this ExtBRC feature in fact has two implementations: one is inside MediaSDK run-time library (libmfxhw64.dll) ,the other one is on application level (on sample_encode level as an example of how applications can write their own BRC). Applications which are fine with MSDK internal implementation of ExtBRC can just do "co2.ExtBRC = MFX_CODINGOPTION_ON;" (it matches  -extbrc:implicit), if application wants to introduce own BRC implementation (which actually can be copy/paste code from sample_encode's BRC with some tuning), it needs to follow https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md#example-3-external-brc-pseudo-code (it matches "-extbrc:on"). 

View solution in original post

0 Kudos
12 Replies
Dmitry_E_Intel
Employee
2,878 Views

Hi,

As far as I remember MB QP is not enabled by default. Can you please :

1) share encoder init parameters( ideally, collected by mediasdk tracer)

2) share example of encoded bitstream

3) try to reproduce the issue with sample_encode? I mean will you be able to get a stream with macroblock QPs changing within one frame. 

0 Kudos
weapon__lau
Beginner
2,878 Views

my sdk version is "Intel(R) Media SDK 2019 R1"

1), sample code as attachment "simple_encode.cpp"

2), sample bitstream as attachment "2.h264"

3), yes. i was use simple_encode.

this is In CBR Mode.

QP of each macroblock is different, and i can only get the slice header qp(slice_qp_delta), but it is always 0.

thanks

0 Kudos
Dmitry_E_Intel
Employee
2,878 Views

Which HW and driver version do you have?

0 Kudos
weapon__lau
Beginner
2,878 Views
i7-5500U CPU@2.40GHz i3-7100 CPU@3.90GHz what is driver? SDK is Intel(R) Media SDK 2019 R1 PluginVersion = 1 APIVersion = 284 //0x011с FileName32 = "mfxplugin32_h264la_hw.dll" FileName64 = "mfxplugin64_h264la_hw.dll" Type = 04 //enc CodecID = "AVC" Default = 0
0 Kudos
Dmitry_E_Intel
Employee
2,878 Views

It's clear about HW versions, thanks. But you described MSDK developer package version, not driver. Please send driver version (from Device Manager).

0 Kudos
weapon__lau
Beginner
2,878 Views

i7-5500U CPU@2.40GHz--------6.1.7600.16385

Intel (R) HD Graphics 5500 ---------10.18.14.4206

and

i3-7100 CPU@3.90GHz----------10.0.18362.693

Intel (R) HD Graphics 630--------26.20.100.7262

0 Kudos
Dmitry_E_Intel
Employee
2,878 Views

Hi,

We've done an analysis. First of all, my assumption that MBBRC is enabled in this case was wrong. All MB in frames have the same QP. Let's just ignore it because it won't help you.

Indeed, WebRTC has such limitation that it can't read QP in MB:

  1. Encoded bitstream is sent to ParseBitstream() function (line 495) as shown in file h264_encoder.impl.cc (this will be replaced with mediasdk based encoder) https://webrtc.googlesource.com/src/+/refs/heads/master/modules/video_coding/codecs/h264/h264_encoder_impl.cc
  2. The ParseBistream is implemented in below file where they try to read the slice header and find the slice_qp_delta https://webrtc.googlesource.com/src/+/refs/heads/master/common_video/h264/h264_bitstream_parser.cc
  3. And slice QP is calculated using following formula: 26 + init_qp_minus26 (from PPS info) + qp_delta(from above step): int parsed_qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_;

 

 From MediaSDK perspective we can't change the logic that qp_offset==0 in slice header. 

But there is still a way to resolve your issue. Please use mfxExtBRC feature: https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md#mfxExtBRC

You just need to turn mfxExtCodingOption2::ExtBRC option ON . This feature means MediaSDK owns BRC on software level, not inside HW. So MediaSDK writes actual QP into slice header.

 

Regards,

Dmitry

 

0 Kudos
weapon__lau
Beginner
2,878 Views

Dmitry E. (Intel) wrote:

Hi,

We've done an analysis. First of all, my assumption that MBBRC is enabled in this case was wrong. All MB in frames have the same QP. Let's just ignore it because it won't help you.

Indeed, WebRTC has such limitation that it can't read QP in MB:

  1. Encoded bitstream is sent to ParseBitstream() function (line 495) as shown in file h264_encoder.impl.cc (this will be replaced with mediasdk based encoder) https://webrtc.googlesource.com/src/+/refs/heads/master/modules/video_co...
  2. The ParseBistream is implemented in below file where they try to read the slice header and find the slice_qp_delta https://webrtc.googlesource.com/src/+/refs/heads/master/common_video/h26...
  3. And slice QP is calculated using following formula: 26 + init_qp_minus26 (from PPS info) + qp_delta(from above step): int parsed_qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_;

 

 From MediaSDK perspective we can't change the logic that qp_offset==0 in slice header. 

But there is still a way to resolve your issue. Please use mfxExtBRC feature: https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man...

You just need to turn mfxExtCodingOption2::ExtBRC option ON . This feature means MediaSDK owns BRC on software level, not inside HW. So MediaSDK writes actual QP into slice header.

Regards,

Dmitry

 

thank you very much.

the mb qp logic is:

[ the first MB QP0​=pic_init_qp+26+slice_qp_delta+mb_qp_delta

others MB QPn​=(QPm​+mb_qp_delta+52)%52 ]

this also means that the logic slice_qp_delta==0 can't be change

And , Now I need a more detailed example about ExtBRC. i want to test if it can work well.

0 Kudos
ludiqiu
Beginner
2,878 Views

Dmitry E. (Intel) wrote:

Hi,

We've done an analysis. First of all, my assumption that MBBRC is enabled in this case was wrong. All MB in frames have the same QP. Let's just ignore it because it won't help you.

Indeed, WebRTC has such limitation that it can't read QP in MB:

  1. Encoded bitstream is sent to ParseBitstream() function (line 495) as shown in file h264_encoder.impl.cc (this will be replaced with mediasdk based encoder) https://webrtc.googlesource.com/src/+/refs/heads/master/modules/video_co...
  2. The ParseBistream is implemented in below file where they try to read the slice header and find the slice_qp_delta https://webrtc.googlesource.com/src/+/refs/heads/master/common_video/h26...
  3. And slice QP is calculated using following formula: 26 + init_qp_minus26 (from PPS info) + qp_delta(from above step): int parsed_qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_;

 

 From MediaSDK perspective we can't change the logic that qp_offset==0 in slice header. 

But there is still a way to resolve your issue. Please use mfxExtBRC feature: https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man...

You just need to turn mfxExtCodingOption2::ExtBRC option ON . This feature means MediaSDK owns BRC on software level, not inside HW. So MediaSDK writes actual QP into slice header.

 

Regards,

Dmitry

 

 

Hi, I read the doc from

https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md#example-3-external-brc-pseudo-code

which has the line "frame->QP = <decrease QP>;" .

I still not understand how to do it. can you give a runnable code sample, please ? 

thanks in advance !

 

0 Kudos
ludiqiu
Beginner
2,878 Views

Dmitry E. (Intel) wrote:

Hi,  I read the doc,

https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md#example-3-external-brc-pseudo-code

which has line "frame->QP = <increase QP>" that I does not know how to do it.

Can you give a `runnable` code sample, please ?

Thanks in adanvce !

We've done an analysis. First of all, my assumption that MBBRC is enabled in this case was wrong. All MB in frames have the same QP. Let's just ignore it because it won't help you.

Indeed, WebRTC has such limitation that it can't read QP in MB:

  1. Encoded bitstream is sent to ParseBitstream() function (line 495) as shown in file h264_encoder.impl.cc (this will be replaced with mediasdk based encoder) https://webrtc.googlesource.com/src/+/refs/heads/master/modules/video_co...
  2. The ParseBistream is implemented in below file where they try to read the slice header and find the slice_qp_delta https://webrtc.googlesource.com/src/+/refs/heads/master/common_video/h26...
  3. And slice QP is calculated using following formula: 26 + init_qp_minus26 (from PPS info) + qp_delta(from above step): int parsed_qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_;

 

 From MediaSDK perspective we can't change the logic that qp_offset==0 in slice header. 

But there is still a way to resolve your issue. Please use mfxExtBRC feature: https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man...

You just need to turn mfxExtCodingOption2::ExtBRC option ON . This feature means MediaSDK owns BRC on software level, not inside HW. So MediaSDK writes actual QP into slice header.

 

Regards,

Dmitry

 

0 Kudos
Dmitry_E_Intel
Employee
2,879 Views

Hi,

Sorry for slow reply, I missed your update above. 

You can ignore info posted at https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md#example-3-external-brc-pseudo-code. In the first step, please instead just do something like:

    mfxExtCodingOption2 co2;

    co2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
    co2.Header.BufferSz = sizeof(mfxExtCodingOption2);
    co2.ExtBRC = MFX_CODINGOPTION_ON;

You can test it quickly by running sample_encode with "-extbrc:implicit" option:

https://github.com/Intel-Media-SDK/MediaSDK/blob/master/samples/sample_encode/src/sample_encode.cpp#L183

 

This ExtBRC feature means that BRC is done on SW level (not inside HW as it's done in regular cases). And this ExtBRC feature in fact has two implementations: one is inside MediaSDK run-time library (libmfxhw64.dll) ,the other one is on application level (on sample_encode level as an example of how applications can write their own BRC). Applications which are fine with MSDK internal implementation of ExtBRC can just do "co2.ExtBRC = MFX_CODINGOPTION_ON;" (it matches  -extbrc:implicit), if application wants to introduce own BRC implementation (which actually can be copy/paste code from sample_encode's BRC with some tuning), it needs to follow https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md#example-3-external-brc-pseudo-code (it matches "-extbrc:on"). 

0 Kudos
weapon__lau
Beginner
2,878 Views

thank u vevry much!!!

if i want to calc the slice_qp. which one is more acurate?

1,average value of all macroblock.

2,median value of all macroblock.

3, weighted average value of all macroblock. and how?

0 Kudos
Reply