diff mbox

[FFmpeg-devel,3/5] libx265: Update ROI behaviour to match documentation

Message ID 20190227220023.16040-3-sw@jkqxz.net
State New
Headers show

Commit Message

Mark Thompson Feb. 27, 2019, 10 p.m. UTC
Equivalent to the previous patch for libx264.
---
 libavcodec/libx265.c | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

Comments

Guo, Yejun Feb. 28, 2019, 2:57 a.m. UTC | #1
> -----Original Message-----

> From: ffmpeg-devel [mailto:ffmpeg-devel-bounces@ffmpeg.org] On Behalf

> Of Mark Thompson

> Sent: Thursday, February 28, 2019 6:00 AM

> To: ffmpeg-devel@ffmpeg.org

> Subject: [FFmpeg-devel] [PATCH 3/5] libx265: Update ROI behaviour to

> match documentation

> 

> Equivalent to the previous patch for libx264.

> ---

>  libavcodec/libx265.c | 41 ++++++++++++++++++++---------------------

>  1 file changed, 20 insertions(+), 21 deletions(-)

> 

> diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c

> index 98415366da..af1a2b2287 100644

> --- a/libavcodec/libx265.c

> +++ b/libavcodec/libx265.c

> @@ -296,27 +296,33 @@ static av_cold int

> libx265_encode_set_roi(libx265Context *ctx, const AVFrame *fr

>              int mb_size = (ctx->params->rc.qgSize == 8) ? 8 : 16;

>              int mbx = (frame->width + mb_size - 1) / mb_size;

>              int mby = (frame->height + mb_size - 1) / mb_size;

> +            int qp_range = 51 + 6 * (pic->bitDepth - 8);


just found following from "$ ./x265 --fullhelp", not sure what 69 means here. Shall we change our qoffset formula accordingly?
   --qpmin <integer>             sets a hard lower limit on QP allowed to ratecontrol. Default 0
   --qpmax <integer>             sets a hard upper limit on QP allowed to ratecontrol. Default 69


>              int nb_rois;

> -            AVRegionOfInterest *roi;

> +            const AVRegionOfInterest *roi;

>              float *qoffsets;         /* will be freed after encode is called. */

>              qoffsets = av_mallocz_array(mbx * mby, sizeof(*qoffsets));

>              if (!qoffsets)

>                  return AVERROR(ENOMEM);

> 

> -            nb_rois = sd->size / sizeof(AVRegionOfInterest);

> -            roi = (AVRegionOfInterest*)sd->data;

> -            for (int count = 0; count < nb_rois; count++) {

> -                int starty = FFMIN(mby, roi->top / mb_size);

> -                int endy   = FFMIN(mby, (roi->bottom + mb_size - 1)/ mb_size);

> -                int startx = FFMIN(mbx, roi->left / mb_size);

> -                int endx   = FFMIN(mbx, (roi->right + mb_size - 1)/ mb_size);

> +            roi = (const AVRegionOfInterest*)sd->data;

> +            if (!roi->self_size || sd->size % roi->self_size != 0) {

> +                av_log(ctx, AV_LOG_ERROR, "Invalid

> AVRegionOfInterest.self_size.\n");

> +                return AVERROR(EINVAL);

> +            }

> +            nb_rois = sd->size / roi->self_size;

> +

> +            // This list must be iterated in reverse because regions are

> +            // defined in order of decreasing importance.


same comments as libx264

> +            for (int i = nb_rois - 1; i >= 0; i--) {

> +                int startx, endx, starty, endy;

>                  float qoffset;

> 

> -                if (roi->self_size == 0) {

> -                    av_free(qoffsets);

> -                    av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.self_size must

> be set to sizeof(AVRegionOfInterest).\n");

> -                    return AVERROR(EINVAL);

> -                }

> +                roi = (const AVRegionOfInterest*)(sd->data + roi->self_size * i);

> +

> +                starty = av_clip(roi->top / mb_size, 0, mby);

> +                endy   = av_clip((frame->height - roi->bottom + mb_size - 1) /

> mb_size, 0, mby);

> +                startx = av_clip(roi->left / mb_size, 0, mbx);

> +                endx   = av_clip((frame->width - roi->right + mb_size - 1) / mb_size,

> 0, mbx);


same comments as libx264

> 

>                  if (roi->qoffset.den == 0) {

>                      av_free(qoffsets);

> @@ -324,18 +330,11 @@ static av_cold int

> libx265_encode_set_roi(libx265Context *ctx, const AVFrame *fr

>                      return AVERROR(EINVAL);

>                  }

>                  qoffset = roi->qoffset.num * 1.0f / roi->qoffset.den;

> -                qoffset = av_clipf(qoffset, -1.0f, 1.0f);

> -

> -                /* qp range of x265 is from 0 to 51, just choose 25 as the scale value,

> -                 * so the range of final qoffset is [-25.0, 25.0].

> -                 */

> -                qoffset = qoffset * 25;

> +                qoffset = av_clipf(qoffset * qp_range, -qp_range, +qp_range);

> 

>                  for (int y = starty; y < endy; y++)

>                      for (int x = startx; x < endx; x++)

>                          qoffsets[x + y*mbx] = qoffset;

> -

> -                roi = (AVRegionOfInterest*)((char*)roi + roi->self_size);

>              }

> 

>              pic->quantOffsets = qoffsets;

> --

> 2.19.2

> 

> _______________________________________________

> ffmpeg-devel mailing list

> ffmpeg-devel@ffmpeg.org

> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
diff mbox

Patch

diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c
index 98415366da..af1a2b2287 100644
--- a/libavcodec/libx265.c
+++ b/libavcodec/libx265.c
@@ -296,27 +296,33 @@  static av_cold int libx265_encode_set_roi(libx265Context *ctx, const AVFrame *fr
             int mb_size = (ctx->params->rc.qgSize == 8) ? 8 : 16;
             int mbx = (frame->width + mb_size - 1) / mb_size;
             int mby = (frame->height + mb_size - 1) / mb_size;
+            int qp_range = 51 + 6 * (pic->bitDepth - 8);
             int nb_rois;
-            AVRegionOfInterest *roi;
+            const AVRegionOfInterest *roi;
             float *qoffsets;         /* will be freed after encode is called. */
             qoffsets = av_mallocz_array(mbx * mby, sizeof(*qoffsets));
             if (!qoffsets)
                 return AVERROR(ENOMEM);
 
-            nb_rois = sd->size / sizeof(AVRegionOfInterest);
-            roi = (AVRegionOfInterest*)sd->data;
-            for (int count = 0; count < nb_rois; count++) {
-                int starty = FFMIN(mby, roi->top / mb_size);
-                int endy   = FFMIN(mby, (roi->bottom + mb_size - 1)/ mb_size);
-                int startx = FFMIN(mbx, roi->left / mb_size);
-                int endx   = FFMIN(mbx, (roi->right + mb_size - 1)/ mb_size);
+            roi = (const AVRegionOfInterest*)sd->data;
+            if (!roi->self_size || sd->size % roi->self_size != 0) {
+                av_log(ctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n");
+                return AVERROR(EINVAL);
+            }
+            nb_rois = sd->size / roi->self_size;
+
+            // This list must be iterated in reverse because regions are
+            // defined in order of decreasing importance.
+            for (int i = nb_rois - 1; i >= 0; i--) {
+                int startx, endx, starty, endy;
                 float qoffset;
 
-                if (roi->self_size == 0) {
-                    av_free(qoffsets);
-                    av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.self_size must be set to sizeof(AVRegionOfInterest).\n");
-                    return AVERROR(EINVAL);
-                }
+                roi = (const AVRegionOfInterest*)(sd->data + roi->self_size * i);
+
+                starty = av_clip(roi->top / mb_size, 0, mby);
+                endy   = av_clip((frame->height - roi->bottom + mb_size - 1) / mb_size, 0, mby);
+                startx = av_clip(roi->left / mb_size, 0, mbx);
+                endx   = av_clip((frame->width - roi->right + mb_size - 1) / mb_size, 0, mbx);
 
                 if (roi->qoffset.den == 0) {
                     av_free(qoffsets);
@@ -324,18 +330,11 @@  static av_cold int libx265_encode_set_roi(libx265Context *ctx, const AVFrame *fr
                     return AVERROR(EINVAL);
                 }
                 qoffset = roi->qoffset.num * 1.0f / roi->qoffset.den;
-                qoffset = av_clipf(qoffset, -1.0f, 1.0f);
-
-                /* qp range of x265 is from 0 to 51, just choose 25 as the scale value,
-                 * so the range of final qoffset is [-25.0, 25.0].
-                 */
-                qoffset = qoffset * 25;
+                qoffset = av_clipf(qoffset * qp_range, -qp_range, +qp_range);
 
                 for (int y = starty; y < endy; y++)
                     for (int x = startx; x < endx; x++)
                         qoffsets[x + y*mbx] = qoffset;
-
-                roi = (AVRegionOfInterest*)((char*)roi + roi->self_size);
             }
 
             pic->quantOffsets = qoffsets;