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"
40 #define CLOCK_BITSIZE_MIN 0.2f
41 #define CLOCK_BITSIZE_MAX 1.5f
42 #define SYNC_BITSIZE_MIN 12.f
43 #define SYNC_BITSIZE_MAX 15.f
45 typedef struct LineItem {
55 typedef struct CodeItem {
60 typedef struct ReadEIA608Context {
70 uint64_t histogram[256];
76 #define OFFSET(x) offsetof(ReadEIA608Context, x)
77 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
79 static const AVOption readeia608_options[] = {
80 { "scan_min", "set from which line to scan for codes", OFFSET(start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
81 { "scan_max", "set to which line to scan for codes", OFFSET(end), AV_OPT_TYPE_INT, {.i64=29}, 0, INT_MAX, FLAGS },
82 { "spw", "set ratio of width reserved for sync code detection", OFFSET(spw), AV_OPT_TYPE_FLOAT, {.dbl=.27}, 0.1, 0.7, FLAGS },
83 { "chp", "check and apply parity bit", OFFSET(chp), AV_OPT_TYPE_BOOL, {.i64= 0}, 0, 1, FLAGS },
84 { "lp", "lowpass line prior to processing", OFFSET(lp), AV_OPT_TYPE_BOOL, {.i64= 1}, 0, 1, FLAGS },
88 AVFILTER_DEFINE_CLASS(readeia608);
90 static int query_formats(AVFilterContext *ctx)
92 static const enum AVPixelFormat pixel_fmts[] = {
94 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
95 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
96 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
97 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
98 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
102 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
104 return AVERROR(ENOMEM);
105 return ff_set_common_formats(ctx, formats);
108 static int config_input(AVFilterLink *inlink)
110 AVFilterContext *ctx = inlink->dst;
111 ReadEIA608Context *s = ctx->priv;
112 int size = inlink->w + LAG;
114 if (s->end >= inlink->h) {
115 av_log(ctx, AV_LOG_WARNING, "Last line to scan too large, clipping.\n");
116 s->end = inlink->h - 1;
119 if (s->start > s->end) {
120 av_log(ctx, AV_LOG_ERROR, "Invalid range.\n");
121 return AVERROR(EINVAL);
124 s->line = av_calloc(size, sizeof(*s->line));
125 s->code = av_calloc(size, sizeof(*s->code));
126 if (!s->line || !s->code)
127 return AVERROR(ENOMEM);
132 static void build_histogram(ReadEIA608Context *s, const LineItem *line, int len)
134 memset(s->histogram, 0, sizeof(s->histogram));
136 for (int i = LAG; i < len + LAG; i++)
137 s->histogram[line[i].input]++;
140 static void find_black_and_white(ReadEIA608Context *s)
142 int start = 0, end = 0, middle;
143 int black = 0, white = 0;
146 for (int i = 0; i < 256; i++) {
147 if (s->histogram[i]) {
153 for (int i = 255; i >= 0; i--) {
154 if (s->histogram[i]) {
160 middle = start + (end - start) / 2;
163 for (int i = start; i <= middle; i++) {
164 if (s->histogram[i] > cnt) {
165 cnt = s->histogram[i];
171 for (int i = end; i >= middle; i--) {
172 if (s->histogram[i] > cnt) {
173 cnt = s->histogram[i];
182 static float meanf(const LineItem *line, int len)
184 float sum = 0.0, mean = 0.0;
186 for (int i = 0; i < len; i++)
187 sum += line[i].filtered;
194 static float stddevf(const LineItem *line, int len)
196 float m = meanf(line, len);
197 float standard_deviation = 0.f;
199 for (int i = 0; i < len; i++)
200 standard_deviation += (line[i].filtered - m) * (line[i].filtered - m);
202 return sqrtf(standard_deviation / (len - 1));
205 static void thresholding(ReadEIA608Context *s, LineItem *line,
206 int lag, float threshold, float influence, int len)
208 for (int i = lag; i < len + lag; i++) {
209 line[i].unfiltered = line[i].input / 255.f;
210 line[i].filtered = line[i].unfiltered;
213 for (int i = 0; i < lag; i++) {
214 line[i].unfiltered = meanf(line, len * s->spw);
215 line[i].filtered = line[i].unfiltered;
218 line[lag - 1].average = meanf(line, lag);
219 line[lag - 1].deviation = stddevf(line, lag);
221 for (int i = lag; i < len + lag; i++) {
222 if (fabsf(line[i].unfiltered - line[i-1].average) > threshold * line[i-1].deviation) {
223 if (line[i].unfiltered > line[i-1].average) {
224 line[i].output = 255;
229 line[i].filtered = influence * line[i].unfiltered + (1.f - influence) * line[i-1].filtered;
231 int distance_from_black, distance_from_white;
233 distance_from_black = FFABS(line[i].input - s->black);
234 distance_from_white = FFABS(line[i].input - s->white);
236 line[i].output = distance_from_black <= distance_from_white ? 0 : 255;
239 line[i].average = meanf(line + i - lag, lag);
240 line[i].deviation = stddevf(line + i - lag, lag);
244 static int periods(const LineItem *line, CodeItem *code, int len)
246 int hold = line[LAG].output, cnt = 0;
249 memset(code, 0, len * sizeof(*code));
251 for (int i = LAG + 1; i < len + LAG; i++) {
252 if (line[i].output != hold) {
253 code[cnt].size = i - last;
254 code[cnt].bit = hold;
255 hold = line[i].output;
261 code[cnt].size = LAG + len - last;
262 code[cnt].bit = hold;
267 static void dump_code(AVFilterContext *ctx, int len, int item)
269 ReadEIA608Context *s = ctx->priv;
271 av_log(ctx, AV_LOG_DEBUG, "%d:", item);
272 for (int i = 0; i < len; i++) {
273 av_log(ctx, AV_LOG_DEBUG, " %03d", s->code[i].size);
275 av_log(ctx, AV_LOG_DEBUG, "\n");
278 static void extract_line(AVFilterContext *ctx, AVFrame *in, int w, int nb_line)
280 ReadEIA608Context *s = ctx->priv;
281 LineItem *line = s->line;
284 uint8_t byte[2] = { 0 };
285 uint8_t codes[19] = { 0 };
286 float bit_size = 0.f;
289 memset(line, 0, (w + LAG) * sizeof(*line));
291 src = &in->data[0][nb_line * in->linesize[0]];
293 for (i = 0; i < w; i++) {
294 int a = FFMAX(i - 3, 0);
295 int b = FFMAX(i - 2, 0);
296 int c = FFMAX(i - 1, 0);
297 int d = FFMIN(i + 3, w-1);
298 int e = FFMIN(i + 2, w-1);
299 int f = FFMIN(i + 1, w-1);
301 line[LAG + i].input = (src[a] + src[b] + src[c] + src[i] + src[d] + src[e] + src[f] + 6) / 7;
304 for (i = 0; i < w; i++) {
305 line[LAG + i].input = src[i];
309 build_histogram(s, line, w);
310 find_black_and_white(s);
311 if (s->white - s->black < 5)
314 thresholding(s, line, LAG, 1, 0, w);
315 len = periods(line, s->code, w);
316 dump_code(ctx, len, nb_line);
318 s->code[14].bit != 0 ||
319 w / (float)s->code[14].size < SYNC_BITSIZE_MIN ||
320 w / (float)s->code[14].size > SYNC_BITSIZE_MAX) {
324 for (i = 14; i < len; i++) {
325 bit_size += s->code[i].size;
329 for (i = 1; i < 14; i++) {
330 if (s->code[i].size / bit_size > CLOCK_BITSIZE_MAX ||
331 s->code[i].size / bit_size < CLOCK_BITSIZE_MIN) {
336 if (s->code[15].size / bit_size < 0.45f) {
340 for (j = 0, i = 14; i < len; i++) {
343 run = lrintf(s->code[i].size / bit_size);
344 bit = s->code[i].bit;
346 for (int k = 0; j < 19 && k < run; k++) {
354 for (ch = 0; ch < 2; ch++) {
355 for (parity = 0, i = 0; i < 8; i++) {
356 int b = codes[3 + ch * 8 + i];
375 uint8_t key[128], value[128];
377 //snprintf(key, sizeof(key), "lavfi.readeia608.%d.bits", s->nb_found);
378 //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);
379 //av_dict_set(&in->metadata, key, value, 0);
381 snprintf(key, sizeof(key), "lavfi.readeia608.%d.cc", s->nb_found);
382 snprintf(value, sizeof(value), "0x%02X%02X", byte[0], byte[1]);
383 av_dict_set(&in->metadata, key, value, 0);
385 snprintf(key, sizeof(key), "lavfi.readeia608.%d.line", s->nb_found);
386 snprintf(value, sizeof(value), "%d", nb_line);
387 av_dict_set(&in->metadata, key, value, 0);
393 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
395 AVFilterContext *ctx = inlink->dst;
396 AVFilterLink *outlink = ctx->outputs[0];
397 ReadEIA608Context *s = ctx->priv;
401 for (i = s->start; i <= s->end; i++)
402 extract_line(ctx, in, inlink->w, i);
404 return ff_filter_frame(outlink, in);
407 static av_cold void uninit(AVFilterContext *ctx)
409 ReadEIA608Context *s = ctx->priv;
415 static const AVFilterPad readeia608_inputs[] = {
418 .type = AVMEDIA_TYPE_VIDEO,
419 .filter_frame = filter_frame,
420 .config_props = config_input,
425 static const AVFilterPad readeia608_outputs[] = {
428 .type = AVMEDIA_TYPE_VIDEO,
433 AVFilter ff_vf_readeia608 = {
434 .name = "readeia608",
435 .description = NULL_IF_CONFIG_SMALL("Read EIA-608 Closed Caption codes from input video and write them to frame metadata."),
436 .priv_size = sizeof(ReadEIA608Context),
437 .priv_class = &readeia608_class,
438 .query_formats = query_formats,
439 .inputs = readeia608_inputs,
440 .outputs = readeia608_outputs,
442 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,