blob: 870dd688410e5821e4043aeb68472a8bc4ca5a20
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 | |
51 | static av_cold int init(AVFilterContext *ctx) |
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 int query_formats(AVFilterContext *ctx) |
77 | { |
78 | static const enum AVPixelFormat pix_fmts[] = { |
79 | AV_PIX_FMT_GRAY8, |
80 | AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
81 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
82 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
83 | AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
84 | AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
85 | AV_PIX_FMT_YUVJ411P, |
86 | AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P, |
87 | AV_PIX_FMT_NONE |
88 | }; |
89 | |
90 | AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); |
91 | if (!fmts_list) |
92 | return AVERROR(ENOMEM); |
93 | ff_set_common_formats(ctx, fmts_list); |
94 | |
95 | return 0; |
96 | } |
97 | |
98 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
99 | { |
100 | AVDictionary **metadata = avpriv_frame_get_metadatap(in); |
101 | AVFilterContext *ctx = inlink->dst; |
102 | AVFilterLink *outlink = ctx->outputs[0]; |
103 | OCRContext *s = ctx->priv; |
104 | char *result; |
105 | |
106 | result = TessBaseAPIRect(s->tess, in->data[0], 1, |
107 | in->linesize[0], 0, 0, in->width, in->height); |
108 | av_dict_set(metadata, "lavfi.ocr.text", result, 0); |
109 | TessDeleteText(result); |
110 | |
111 | return ff_filter_frame(outlink, in); |
112 | } |
113 | |
114 | static av_cold void uninit(AVFilterContext *ctx) |
115 | { |
116 | OCRContext *s = ctx->priv; |
117 | |
118 | TessBaseAPIEnd(s->tess); |
119 | TessBaseAPIDelete(s->tess); |
120 | } |
121 | |
122 | AVFILTER_DEFINE_CLASS(ocr); |
123 | |
124 | static const AVFilterPad ocr_inputs[] = { |
125 | { |
126 | .name = "default", |
127 | .type = AVMEDIA_TYPE_VIDEO, |
128 | .filter_frame = filter_frame, |
129 | }, |
130 | { NULL } |
131 | }; |
132 | |
133 | static const AVFilterPad ocr_outputs[] = { |
134 | { |
135 | .name = "default", |
136 | .type = AVMEDIA_TYPE_VIDEO, |
137 | }, |
138 | { NULL } |
139 | }; |
140 | |
141 | AVFilter ff_vf_ocr = { |
142 | .name = "ocr", |
143 | .description = NULL_IF_CONFIG_SMALL("Optical Character Recognition."), |
144 | .priv_size = sizeof(OCRContext), |
145 | .priv_class = &ocr_class, |
146 | .query_formats = query_formats, |
147 | .init = init, |
148 | .uninit = uninit, |
149 | .inputs = ocr_inputs, |
150 | .outputs = ocr_outputs, |
151 | }; |
152 |