[FFmpeg-devel] [PATCH] avfilter: add codecview filter

Clément Bœsch u at pkh.me
Mon Aug 18 18:03:39 CEST 2014


From: Clément Bœsch <clement at stupeflix.com>

---
Note: I didn't use FF_API_DEBUG_MV because it seems to overlap with all
kind of other things and I couldn't test properly the conditional
compilation.

BTW, I could add a FATE test easily, but I didn't find any relevant
samples available.

TODO: minor bump avfilter, micro bump avcodec
---
 Changelog                  |   1 +
 RELEASE_NOTES              |   2 +
 doc/filters.texi           |  36 +++++++
 libavcodec/mpegvideo.c     |  13 ++-
 libavcodec/options_table.h |   4 +-
 libavcodec/utils.c         |   6 ++
 libavcodec/version.h       |   4 +
 libavfilter/Makefile       |   1 +
 libavfilter/allfilters.c   |   1 +
 libavfilter/vf_codecview.c | 241 +++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 305 insertions(+), 4 deletions(-)
 create mode 100644 libavfilter/vf_codecview.c

diff --git a/Changelog b/Changelog
index 6260d0c..b2e3896 100644
--- a/Changelog
+++ b/Changelog
@@ -7,6 +7,7 @@ version <next>:
 - large optimizations in dctdnoiz to make it usable
 - request icecast metadata by default
 - support for using metadata in stream specifiers in fftools
+- added codecview filter to visualize information exported by some codecs
 
 
 version 2.3:
diff --git a/RELEASE_NOTES b/RELEASE_NOTES
index eb73e88..3fbf556 100644
--- a/RELEASE_NOTES
+++ b/RELEASE_NOTES
@@ -37,6 +37,7 @@
 
     • ported lenscorrection filter from frei0r filter
     • large optimizations in dctdnoiz to make it usable
+    • added codecview filter to visualize information exported by some codecs
 
    ┌────────────────────────────┐
    │ libavutil                  │
@@ -49,3 +50,4 @@
  └────────────────────────────┘
 
   • dctdnoiz filter now uses a block size of 8x8 instead of 16x16 by default
+  • -vismv option is deprecated in favor of the codecview filter
diff --git a/doc/filters.texi b/doc/filters.texi
index 0ca1d6f..de4cb19 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2755,6 +2755,42 @@ boxblur=luma_radius=min(h\,w)/10:luma_power=1:chroma_radius=min(cw\,ch)/10:chrom
 @end example
 @end itemize
 
+ at section codecview
+
+Visualize information exported by some codecs.
+
+Some codecs can export information through frames using side-data or other
+means. For example, some MPEG based codecs export motion vectors through the
+ at var{export_mvs} flag in the codec @option{flags2} option.
+
+The filter accepts the following option:
+
+ at table @option
+ at item mv
+Set motion vectors to visualize.
+
+Available flags for @var{mv} are:
+
+ at table @samp
+ at item pf
+forward predicted MVs of P-frames
+ at item bf
+forward predicted MVs of B-frames
+ at item bb
+backward predicted MVs of B-frames
+ at end table
+ at end table
+
+ at subsection Examples
+
+ at itemize
+ at item
+Visualizes multi-directionals MVs from P and B-Frames using @command{ffplay}:
+ at example
+ffplay -flags2 +export_mvs input.mpg -vf codecview=mv=pf+bf+bb
+ at end example
+ at end itemize
+
 @section colorbalance
 Modify intensity of primary colors (red, green and blue) of input frames.
 
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 6a0b822..4e63edb 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -1982,7 +1982,7 @@ void ff_mpv_frame_end(MpegEncContext *s)
         ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
 }
 
-
+#if FF_API_VISMV
 static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
 {
     if(*sx > *ex)
@@ -2107,6 +2107,7 @@ static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
     }
     draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
 }
+#endif
 
 static int add_mb(AVMotionVector *mb, uint32_t mb_type,
                   int dst_x, int dst_y,
@@ -2292,13 +2293,15 @@ void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_
 
     if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
         (avctx->debug_mv)) {
-        const int shift = 1 + quarter_sample;
         int mb_y;
-        uint8_t *ptr;
         int i;
         int h_chroma_shift, v_chroma_shift, block_height;
+#if FF_API_VISMV
+        const int shift = 1 + quarter_sample;
+        uint8_t *ptr;
         const int width          = avctx->width;
         const int height         = avctx->height;
+#endif
         const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
         const int mv_stride      = (mb_width << mv_sample_log2) +
                                    (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
@@ -2310,13 +2313,16 @@ void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_
         av_frame_make_writable(pict);
 
         pict->opaque = NULL;
+#if FF_API_VISMV
         ptr          = pict->data[0];
+#endif
         block_height = 16 >> v_chroma_shift;
 
         for (mb_y = 0; mb_y < mb_height; mb_y++) {
             int mb_x;
             for (mb_x = 0; mb_x < mb_width; mb_x++) {
                 const int mb_index = mb_x + mb_y * mb_stride;
+#if FF_API_VISMV
                 if ((avctx->debug_mv) && motion_val[0]) {
                     int type;
                     for (type = 0; type < 3; type++) {
@@ -2396,6 +2402,7 @@ void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_
                         }
                     }
                 }
+#endif
                 if ((avctx->debug & FF_DEBUG_VIS_QP)) {
                     uint64_t c = (qscale_table[mb_index] * 128 / 31) *
                                  0x0101010101010101ULL;
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
index 7000531..ad3d52e 100644
--- a/libavcodec/options_table.h
+++ b/libavcodec/options_table.h
@@ -262,10 +262,12 @@ static const AVOption avcodec_options[] = {
 {"buffers", "picture buffer allocations", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_BUFFERS }, INT_MIN, INT_MAX, V|D, "debug"},
 {"thread_ops", "threading operations", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_THREADS }, INT_MIN, INT_MAX, V|A|D, "debug"},
 {"nomc", "skip motion compensation", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_NOMC }, INT_MIN, INT_MAX, V|A|D, "debug"},
-{"vismv", "visualize motion vectors (MVs)", OFFSET(debug_mv), AV_OPT_TYPE_FLAGS, {.i64 = DEFAULT }, 0, INT_MAX, V|D, "debug_mv"},
+#if FF_API_VISMV
+{"vismv", "visualize motion vectors (MVs) (deprecated)", OFFSET(debug_mv), AV_OPT_TYPE_FLAGS, {.i64 = DEFAULT }, 0, INT_MAX, V|D, "debug_mv"},
 {"pf", "forward predicted MVs of P-frames", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_VIS_MV_P_FOR }, INT_MIN, INT_MAX, V|D, "debug_mv"},
 {"bf", "forward predicted MVs of B-frames", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_VIS_MV_B_FOR }, INT_MIN, INT_MAX, V|D, "debug_mv"},
 {"bb", "backward predicted MVs of B-frames", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_VIS_MV_B_BACK }, INT_MIN, INT_MAX, V|D, "debug_mv"},
+#endif
 {"cmp", "full-pel ME compare function", OFFSET(me_cmp), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E, "cmp_func"},
 {"subcmp", "sub-pel ME compare function", OFFSET(me_sub_cmp), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E, "cmp_func"},
 {"mbcmp", "macroblock compare function", OFFSET(mb_cmp), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E, "cmp_func"},
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 6a40a03..70f5734 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1435,6 +1435,12 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
         goto free_and_end;
     }
 
+#if FF_API_VISMV
+    if (avctx->debug_mv)
+        av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
+               "see the codecview filter instead: http://ffmpeg.org/ffmpeg-filters.html#codecview\n");
+#endif
+
     if (av_codec_is_encoder(avctx->codec)) {
         int i;
         if (avctx->codec->sample_fmts) {
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 2c871ba..9da72ab 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -174,5 +174,9 @@
 #ifndef FF_API_AFD
 #define FF_API_AFD               (LIBAVCODEC_VERSION_MAJOR < 57)
 #endif
+#ifndef FF_API_VISMV
+/* XXX: don't forget to drop the -vismv documentation */
+#define FF_API_VISMV             (LIBAVCODEC_VERSION_MAJOR < 57)
+#endif
 
 #endif /* AVCODEC_VERSION_H */
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 7486f96..ce71ce1 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -97,6 +97,7 @@ OBJS-$(CONFIG_BLACKDETECT_FILTER)            += vf_blackdetect.o
 OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
 OBJS-$(CONFIG_BLEND_FILTER)                  += vf_blend.o dualinput.o framesync.o
 OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
+OBJS-$(CONFIG_CODECVIEW_FILTER)              += vf_codecview.o
 OBJS-$(CONFIG_COLORBALANCE_FILTER)           += vf_colorbalance.o
 OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER)      += vf_colorchannelmixer.o
 OBJS-$(CONFIG_COLORMATRIX_FILTER)            += vf_colormatrix.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index b1d6ff5..8fe2020 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -115,6 +115,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(BLACKFRAME,     blackframe,     vf);
     REGISTER_FILTER(BLEND,          blend,          vf);
     REGISTER_FILTER(BOXBLUR,        boxblur,        vf);
+    REGISTER_FILTER(CODECVIEW,      codecview,      vf);
     REGISTER_FILTER(COLORBALANCE,   colorbalance,   vf);
     REGISTER_FILTER(COLORCHANNELMIXER, colorchannelmixer, vf);
     REGISTER_FILTER(COLORMATRIX,    colormatrix,    vf);
diff --git a/libavfilter/vf_codecview.c b/libavfilter/vf_codecview.c
new file mode 100644
index 0000000..5749631
--- /dev/null
+++ b/libavfilter/vf_codecview.c
@@ -0,0 +1,241 @@
+/*
+ * Copyright (c) 2002-2004 Michael Niedermayer <michaelni at gmx.at>
+ * Copyright (c) 2014 Clément Bœsch <u pkh me>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Codec debug viewer filter.
+ *
+ * TODO: segmentation
+ * TODO: quantization
+ */
+
+#include "libavutil/imgutils.h"
+#include "libavutil/motion_vector.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "internal.h"
+
+#define MV_P_FOR  (1<<0)
+#define MV_B_FOR  (1<<1)
+#define MV_B_BACK (1<<2)
+
+typedef struct {
+    const AVClass *class;
+    unsigned mv;
+} CodecViewContext;
+
+#define OFFSET(x) offsetof(CodecViewContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption codecview_options[] = {
+    { "mv", "set motion vectors to visualize", OFFSET(mv), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "mv" },
+        {"pf", "forward predicted MVs of P-frames",  0, AV_OPT_TYPE_CONST, {.i64 = MV_P_FOR },  INT_MIN, INT_MAX, FLAGS, "mv"},
+        {"bf", "forward predicted MVs of B-frames",  0, AV_OPT_TYPE_CONST, {.i64 = MV_B_FOR },  INT_MIN, INT_MAX, FLAGS, "mv"},
+        {"bb", "backward predicted MVs of B-frames", 0, AV_OPT_TYPE_CONST, {.i64 = MV_B_BACK }, INT_MIN, INT_MAX, FLAGS, "mv"},
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(codecview);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    // TODO: we can probably add way more pixel formats without any other
+    // changes; anything with 8-bit luma in first plane should be working
+    static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE};
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
+{
+    if(*sx > *ex)
+        return clip_line(ex, ey, sx, sy, maxx);
+
+    if (*sx < 0) {
+        if (*ex < 0)
+            return 1;
+        *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
+        *sx = 0;
+    }
+
+    if (*ex > maxx) {
+        if (*sx > maxx)
+            return 1;
+        *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
+        *ex = maxx;
+    }
+    return 0;
+}
+
+/**
+ * Draw a line from (ex, ey) -> (sx, sy).
+ * @param w width of the image
+ * @param h height of the image
+ * @param stride stride/linesize of the image
+ * @param color color of the arrow
+ */
+static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
+                      int w, int h, int stride, int color)
+{
+    int x, y, fr, f;
+
+    if (clip_line(&sx, &sy, &ex, &ey, w - 1))
+        return;
+    if (clip_line(&sy, &sx, &ey, &ex, h - 1))
+        return;
+
+    sx = av_clip(sx, 0, w - 1);
+    sy = av_clip(sy, 0, h - 1);
+    ex = av_clip(ex, 0, w - 1);
+    ey = av_clip(ey, 0, h - 1);
+
+    buf[sy * stride + sx] += color;
+
+    if (FFABS(ex - sx) > FFABS(ey - sy)) {
+        if (sx > ex) {
+            FFSWAP(int, sx, ex);
+            FFSWAP(int, sy, ey);
+        }
+        buf += sx + sy * stride;
+        ex  -= sx;
+        f    = ((ey - sy) << 16) / ex;
+        for (x = 0; x <= ex; x++) {
+            y  = (x * f) >> 16;
+            fr = (x * f) & 0xFFFF;
+                   buf[ y      * stride + x] += (color * (0x10000 - fr)) >> 16;
+            if(fr) buf[(y + 1) * stride + x] += (color *            fr ) >> 16;
+        }
+    } else {
+        if (sy > ey) {
+            FFSWAP(int, sx, ex);
+            FFSWAP(int, sy, ey);
+        }
+        buf += sx + sy * stride;
+        ey  -= sy;
+        if (ey)
+            f = ((ex - sx) << 16) / ey;
+        else
+            f = 0;
+        for(y= 0; y <= ey; y++){
+            x  = (y*f) >> 16;
+            fr = (y*f) & 0xFFFF;
+                   buf[y * stride + x    ] += (color * (0x10000 - fr)) >> 16;
+            if(fr) buf[y * stride + x + 1] += (color *            fr ) >> 16;
+        }
+    }
+}
+
+/**
+ * Draw an arrow from (ex, ey) -> (sx, sy).
+ * @param w width of the image
+ * @param h height of the image
+ * @param stride stride/linesize of the image
+ * @param color color of the arrow
+ */
+static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
+                       int ey, int w, int h, int stride, int color, int tail, int direction)
+{
+    int dx,dy;
+
+    if (direction) {
+        FFSWAP(int, sx, ex);
+        FFSWAP(int, sy, ey);
+    }
+
+    sx = av_clip(sx, -100, w + 100);
+    sy = av_clip(sy, -100, h + 100);
+    ex = av_clip(ex, -100, w + 100);
+    ey = av_clip(ey, -100, h + 100);
+
+    dx = ex - sx;
+    dy = ey - sy;
+
+    if (dx * dx + dy * dy > 3 * 3) {
+        int rx =  dx + dy;
+        int ry = -dx + dy;
+        int length = sqrt((rx * rx + ry * ry) << 8);
+
+        // FIXME subpixel accuracy
+        rx = ROUNDED_DIV(rx * 3 << 4, length);
+        ry = ROUNDED_DIV(ry * 3 << 4, length);
+
+        if (tail) {
+            rx = -rx;
+            ry = -ry;
+        }
+
+        draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
+        draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
+    }
+    draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    AVFilterContext *ctx = inlink->dst;
+    CodecViewContext *s = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+
+    AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
+    if (sd) {
+        int i;
+        const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
+        for (i = 0; i < sd->size / sizeof(*mvs); i++) {
+            const AVMotionVector *mv = &mvs[i];
+            const int direction = mv->source > 0;
+            if ((direction == 0 && (s->mv & MV_P_FOR)  && frame->pict_type == AV_PICTURE_TYPE_P) ||
+                (direction == 0 && (s->mv & MV_B_FOR)  && frame->pict_type == AV_PICTURE_TYPE_B) ||
+                (direction == 1 && (s->mv & MV_B_BACK) && frame->pict_type == AV_PICTURE_TYPE_B))
+                draw_arrow(frame->data[0], mv->dst_x, mv->dst_y, mv->src_x, mv->src_y,
+                           frame->width, frame->height, frame->linesize[0],
+                           100, 0, mv->source > 0);
+        }
+    }
+    return ff_filter_frame(outlink, frame);
+}
+
+static const AVFilterPad codecview_inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_VIDEO,
+        .filter_frame   = filter_frame,
+        .needs_writable = 1,
+     },
+     { NULL }
+};
+
+static const AVFilterPad codecview_outputs[] = {
+     {
+         .name = "default",
+         .type = AVMEDIA_TYPE_VIDEO,
+     },
+     { NULL }
+};
+
+AVFilter ff_vf_codecview = {
+    .name          = "codecview",
+    .description   = NULL_IF_CONFIG_SMALL("Visualize information about some codecs"),
+    .priv_size     = sizeof(CodecViewContext),
+    .query_formats = query_formats,
+    .inputs        = codecview_inputs,
+    .outputs       = codecview_outputs,
+    .priv_class    = &codecview_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
-- 
2.0.4



More information about the ffmpeg-devel mailing list