diff mbox

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

Message ID 20190603231905.9536-3-sw@jkqxz.net
State Accepted
Commit f6c572bd89e60854b16ce48824ff02bfd55702d7
Headers show

Commit Message

Mark Thompson June 3, 2019, 11:19 p.m. UTC
Equivalent to the previous patch for libx264.
---
 libavcodec/libx265.c | 44 +++++++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 21 deletions(-)

Comments

Guo, Yejun June 4, 2019, 6:45 a.m. UTC | #1
> -----Original Message-----

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

> Mark Thompson

> Sent: Tuesday, June 04, 2019 7:19 AM

> To: ffmpeg-devel@ffmpeg.org

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

> documentation

> 

> Equivalent to the previous patch for libx264.

> ---

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

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

> 

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

> index f56def53d5..665b780643 100644

> --- a/libavcodec/libx265.c

> +++ b/libavcodec/libx265.c

> @@ -316,27 +316,36 @@ 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;

> +            uint32_t roi_size;

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

> */

> +

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

> +            roi_size = roi->self_size;

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

> +                av_log(ctx, AV_LOG_ERROR, "Invalid

> AVRegionOfInterest.self_size.\n");

> +                return AVERROR(EINVAL);

> +            }

> +            nb_rois = sd->size / roi_size;

> +

>              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);

> +            // This list must be iterated in reverse because the first

> +            // region in the list applies when regions overlap.

> +            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_size * i);

> +

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

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

> mb_size);

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

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

> mb_size);

> 

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

>                      av_free(qoffsets);

> @@ -344,18 +353,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;


looks good to me, thanks.

> --

> 2.20.1

> 

> _______________________________________________

> ffmpeg-devel mailing list

> ffmpeg-devel@ffmpeg.org

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

> 

> To unsubscribe, visit link above, or email

> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Guo, Yejun June 25, 2019, 8:47 a.m. UTC | #2
> -----Original Message-----

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

> Guo, Yejun

> Sent: Tuesday, June 04, 2019 2:46 PM

> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>

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

> match documentation

> 

> 

> 

> > -----Original Message-----

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

> > Mark Thompson

> > Sent: Tuesday, June 04, 2019 7:19 AM

> > To: ffmpeg-devel@ffmpeg.org

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

> match

> > documentation

> >

> > Equivalent to the previous patch for libx264.

> > ---

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

> 

> looks good to me, thanks.


is it possible to first push 1-3 patches of this patch set? These are the necessary refine for my original ROI patch. 
I hope the next ffmpeg release version will contain a complete code base for ROI (libx264 and libx265), thanks.
Mark Thompson July 7, 2019, 6:39 p.m. UTC | #3
On 25/06/2019 09:47, Guo, Yejun wrote:>> -----Original Message-----
>> From: ffmpeg-devel [mailto:ffmpeg-devel-bounces@ffmpeg.org] On Behalf Of
>> Guo, Yejun
>> Sent: Tuesday, June 04, 2019 2:46 PM
>> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
>> Subject: Re: [FFmpeg-devel] [PATCH v3 3/5] libx265: Update ROI behaviour to
>> match documentation
>>
>>
>>
>>> -----Original Message-----
>>> From: ffmpeg-devel [mailto:ffmpeg-devel-bounces@ffmpeg.org] On Behalf Of
>>> Mark Thompson
>>> Sent: Tuesday, June 04, 2019 7:19 AM
>>> To: ffmpeg-devel@ffmpeg.org
>>> Subject: [FFmpeg-devel] [PATCH v3 3/5] libx265: Update ROI behaviour to
>> match
>>> documentation
>>>
>>> Equivalent to the previous patch for libx264.
>>> ---
>>>  libavcodec/libx265.c | 44 +++++++++++++++++++++++---------------------
>>
>> looks good to me, thanks.
> 
> is it possible to first push 1-3 patches of this patch set? These are the necessary refine for my original ROI patch. 
> I hope the next ffmpeg release version will contain a complete code base for ROI (libx264 and libx265), thanks.

Applied.  I'll send an update for the last two patches soonish.

Thanks,

- Mark
diff mbox

Patch

diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c
index f56def53d5..665b780643 100644
--- a/libavcodec/libx265.c
+++ b/libavcodec/libx265.c
@@ -316,27 +316,36 @@  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;
+            uint32_t roi_size;
             float *qoffsets;         /* will be freed after encode is called. */
+
+            roi = (const AVRegionOfInterest*)sd->data;
+            roi_size = roi->self_size;
+            if (!roi_size || sd->size % roi_size != 0) {
+                av_log(ctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n");
+                return AVERROR(EINVAL);
+            }
+            nb_rois = sd->size / roi_size;
+
             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);
+            // This list must be iterated in reverse because the first
+            // region in the list applies when regions overlap.
+            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_size * i);
+
+                starty = FFMIN(mby, roi->top / mb_size);
+                endy   = FFMIN(mby, (roi->bottom + mb_size - 1)/ mb_size);
+                startx = FFMIN(mbx, roi->left / mb_size);
+                endx   = FFMIN(mbx, (roi->right + mb_size - 1)/ mb_size);
 
                 if (roi->qoffset.den == 0) {
                     av_free(qoffsets);
@@ -344,18 +353,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;