[FFmpeg-cvslog] lavfi: add quirc filter

Stefano Sabatini git at videolan.org
Tue Jan 2 23:05:50 EET 2024


ffmpeg | branch: master | Stefano Sabatini <stefasab at gmail.com> | Tue Dec 26 16:19:10 2023 +0100| [030e1401451200566a5303f35cbe1456e31dd81e] | committer: Stefano Sabatini

lavfi: add quirc filter

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=030e1401451200566a5303f35cbe1456e31dd81e
---

 Changelog                |   1 +
 configure                |   4 ++
 doc/filters.texi         |  28 ++++++++
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/vf_quirc.c   | 183 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 218 insertions(+)

diff --git a/Changelog b/Changelog
index b483bb9c69..424bfc11af 100644
--- a/Changelog
+++ b/Changelog
@@ -14,6 +14,7 @@ version <next>:
 - D3D12VA hardware accelerated H264, HEVC, VP9, AV1, MPEG-2 and VC1 decoding
 - tiltandshift filter
 - qrencode filter and qrencodesrc source
+- quirc filter
 
 version 6.1:
 - libaribcaption decoder
diff --git a/configure b/configure
index b1853204ef..dc400db176 100755
--- a/configure
+++ b/configure
@@ -257,6 +257,7 @@ External library support:
   --enable-libplacebo      enable libplacebo library [no]
   --enable-libpulse        enable Pulseaudio input via libpulse [no]
   --enable-libqrencode     enable QR encode generation via libqrencode [no]
+  --enable-libquirc        enable QR decoding via libquirc [no]
   --enable-librabbitmq     enable RabbitMQ library [no]
   --enable-librav1e        enable AV1 encoding via rav1e [no]
   --enable-librist         enable RIST via librist [no]
@@ -1883,6 +1884,7 @@ EXTERNAL_LIBRARY_LIST="
     libplacebo
     libpulse
     libqrencode
+    libquirc
     librabbitmq
     librav1e
     librist
@@ -3793,6 +3795,7 @@ ocv_filter_deps="libopencv"
 openclsrc_filter_deps="opencl"
 qrencode_filter_deps="libqrencode"
 qrencodesrc_filter_deps="libqrencode"
+quirc_filter_deps="libquirc"
 overlay_opencl_filter_deps="opencl"
 overlay_qsv_filter_deps="libmfx"
 overlay_qsv_filter_select="qsvvpp"
@@ -6845,6 +6848,7 @@ enabled libopus           && {
 enabled libplacebo        && require_pkg_config libplacebo "libplacebo >= 4.192.0" libplacebo/vulkan.h pl_vulkan_create
 enabled libpulse          && require_pkg_config libpulse libpulse pulse/pulseaudio.h pa_context_new
 enabled libqrencode       && require_pkg_config libqrencode libqrencode qrencode.h QRcode_encodeString
+enabled libquirc          && require libquirc quirc.h quirc_decode -lquirc
 enabled librabbitmq       && require_pkg_config librabbitmq "librabbitmq >= 0.7.1" amqp.h amqp_new_connection
 enabled librav1e          && require_pkg_config librav1e "rav1e >= 0.5.0" rav1e.h rav1e_context_new
 enabled librist           && require_pkg_config librist "librist >= 0.2.7" librist/librist.h rist_receiver_create
diff --git a/doc/filters.texi b/doc/filters.texi
index 9b120ce0a1..f54359d533 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -20392,6 +20392,34 @@ qrencode=text=%@{pts@}
 
 @end itemize
 
+ at section quirc
+
+Identify and decode a QR code using the libquirc library (see
+ at url{https://github.com/dlbeer/quirc/}), and print the identified QR codes
+positions and payload as metadata.
+
+To enable the compilation of this filter, you need to configure FFmpeg with with
+ at code{--enable-libquirc}.
+
+For each found QR code in the input video, some metadata entries are added with
+the prefix @var{lavfi.quirc.N}, where @var{N} is the index, starting from 0,
+associated to the QR code.
+
+A description of each metadata value follows:
+
+ at table @option
+ at item lavfi.quirc.count
+the number of found QR codes, it is not set in case none was found
+
+ at item lavfi.quirc.N.corner.M.x
+ at item lavfi.quirc.N.coreer.M.y
+the x/y positions of the four corners of the square containing the QR code,
+where @var{M} is the index of the corner starting from 0
+
+ at item lavfi.quirc.N.payload
+the payload of the QR code
+ at end table
+
 @section random
 
 Flush video frames from internal cache of frames into a random order.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 31371ceb1a..f65fb9a5a7 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -437,6 +437,7 @@ OBJS-$(CONFIG_PSEUDOCOLOR_FILTER)            += vf_pseudocolor.o
 OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o framesync.o
 OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
 OBJS-$(CONFIG_QP_FILTER)                     += vf_qp.o
+OBJS-$(CONFIG_QUIRC_FILTER)                  += vf_quirc.o
 OBJS-$(CONFIG_RANDOM_FILTER)                 += vf_random.o
 OBJS-$(CONFIG_READEIA608_FILTER)             += vf_readeia608.o
 OBJS-$(CONFIG_READVITC_FILTER)               += vf_readvitc.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 20feb37967..b8570dbab2 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -412,6 +412,7 @@ extern const AVFilter ff_vf_psnr;
 extern const AVFilter ff_vf_pullup;
 extern const AVFilter ff_vf_qp;
 extern const AVFilter ff_vf_qrencode;
+extern const AVFilter ff_vf_quirc;
 extern const AVFilter ff_vf_random;
 extern const AVFilter ff_vf_readeia608;
 extern const AVFilter ff_vf_readvitc;
diff --git a/libavfilter/vf_quirc.c b/libavfilter/vf_quirc.c
new file mode 100644
index 0000000000..62eb29b7ce
--- /dev/null
+++ b/libavfilter/vf_quirc.c
@@ -0,0 +1,183 @@
+/*
+ * 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 QR decoder video filter
+ *
+ * Use libquirc library to decode the content of QR codes, and put the decoded
+ * content to metadata. See:
+ * https://github.com/dlbeer/quirc
+ */
+
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "video.h"
+#include <quirc.h>
+
+typedef struct QuircContext {
+    const AVClass *class;
+
+    struct quirc *quirc;
+} QuircContext;
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    QuircContext *quirc = ctx->priv;
+
+    quirc->quirc = quirc_new();
+    if (!quirc->quirc) {
+        return AVERROR(ENOMEM);
+    }
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    QuircContext *quirc = ctx->priv;
+
+    quirc_destroy(quirc->quirc);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    QuircContext *quirc = ctx->priv;
+    int err;
+
+    err = quirc_resize(quirc->quirc, inlink->w, inlink->h);
+    if (err == -1) {
+        return AVERROR(ENOMEM);
+    }
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_GRAY8,
+        AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
+        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
+        AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
+        AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
+        AV_PIX_FMT_YUVJ440P,
+        AV_PIX_FMT_NONE
+    };
+
+    return ff_set_common_formats_from_list(ctx, pix_fmts);
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    QuircContext *quirc = ctx->priv;
+    int codes_count;
+    uint8_t *image;
+
+    /* copy input image to quirc buffer */
+    image = quirc_begin(quirc->quirc, NULL, NULL);
+    av_image_copy_plane(image, inlink->w,
+                        frame->data[0], frame->linesize[0], inlink->w, inlink->h);
+
+    quirc_end(quirc->quirc);
+
+    codes_count = quirc_count(quirc->quirc);
+    av_log(ctx, AV_LOG_VERBOSE,
+           "Found count %d codes in image #%ld\n", codes_count, inlink->frame_count_out);
+
+    if (codes_count) {
+        int i, j;
+        AVDictionary **metadata = &frame->metadata;
+
+        av_dict_set_int(metadata, "lavfi.quirc.count", codes_count, 0);
+
+        for (i = 0; i < codes_count; i++) {
+            struct quirc_code code;
+            struct quirc_data data;
+            quirc_decode_error_t err;
+            char metadata_key[64];
+
+            quirc_extract(quirc->quirc, i, &code);
+
+            err = quirc_decode(&code, &data);
+            if (err) {
+                av_log(ctx, AV_LOG_WARNING,
+                       "Failed to decode image: %s\n", quirc_strerror(err));
+                continue;
+            }
+
+            for (j = 0; j < 4; j++) {
+                struct quirc_point corner = code.corners[j];
+
+#define SET_CORNER_METADATA(key_, value_)                               \
+                snprintf(metadata_key, sizeof(metadata_key)-1,          \
+                         "lavfi.quirc.%d.corner.%d." #key_, i, j);      \
+                av_dict_set_int(metadata, metadata_key, value_, 0)
+
+                SET_CORNER_METADATA(x, corner.x);
+                SET_CORNER_METADATA(y, corner.y);
+            }
+
+            snprintf(metadata_key, sizeof(metadata_key)-1, "lavfi.quirc.%d.payload", i); \
+            av_dict_set(metadata, metadata_key, data.payload, 0);
+
+            av_log(ctx, AV_LOG_INFO,
+                   "Found QR code at position %d,%d - %d,%d with payload: %s\n",
+                   code.corners[0].x, code.corners[0].y,
+                   code.corners[3].x, code.corners[3].y, data.payload);
+        }
+    }
+
+    return ff_filter_frame(outlink, frame);
+}
+
+static const AVClass quirc_class = {
+    .class_name = "quirc",
+    .version    = LIBAVUTIL_VERSION_INT,
+    .category   = AV_CLASS_CATEGORY_FILTER
+};
+
+static const AVFilterPad inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input
+    },
+};
+
+const AVFilter ff_vf_quirc = {
+    .name        = "quirc",
+    .description = NULL_IF_CONFIG_SMALL("Decode and show QR codes content."),
+    .priv_size   = sizeof(QuircContext),
+    .priv_class  = &quirc_class,
+    .init        = init,
+    .uninit      = uninit,
+    FILTER_INPUTS(inputs),
+    FILTER_OUTPUTS(ff_video_default_filterpad),
+    FILTER_QUERY_FUNC(query_formats),
+    .flags       = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
+                   AVFILTER_FLAG_METADATA_ONLY,
+};
+



More information about the ffmpeg-cvslog mailing list