FFmpeg
vf_ocr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <tesseract/capi.h>
22 
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 typedef struct OCRContext {
30  const AVClass *class;
31 
32  char *datapath;
33  char *language;
34  char *whitelist;
35  char *blacklist;
36 
37  TessBaseAPI *tess;
38 } OCRContext;
39 
40 #define OFFSET(x) offsetof(OCRContext, x)
41 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
42 
43 static const AVOption ocr_options[] = {
44  { "datapath", "set datapath", OFFSET(datapath), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
45  { "language", "set language", OFFSET(language), AV_OPT_TYPE_STRING, {.str="eng"}, 0, 0, FLAGS },
46  { "whitelist", "set character whitelist", OFFSET(whitelist), AV_OPT_TYPE_STRING, {.str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:;,-+_!?\"'[]{}()<>|/\\=*&%$#@!~ "}, 0, 0, FLAGS },
47  { "blacklist", "set character blacklist", OFFSET(blacklist), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, FLAGS },
48  { NULL }
49 };
50 
52 {
53  OCRContext *s = ctx->priv;
54 
55  s->tess = TessBaseAPICreate();
56  if (TessBaseAPIInit3(s->tess, s->datapath, s->language) == -1) {
57  av_log(ctx, AV_LOG_ERROR, "failed to init tesseract\n");
58  return AVERROR(EINVAL);
59  }
60 
61  if (!TessBaseAPISetVariable(s->tess, "tessedit_char_whitelist", s->whitelist)) {
62  av_log(ctx, AV_LOG_ERROR, "failed to set whitelist\n");
63  return AVERROR(EINVAL);
64  }
65 
66  if (!TessBaseAPISetVariable(s->tess, "tessedit_char_blacklist", s->blacklist)) {
67  av_log(ctx, AV_LOG_ERROR, "failed to set blacklist\n");
68  return AVERROR(EINVAL);
69  }
70 
71  av_log(ctx, AV_LOG_DEBUG, "Tesseract version: %s\n", TessVersion());
72 
73  return 0;
74 }
75 
76 static const enum AVPixelFormat pix_fmts[] = {
86 };
87 
89 {
90  AVDictionary **metadata = &in->metadata;
91  AVFilterContext *ctx = inlink->dst;
92  AVFilterLink *outlink = ctx->outputs[0];
93  OCRContext *s = ctx->priv;
94  char *result;
95  int *confs;
96 
97  result = TessBaseAPIRect(s->tess, in->data[0], 1,
98  in->linesize[0], 0, 0, in->width, in->height);
99  confs = TessBaseAPIAllWordConfidences(s->tess);
100  av_dict_set(metadata, "lavfi.ocr.text", result, 0);
101  for (int i = 0; confs[i] != -1; i++) {
102  char number[256];
103 
104  snprintf(number, sizeof(number), "%d ", confs[i]);
105  av_dict_set(metadata, "lavfi.ocr.confidence", number, AV_DICT_APPEND);
106  }
107 
108  TessDeleteText(result);
109  TessDeleteIntArray(confs);
110 
111  return ff_filter_frame(outlink, in);
112 }
113 
115 {
116  OCRContext *s = ctx->priv;
117 
118  TessBaseAPIEnd(s->tess);
119  TessBaseAPIDelete(s->tess);
120 }
121 
123 
124 static const AVFilterPad ocr_inputs[] = {
125  {
126  .name = "default",
127  .type = AVMEDIA_TYPE_VIDEO,
128  .filter_frame = filter_frame,
129  },
130 };
131 
132 static const AVFilterPad ocr_outputs[] = {
133  {
134  .name = "default",
135  .type = AVMEDIA_TYPE_VIDEO,
136  },
137 };
138 
140  .name = "ocr",
141  .description = NULL_IF_CONFIG_SMALL("Optical Character Recognition."),
142  .priv_size = sizeof(OCRContext),
143  .priv_class = &ocr_class,
144  .init = init,
145  .uninit = uninit,
150 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
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
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_ocr.c:51
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:174
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
OCRContext::tess
TessBaseAPI * tess
Definition: vf_ocr.c:37
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_ocr.c:114
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVFrame::width
int width
Definition: frame.h:402
AVOption
AVOption.
Definition: opt.h:251
AV_DICT_APPEND
#define AV_DICT_APPEND
If the entry already exists, append to it.
Definition: dict.h:82
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
AVDictionary
Definition: dict.c:32
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
video.h
FLAGS
#define FLAGS
Definition: vf_ocr.c:41
ocr_outputs
static const AVFilterPad ocr_outputs[]
Definition: vf_ocr.c:132
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:351
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(ocr)
formats.h
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
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:276
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
OCRContext::datapath
char * datapath
Definition: vf_ocr.c:32
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:79
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
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:66
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
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:80
OCRContext::blacklist
char * blacklist
Definition: vf_ocr.c:35
ocr_inputs
static const AVFilterPad ocr_inputs[]
Definition: vf_ocr.c:124
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
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:78
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
ff_vf_ocr
const AVFilter ff_vf_ocr
Definition: vf_ocr.c:139
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:115
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_ocr.c:88
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:167
OCRContext
Definition: vf_ocr.c:29
internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
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:100
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_ocr.c:76
AVFilter
Filter definition.
Definition: avfilter.h:161
language
Undefined Behavior In the C language
Definition: undefined.txt:3
OCRContext::whitelist
char * whitelist
Definition: vf_ocr.c:34
OFFSET
#define OFFSET(x)
Definition: vf_ocr.c:40
OCRContext::language
char * language
Definition: vf_ocr.c:33
AVFrame::height
int height
Definition: frame.h:402
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
AVFrame::metadata
AVDictionary * metadata
metadata.
Definition: frame.h:639
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
ocr_options
static const AVOption ocr_options[]
Definition: vf_ocr.c:43
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:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
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:70
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:86
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:73
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
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:375
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:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
snprintf
#define snprintf
Definition: snprintf.h:34
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:166