[FFmpeg-devel] [PATCH v5 3/3] libavfilter/vf_find_rect: convert the object image to gray8 format instead of failed directly

lance.lmwang at gmail.com lance.lmwang at gmail.com
Fri Jun 14 19:48:26 EEST 2019


From: Limin Wang <lance.lmwang at gmail.com>

Signed-off-by: Limin Wang <lance.lmwang at gmail.com>
---
 doc/filters.texi            |  2 +-
 libavfilter/vf_cover_rect.c | 40 ++++++++++++++++++++++++++++++++--------
 2 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 4d48068..4594a61 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -10180,7 +10180,7 @@ It accepts the following options:
 
 @table @option
 @item cover
-Filepath of the optional cover image, needs to be in yuv420.
+Filepath of the optional cover image.
 
 @item mode
 Set covering mode.
diff --git a/libavfilter/vf_cover_rect.c b/libavfilter/vf_cover_rect.c
index b66c40b..502939f 100644
--- a/libavfilter/vf_cover_rect.c
+++ b/libavfilter/vf_cover_rect.c
@@ -28,6 +28,7 @@
 #include "internal.h"
 
 #include "lavfutils.h"
+#include "lswsutils.h"
 
 enum mode {
     MODE_COVER,
@@ -40,6 +41,7 @@ typedef struct CoverContext {
     int mode;
     char *cover_filename;
     AVFrame *cover_frame;
+    AVFrame *match_frame;
     int width, height;
 } CoverContext;
 
@@ -139,8 +141,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     CoverContext *cover = ctx->priv;
     AVDictionaryEntry *ex, *ey, *ew, *eh;
     int x = -1, y = -1, w = -1, h = -1;
+    enum AVPixelFormat in_format;
     char *xendptr = NULL, *yendptr = NULL, *wendptr = NULL, *hendptr = NULL;
     AVFrame *cover_frame = NULL;
+    int ret;
 
     ex = av_dict_get(in->metadata, "lavfi.rect.x", NULL, AV_DICT_MATCH_CASE);
     ey = av_dict_get(in->metadata, "lavfi.rect.y", NULL, AV_DICT_MATCH_CASE);
@@ -169,14 +173,35 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     }
     w = FFMIN(w, in->width  - x);
     h = FFMIN(h, in->height - y);
+    in_format = in->format;
 
     if (w > in->width || h > in->height || w <= 0 || h <= 0)
         return AVERROR(EINVAL);
 
     if (cover->cover_frame) {
-        if (w != cover->cover_frame->width || h != cover->cover_frame->height)
-            return AVERROR(EINVAL);
-        cover_frame = cover->cover_frame;
+        if (w != cover->cover_frame->width || h != cover->cover_frame->height ||
+                in_format != cover->cover_frame->format) {
+            if (!cover->match_frame || (w != cover->match_frame->width || h != cover->match_frame->height
+                        || in_format != cover->match_frame->format)) {
+                if (cover->match_frame)
+                    av_freep(&cover->match_frame->data[0]);
+                else if (!(cover->match_frame = av_frame_alloc()))
+                    return AVERROR(ENOMEM);
+
+                if ((ret = ff_scale_image(cover->match_frame->data, cover->match_frame->linesize,
+                                w, h, in_format, cover->cover_frame->data, cover->cover_frame->linesize,
+                                cover->cover_frame->width, cover->cover_frame->height,
+                                cover->cover_frame->format, ctx)) < 0)
+                    return AVERROR(ENOMEM);
+
+                cover->match_frame->width  = w;
+                cover->match_frame->height = h;
+                cover->match_frame->format = in_format;
+            }
+
+            cover_frame = cover->match_frame;
+        } else
+            cover_frame = cover->cover_frame;
     }
 
     cover->width  = w;
@@ -202,6 +227,10 @@ static av_cold void uninit(AVFilterContext *ctx)
     if (cover->cover_frame)
         av_freep(&cover->cover_frame->data[0]);
     av_frame_free(&cover->cover_frame);
+
+    if (cover->match_frame)
+        av_freep(&cover->match_frame->data[0]);
+    av_frame_free(&cover->match_frame);
 }
 
 static av_cold int init(AVFilterContext *ctx)
@@ -223,11 +252,6 @@ static av_cold int init(AVFilterContext *ctx)
                                 &cover->cover_frame->width, &cover->cover_frame->height,
                                 &cover->cover_frame->format, cover->cover_filename, ctx)) < 0)
             return ret;
-
-        if (cover->cover_frame->format != AV_PIX_FMT_YUV420P && cover->cover_frame->format != AV_PIX_FMT_YUVJ420P) {
-            av_log(ctx, AV_LOG_ERROR, "cover image is not a YUV420 image\n");
-            return AVERROR(EINVAL);
-        }
     }
 
     return 0;
-- 
2.6.4



More information about the ffmpeg-devel mailing list