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 CodeItem {
48 typedef struct ReadEIA608Context {
58 uint64_t histogram[256];
69 #define OFFSET(x) offsetof(ReadEIA608Context, x)
70 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
72 static const AVOption readeia608_options[] = {
73 { "scan_min", "set from which line to scan for codes", OFFSET(start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
74 { "scan_max", "set to which line to scan for codes", OFFSET(end), AV_OPT_TYPE_INT, {.i64=29}, 0, INT_MAX, FLAGS },
75 { "spw", "set ratio of width reserved for sync code detection", OFFSET(spw), AV_OPT_TYPE_FLOAT, {.dbl=.27}, 0.1, 0.7, FLAGS },
76 { "chp", "check and apply parity bit", OFFSET(chp), AV_OPT_TYPE_BOOL, {.i64= 0}, 0, 1, FLAGS },
77 { "lp", "lowpass line prior to processing", OFFSET(lp), AV_OPT_TYPE_BOOL, {.i64= 1}, 0, 1, FLAGS },
81 AVFILTER_DEFINE_CLASS(readeia608);
83 static int query_formats(AVFilterContext *ctx)
85 static const enum AVPixelFormat pixel_fmts[] = {
87 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
88 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
89 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
90 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
91 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
95 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
97 return AVERROR(ENOMEM);
98 return ff_set_common_formats(ctx, formats);
101 static int config_input(AVFilterLink *inlink)
103 AVFilterContext *ctx = inlink->dst;
104 ReadEIA608Context *s = ctx->priv;
105 int size = inlink->w + LAG;
107 if (s->end >= inlink->h) {
108 av_log(ctx, AV_LOG_WARNING, "Last line to scan too large, clipping.\n");
109 s->end = inlink->h - 1;
112 if (s->start > s->end) {
113 av_log(ctx, AV_LOG_ERROR, "Invalid range.\n");
114 return AVERROR(EINVAL);
117 s->unfiltered = av_calloc(size, sizeof(*s->unfiltered));
118 s->filtered = av_calloc(size, sizeof(*s->filtered));
119 s->avg_filter = av_calloc(size, sizeof(*s->avg_filter));
120 s->std_filter = av_calloc(size, sizeof(*s->std_filter));
121 s->signal = av_calloc(size, sizeof(*s->signal));
122 s->code = av_calloc(size, sizeof(*s->code));
123 s->temp = av_calloc(size, sizeof(*s->temp));
125 return AVERROR(ENOMEM);
130 static void build_histogram(ReadEIA608Context *s, const uint8_t *src, int len)
132 memset(s->histogram, 0, sizeof(s->histogram));
134 for (int i = 0; i < len; i++)
135 s->histogram[src[i]]++;
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(float *data, int len)
182 float sum = 0.0, mean = 0.0;
184 for (int i = 0; i < len; i++)
192 static float stddevf(float *data, int len)
194 float m = meanf(data, len);
195 float standard_deviation = 0.f;
197 for (int i = 0; i < len; i++)
198 standard_deviation += (data[i] - m) * (data[i] - m);
200 return sqrtf(standard_deviation / (len - 1));
203 static void thresholding(ReadEIA608Context *s, const uint8_t *y, uint8_t *signal,
204 float *unfiltered, float *filtered, float *avg_filter, float *std_filter,
205 int lag, float threshold, float influence, int len)
207 for (int i = lag; i < len + lag; i++) {
208 unfiltered[i] = y[i - lag] / 255.f;
209 filtered[i] = unfiltered[i];
212 for (int i = 0; i < lag; i++) {
213 unfiltered[i] = meanf(unfiltered, len * s->spw);
214 filtered[i] = unfiltered[i];
217 memset(signal, 0, len);
219 avg_filter[lag - 1] = meanf(unfiltered, lag);
220 std_filter[lag - 1] = stddevf(unfiltered, lag);
222 for (int i = lag; i < len + lag; i++) {
223 if (fabsf(unfiltered[i] - avg_filter[i-1]) > threshold * std_filter[i-1]) {
224 if (unfiltered[i] > avg_filter[i-1]) {
225 signal[i - lag] = 255;
229 filtered[i] = influence * unfiltered[i] + (1.f - influence) * filtered[i-1];
231 int distance_from_black, distance_from_white;
233 distance_from_black = FFABS(y[i - lag] - s->black);
234 distance_from_white = FFABS(y[i - lag] - s->white);
236 signal[i - lag] = distance_from_black <= distance_from_white ? 0 : 255;
238 avg_filter[i] = meanf(filtered + i - lag, lag);
239 std_filter[i] = stddevf(filtered + i - lag, lag);
243 static int periods(const uint8_t *signal, CodeItem *code, int len)
245 int hold = signal[0], cnt = 0;
248 memset(code, 0, len * sizeof(*code));
250 for (int i = 1; i < len; i++) {
251 if (signal[i] != hold) {
252 code[cnt].size = i - last;
253 code[cnt].bit = hold;
260 code[cnt].size = len - last;
261 code[cnt].bit = hold;
266 static void dump_code(AVFilterContext *ctx, int len, int item)
268 ReadEIA608Context *s = ctx->priv;
270 av_log(ctx, AV_LOG_DEBUG, "%d:", item);
271 for (int i = 0; i < len; i++) {
272 av_log(ctx, AV_LOG_DEBUG, " %03d", s->code[i].size);
274 av_log(ctx, AV_LOG_DEBUG, "\n");
277 static void extract_line(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *in, int line)
279 ReadEIA608Context *s = ctx->priv;
282 uint8_t byte[2] = { 0 };
283 uint8_t codes[19] = { 0 };
284 float bit_size = 0.f;
287 src = &in->data[0][line * in->linesize[0]];
289 uint8_t *dst = s->temp;
290 int w = inlink->w - 1;
292 for (i = 0; i < inlink->w; i++) {
293 int a = FFMAX(i - 3, 0);
294 int b = FFMAX(i - 2, 0);
295 int c = FFMAX(i - 1, 0);
296 int d = FFMIN(i + 3, w);
297 int e = FFMIN(i + 2, w);
298 int f = FFMIN(i + 1, w);
300 dst[i] = (src[a] + src[b] + src[c] + src[i] + src[d] + src[e] + src[f] + 6) / 7;
306 build_histogram(s, src, inlink->w);
307 find_black_and_white(s);
308 if (s->white - s->black < 5)
311 thresholding(s, src, s->signal, s->unfiltered, s->filtered,
312 s->avg_filter, s->std_filter,
313 LAG, 1, 0, inlink->w);
314 //memcpy(&in->data[0][line * in->linesize[0]], s->signal, inlink->w);
315 len = periods(s->signal, s->code, inlink->w);
316 dump_code(ctx, len, line);
318 s->code[14].bit != 0 ||
319 inlink->w / (float)s->code[14].size < SYNC_MIN ||
320 inlink->w / (float)s->code[14].size > SYNC_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 * 1.5f) {
335 if (s->code[15].size / bit_size < 0.45f) {
339 for (j = 0, i = 14; i < len; i++) {
342 run = lrintf(s->code[i].size / bit_size);
343 bit = s->code[i].bit;
345 for (int k = 0; j < 19 && k < run; k++) {
353 for (ch = 0; ch < 2; ch++) {
354 for (parity = 0, i = 0; i < 8; i++) {
355 int b = codes[3 + ch * 8 + i];
374 uint8_t key[128], value[128];
376 //snprintf(key, sizeof(key), "lavfi.readeia608.%d.bits", s->nb_found);
377 //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);
378 //av_dict_set(&in->metadata, key, value, 0);
380 snprintf(key, sizeof(key), "lavfi.readeia608.%d.cc", s->nb_found);
381 snprintf(value, sizeof(value), "0x%02X%02X", byte[0], byte[1]);
382 av_dict_set(&in->metadata, key, value, 0);
384 snprintf(key, sizeof(key), "lavfi.readeia608.%d.line", s->nb_found);
385 snprintf(value, sizeof(value), "%d", line);
386 av_dict_set(&in->metadata, key, value, 0);
392 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
394 AVFilterContext *ctx = inlink->dst;
395 AVFilterLink *outlink = ctx->outputs[0];
396 ReadEIA608Context *s = ctx->priv;
400 for (i = s->start; i <= s->end; i++)
401 extract_line(ctx, inlink, in, i);
403 return ff_filter_frame(outlink, in);
406 static av_cold void uninit(AVFilterContext *ctx)
408 ReadEIA608Context *s = ctx->priv;
412 av_freep(&s->signal);
413 av_freep(&s->unfiltered);
414 av_freep(&s->filtered);
415 av_freep(&s->avg_filter);
416 av_freep(&s->std_filter);
419 static const AVFilterPad readeia608_inputs[] = {
422 .type = AVMEDIA_TYPE_VIDEO,
423 .filter_frame = filter_frame,
424 .config_props = config_input,
429 static const AVFilterPad readeia608_outputs[] = {
432 .type = AVMEDIA_TYPE_VIDEO,
437 AVFilter ff_vf_readeia608 = {
438 .name = "readeia608",
439 .description = NULL_IF_CONFIG_SMALL("Read EIA-608 Closed Caption codes from input video and write them to frame metadata."),
440 .priv_size = sizeof(ReadEIA608Context),
441 .priv_class = &readeia608_class,
442 .query_formats = query_formats,
443 .inputs = readeia608_inputs,
444 .outputs = readeia608_outputs,
446 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,