FFmpeg
vf_quirc.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file QR decoder video filter
21  *
22  * Use libquirc library to decode the content of QR codes, and put the decoded
23  * content to metadata. See:
24  * https://github.com/dlbeer/quirc
25  */
26 
27 #include "libavutil/imgutils.h"
28 #include "libavutil/opt.h"
29 #include "avfilter.h"
30 #include "filters.h"
31 #include "formats.h"
32 #include "video.h"
33 #include <quirc.h>
34 
35 typedef struct QuircContext {
36  const AVClass *class;
37 
38  struct quirc *quirc;
39 } QuircContext;
40 
42 {
43  QuircContext *quirc = ctx->priv;
44 
45  quirc->quirc = quirc_new();
46  if (!quirc->quirc) {
47  return AVERROR(ENOMEM);
48  }
49 
50  return 0;
51 }
52 
54 {
55  QuircContext *quirc = ctx->priv;
56 
57  quirc_destroy(quirc->quirc);
58 }
59 
61 {
62  AVFilterContext *ctx = inlink->dst;
63  QuircContext *quirc = ctx->priv;
64  int err;
65 
66  err = quirc_resize(quirc->quirc, inlink->w, inlink->h);
67  if (err == -1) {
68  return AVERROR(ENOMEM);
69  }
70 
71  return 0;
72 }
73 
75 {
77  AVFilterContext *ctx = inlink->dst;
78  AVFilterLink *outlink = ctx->outputs[0];
79  QuircContext *quirc = ctx->priv;
80  int codes_count;
81  uint8_t *image;
82 
83  /* copy input image to quirc buffer */
84  image = quirc_begin(quirc->quirc, NULL, NULL);
85  av_image_copy_plane(image, inlink->w,
86  frame->data[0], frame->linesize[0], inlink->w, inlink->h);
87 
88  quirc_end(quirc->quirc);
89 
90  codes_count = quirc_count(quirc->quirc);
92  "Found count %d codes in image #%ld\n", codes_count, inl->frame_count_out);
93 
94  if (codes_count) {
95  int i, j;
96  AVDictionary **metadata = &frame->metadata;
97 
98  av_dict_set_int(metadata, "lavfi.quirc.count", codes_count, 0);
99 
100  for (i = 0; i < codes_count; i++) {
101  struct quirc_code code;
102  struct quirc_data data;
103  quirc_decode_error_t err;
104  char metadata_key[64];
105 
106  quirc_extract(quirc->quirc, i, &code);
107 
108  err = quirc_decode(&code, &data);
109  if (err) {
111  "Failed to decode image: %s\n", quirc_strerror(err));
112  continue;
113  }
114 
115  for (j = 0; j < 4; j++) {
116  struct quirc_point corner = code.corners[j];
117 
118 #define SET_CORNER_METADATA(key_, value_) \
119  snprintf(metadata_key, sizeof(metadata_key)-1, \
120  "lavfi.quirc.%d.corner.%d." #key_, i, j); \
121  av_dict_set_int(metadata, metadata_key, value_, 0)
122 
123  SET_CORNER_METADATA(x, corner.x);
124  SET_CORNER_METADATA(y, corner.y);
125  }
126 
127  snprintf(metadata_key, sizeof(metadata_key)-1, "lavfi.quirc.%d.payload", i); \
128  av_dict_set(metadata, metadata_key, data.payload, 0);
129 
131  "Found QR code at position %d,%d - %d,%d with payload: %s\n",
132  code.corners[0].x, code.corners[0].y,
133  code.corners[3].x, code.corners[3].y, data.payload);
134  }
135  }
136 
137  return ff_filter_frame(outlink, frame);
138 }
139 
140 static const enum AVPixelFormat pix_fmts[] = {
150 };
151 
152 static const AVClass quirc_class = {
153  .class_name = "quirc",
154  .version = LIBAVUTIL_VERSION_INT,
155  .category = AV_CLASS_CATEGORY_FILTER
156 };
157 
158 static const AVFilterPad inputs[] = {
159  {
160  .name = "default",
161  .type = AVMEDIA_TYPE_VIDEO,
162  .filter_frame = filter_frame,
163  .config_props = config_input
164  },
165 };
166 
168  .name = "quirc",
169  .description = NULL_IF_CONFIG_SMALL("Decode and show QR codes content."),
170  .priv_size = sizeof(QuircContext),
171  .priv_class = &quirc_class,
172  .init = init,
173  .uninit = uninit,
179 };
180 
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: filters.h:242
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
inputs
static const AVFilterPad inputs[]
Definition: vf_quirc.c:158
data
const char data[16]
Definition: mxf.c:149
quirc_class
static const AVClass quirc_class
Definition: vf_quirc.c:152
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_quirc.c:41
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:106
AVDictionary
Definition: dict.c:34
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
av_image_copy_plane
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:374
formats.h
QuircContext::quirc
struct quirc * quirc
Definition: vf_quirc.c:38
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AV_PIX_FMT_YUVJ411P
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:283
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:86
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:87
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:85
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_quirc.c:74
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
AV_CLASS_CATEGORY_FILTER
@ AV_CLASS_CATEGORY_FILTER
Definition: log.h:36
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_quirc.c:53
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_quirc.c:60
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ff_vf_quirc
const AVFilter ff_vf_quirc
Definition: vf_quirc.c:167
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:182
SET_CORNER_METADATA
#define SET_CORNER_METADATA(key_, value_)
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
AV_PIX_FMT_YUVJ440P
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:107
AV_PIX_FMT_NV21
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition: pixfmt.h:97
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AVFilter
Filter definition.
Definition: avfilter.h:201
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:168
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
av_dict_set_int
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition: dict.c:167
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_quirc.c:140
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
QuircContext
Definition: vf_quirc.c:35
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:80
imgutils.h
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:79
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
snprintf
#define snprintf
Definition: snprintf.h:34