[FFmpeg-devel,v2,4/6] libx265: Update ROI behaviour to match documentation

Submitted by Mark Thompson on March 13, 2019, 12:17 a.m.

Details

Message ID 20190313001748.11781-4-sw@jkqxz.net
State New
Headers show

Commit Message

Mark Thompson March 13, 2019, 12:17 a.m.
Equivalent to the previous patch for libx264.
---
 libavcodec/libx265.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)

Comments

Guo, Yejun March 13, 2019, 1:20 a.m.
> -----Original Message-----

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

> Of Mark Thompson

> Sent: Wednesday, March 13, 2019 8:18 AM

> To: ffmpeg-devel@ffmpeg.org

> Subject: [FFmpeg-devel] [PATCH v2 4/6] libx265: Update ROI behaviour to

> match documentation

> 

> Equivalent to the previous patch for libx264.

> ---

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

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

> 

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

> index c2c1f8b9bc..a580d113b9 100644

> --- a/libavcodec/libx265.c

> +++ b/libavcodec/libx265.c

> @@ -304,27 +304,34 @@ 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. */

> +

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

> +

>              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 = av_clip(roi->y / mb_size, 0, mby);

> -                int endy   = av_clip((roi->y + roi->height + mb_size - 1) / mb_size, 0,

> mby);

> -                int startx = av_clip(roi->x / mb_size, 0, mbx);

> -                int endx   = av_clip((roi->x + roi->width  + mb_size - 1) / mb_size, 0,

> mbx);

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


same comment as libx264

> +

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

> +                endy   = av_clip((roi->y + roi->height + mb_size - 1) / mb_size, 0,

> mby);

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

> +                endx   = av_clip((roi->x + roi->width  + mb_size - 1) / mb_size, 0,

> mbx);

> 

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

>                      av_free(qoffsets);

> @@ -332,18 +339,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
Michael Niedermayer March 13, 2019, 3:44 p.m.
On Wed, Mar 13, 2019 at 12:17:46AM +0000, Mark Thompson wrote:
> Equivalent to the previous patch for libx264.
> ---
>  libavcodec/libx265.c | 42 +++++++++++++++++++++---------------------
>  1 file changed, 21 insertions(+), 21 deletions(-)

breaks build

CC	libavcodec/vaapi_encode.o
libavcodec/vaapi_encode.c: In function ‘vaapi_encode_issue’:
libavcodec/vaapi_encode.c:566:18: error: ‘VAAPIEncodePicture’ has no member named ‘roi’
     av_freep(&pic->roi);
                  ^
libavcodec/vaapi_encode.c: In function ‘vaapi_encode_free’:
libavcodec/vaapi_encode.c:701:18: error: ‘VAAPIEncodePicture’ has no member named ‘roi’
     av_freep(&pic->roi);
                  ^
make: *** [libavcodec/vaapi_encode.o] Error 1


[...]
Michael Niedermayer March 13, 2019, 3:46 p.m.
On Wed, Mar 13, 2019 at 04:44:29PM +0100, Michael Niedermayer wrote:
> On Wed, Mar 13, 2019 at 12:17:46AM +0000, Mark Thompson wrote:
> > Equivalent to the previous patch for libx264.
> > ---
> >  libavcodec/libx265.c | 42 +++++++++++++++++++++---------------------
> >  1 file changed, 21 insertions(+), 21 deletions(-)
> 
> breaks build
> 
> CC	libavcodec/vaapi_encode.o
> libavcodec/vaapi_encode.c: In function ‘vaapi_encode_issue’:
> libavcodec/vaapi_encode.c:566:18: error: ‘VAAPIEncodePicture’ has no member named ‘roi’
>      av_freep(&pic->roi);
>                   ^
> libavcodec/vaapi_encode.c: In function ‘vaapi_encode_free’:
> libavcodec/vaapi_encode.c:701:18: error: ‘VAAPIEncodePicture’ has no member named ‘roi’
>      av_freep(&pic->roi);
>                   ^
> make: *** [libavcodec/vaapi_encode.o] Error 1

wrong commit this is caused by "vaapi_encode: Add ROI support"

sorry

[...]

Patch hide | download patch | download mbox

diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c
index c2c1f8b9bc..a580d113b9 100644
--- a/libavcodec/libx265.c
+++ b/libavcodec/libx265.c
@@ -304,27 +304,34 @@  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. */
+
+            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;
+
             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 = av_clip(roi->y / mb_size, 0, mby);
-                int endy   = av_clip((roi->y + roi->height + mb_size - 1) / mb_size, 0, mby);
-                int startx = av_clip(roi->x / mb_size, 0, mbx);
-                int endx   = av_clip((roi->x + roi->width  + mb_size - 1) / mb_size, 0, mbx);
+            // 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->self_size * i);
+
+                starty = av_clip(roi->y / mb_size, 0, mby);
+                endy   = av_clip((roi->y + roi->height + mb_size - 1) / mb_size, 0, mby);
+                startx = av_clip(roi->x / mb_size, 0, mbx);
+                endx   = av_clip((roi->x + roi->width  + mb_size - 1) / mb_size, 0, mbx);
 
                 if (roi->qoffset.den == 0) {
                     av_free(qoffsets);
@@ -332,18 +339,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;