#include "video.h"
#define LAG 25
-#define SYNC_MIN 12.f
-#define SYNC_MAX 15.f
+#define CLOCK_BITSIZE_MIN 0.2f
+#define CLOCK_BITSIZE_MAX 1.5f
+#define SYNC_BITSIZE_MIN 12.f
+#define SYNC_BITSIZE_MAX 15.f
+
+typedef struct LineItem {
+ int input;
+ int output;
+
+ float unfiltered;
+ float filtered;
+ float average;
+ float deviation;
+} LineItem;
typedef struct CodeItem {
uint8_t bit;
int size;
} CodeItem;
+typedef struct ScanItem {
+ int nb_line;
+ int found;
+ int white;
+ int black;
+ uint64_t *histogram;
+ uint8_t byte[2];
+
+ CodeItem *code;
+ LineItem *line;
+} ScanItem;
+
typedef struct ReadEIA608Context {
const AVClass *class;
+
int start, end;
- int nb_found;
- int white;
- int black;
float spw;
int chp;
int lp;
- uint64_t histogram[256];
+ int depth;
+ int max;
+ int nb_allocated;
+ ScanItem *scan;
- uint8_t *temp;
- uint8_t *signal;
- CodeItem *code;
- float *unfiltered;
- float *filtered;
- float *avg_filter;
- float *std_filter;
+ void (*read_line[2])(AVFrame *in, int nb_line,
+ LineItem *line, int lp, int w);
} ReadEIA608Context;
#define OFFSET(x) offsetof(ReadEIA608Context, x)
-#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
static const AVOption readeia608_options[] = {
{ "scan_min", "set from which line to scan for codes", OFFSET(start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
static int query_formats(AVFilterContext *ctx)
{
static const enum AVPixelFormat pixel_fmts[] = {
- AV_PIX_FMT_GRAY8,
+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
+ AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
+ AV_PIX_FMT_GRAY16,
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
AV_PIX_FMT_YUVJ411P,
+ AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
+ AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
+ AV_PIX_FMT_YUV440P10,
+ AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
+ AV_PIX_FMT_YUV440P12,
+ AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
+ AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
+ AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
+ AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
+ AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
+ AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
AV_PIX_FMT_NONE
};
AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
return ff_set_common_formats(ctx, formats);
}
-static int config_input(AVFilterLink *inlink)
+static int config_filter(AVFilterContext *ctx, int start, int end)
{
- AVFilterContext *ctx = inlink->dst;
ReadEIA608Context *s = ctx->priv;
+ AVFilterLink *inlink = ctx->inputs[0];
int size = inlink->w + LAG;
- if (s->end >= inlink->h) {
+ if (end >= inlink->h) {
av_log(ctx, AV_LOG_WARNING, "Last line to scan too large, clipping.\n");
- s->end = inlink->h - 1;
+ end = inlink->h - 1;
}
- if (s->start > s->end) {
+ if (start > end) {
av_log(ctx, AV_LOG_ERROR, "Invalid range.\n");
return AVERROR(EINVAL);
}
- s->unfiltered = av_calloc(size, sizeof(*s->unfiltered));
- s->filtered = av_calloc(size, sizeof(*s->filtered));
- s->avg_filter = av_calloc(size, sizeof(*s->avg_filter));
- s->std_filter = av_calloc(size, sizeof(*s->std_filter));
- s->signal = av_calloc(size, sizeof(*s->signal));
- s->code = av_calloc(size, sizeof(*s->code));
- s->temp = av_calloc(size, sizeof(*s->temp));
- if (!s->temp)
- return AVERROR(ENOMEM);
+ if (s->nb_allocated < end - start + 1) {
+ const int diff = end - start + 1 - s->nb_allocated;
+
+ s->scan = av_realloc_f(s->scan, end - start + 1, sizeof(*s->scan));
+ if (!s->scan)
+ return AVERROR(ENOMEM);
+ memset(&s->scan[s->nb_allocated], 0, diff * sizeof(*s->scan));
+ s->nb_allocated = end - start + 1;
+ }
+
+ for (int i = 0; i < s->nb_allocated; i++) {
+ ScanItem *scan = &s->scan[i];
+
+ if (!scan->histogram)
+ scan->histogram = av_calloc(s->max + 1, sizeof(*scan->histogram));
+ if (!scan->line)
+ scan->line = av_calloc(size, sizeof(*scan->line));
+ if (!scan->code)
+ scan->code = av_calloc(size, sizeof(*scan->code));
+ if (!scan->line || !scan->code || !scan->histogram)
+ return AVERROR(ENOMEM);
+ }
+
+ s->start = start;
+ s->end = end;
return 0;
}
-static void build_histogram(ReadEIA608Context *s, const uint8_t *src, int len)
+static void build_histogram(ReadEIA608Context *s, ScanItem *scan, const LineItem *line, int len)
{
- memset(s->histogram, 0, sizeof(s->histogram));
+ memset(scan->histogram, 0, (s->max + 1) * sizeof(*scan->histogram));
- for (int i = 0; i < len; i++)
- s->histogram[src[i]]++;
+ for (int i = LAG; i < len + LAG; i++)
+ scan->histogram[line[i].input]++;
}
-static void find_black_and_white(ReadEIA608Context *s)
+static void find_black_and_white(ReadEIA608Context *s, ScanItem *scan)
{
+ const int max = s->max;
int start = 0, end = 0, middle;
int black = 0, white = 0;
int cnt;
- for (int i = 0; i < 256; i++) {
- if (s->histogram[i]) {
+ for (int i = 0; i <= max; i++) {
+ if (scan->histogram[i]) {
start = i;
break;
}
}
- for (int i = 255; i >= 0; i--) {
- if (s->histogram[i]) {
+ for (int i = max; i >= 0; i--) {
+ if (scan->histogram[i]) {
end = i;
break;
}
cnt = 0;
for (int i = start; i <= middle; i++) {
- if (s->histogram[i] > cnt) {
- cnt = s->histogram[i];
+ if (scan->histogram[i] > cnt) {
+ cnt = scan->histogram[i];
black = i;
}
}
cnt = 0;
for (int i = end; i >= middle; i--) {
- if (s->histogram[i] > cnt) {
- cnt = s->histogram[i];
+ if (scan->histogram[i] > cnt) {
+ cnt = scan->histogram[i];
white = i;
}
}
- s->black = black;
- s->white = white;
+ scan->black = black;
+ scan->white = white;
}
-static float meanf(float *data, int len)
+static float meanf(const LineItem *line, int len)
{
float sum = 0.0, mean = 0.0;
for (int i = 0; i < len; i++)
- sum += data[i];
+ sum += line[i].filtered;
mean = sum / len;
return mean;
}
-static float stddevf(float *data, int len)
+static float stddevf(const LineItem *line, int len)
{
- float m = meanf(data, len);
+ float m = meanf(line, len);
float standard_deviation = 0.f;
for (int i = 0; i < len; i++)
- standard_deviation += (data[i] - m) * (data[i] - m);
+ standard_deviation += (line[i].filtered - m) * (line[i].filtered - m);
return sqrtf(standard_deviation / (len - 1));
}
-static void thresholding(ReadEIA608Context *s, const uint8_t *y, uint8_t *signal,
- float *unfiltered, float *filtered, float *avg_filter, float *std_filter,
+static void thresholding(ReadEIA608Context *s, ScanItem *scan, LineItem *line,
int lag, float threshold, float influence, int len)
{
for (int i = lag; i < len + lag; i++) {
- unfiltered[i] = y[i - lag] / 255.f;
- filtered[i] = unfiltered[i];
+ line[i].unfiltered = line[i].input / 255.f;
+ line[i].filtered = line[i].unfiltered;
}
for (int i = 0; i < lag; i++) {
- unfiltered[i] = meanf(unfiltered, len * s->spw);
- filtered[i] = unfiltered[i];
+ line[i].unfiltered = meanf(line, len * s->spw);
+ line[i].filtered = line[i].unfiltered;
}
- memset(signal, 0, len);
-
- avg_filter[lag - 1] = meanf(unfiltered, lag);
- std_filter[lag - 1] = stddevf(unfiltered, lag);
+ line[lag - 1].average = meanf(line, lag);
+ line[lag - 1].deviation = stddevf(line, lag);
for (int i = lag; i < len + lag; i++) {
- if (fabsf(unfiltered[i] - avg_filter[i-1]) > threshold * std_filter[i-1]) {
- if (unfiltered[i] > avg_filter[i-1]) {
- signal[i - lag] = 255;
+ if (fabsf(line[i].unfiltered - line[i-1].average) > threshold * line[i-1].deviation) {
+ if (line[i].unfiltered > line[i-1].average) {
+ line[i].output = 255;
} else {
- signal[i - lag] = 0;
+ line[i].output = 0;
}
- filtered[i] = influence * unfiltered[i] + (1.f - influence) * filtered[i-1];
+
+ line[i].filtered = influence * line[i].unfiltered + (1.f - influence) * line[i-1].filtered;
} else {
int distance_from_black, distance_from_white;
- distance_from_black = FFABS(y[i - lag] - s->black);
- distance_from_white = FFABS(y[i - lag] - s->white);
+ distance_from_black = FFABS(line[i].input - scan->black);
+ distance_from_white = FFABS(line[i].input - scan->white);
- signal[i - lag] = distance_from_black <= distance_from_white ? 0 : 255;
+ line[i].output = distance_from_black <= distance_from_white ? 0 : 255;
}
- avg_filter[i] = meanf(filtered + i - lag, lag);
- std_filter[i] = stddevf(filtered + i - lag, lag);
+
+ line[i].average = meanf(line + i - lag, lag);
+ line[i].deviation = stddevf(line + i - lag, lag);
}
}
-static int periods(const uint8_t *signal, CodeItem *code, int len)
+static int periods(const LineItem *line, CodeItem *code, int len)
{
- int hold = signal[0], cnt = 0;
- int last = 0;
+ int hold = line[LAG].output, cnt = 0;
+ int last = LAG;
memset(code, 0, len * sizeof(*code));
- for (int i = 1; i < len; i++) {
- if (signal[i] != hold) {
+ for (int i = LAG + 1; i < len + LAG; i++) {
+ if (line[i].output != hold) {
code[cnt].size = i - last;
code[cnt].bit = hold;
- hold = signal[i];
+ hold = line[i].output;
last = i;
cnt++;
}
}
- code[cnt].size = len - last;
+ code[cnt].size = LAG + len - last;
code[cnt].bit = hold;
return cnt + 1;
}
-static void dump_code(AVFilterContext *ctx, int len, int item)
+static void dump_code(AVFilterContext *ctx, ScanItem *scan, int len, int item)
{
- ReadEIA608Context *s = ctx->priv;
-
av_log(ctx, AV_LOG_DEBUG, "%d:", item);
for (int i = 0; i < len; i++) {
- av_log(ctx, AV_LOG_DEBUG, " %03d", s->code[i].size);
+ av_log(ctx, AV_LOG_DEBUG, " %03d", scan->code[i].size);
}
av_log(ctx, AV_LOG_DEBUG, "\n");
}
-static void extract_line(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *in, int line)
+#define READ_LINE(type, name) \
+static void read_##name(AVFrame *in, int nb_line, LineItem *line, int lp, int w) \
+{ \
+ const type *src = (const type *)(&in->data[0][nb_line * in->linesize[0]]);\
+ \
+ if (lp) { \
+ for (int i = 0; i < w; i++) { \
+ int a = FFMAX(i - 3, 0); \
+ int b = FFMAX(i - 2, 0); \
+ int c = FFMAX(i - 1, 0); \
+ int d = FFMIN(i + 3, w-1); \
+ int e = FFMIN(i + 2, w-1); \
+ int f = FFMIN(i + 1, w-1); \
+ \
+ line[LAG + i].input = (src[a] + src[b] + src[c] + src[i] + \
+ src[d] + src[e] + src[f] + 6) / 7; \
+ } \
+ } else { \
+ for (int i = 0; i < w; i++) { \
+ line[LAG + i].input = src[i]; \
+ } \
+ } \
+}
+
+READ_LINE(uint8_t, byte)
+READ_LINE(uint16_t, word)
+
+static int config_input(AVFilterLink *inlink)
{
+ AVFilterContext *ctx = inlink->dst;
ReadEIA608Context *s = ctx->priv;
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+
+ if (!desc)
+ return AVERROR_BUG;
+ s->depth = desc->comp[0].depth;
+ s->max = (1 << desc->comp[0].depth) - 1;
+ s->read_line[0] = read_byte;
+ s->read_line[1] = read_word;
+
+ return config_filter(ctx, s->start, s->end);
+}
+
+static void extract_line(AVFilterContext *ctx, AVFrame *in, ScanItem *scan, int w, int nb_line)
+{
+ ReadEIA608Context *s = ctx->priv;
+ LineItem *line = scan->line;
int i, j, ch, len;
- const uint8_t *src;
- uint8_t byte[2] = { 0 };
uint8_t codes[19] = { 0 };
float bit_size = 0.f;
int parity;
- src = &in->data[0][line * in->linesize[0]];
- if (s->lp) {
- uint8_t *dst = s->temp;
- int w = inlink->w - 1;
-
- for (i = 0; i < inlink->w; i++) {
- int a = FFMAX(i - 3, 0);
- int b = FFMAX(i - 2, 0);
- int c = FFMAX(i - 1, 0);
- int d = FFMIN(i + 3, w);
- int e = FFMIN(i + 2, w);
- int f = FFMIN(i + 1, w);
+ memset(line, 0, (w + LAG) * sizeof(*line));
+ scan->byte[0] = scan->byte[1] = 0;
+ scan->found = 0;
- dst[i] = (src[a] + src[b] + src[c] + src[i] + src[d] + src[e] + src[f] + 6) / 7;
- }
-
- src = s->temp;
- }
+ s->read_line[s->depth > 8](in, nb_line, line, s->lp, w);
- build_histogram(s, src, inlink->w);
- find_black_and_white(s);
- if (s->white - s->black < 5)
+ build_histogram(s, scan, line, w);
+ find_black_and_white(s, scan);
+ if (scan->white - scan->black < 5)
return;
- thresholding(s, src, s->signal, s->unfiltered, s->filtered,
- s->avg_filter, s->std_filter,
- LAG, 1, 0, inlink->w);
- //memcpy(&in->data[0][line * in->linesize[0]], s->signal, inlink->w);
- len = periods(s->signal, s->code, inlink->w);
- dump_code(ctx, len, line);
+ thresholding(s, scan, line, LAG, 1, 0, w);
+ len = periods(line, scan->code, w);
+ dump_code(ctx, scan, len, nb_line);
if (len < 15 ||
- s->code[14].bit != 0 ||
- inlink->w / (float)s->code[14].size < SYNC_MIN ||
- inlink->w / (float)s->code[14].size > SYNC_MAX) {
+ scan->code[14].bit != 0 ||
+ w / (float)scan->code[14].size < SYNC_BITSIZE_MIN ||
+ w / (float)scan->code[14].size > SYNC_BITSIZE_MAX) {
return;
}
for (i = 14; i < len; i++) {
- bit_size += s->code[i].size;
+ bit_size += scan->code[i].size;
}
bit_size /= 19.f;
for (i = 1; i < 14; i++) {
- if (s->code[i].size > bit_size * 1.5f) {
+ if (scan->code[i].size / bit_size > CLOCK_BITSIZE_MAX ||
+ scan->code[i].size / bit_size < CLOCK_BITSIZE_MIN) {
return;
}
}
- if (s->code[15].size / bit_size < 0.45f) {
+ if (scan->code[15].size / bit_size < 0.45f) {
return;
}
for (j = 0, i = 14; i < len; i++) {
int run, bit;
- run = lrintf(s->code[i].size / bit_size);
- bit = s->code[i].bit;
+ run = lrintf(scan->code[i].size / bit_size);
+ bit = scan->code[i].bit;
for (int k = 0; j < 19 && k < run; k++) {
codes[j++] = bit;
} else {
b = 0;
}
- byte[ch] |= b << i;
+ scan->byte[ch] |= b << i;
}
if (s->chp) {
if (!(parity & 1)) {
- byte[ch] = 0;
+ scan->byte[ch] = 0x7F;
}
}
}
- {
- uint8_t key[128], value[128];
+ scan->nb_line = nb_line;
+ scan->found = 1;
+}
- //snprintf(key, sizeof(key), "lavfi.readeia608.%d.bits", s->nb_found);
- //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);
- //av_dict_set(&in->metadata, key, value, 0);
+static int extract_lines(AVFilterContext *ctx, void *arg,
+ int job, int nb_jobs)
+{
+ ReadEIA608Context *s = ctx->priv;
+ AVFilterLink *inlink = ctx->inputs[0];
+ const int h = s->end - s->start + 1;
+ const int start = (h * job) / nb_jobs;
+ const int end = (h * (job+1)) / nb_jobs;
+ AVFrame *in = arg;
- snprintf(key, sizeof(key), "lavfi.readeia608.%d.cc", s->nb_found);
- snprintf(value, sizeof(value), "0x%02X%02X", byte[0], byte[1]);
- av_dict_set(&in->metadata, key, value, 0);
+ for (int i = start; i < end; i++) {
+ ScanItem *scan = &s->scan[i];
- snprintf(key, sizeof(key), "lavfi.readeia608.%d.line", s->nb_found);
- snprintf(value, sizeof(value), "%d", line);
- av_dict_set(&in->metadata, key, value, 0);
+ extract_line(ctx, in, scan, inlink->w, i);
}
- s->nb_found++;
+ return 0;
}
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
AVFilterContext *ctx = inlink->dst;
AVFilterLink *outlink = ctx->outputs[0];
ReadEIA608Context *s = ctx->priv;
- int i;
+ int nb_found;
+
+ ctx->internal->execute(ctx, extract_lines, in, NULL, FFMIN(FFMAX(s->end - s->start + 1, 1),
+ ff_filter_get_nb_threads(ctx)));
+
+ nb_found = 0;
+ for (int i = 0; i < s->end - s->start + 1; i++) {
+ ScanItem *scan = &s->scan[i];
+ uint8_t key[128], value[128];
+
+ if (!scan->found)
+ continue;
+
+ //snprintf(key, sizeof(key), "lavfi.readeia608.%d.bits", nb_found);
+ //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);
+ //av_dict_set(&in->metadata, key, value, 0);
+
+ snprintf(key, sizeof(key), "lavfi.readeia608.%d.cc", nb_found);
+ snprintf(value, sizeof(value), "0x%02X%02X", scan->byte[0], scan->byte[1]);
+ av_dict_set(&in->metadata, key, value, 0);
+
+ snprintf(key, sizeof(key), "lavfi.readeia608.%d.line", nb_found);
+ snprintf(value, sizeof(value), "%d", scan->nb_line);
+ av_dict_set(&in->metadata, key, value, 0);
- s->nb_found = 0;
- for (i = s->start; i <= s->end; i++)
- extract_line(ctx, inlink, in, i);
+ nb_found++;
+ }
return ff_filter_frame(outlink, in);
}
{
ReadEIA608Context *s = ctx->priv;
- av_freep(&s->temp);
- av_freep(&s->code);
- av_freep(&s->signal);
- av_freep(&s->unfiltered);
- av_freep(&s->filtered);
- av_freep(&s->avg_filter);
- av_freep(&s->std_filter);
+ for (int i = 0; i < s->nb_allocated; i++) {
+ ScanItem *scan = &s->scan[i];
+
+ av_freep(&scan->histogram);
+ av_freep(&scan->code);
+ av_freep(&scan->line);
+ }
+
+ s->nb_allocated = 0;
+ av_freep(&s->scan);
+}
+
+static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
+ char *res, int res_len, int flags)
+{
+ ReadEIA608Context *s = ctx->priv;
+ int ret, start = s->start, end = s->end;
+
+ ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
+ if (ret < 0)
+ return ret;
+
+ ret = config_filter(ctx, s->start, s->end);
+ if (ret < 0) {
+ s->start = start;
+ s->end = end;
+ }
+
+ return 0;
}
static const AVFilterPad readeia608_inputs[] = {
{ NULL }
};
-AVFilter ff_vf_readeia608 = {
+const AVFilter ff_vf_readeia608 = {
.name = "readeia608",
.description = NULL_IF_CONFIG_SMALL("Read EIA-608 Closed Caption codes from input video and write them to frame metadata."),
.priv_size = sizeof(ReadEIA608Context),
.inputs = readeia608_inputs,
.outputs = readeia608_outputs,
.uninit = uninit,
- .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
+ .process_command = process_command,
};