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

Mark Thompson sw at jkqxz.net
Wed Mar 13 02:17:46 EET 2019


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



More information about the ffmpeg-devel mailing list