diff mbox series

[FFmpeg-devel,3/5] lavf/libkvazaar: export encoded frame stats

Message ID 1595763675-1857-3-git-send-email-mypopydev@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/5] lavc/libkvazaar: fix framerate setting | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Jun Zhao July 26, 2020, 11:41 a.m. UTC
From: Jun Zhao <barryjzhao@tencent.com>

Export choosen pict_type and qp.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
---
 libavcodec/libkvazaar.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

Comments

mypopy@gmail.com Aug. 7, 2020, 2:40 a.m. UTC | #1
On Sun, Jul 26, 2020 at 8:45 PM Jun Zhao <mypopydev@gmail.com> wrote:
>
> From: Jun Zhao <barryjzhao@tencent.com>
>
> Export choosen pict_type and qp.
>
> Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
> ---
>  libavcodec/libkvazaar.c | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
>
> diff --git a/libavcodec/libkvazaar.c b/libavcodec/libkvazaar.c
> index 71c9c8f..9032547 100644
> --- a/libavcodec/libkvazaar.c
> +++ b/libavcodec/libkvazaar.c
> @@ -37,6 +37,7 @@
>
>  #include "avcodec.h"
>  #include "internal.h"
> +#include "packet_internal.h"
>
>  typedef struct LibkvazaarContext {
>      const AVClass *class;
> @@ -170,6 +171,7 @@ static int libkvazaar_encode(AVCodecContext *avctx,
>      kvz_data_chunk *data_out = NULL;
>      uint32_t len_out = 0;
>      int retval = 0;
> +    int pict_type;
>
>      *got_packet_ptr = 0;
>
> @@ -257,6 +259,34 @@ static int libkvazaar_encode(AVCodecContext *avctx,
>              avpkt->flags |= AV_PKT_FLAG_KEY;
>          }
>
> +        switch (frame_info.slice_type) {
> +        case KVZ_SLICE_I:
> +            pict_type = AV_PICTURE_TYPE_I;
> +            break;
> +        case KVZ_SLICE_P:
> +            pict_type = AV_PICTURE_TYPE_P;
> +            break;
> +        case KVZ_SLICE_B:
> +            pict_type = AV_PICTURE_TYPE_B;
> +            break;
> +        default:
> +            av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
> +            return AVERROR_EXTERNAL;
> +        }
> +#if FF_API_CODED_FRAME
> +FF_DISABLE_DEPRECATION_WARNINGS
> +        avctx->coded_frame->pict_type = pict_type;
> +FF_ENABLE_DEPRECATION_WARNINGS
> +#endif
> +
> +        ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
> +
> +#if FF_API_CODED_FRAME
> +FF_DISABLE_DEPRECATION_WARNINGS
> +        avctx->coded_frame->quality = frame_info.qp * FF_QP2LAMBDA;
> +FF_ENABLE_DEPRECATION_WARNINGS
> +#endif
> +
>          *got_packet_ptr = 1;
>      }
>
> --
> 2.7.4
>
ping ?
Andreas Rheinhardt Aug. 7, 2020, 3:16 a.m. UTC | #2
mypopy@gmail.com:
> On Sun, Jul 26, 2020 at 8:45 PM Jun Zhao <mypopydev@gmail.com> wrote:
>>
>> From: Jun Zhao <barryjzhao@tencent.com>
>>
>> Export choosen pict_type and qp.
>>
>> Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
>> ---
>>  libavcodec/libkvazaar.c | 30 ++++++++++++++++++++++++++++++
>>  1 file changed, 30 insertions(+)
>>
>> diff --git a/libavcodec/libkvazaar.c b/libavcodec/libkvazaar.c
>> index 71c9c8f..9032547 100644
>> --- a/libavcodec/libkvazaar.c
>> +++ b/libavcodec/libkvazaar.c
>> @@ -37,6 +37,7 @@
>>
>>  #include "avcodec.h"
>>  #include "internal.h"
>> +#include "packet_internal.h"
>>
>>  typedef struct LibkvazaarContext {
>>      const AVClass *class;
>> @@ -170,6 +171,7 @@ static int libkvazaar_encode(AVCodecContext *avctx,
>>      kvz_data_chunk *data_out = NULL;
>>      uint32_t len_out = 0;
>>      int retval = 0;
>> +    int pict_type;
>>
>>      *got_packet_ptr = 0;
>>
>> @@ -257,6 +259,34 @@ static int libkvazaar_encode(AVCodecContext *avctx,
>>              avpkt->flags |= AV_PKT_FLAG_KEY;
>>          }
>>
>> +        switch (frame_info.slice_type) {
>> +        case KVZ_SLICE_I:
>> +            pict_type = AV_PICTURE_TYPE_I;
>> +            break;
>> +        case KVZ_SLICE_P:
>> +            pict_type = AV_PICTURE_TYPE_P;
>> +            break;
>> +        case KVZ_SLICE_B:
>> +            pict_type = AV_PICTURE_TYPE_B;
>> +            break;
>> +        default:
>> +            av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
>> +            return AVERROR_EXTERNAL;
>> +        }
>> +#if FF_API_CODED_FRAME
>> +FF_DISABLE_DEPRECATION_WARNINGS
>> +        avctx->coded_frame->pict_type = pict_type;
>> +FF_ENABLE_DEPRECATION_WARNINGS
>> +#endif
>> +
>> +        ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
>> +
>> +#if FF_API_CODED_FRAME
>> +FF_DISABLE_DEPRECATION_WARNINGS
>> +        avctx->coded_frame->quality = frame_info.qp * FF_QP2LAMBDA;
>> +FF_ENABLE_DEPRECATION_WARNINGS
>> +#endif
>> +
>>          *got_packet_ptr = 1;
>>      }
>>
>> --
>> 2.7.4
>>
> ping ?

Wrong commit message: It is not lavf. I have no opinion on the actual
change.

- Andreas
mypopy@gmail.com Aug. 8, 2020, 5:22 a.m. UTC | #3
On Fri, Aug 7, 2020 at 11:16 AM Andreas Rheinhardt
<andreas.rheinhardt@gmail.com> wrote:
>
> mypopy@gmail.com:
> > On Sun, Jul 26, 2020 at 8:45 PM Jun Zhao <mypopydev@gmail.com> wrote:
> >>
> >> From: Jun Zhao <barryjzhao@tencent.com>
> >>
> >> Export choosen pict_type and qp.
> >>
> >> Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
> >> ---
> >>  libavcodec/libkvazaar.c | 30 ++++++++++++++++++++++++++++++
> >>  1 file changed, 30 insertions(+)
> >>
> >> diff --git a/libavcodec/libkvazaar.c b/libavcodec/libkvazaar.c
> >> index 71c9c8f..9032547 100644
> >> --- a/libavcodec/libkvazaar.c
> >> +++ b/libavcodec/libkvazaar.c
> >> @@ -37,6 +37,7 @@
> >>
> >>  #include "avcodec.h"
> >>  #include "internal.h"
> >> +#include "packet_internal.h"
> >>
> >>  typedef struct LibkvazaarContext {
> >>      const AVClass *class;
> >> @@ -170,6 +171,7 @@ static int libkvazaar_encode(AVCodecContext *avctx,
> >>      kvz_data_chunk *data_out = NULL;
> >>      uint32_t len_out = 0;
> >>      int retval = 0;
> >> +    int pict_type;
> >>
> >>      *got_packet_ptr = 0;
> >>
> >> @@ -257,6 +259,34 @@ static int libkvazaar_encode(AVCodecContext *avctx,
> >>              avpkt->flags |= AV_PKT_FLAG_KEY;
> >>          }
> >>
> >> +        switch (frame_info.slice_type) {
> >> +        case KVZ_SLICE_I:
> >> +            pict_type = AV_PICTURE_TYPE_I;
> >> +            break;
> >> +        case KVZ_SLICE_P:
> >> +            pict_type = AV_PICTURE_TYPE_P;
> >> +            break;
> >> +        case KVZ_SLICE_B:
> >> +            pict_type = AV_PICTURE_TYPE_B;
> >> +            break;
> >> +        default:
> >> +            av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
> >> +            return AVERROR_EXTERNAL;
> >> +        }
> >> +#if FF_API_CODED_FRAME
> >> +FF_DISABLE_DEPRECATION_WARNINGS
> >> +        avctx->coded_frame->pict_type = pict_type;
> >> +FF_ENABLE_DEPRECATION_WARNINGS
> >> +#endif
> >> +
> >> +        ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
> >> +
> >> +#if FF_API_CODED_FRAME
> >> +FF_DISABLE_DEPRECATION_WARNINGS
> >> +        avctx->coded_frame->quality = frame_info.qp * FF_QP2LAMBDA;
> >> +FF_ENABLE_DEPRECATION_WARNINGS
> >> +#endif
> >> +
> >>          *got_packet_ptr = 1;
> >>      }
> >>
> >> --
> >> 2.7.4
> >>
> > ping ?
>
> Wrong commit message: It is not lavf. I have no opinion on the actual
> change.
>
fixed the commit message typo in local.
Joose Sainio Aug. 17, 2020, 11:07 a.m. UTC | #4
On 7.8.2020 5.40, mypopy@gmail.com wrote:
> On Sun, Jul 26, 2020 at 8:45 PM Jun Zhao <mypopydev@gmail.com> wrote:
>> From: Jun Zhao <barryjzhao@tencent.com>
>>
>> Export choosen pict_type and qp.
>>
>> Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
>> ---
>> libavcodec/libkvazaar.c | 30 ++++++++++++++++++++++++++++++
>> 1 file changed, 30 insertions(+)
>>
>> diff --git a/libavcodec/libkvazaar.c b/libavcodec/libkvazaar.c
>> index 71c9c8f..9032547 100644
>> --- a/libavcodec/libkvazaar.c
>> +++ b/libavcodec/libkvazaar.c
>> @@ -37,6 +37,7 @@
>>
>> #include "avcodec.h"
>> #include "internal.h"
>> +#include "packet_internal.h"
>>
>> typedef struct LibkvazaarContext {
>> const AVClass *class;
>> @@ -170,6 +171,7 @@ static int libkvazaar_encode(AVCodecContext *avctx,
>> kvz_data_chunk *data_out = NULL;
>> uint32_t len_out = 0;
>> int retval = 0;
>> + int pict_type;
>>
>> *got_packet_ptr = 0;
>>
>> @@ -257,6 +259,34 @@ static int libkvazaar_encode(AVCodecContext *avctx,
>> avpkt->flags |= AV_PKT_FLAG_KEY;
>> }
>>
>> + switch (frame_info.slice_type) {
>> + case KVZ_SLICE_I:
>> + pict_type = AV_PICTURE_TYPE_I;
>> + break;
>> + case KVZ_SLICE_P:
>> + pict_type = AV_PICTURE_TYPE_P;
>> + break;
>> + case KVZ_SLICE_B:
>> + pict_type = AV_PICTURE_TYPE_B;
>> + break;
>> + default:
>> + av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
>> + return AVERROR_EXTERNAL;
>> + }
>> +#if FF_API_CODED_FRAME
>> +FF_DISABLE_DEPRECATION_WARNINGS
>> + avctx->coded_frame->pict_type = pict_type;
>> +FF_ENABLE_DEPRECATION_WARNINGS
>> +#endif
>> +
>> + ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA, 
>> NULL, 0, pict_type);
>> +
>> +#if FF_API_CODED_FRAME
>> +FF_DISABLE_DEPRECATION_WARNINGS
>> + avctx->coded_frame->quality = frame_info.qp * FF_QP2LAMBDA;
>> +FF_ENABLE_DEPRECATION_WARNINGS
>> +#endif
>> +
>> *got_packet_ptr = 1;
>> }
>>
>> --
>> 2.7.4
>>
> ping ?
Sorry, I forgot I was subscribed to the list on the old organization
so the message didn't go through originally.

In terms of Kvazaar this seems ok but the QP to lambda conversion will 
result
in vastly different lambda value than what Kvazaar would have used
internally (and in fact the FF_QP2LAMBDA macro states it is for h.263).
I can't comment whether this is problem on the ffmpeg side.

The correct formula for hevc/h.265 would be
lambda = 0.57 * pow(2.0, (qp - 12 / 3.0))

- Joose
mypopy@gmail.com Aug. 18, 2020, 2:41 a.m. UTC | #5
On Mon, Aug 17, 2020 at 7:08 PM Joose Sainio <joose.sainio@tuni.fi> wrote:
>
> On 7.8.2020 5.40, mypopy@gmail.com wrote:
> > On Sun, Jul 26, 2020 at 8:45 PM Jun Zhao <mypopydev@gmail.com> wrote:
> >> From: Jun Zhao <barryjzhao@tencent.com>
> >>
> >> Export choosen pict_type and qp.
> >>
> >> Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
> >> ---
> >> libavcodec/libkvazaar.c | 30 ++++++++++++++++++++++++++++++
> >> 1 file changed, 30 insertions(+)
> >>
> >> diff --git a/libavcodec/libkvazaar.c b/libavcodec/libkvazaar.c
> >> index 71c9c8f..9032547 100644
> >> --- a/libavcodec/libkvazaar.c
> >> +++ b/libavcodec/libkvazaar.c
> >> @@ -37,6 +37,7 @@
> >>
> >> #include "avcodec.h"
> >> #include "internal.h"
> >> +#include "packet_internal.h"
> >>
> >> typedef struct LibkvazaarContext {
> >> const AVClass *class;
> >> @@ -170,6 +171,7 @@ static int libkvazaar_encode(AVCodecContext *avctx,
> >> kvz_data_chunk *data_out = NULL;
> >> uint32_t len_out = 0;
> >> int retval = 0;
> >> + int pict_type;
> >>
> >> *got_packet_ptr = 0;
> >>
> >> @@ -257,6 +259,34 @@ static int libkvazaar_encode(AVCodecContext *avctx,
> >> avpkt->flags |= AV_PKT_FLAG_KEY;
> >> }
> >>
> >> + switch (frame_info.slice_type) {
> >> + case KVZ_SLICE_I:
> >> + pict_type = AV_PICTURE_TYPE_I;
> >> + break;
> >> + case KVZ_SLICE_P:
> >> + pict_type = AV_PICTURE_TYPE_P;
> >> + break;
> >> + case KVZ_SLICE_B:
> >> + pict_type = AV_PICTURE_TYPE_B;
> >> + break;
> >> + default:
> >> + av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
> >> + return AVERROR_EXTERNAL;
> >> + }
> >> +#if FF_API_CODED_FRAME
> >> +FF_DISABLE_DEPRECATION_WARNINGS
> >> + avctx->coded_frame->pict_type = pict_type;
> >> +FF_ENABLE_DEPRECATION_WARNINGS
> >> +#endif
> >> +
> >> + ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA,
> >> NULL, 0, pict_type);
> >> +
> >> +#if FF_API_CODED_FRAME
> >> +FF_DISABLE_DEPRECATION_WARNINGS
> >> + avctx->coded_frame->quality = frame_info.qp * FF_QP2LAMBDA;
> >> +FF_ENABLE_DEPRECATION_WARNINGS
> >> +#endif
> >> +
> >> *got_packet_ptr = 1;
> >> }
> >>
> >> --
> >> 2.7.4
> >>
> > ping ?
> Sorry, I forgot I was subscribed to the list on the old organization
> so the message didn't go through originally.
>
> In terms of Kvazaar this seems ok but the QP to lambda conversion will
> result
> in vastly different lambda value than what Kvazaar would have used
> internally (and in fact the FF_QP2LAMBDA macro states it is for h.263).
> I can't comment whether this is problem on the ffmpeg side.
>
> The correct formula for hevc/h.265 would be
> lambda = 0.57 * pow(2.0, (qp - 12 / 3.0))
This is the other issue, I think, will try to fix it.
>
> - Joose
>
mypopy@gmail.com Aug. 20, 2020, 7 a.m. UTC | #6
On Tue, Aug 18, 2020 at 10:41 AM mypopy@gmail.com <mypopy@gmail.com> wrote:
>
> On Mon, Aug 17, 2020 at 7:08 PM Joose Sainio <joose.sainio@tuni.fi> wrote:
> >
> > On 7.8.2020 5.40, mypopy@gmail.com wrote:
> > > On Sun, Jul 26, 2020 at 8:45 PM Jun Zhao <mypopydev@gmail.com> wrote:
> > >> From: Jun Zhao <barryjzhao@tencent.com>
> > >>
> > >> Export choosen pict_type and qp.
> > >>
> > >> Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
> > >> ---
> > >> libavcodec/libkvazaar.c | 30 ++++++++++++++++++++++++++++++
> > >> 1 file changed, 30 insertions(+)
> > >>
> > >> diff --git a/libavcodec/libkvazaar.c b/libavcodec/libkvazaar.c
> > >> index 71c9c8f..9032547 100644
> > >> --- a/libavcodec/libkvazaar.c
> > >> +++ b/libavcodec/libkvazaar.c
> > >> @@ -37,6 +37,7 @@
> > >>
> > >> #include "avcodec.h"
> > >> #include "internal.h"
> > >> +#include "packet_internal.h"
> > >>
> > >> typedef struct LibkvazaarContext {
> > >> const AVClass *class;
> > >> @@ -170,6 +171,7 @@ static int libkvazaar_encode(AVCodecContext *avctx,
> > >> kvz_data_chunk *data_out = NULL;
> > >> uint32_t len_out = 0;
> > >> int retval = 0;
> > >> + int pict_type;
> > >>
> > >> *got_packet_ptr = 0;
> > >>
> > >> @@ -257,6 +259,34 @@ static int libkvazaar_encode(AVCodecContext *avctx,
> > >> avpkt->flags |= AV_PKT_FLAG_KEY;
> > >> }
> > >>
> > >> + switch (frame_info.slice_type) {
> > >> + case KVZ_SLICE_I:
> > >> + pict_type = AV_PICTURE_TYPE_I;
> > >> + break;
> > >> + case KVZ_SLICE_P:
> > >> + pict_type = AV_PICTURE_TYPE_P;
> > >> + break;
> > >> + case KVZ_SLICE_B:
> > >> + pict_type = AV_PICTURE_TYPE_B;
> > >> + break;
> > >> + default:
> > >> + av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
> > >> + return AVERROR_EXTERNAL;
> > >> + }
> > >> +#if FF_API_CODED_FRAME
> > >> +FF_DISABLE_DEPRECATION_WARNINGS
> > >> + avctx->coded_frame->pict_type = pict_type;
> > >> +FF_ENABLE_DEPRECATION_WARNINGS
> > >> +#endif
> > >> +
> > >> + ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA,
> > >> NULL, 0, pict_type);
> > >> +
> > >> +#if FF_API_CODED_FRAME
> > >> +FF_DISABLE_DEPRECATION_WARNINGS
> > >> + avctx->coded_frame->quality = frame_info.qp * FF_QP2LAMBDA;
> > >> +FF_ENABLE_DEPRECATION_WARNINGS
> > >> +#endif
> > >> +
> > >> *got_packet_ptr = 1;
> > >> }
> > >>
> > >> --
> > >> 2.7.4
> > >>
> > > ping ?
> > Sorry, I forgot I was subscribed to the list on the old organization
> > so the message didn't go through originally.
> >
> > In terms of Kvazaar this seems ok but the QP to lambda conversion will
> > result
> > in vastly different lambda value than what Kvazaar would have used
> > internally (and in fact the FF_QP2LAMBDA macro states it is for h.263).
> > I can't comment whether this is problem on the ffmpeg side.
> >
> > The correct formula for hevc/h.265 would be
> > lambda = 0.57 * pow(2.0, (qp - 12 / 3.0))
> This is the other issue, I think, will try to fix it.
After grep and dig in the ff_side_data_set_encoder_stats, I think use
frame_info.qp * FF_QP2LAMBDA (Avg frame QP as frame quality number
with scale number FF_QP2LAMBDA ) is Ok in this case.

In fact, the function int ff_side_data_set_encoder_stats(AVPacket
*pkt, int quality, int64_t *error, int error_count, int pict_type);
we just give a inaccuracy quality number for encoded Frame and depend
on the  specific codec
diff mbox series

Patch

diff --git a/libavcodec/libkvazaar.c b/libavcodec/libkvazaar.c
index 71c9c8f..9032547 100644
--- a/libavcodec/libkvazaar.c
+++ b/libavcodec/libkvazaar.c
@@ -37,6 +37,7 @@ 
 
 #include "avcodec.h"
 #include "internal.h"
+#include "packet_internal.h"
 
 typedef struct LibkvazaarContext {
     const AVClass *class;
@@ -170,6 +171,7 @@  static int libkvazaar_encode(AVCodecContext *avctx,
     kvz_data_chunk *data_out = NULL;
     uint32_t len_out = 0;
     int retval = 0;
+    int pict_type;
 
     *got_packet_ptr = 0;
 
@@ -257,6 +259,34 @@  static int libkvazaar_encode(AVCodecContext *avctx,
             avpkt->flags |= AV_PKT_FLAG_KEY;
         }
 
+        switch (frame_info.slice_type) {
+        case KVZ_SLICE_I:
+            pict_type = AV_PICTURE_TYPE_I;
+            break;
+        case KVZ_SLICE_P:
+            pict_type = AV_PICTURE_TYPE_P;
+            break;
+        case KVZ_SLICE_B:
+            pict_type = AV_PICTURE_TYPE_B;
+            break;
+        default:
+            av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
+            return AVERROR_EXTERNAL;
+        }
+#if FF_API_CODED_FRAME
+FF_DISABLE_DEPRECATION_WARNINGS
+        avctx->coded_frame->pict_type = pict_type;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
+        ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
+
+#if FF_API_CODED_FRAME
+FF_DISABLE_DEPRECATION_WARNINGS
+        avctx->coded_frame->quality = frame_info.qp * FF_QP2LAMBDA;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
         *got_packet_ptr = 1;
     }