2 * Copyright (c) 2017 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
23 * Filter for reading closed captioning data (EIA-608).
24 * See also https://en.wikipedia.org/wiki/EIA-608
29 #include "libavutil/internal.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/timestamp.h"
43 typedef struct LineItem {
53 typedef struct CodeItem {
58 typedef struct ReadEIA608Context {
68 uint64_t histogram[256];
74 #define OFFSET(x) offsetof(ReadEIA608Context, x)
75 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
77 static const AVOption readeia608_options[] = {
78 { "scan_min", "set from which line to scan for codes", OFFSET(start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
79 { "scan_max", "set to which line to scan for codes", OFFSET(end), AV_OPT_TYPE_INT, {.i64=29}, 0, INT_MAX, FLAGS },
80 { "spw", "set ratio of width reserved for sync code detection", OFFSET(spw), AV_OPT_TYPE_FLOAT, {.dbl=.27}, 0.1, 0.7, FLAGS },
81 { "chp", "check and apply parity bit", OFFSET(chp), AV_OPT_TYPE_BOOL, {.i64= 0}, 0, 1, FLAGS },
82 { "lp", "lowpass line prior to processing", OFFSET(lp), AV_OPT_TYPE_BOOL, {.i64= 1}, 0, 1, FLAGS },
86 AVFILTER_DEFINE_CLASS(readeia608);
88 static int query_formats(AVFilterContext *ctx)
90 static const enum AVPixelFormat pixel_fmts[] = {
92 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
93 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
94 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
95 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
96 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
100 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
102 return AVERROR(ENOMEM);
103 return ff_set_common_formats(ctx, formats);
106 static int config_input(AVFilterLink *inlink)
108 AVFilterContext *ctx = inlink->dst;
109 ReadEIA608Context *s = ctx->priv;
110 int size = inlink->w + LAG;
112 if (s->end >= inlink->h) {
113 av_log(ctx, AV_LOG_WARNING, "Last line to scan too large, clipping.\n");
114 s->end = inlink->h - 1;
117 if (s->start > s->end) {
118 av_log(ctx, AV_LOG_ERROR, "Invalid range.\n");
119 return AVERROR(EINVAL);
122 s->line = av_calloc(size, sizeof(*s->line));
123 s->code = av_calloc(size, sizeof(*s->code));
124 if (!s->line || !s->code)
125 return AVERROR(ENOMEM);
130 static void build_histogram(ReadEIA608Context *s, const LineItem *line, int len)
132 memset(s->histogram, 0, sizeof(s->histogram));
134 for (int i = LAG; i < len + LAG; i++)
135 s->histogram[line[i].input]++;
138 static void find_black_and_white(ReadEIA608Context *s)
140 int start = 0, end = 0, middle;
141 int black = 0, white = 0;
144 for (int i = 0; i < 256; i++) {
145 if (s->histogram[i]) {
151 for (int i = 255; i >= 0; i--) {
152 if (s->histogram[i]) {
158 middle = start + (end - start) / 2;
161 for (int i = start; i <= middle; i++) {
162 if (s->histogram[i] > cnt) {
163 cnt = s->histogram[i];
169 for (int i = end; i >= middle; i--) {
170 if (s->histogram[i] > cnt) {
171 cnt = s->histogram[i];
180 static float meanf(const LineItem *line, int len)
182 float sum = 0.0, mean = 0.0;
184 for (int i = 0; i < len; i++)
185 sum += line[i].filtered;
192 static float stddevf(const LineItem *line, int len)
194 float m = meanf(line, len);
195 float standard_deviation = 0.f;
197 for (int i = 0; i < len; i++)
198 standard_deviation += (line[i].filtered - m) * (line[i].filtered - m);
200 return sqrtf(standard_deviation / (len - 1));
203 static void thresholding(ReadEIA608Context *s, LineItem *line,
204 int lag, float threshold, float influence, int len)
206 for (int i = lag; i < len + lag; i++) {
207 line[i].unfiltered = line[i].input / 255.f;
208 line[i].filtered = line[i].unfiltered;
211 for (int i = 0; i < lag; i++) {
212 line[i].unfiltered = meanf(line, len * s->spw);
213 line[i].filtered = line[i].unfiltered;
216 line[lag - 1].average = meanf(line, lag);
217 line[lag - 1].deviation = stddevf(line, lag);
219 for (int i = lag; i < len + lag; i++) {
220 if (fabsf(line[i].unfiltered - line[i-1].average) > threshold * line[i-1].deviation) {
221 if (line[i].unfiltered > line[i-1].average) {
222 line[i].output = 255;
227 line[i].filtered = influence * line[i].unfiltered + (1.f - influence) * line[i-1].filtered;
229 int distance_from_black, distance_from_white;
231 distance_from_black = FFABS(line[i].input - s->black);
232 distance_from_white = FFABS(line[i].input - s->white);
234 line[i].output = distance_from_black <= distance_from_white ? 0 : 255;
237 line[i].average = meanf(line + i - lag, lag);
238 line[i].deviation = stddevf(line + i - lag, lag);
242 static int periods(const LineItem *line, CodeItem *code, int len)
244 int hold = line[LAG].output, cnt = 0;
247 memset(code, 0, len * sizeof(*code));
249 for (int i = LAG + 1; i < len + LAG; i++) {
250 if (line[i].output != hold) {
251 code[cnt].size = i - last;
252 code[cnt].bit = hold;
253 hold = line[i].output;
259 code[cnt].size = LAG + len - last;
260 code[cnt].bit = hold;
265 static void dump_code(AVFilterContext *ctx, int len, int item)
267 ReadEIA608Context *s = ctx->priv;
269 av_log(ctx, AV_LOG_DEBUG, "%d:", item);
270 for (int i = 0; i < len; i++) {
271 av_log(ctx, AV_LOG_DEBUG, " %03d", s->code[i].size);
273 av_log(ctx, AV_LOG_DEBUG, "\n");
276 static void extract_line(AVFilterContext *ctx, AVFrame *in, int w, int nb_line)
278 ReadEIA608Context *s = ctx->priv;
279 LineItem *line = s->line;
282 uint8_t byte[2] = { 0 };
283 uint8_t codes[19] = { 0 };
284 float bit_size = 0.f;
287 memset(line, 0, (w + LAG) * sizeof(*line));
289 src = &in->data[0][nb_line * in->linesize[0]];
291 for (i = 0; i < w; i++) {
292 int a = FFMAX(i - 3, 0);
293 int b = FFMAX(i - 2, 0);
294 int c = FFMAX(i - 1, 0);
295 int d = FFMIN(i + 3, w-1);
296 int e = FFMIN(i + 2, w-1);
297 int f = FFMIN(i + 1, w-1);
299 line[LAG + i].input = (src[a] + src[b] + src[c] + src[i] + src[d] + src[e] + src[f] + 6) / 7;
302 for (i = 0; i < w; i++) {
303 line[LAG + i].input = src[i];
307 build_histogram(s, line, w);
308 find_black_and_white(s);
309 if (s->white - s->black < 5)
312 thresholding(s, line, LAG, 1, 0, w);
313 len = periods(line, s->code, w);
314 dump_code(ctx, len, nb_line);
316 s->code[14].bit != 0 ||
317 w / (float)s->code[14].size < SYNC_MIN ||
318 w / (float)s->code[14].size > SYNC_MAX) {
322 for (i = 14; i < len; i++) {
323 bit_size += s->code[i].size;
327 for (i = 1; i < 14; i++) {
328 if (s->code[i].size > bit_size * 1.5f) {
333 if (s->code[15].size / bit_size < 0.45f) {
337 for (j = 0, i = 14; i < len; i++) {
340 run = lrintf(s->code[i].size / bit_size);
341 bit = s->code[i].bit;
343 for (int k = 0; j < 19 && k < run; k++) {
351 for (ch = 0; ch < 2; ch++) {
352 for (parity = 0, i = 0; i < 8; i++) {
353 int b = codes[3 + ch * 8 + i];
372 uint8_t key[128], value[128];
374 //snprintf(key, sizeof(key), "lavfi.readeia608.%d.bits", s->nb_found);
375 //snprintf(value, sizeof(value), "0b%d%d%d%d%d%d%d%d 0b%d%d%d%d%d%d%d%d", codes[3]==255,codes[4]==255,codes[5]==255,codes[6]==255,codes[7]==255,codes[8]==255,codes[9]==255,codes[10]==255,codes[11]==255,codes[12]==255,codes[13]==255,codes[14]==255,codes[15]==255,codes[16]==255,codes[17]==255,codes[18]==255);
376 //av_dict_set(&in->metadata, key, value, 0);
378 snprintf(key, sizeof(key), "lavfi.readeia608.%d.cc", s->nb_found);
379 snprintf(value, sizeof(value), "0x%02X%02X", byte[0], byte[1]);
380 av_dict_set(&in->metadata, key, value, 0);
382 snprintf(key, sizeof(key), "lavfi.readeia608.%d.line", s->nb_found);
383 snprintf(value, sizeof(value), "%d", nb_line);
384 av_dict_set(&in->metadata, key, value, 0);
390 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
392 AVFilterContext *ctx = inlink->dst;
393 AVFilterLink *outlink = ctx->outputs[0];
394 ReadEIA608Context *s = ctx->priv;
398 for (i = s->start; i <= s->end; i++)
399 extract_line(ctx, in, inlink->w, i);
401 return ff_filter_frame(outlink, in);
404 static av_cold void uninit(AVFilterContext *ctx)
406 ReadEIA608Context *s = ctx->priv;
412 static const AVFilterPad readeia608_inputs[] = {
415 .type = AVMEDIA_TYPE_VIDEO,
416 .filter_frame = filter_frame,
417 .config_props = config_input,
422 static const AVFilterPad readeia608_outputs[] = {
425 .type = AVMEDIA_TYPE_VIDEO,
430 AVFilter ff_vf_readeia608 = {
431 .name = "readeia608",
432 .description = NULL_IF_CONFIG_SMALL("Read EIA-608 Closed Caption codes from input video and write them to frame metadata."),
433 .priv_size = sizeof(ReadEIA608Context),
434 .priv_class = &readeia608_class,
435 .query_formats = query_formats,
436 .inputs = readeia608_inputs,
437 .outputs = readeia608_outputs,
439 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,