[FFmpeg-devel,V5,2/2] avcodec/libx264: add support for ROI-based encoding

Submitted by Guo, Yejun on Jan. 3, 2019, 4:35 p.m.

Details

Message ID 1546533343-30618-1-git-send-email-yejun.guo@intel.com
State Accepted
Headers show

Commit Message

Guo, Yejun Jan. 3, 2019, 4:35 p.m.
This patch just enables the path from ffmpeg to libx264,
the more encoders can be added later.

Signed-off-by: Guo, Yejun <yejun.guo@intel.com>
---
 libavcodec/libx264.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

Comments

Moritz Barsnick Jan. 4, 2019, 11:58 a.m.
On Fri, Jan 04, 2019 at 00:35:43 +0800, Guo, Yejun wrote:
> @@ -345,6 +350,50 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
[...]
> +        if (sd) {
> +            if (x4->params.rc.i_aq_mode == X264_AQ_NONE) {
> +                av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be enabled to use ROI encoding, skipping ROI.\n");
> +            } else {
> +                if (frame->interlaced_frame == 0) {
[...]
> +                } else {
> +                    av_log(ctx, AV_LOG_WARNING, "interlaced_frame not supported for ROI encoding yet, skipping ROI.\n");
> +                }

Are these warnings given on every frame, if applicable? If so, that may
give a lot of console spam.

Moritz
Guo, Yejun Jan. 7, 2019, 6:58 a.m.
> -----Original Message-----

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

> Of Moritz Barsnick

> Sent: Friday, January 04, 2019 7:58 PM

> To: FFmpeg development discussions and patches <ffmpeg-

> devel@ffmpeg.org>

> Subject: Re: [FFmpeg-devel] [PATCH V5 2/2] avcodec/libx264: add support

> for ROI-based encoding

> 

> On Fri, Jan 04, 2019 at 00:35:43 +0800, Guo, Yejun wrote:

> > @@ -345,6 +350,50 @@ static int X264_frame(AVCodecContext *ctx,

> > AVPacket *pkt, const AVFrame *frame,

> [...]

> > +        if (sd) {

> > +            if (x4->params.rc.i_aq_mode == X264_AQ_NONE) {

> > +                av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be

> enabled to use ROI encoding, skipping ROI.\n");

> > +            } else {

> > +                if (frame->interlaced_frame == 0) {

> [...]

> > +                } else {

> > +                    av_log(ctx, AV_LOG_WARNING, "interlaced_frame not

> supported for ROI encoding yet, skipping ROI.\n");

> > +                }

> 

> Are these warnings given on every frame, if applicable? If so, that may give a

> lot of console spam.


yes, it acts so, just like the av_log in function reconfig_encoder in the same file.

looks it is not good, I'll add a flag to print only once.

> 

> Moritz

> _______________________________________________

> ffmpeg-devel mailing list

> ffmpeg-devel@ffmpeg.org

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

Patch hide | download patch | download mbox

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index a68d0a7..5085d13 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -40,6 +40,10 @@ 
 #include <stdlib.h>
 #include <string.h>
 
+// from x264.h, for quant_offsets, Macroblocks are 16x16
+// blocks of pixels (with respect to the luma plane)
+#define MB_SIZE 16
+
 typedef struct X264Context {
     AVClass        *class;
     x264_param_t    params;
@@ -282,6 +286,7 @@  static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
     x264_picture_t pic_out = {0};
     int pict_type;
     int64_t *out_opaque;
+    AVFrameSideData *sd;
 
     x264_picture_init( &x4->pic );
     x4->pic.img.i_csp   = x4->params.i_csp;
@@ -345,6 +350,50 @@  static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
                 }
             }
         }
+
+        sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
+        if (sd) {
+            if (x4->params.rc.i_aq_mode == X264_AQ_NONE) {
+                av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be enabled to use ROI encoding, skipping ROI.\n");
+            } else {
+                if (frame->interlaced_frame == 0) {
+                    size_t mbx = (frame->width + MB_SIZE - 1) / MB_SIZE;
+                    size_t mby = (frame->height + MB_SIZE - 1) / MB_SIZE;
+                    size_t nb_rois;
+                    AVRegionOfInterest* roi;
+                    float* qoffsets;
+                    qoffsets = (float*)av_mallocz_array(mbx * mby, sizeof(*qoffsets));
+                    if (!qoffsets)
+                        return AVERROR(ENOMEM);
+
+                    nb_rois = sd->size / sizeof(AVRegionOfInterest);
+                    roi = (AVRegionOfInterest*)sd->data;
+                    for (size_t 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);
+                        for (int y = starty; y < endy; y++) {
+                            for (int x = startx; x < endx; x++) {
+                                qoffsets[x + y*mbx] = roi->qoffset;
+                            }
+                        }
+
+                        if (roi->self_size == 0) {
+                            av_free(qoffsets);
+                            av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.self_size should be set to sizeof(AVRegionOfInterest).\n");
+                            return AVERROR(EINVAL);
+                        }
+                        roi = (AVRegionOfInterest*)((char*)roi + roi->self_size);
+                    }
+
+                    x4->pic.prop.quant_offsets = qoffsets;
+                    x4->pic.prop.quant_offsets_free = av_free;
+                } else {
+                    av_log(ctx, AV_LOG_WARNING, "interlaced_frame not supported for ROI encoding yet, skipping ROI.\n");
+                }
+            }
+        }
     }
 
     do {