diff mbox

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

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

Commit Message

Guo, Yejun Dec. 28, 2018, 10:10 a.m. UTC
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 | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

Comments

Derek Buitenhuis Dec. 28, 2018, 4:06 p.m. UTC | #1
On 28/12/2018 10:10, Guo, Yejun wrote:
> 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 | 39 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 39 insertions(+)

Seems OK.

- Derek
Guo, Yejun Jan. 2, 2019, 12:54 a.m. UTC | #2
this patch set asks for pushing if no other issues, thanks.

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

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

> Of Derek Buitenhuis

> Sent: Saturday, December 29, 2018 12:06 AM

> To: ffmpeg-devel@ffmpeg.org

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

> for ROI-based encoding

> 

> On 28/12/2018 10:10, Guo, Yejun wrote:

> > 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 | 39

> +++++++++++++++++++++++++++++++++++++++

> >  1 file changed, 39 insertions(+)

> 

> Seems OK.

> 

> - Derek

> _______________________________________________

> ffmpeg-devel mailing list

> ffmpeg-devel@ffmpeg.org

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

Patch

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index a68d0a7..927156d 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;
@@ -345,6 +349,41 @@  static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
                 }
             }
         }
+
+        AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ROIS);
+        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;
+                    float* qoffsets;
+                    qoffsets = (float*)av_mallocz_array(mbx * mby, sizeof(*qoffsets));
+                    if (!qoffsets)
+                        return AVERROR(ENOMEM);
+
+                    size_t nb_rois = sd->size / sizeof(AVROI);
+                    AVROI* rois = (AVROI*)sd->data;
+                    for (size_t roi = 0; roi < nb_rois; roi++) {
+                        int starty = FFMIN(mby, rois[roi].top / MB_SIZE);
+                        int endy = FFMIN(mby, (rois[roi].bottom + MB_SIZE - 1)/ MB_SIZE);
+                        int startx = FFMIN(mbx, rois[roi].left / MB_SIZE);
+                        int endx = FFMIN(mbx, (rois[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] = rois[roi].qoffset;
+                            }
+                        }
+                    }
+
+                    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 {