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 "formats.h"
31 #include "video.h"
32 #include <quirc.h>
33 
34 typedef struct QuircContext {
35  const AVClass *class;
36 
37  struct quirc *quirc;
38 } QuircContext;
39 
41 {
42  QuircContext *quirc = ctx->priv;
43 
44  quirc->quirc = quirc_new();
45  if (!quirc->quirc) {
46  return AVERROR(ENOMEM);
47  }
48 
49  return 0;
50 }
51 
53 {
54  QuircContext *quirc = ctx->priv;
55 
56  quirc_destroy(quirc->quirc);
57 }
58 
60 {
61  AVFilterContext *ctx = inlink->dst;
62  QuircContext *quirc = ctx->priv;
63  int err;
64 
65  err = quirc_resize(quirc->quirc, inlink->w, inlink->h);
66  if (err == -1) {
67  return AVERROR(ENOMEM);
68  }
69 
70  return 0;
71 }
72 
74 {
75  static const enum AVPixelFormat pix_fmts[] = {
85  };
86 
88 }
89 
91 {
92  AVFilterContext *ctx = inlink->dst;
93  AVFilterLink *outlink = ctx->outputs[0];
94  QuircContext *quirc = ctx->priv;
95  int codes_count;
96  uint8_t *image;
97 
98  /* copy input image to quirc buffer */
99  image = quirc_begin(quirc->quirc, NULL, NULL);
100  av_image_copy_plane(image, inlink->w,
101  frame->data[0], frame->linesize[0], inlink->w, inlink->h);
102 
103  quirc_end(quirc->quirc);
104 
105  codes_count = quirc_count(quirc->quirc);
107  "Found count %d codes in image #%ld\n", codes_count, inlink->frame_count_out);
108 
109  if (codes_count) {
110  int i, j;
111  AVDictionary **metadata = &frame->metadata;
112 
113  av_dict_set_int(metadata, "lavfi.quirc.count", codes_count, 0);
114 
115  for (i = 0; i < codes_count; i++) {
116  struct quirc_code code;
117  struct quirc_data data;
118  quirc_decode_error_t err;
119  char metadata_key[64];
120 
121  quirc_extract(quirc->quirc, i, &code);
122 
123  err = quirc_decode(&code, &data);
124  if (err) {
126  "Failed to decode image: %s\n", quirc_strerror(err));
127  continue;
128  }
129 
130  for (j = 0; j < 4; j++) {
131  struct quirc_point corner = code.corners[j];
132 
133 #define SET_CORNER_METADATA(key_, value_) \
134  snprintf(metadata_key, sizeof(metadata_key)-1, \
135  "lavfi.quirc.%d.corner.%d." #key_, i, j); \
136  av_dict_set_int(metadata, metadata_key, value_, 0)
137 
138  SET_CORNER_METADATA(x, corner.x);
139  SET_CORNER_METADATA(y, corner.y);
140  }
141 
142  snprintf(metadata_key, sizeof(metadata_key)-1, "lavfi.quirc.%d.payload", i); \
143  av_dict_set(metadata, metadata_key, data.payload, 0);
144 
146  "Found QR code at position %d,%d - %d,%d with payload: %s\n",
147  code.corners[0].x, code.corners[0].y,
148  code.corners[3].x, code.corners[3].y, data.payload);
149  }
150  }
151 
152  return ff_filter_frame(outlink, frame);
153 }
154 
155 static const AVClass quirc_class = {
156  .class_name = "quirc",
157  .version = LIBAVUTIL_VERSION_INT,
158  .category = AV_CLASS_CATEGORY_FILTER
159 };
160 
161 static const AVFilterPad inputs[] = {
162  {
163  .name = "default",
164  .type = AVMEDIA_TYPE_VIDEO,
165  .filter_frame = filter_frame,
166  .config_props = config_input
167  },
168 };
169 
171  .name = "quirc",
172  .description = NULL_IF_CONFIG_SMALL("Decode and show QR codes content."),
173  .priv_size = sizeof(QuircContext),
174  .priv_class = &quirc_class,
175  .init = init,
176  .uninit = uninit,
182 };
183 
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
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
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
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
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
inputs
static const AVFilterPad inputs[]
Definition: vf_quirc.c:161
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
data
const char data[16]
Definition: mxf.c:148
quirc_class
static const AVClass quirc_class
Definition: vf_quirc.c:155
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_quirc.c:40
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
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:170
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
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:37
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
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
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:873
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:304
ctx
AVFormatContext * ctx
Definition: movenc.c:48
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_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
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
frame
static AVFrame * frame
Definition: demux_decode.c:54
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:90
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
AV_CLASS_CATEGORY_FILTER
@ AV_CLASS_CATEGORY_FILTER
Definition: log.h:36
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_quirc.c:52
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_quirc.c:59
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:106
ff_vf_quirc
const AVFilter ff_vf_quirc
Definition: vf_quirc.c:170
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
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:147
SET_CORNER_METADATA
#define SET_CORNER_METADATA(key_, value_)
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
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: internal.h:39
AVFilter
Filter definition.
Definition: avfilter.h:166
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:71
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
avfilter.h
AVFrame::metadata
AVDictionary * metadata
metadata.
Definition: frame.h:662
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:133
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:407
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
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
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_quirc.c:73
QuircContext
Definition: vf_quirc.c:34
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
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
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
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