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 ScanItem {
72 typedef struct ReadEIA608Context {
85 void (*read_line[2])(AVFrame *in, int nb_line,
86 LineItem *line, int lp, int w);
89 #define OFFSET(x) offsetof(ReadEIA608Context, x)
90 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
92 static const AVOption readeia608_options[] = {
93 { "scan_min", "set from which line to scan for codes", OFFSET(start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
94 { "scan_max", "set to which line to scan for codes", OFFSET(end), AV_OPT_TYPE_INT, {.i64=29}, 0, INT_MAX, FLAGS },
95 { "spw", "set ratio of width reserved for sync code detection", OFFSET(spw), AV_OPT_TYPE_FLOAT, {.dbl=.27}, 0.1, 0.7, FLAGS },
96 { "chp", "check and apply parity bit", OFFSET(chp), AV_OPT_TYPE_BOOL, {.i64= 0}, 0, 1, FLAGS },
97 { "lp", "lowpass line prior to processing", OFFSET(lp), AV_OPT_TYPE_BOOL, {.i64= 1}, 0, 1, FLAGS },
101 AVFILTER_DEFINE_CLASS(readeia608);
103 static int query_formats(AVFilterContext *ctx)
105 static const enum AVPixelFormat pixel_fmts[] = {
106 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
107 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
109 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
110 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
111 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
112 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
113 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
115 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
116 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
117 AV_PIX_FMT_YUV440P10,
118 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
119 AV_PIX_FMT_YUV440P12,
120 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
121 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
122 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
123 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
124 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
125 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
128 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
130 return AVERROR(ENOMEM);
131 return ff_set_common_formats(ctx, formats);
134 static int config_filter(AVFilterContext *ctx, int start, int end)
136 ReadEIA608Context *s = ctx->priv;
137 AVFilterLink *inlink = ctx->inputs[0];
138 int size = inlink->w + LAG;
140 if (end >= inlink->h) {
141 av_log(ctx, AV_LOG_WARNING, "Last line to scan too large, clipping.\n");
146 av_log(ctx, AV_LOG_ERROR, "Invalid range.\n");
147 return AVERROR(EINVAL);
150 if (s->nb_allocated < end - start + 1) {
151 const int diff = end - start + 1 - s->nb_allocated;
153 s->scan = av_realloc_f(s->scan, end - start + 1, sizeof(*s->scan));
155 return AVERROR(ENOMEM);
156 memset(&s->scan[s->nb_allocated], 0, diff * sizeof(*s->scan));
157 s->nb_allocated = end - start + 1;
160 for (int i = 0; i < s->nb_allocated; i++) {
161 ScanItem *scan = &s->scan[i];
163 if (!scan->histogram)
164 scan->histogram = av_calloc(s->max + 1, sizeof(*scan->histogram));
166 scan->line = av_calloc(size, sizeof(*scan->line));
168 scan->code = av_calloc(size, sizeof(*scan->code));
169 if (!scan->line || !scan->code || !scan->histogram)
170 return AVERROR(ENOMEM);
179 static void build_histogram(ReadEIA608Context *s, ScanItem *scan, const LineItem *line, int len)
181 memset(scan->histogram, 0, (s->max + 1) * sizeof(*scan->histogram));
183 for (int i = LAG; i < len + LAG; i++)
184 scan->histogram[line[i].input]++;
187 static void find_black_and_white(ReadEIA608Context *s, ScanItem *scan)
189 const int max = s->max;
190 int start = 0, end = 0, middle;
191 int black = 0, white = 0;
194 for (int i = 0; i <= max; i++) {
195 if (scan->histogram[i]) {
201 for (int i = max; i >= 0; i--) {
202 if (scan->histogram[i]) {
208 middle = start + (end - start) / 2;
211 for (int i = start; i <= middle; i++) {
212 if (scan->histogram[i] > cnt) {
213 cnt = scan->histogram[i];
219 for (int i = end; i >= middle; i--) {
220 if (scan->histogram[i] > cnt) {
221 cnt = scan->histogram[i];
230 static float meanf(const LineItem *line, int len)
232 float sum = 0.0, mean = 0.0;
234 for (int i = 0; i < len; i++)
235 sum += line[i].filtered;
242 static float stddevf(const LineItem *line, int len)
244 float m = meanf(line, len);
245 float standard_deviation = 0.f;
247 for (int i = 0; i < len; i++)
248 standard_deviation += (line[i].filtered - m) * (line[i].filtered - m);
250 return sqrtf(standard_deviation / (len - 1));
253 static void thresholding(ReadEIA608Context *s, ScanItem *scan, LineItem *line,
254 int lag, float threshold, float influence, int len)
256 for (int i = lag; i < len + lag; i++) {
257 line[i].unfiltered = line[i].input / 255.f;
258 line[i].filtered = line[i].unfiltered;
261 for (int i = 0; i < lag; i++) {
262 line[i].unfiltered = meanf(line, len * s->spw);
263 line[i].filtered = line[i].unfiltered;
266 line[lag - 1].average = meanf(line, lag);
267 line[lag - 1].deviation = stddevf(line, lag);
269 for (int i = lag; i < len + lag; i++) {
270 if (fabsf(line[i].unfiltered - line[i-1].average) > threshold * line[i-1].deviation) {
271 if (line[i].unfiltered > line[i-1].average) {
272 line[i].output = 255;
277 line[i].filtered = influence * line[i].unfiltered + (1.f - influence) * line[i-1].filtered;
279 int distance_from_black, distance_from_white;
281 distance_from_black = FFABS(line[i].input - scan->black);
282 distance_from_white = FFABS(line[i].input - scan->white);
284 line[i].output = distance_from_black <= distance_from_white ? 0 : 255;
287 line[i].average = meanf(line + i - lag, lag);
288 line[i].deviation = stddevf(line + i - lag, lag);
292 static int periods(const LineItem *line, CodeItem *code, int len)
294 int hold = line[LAG].output, cnt = 0;
297 memset(code, 0, len * sizeof(*code));
299 for (int i = LAG + 1; i < len + LAG; i++) {
300 if (line[i].output != hold) {
301 code[cnt].size = i - last;
302 code[cnt].bit = hold;
303 hold = line[i].output;
309 code[cnt].size = LAG + len - last;
310 code[cnt].bit = hold;
315 static void dump_code(AVFilterContext *ctx, ScanItem *scan, int len, int item)
317 av_log(ctx, AV_LOG_DEBUG, "%d:", item);
318 for (int i = 0; i < len; i++) {
319 av_log(ctx, AV_LOG_DEBUG, " %03d", scan->code[i].size);
321 av_log(ctx, AV_LOG_DEBUG, "\n");
324 #define READ_LINE(type, name) \
325 static void read_##name(AVFrame *in, int nb_line, LineItem *line, int lp, int w) \
327 const type *src = (const type *)(&in->data[0][nb_line * in->linesize[0]]);\
330 for (int i = 0; i < w; i++) { \
331 int a = FFMAX(i - 3, 0); \
332 int b = FFMAX(i - 2, 0); \
333 int c = FFMAX(i - 1, 0); \
334 int d = FFMIN(i + 3, w-1); \
335 int e = FFMIN(i + 2, w-1); \
336 int f = FFMIN(i + 1, w-1); \
338 line[LAG + i].input = (src[a] + src[b] + src[c] + src[i] + \
339 src[d] + src[e] + src[f] + 6) / 7; \
342 for (int i = 0; i < w; i++) { \
343 line[LAG + i].input = src[i]; \
348 READ_LINE(uint8_t, byte)
349 READ_LINE(uint16_t, word)
351 static int config_input(AVFilterLink *inlink)
353 AVFilterContext *ctx = inlink->dst;
354 ReadEIA608Context *s = ctx->priv;
355 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
359 s->depth = desc->comp[0].depth;
360 s->max = (1 << desc->comp[0].depth) - 1;
361 s->read_line[0] = read_byte;
362 s->read_line[1] = read_word;
364 return config_filter(ctx, s->start, s->end);
367 static void extract_line(AVFilterContext *ctx, AVFrame *in, ScanItem *scan, int w, int nb_line)
369 ReadEIA608Context *s = ctx->priv;
370 LineItem *line = scan->line;
372 uint8_t codes[19] = { 0 };
373 float bit_size = 0.f;
376 memset(line, 0, (w + LAG) * sizeof(*line));
377 scan->byte[0] = scan->byte[1] = 0;
380 s->read_line[s->depth > 8](in, nb_line, line, s->lp, w);
382 build_histogram(s, scan, line, w);
383 find_black_and_white(s, scan);
384 if (scan->white - scan->black < 5)
387 thresholding(s, scan, line, LAG, 1, 0, w);
388 len = periods(line, scan->code, w);
389 dump_code(ctx, scan, len, nb_line);
391 scan->code[14].bit != 0 ||
392 w / (float)scan->code[14].size < SYNC_BITSIZE_MIN ||
393 w / (float)scan->code[14].size > SYNC_BITSIZE_MAX) {
397 for (i = 14; i < len; i++) {
398 bit_size += scan->code[i].size;
402 for (i = 1; i < 14; i++) {
403 if (scan->code[i].size / bit_size > CLOCK_BITSIZE_MAX ||
404 scan->code[i].size / bit_size < CLOCK_BITSIZE_MIN) {
409 if (scan->code[15].size / bit_size < 0.45f) {
413 for (j = 0, i = 14; i < len; i++) {
416 run = lrintf(scan->code[i].size / bit_size);
417 bit = scan->code[i].bit;
419 for (int k = 0; j < 19 && k < run; k++) {
427 for (ch = 0; ch < 2; ch++) {
428 for (parity = 0, i = 0; i < 8; i++) {
429 int b = codes[3 + ch * 8 + i];
437 scan->byte[ch] |= b << i;
442 scan->byte[ch] = 0x7F;
447 scan->nb_line = nb_line;
451 static int extract_lines(AVFilterContext *ctx, void *arg,
452 int job, int nb_jobs)
454 ReadEIA608Context *s = ctx->priv;
455 AVFilterLink *inlink = ctx->inputs[0];
456 const int h = s->end - s->start + 1;
457 const int start = (h * job) / nb_jobs;
458 const int end = (h * (job+1)) / nb_jobs;
461 for (int i = start; i < end; i++) {
462 ScanItem *scan = &s->scan[i];
464 extract_line(ctx, in, scan, inlink->w, i);
470 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
472 AVFilterContext *ctx = inlink->dst;
473 AVFilterLink *outlink = ctx->outputs[0];
474 ReadEIA608Context *s = ctx->priv;
477 ctx->internal->execute(ctx, extract_lines, in, NULL, FFMIN(FFMAX(s->end - s->start + 1, 1),
478 ff_filter_get_nb_threads(ctx)));
481 for (int i = 0; i < s->end - s->start + 1; i++) {
482 ScanItem *scan = &s->scan[i];
483 uint8_t key[128], value[128];
488 //snprintf(key, sizeof(key), "lavfi.readeia608.%d.bits", nb_found);
489 //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);
490 //av_dict_set(&in->metadata, key, value, 0);
492 snprintf(key, sizeof(key), "lavfi.readeia608.%d.cc", nb_found);
493 snprintf(value, sizeof(value), "0x%02X%02X", scan->byte[0], scan->byte[1]);
494 av_dict_set(&in->metadata, key, value, 0);
496 snprintf(key, sizeof(key), "lavfi.readeia608.%d.line", nb_found);
497 snprintf(value, sizeof(value), "%d", scan->nb_line);
498 av_dict_set(&in->metadata, key, value, 0);
503 return ff_filter_frame(outlink, in);
506 static av_cold void uninit(AVFilterContext *ctx)
508 ReadEIA608Context *s = ctx->priv;
510 for (int i = 0; i < s->nb_allocated; i++) {
511 ScanItem *scan = &s->scan[i];
513 av_freep(&scan->histogram);
514 av_freep(&scan->code);
515 av_freep(&scan->line);
522 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
523 char *res, int res_len, int flags)
525 ReadEIA608Context *s = ctx->priv;
526 int ret, start = s->start, end = s->end;
528 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
532 ret = config_filter(ctx, s->start, s->end);
541 static const AVFilterPad readeia608_inputs[] = {
544 .type = AVMEDIA_TYPE_VIDEO,
545 .filter_frame = filter_frame,
546 .config_props = config_input,
551 static const AVFilterPad readeia608_outputs[] = {
554 .type = AVMEDIA_TYPE_VIDEO,
559 const AVFilter ff_vf_readeia608 = {
560 .name = "readeia608",
561 .description = NULL_IF_CONFIG_SMALL("Read EIA-608 Closed Caption codes from input video and write them to frame metadata."),
562 .priv_size = sizeof(ReadEIA608Context),
563 .priv_class = &readeia608_class,
564 .query_formats = query_formats,
565 .inputs = readeia608_inputs,
566 .outputs = readeia608_outputs,
568 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
569 .process_command = process_command,