libx265: Update ROI behaviour to match documentation

Equivalent to the previous patch for libx264.
This commit is contained in:
Mark Thompson 2019-06-04 00:19:03 +01:00
parent d76e2aaf08
commit f6c572bd89
1 changed files with 23 additions and 21 deletions

View File

@ -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 mb_size = (ctx->params->rc.qgSize == 8) ? 8 : 16;
int mbx = (frame->width + mb_size - 1) / mb_size; int mbx = (frame->width + mb_size - 1) / mb_size;
int mby = (frame->height + 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; int nb_rois;
AVRegionOfInterest *roi; const AVRegionOfInterest *roi;
uint32_t roi_size;
float *qoffsets; /* will be freed after encode is called. */ 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)); qoffsets = av_mallocz_array(mbx * mby, sizeof(*qoffsets));
if (!qoffsets) if (!qoffsets)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
nb_rois = sd->size / sizeof(AVRegionOfInterest); // This list must be iterated in reverse because the first
roi = (AVRegionOfInterest*)sd->data; // region in the list applies when regions overlap.
for (int count = 0; count < nb_rois; count++) { for (int i = nb_rois - 1; i >= 0; i--) {
int starty = FFMIN(mby, roi->top / mb_size); int startx, endx, starty, endy;
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);
float qoffset; float qoffset;
if (roi->self_size == 0) { roi = (const AVRegionOfInterest*)(sd->data + roi_size * i);
av_free(qoffsets);
av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.self_size must be set to sizeof(AVRegionOfInterest).\n"); starty = FFMIN(mby, roi->top / mb_size);
return AVERROR(EINVAL); 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) { if (roi->qoffset.den == 0) {
av_free(qoffsets); av_free(qoffsets);
@ -344,18 +353,11 @@ static av_cold int libx265_encode_set_roi(libx265Context *ctx, const AVFrame *fr
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
qoffset = roi->qoffset.num * 1.0f / roi->qoffset.den; qoffset = roi->qoffset.num * 1.0f / roi->qoffset.den;
qoffset = av_clipf(qoffset, -1.0f, 1.0f); qoffset = av_clipf(qoffset * qp_range, -qp_range, +qp_range);
/* 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;
for (int y = starty; y < endy; y++) for (int y = starty; y < endy; y++)
for (int x = startx; x < endx; x++) for (int x = startx; x < endx; x++)
qoffsets[x + y*mbx] = qoffset; qoffsets[x + y*mbx] = qoffset;
roi = (AVRegionOfInterest*)((char*)roi + roi->self_size);
} }
pic->quantOffsets = qoffsets; pic->quantOffsets = qoffsets;