Lots of options are now obsolete.
Showing the changes to libavfilter (1 of 2 files). Show all
Unified · Split
| @@ -36,41 +36,45 @@ · 35 unmodified lines | ||
| 36 | 36 | #include "internal.h" |
| 37 | 37 | #include "video.h" |
| 38 | 38 | |
| 39 | −#define FALL 0 | |
| 40 | −#define RISE 1 | |
| 39 | +#define LAG 25 | |
| 40 | +#define SYNC_MIN 12.f | |
| 41 | +#define SYNC_MAX 15.f | |
| 42 | + | |
| 43 | +typedef struct CodeItem { | |
| 44 | + uint8_t bit; | |
| 45 | + int size; | |
| 46 | +} CodeItem; | |
| 41 | 47 | |
| 42 | 48 | typedef struct ReadEIA608Context { |
| 43 | 49 | const AVClass *class; |
| 44 | 50 | int start, end; |
| 45 | − int min_range; | |
| 46 | − int max_peak_diff; | |
| 47 | − int max_period_diff; | |
| 48 | − int max_start_diff; | |
| 49 | 51 | int nb_found; |
| 50 | 52 | int white; |
| 51 | 53 | int black; |
| 52 | − float mpd, mhd, msd, mac, spw, bhd, wth, bth; | |
| 54 | + float spw; | |
| 53 | 55 | int chp; |
| 54 | 56 | int lp; |
| 57 | + | |
| 58 | + uint64_t histogram[256]; | |
| 59 | + | |
| 55 | 60 | uint8_t *temp; |
| 61 | + uint8_t *signal; | |
| 62 | + CodeItem *code; | |
| 63 | + float *unfiltered; | |
| 64 | + float *filtered; | |
| 65 | + float *avg_filter; | |
| 66 | + float *std_filter; | |
| 56 | 67 | } ReadEIA608Context; |
| 57 | 68 | |
| 58 | 69 | #define OFFSET(x) offsetof(ReadEIA608Context, x) |
| 59 | 70 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
| 60 | 71 | |
| 61 | 72 | static const AVOption readeia608_options[] = { |
| 62 | − { "scan_min", "set from which line to scan for codes", OFFSET(start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS }, | |
| 63 | − { "scan_max", "set to which line to scan for codes", OFFSET(end), AV_OPT_TYPE_INT, {.i64=29}, 0, INT_MAX, FLAGS }, | |
| 64 | − { "mac", "set minimal acceptable amplitude change for sync codes detection", OFFSET(mac), AV_OPT_TYPE_FLOAT, {.dbl=.2}, 0.001, 1, FLAGS }, | |
| 65 | − { "spw", "set ratio of width reserved for sync code detection", OFFSET(spw), AV_OPT_TYPE_FLOAT, {.dbl=.27}, 0.1, 0.7, FLAGS }, | |
| 66 | − { "mhd", "set max peaks height difference for sync code detection", OFFSET(mhd), AV_OPT_TYPE_FLOAT, {.dbl=.1}, 0, 0.5, FLAGS }, | |
| 67 | − { "mpd", "set max peaks period difference for sync code detection", OFFSET(mpd), AV_OPT_TYPE_FLOAT, {.dbl=.1}, 0, 0.5, FLAGS }, | |
| 68 | − { "msd", "set first two max start code bits differences", OFFSET(msd), AV_OPT_TYPE_FLOAT, {.dbl=.02}, 0, 0.5, FLAGS }, | |
| 69 | − { "bhd", "set min ratio of bits height compared to 3rd start code bit", OFFSET(bhd), AV_OPT_TYPE_FLOAT, {.dbl=.75}, 0.01, 1, FLAGS }, | |
| 70 | − { "th_w", "set white color threshold", OFFSET(wth), AV_OPT_TYPE_FLOAT, {.dbl=.35}, 0.1, 1, FLAGS }, | |
| 71 | − { "th_b", "set black color threshold", OFFSET(bth), AV_OPT_TYPE_FLOAT, {.dbl=.15}, 0, 0.5, FLAGS }, | |
| 72 | − { "chp", "check and apply parity bit", OFFSET(chp), AV_OPT_TYPE_BOOL, {.i64= 0}, 0, 1, FLAGS }, | |
| 73 | − { "lp", "lowpass line prior to processing", OFFSET(lp), AV_OPT_TYPE_BOOL, {.i64= 0}, 0, 1, FLAGS }, | |
| 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 }, | |
| 74 | 78 | { NULL } |
| 75 | 79 | }; |
| 76 | 80 | |
| @@ -96,10 +100,9 @@ static int query_formats(AVFilterContext *ctx) · 19 unmodified lines | ||
| 96 | 100 | |
| 97 | 101 | static int config_input(AVFilterLink *inlink) |
| 98 | 102 | { |
| 99 | − const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 100 | 103 | AVFilterContext *ctx = inlink->dst; |
| 101 | 104 | ReadEIA608Context *s = ctx->priv; |
| 102 | − int depth = desc->comp[0].depth; | |
| 105 | + int size = inlink->w + LAG; | |
| 103 | 106 | |
| 104 | 107 | if (s->end >= inlink->h) { |
| 105 | 108 | av_log(ctx, AV_LOG_WARNING, "Last line to scan too large, clipping.\n"); |
| @@ -111,34 +114,177 @@ static int config_input(AVFilterLink *inlink) · 5 unmodified lines | ||
| 111 | 114 | return AVERROR(EINVAL); |
| 112 | 115 | } |
| 113 | 116 | |
| 114 | − s->min_range = s->mac * ((1 << depth) - 1); | |
| 115 | − s->max_peak_diff = s->mhd * ((1 << depth) - 1); | |
| 116 | − s->max_period_diff = s->mpd * ((1 << depth) - 1); | |
| 117 | − s->max_start_diff = s->msd * ((1 << depth) - 1); | |
| 118 | − s->white = s->wth * ((1 << depth) - 1); | |
| 119 | − s->black = s->bth * ((1 << depth) - 1); | |
| 120 | − s->temp = av_calloc(inlink->w, sizeof(*s->temp)); | |
| 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)); | |
| 121 | 124 | if (!s->temp) |
| 122 | 125 | return AVERROR(ENOMEM); |
| 123 | 126 | |
| 124 | 127 | return 0; |
| 125 | 128 | } |
| 126 | 129 | |
| 130 | +static void build_histogram(ReadEIA608Context *s, const uint8_t *src, int len) | |
| 131 | +{ | |
| 132 | + memset(s->histogram, 0, sizeof(s->histogram)); | |
| 133 | + | |
| 134 | + for (int i = 0; i < len; i++) | |
| 135 | + s->histogram[src[i]]++; | |
| 136 | +} | |
| 137 | + | |
| 138 | +static void find_black_and_white(ReadEIA608Context *s) | |
| 139 | +{ | |
| 140 | + int start = 0, end = 0, middle; | |
| 141 | + int black = 0, white = 0; | |
| 142 | + int cnt; | |
| 143 | + | |
| 144 | + for (int i = 0; i < 256; i++) { | |
| 145 | + if (s->histogram[i]) { | |
| 146 | + start = i; | |
| 147 | + break; | |
| 148 | + } | |
| 149 | + } | |
| 150 | + | |
| 151 | + for (int i = 255; i >= 0; i--) { | |
| 152 | + if (s->histogram[i]) { | |
| 153 | + end = i; | |
| 154 | + break; | |
| 155 | + } | |
| 156 | + } | |
| 157 | + | |
| 158 | + middle = start + (end - start) / 2; | |
| 159 | + | |
| 160 | + cnt = 0; | |
| 161 | + for (int i = start; i <= middle; i++) { | |
| 162 | + if (s->histogram[i] > cnt) { | |
| 163 | + cnt = s->histogram[i]; | |
| 164 | + black = i; | |
| 165 | + } | |
| 166 | + } | |
| 167 | + | |
| 168 | + cnt = 0; | |
| 169 | + for (int i = end; i >= middle; i--) { | |
| 170 | + if (s->histogram[i] > cnt) { | |
| 171 | + cnt = s->histogram[i]; | |
| 172 | + white = i; | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + s->black = black; | |
| 177 | + s->white = white; | |
| 178 | +} | |
| 179 | + | |
| 180 | +static float meanf(float *data, int len) | |
| 181 | +{ | |
| 182 | + float sum = 0.0, mean = 0.0; | |
| 183 | + | |
| 184 | + for (int i = 0; i < len; i++) | |
| 185 | + sum += data[i]; | |
| 186 | + | |
| 187 | + mean = sum / len; | |
| 188 | + | |
| 189 | + return mean; | |
| 190 | +} | |
| 191 | + | |
| 192 | +static float stddevf(float *data, int len) | |
| 193 | +{ | |
| 194 | + float m = meanf(data, len); | |
| 195 | + float standard_deviation = 0.f; | |
| 196 | + | |
| 197 | + for (int i = 0; i < len; i++) | |
| 198 | + standard_deviation += (data[i] - m) * (data[i] - m); | |
| 199 | + | |
| 200 | + return sqrtf(standard_deviation / (len - 1)); | |
| 201 | +} | |
| 202 | + | |
| 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) | |
| 206 | +{ | |
| 207 | + for (int i = lag; i < len + lag; i++) { | |
| 208 | + unfiltered[i] = y[i - lag] / 255.f; | |
| 209 | + filtered[i] = unfiltered[i]; | |
| 210 | + } | |
| 211 | + | |
| 212 | + for (int i = 0; i < lag; i++) { | |
| 213 | + unfiltered[i] = meanf(unfiltered, len * s->spw); | |
| 214 | + filtered[i] = unfiltered[i]; | |
| 215 | + } | |
| 216 | + | |
| 217 | + memset(signal, 0, len); | |
| 218 | + | |
| 219 | + avg_filter[lag - 1] = meanf(unfiltered, lag); | |
| 220 | + std_filter[lag - 1] = stddevf(unfiltered, lag); | |
| 221 | + | |
| 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; | |
| 226 | + } else { | |
| 227 | + signal[i - lag] = 0; | |
| 228 | + } | |
| 229 | + filtered[i] = influence * unfiltered[i] + (1.f - influence) * filtered[i-1]; | |
| 230 | + } else { | |
| 231 | + int distance_from_black, distance_from_white; | |
| 232 | + | |
| 233 | + distance_from_black = FFABS(y[i - lag] - s->black); | |
| 234 | + distance_from_white = FFABS(y[i - lag] - s->white); | |
| 235 | + | |
| 236 | + signal[i - lag] = distance_from_black <= distance_from_white ? 0 : 255; | |
| 237 | + } | |
| 238 | + avg_filter[i] = meanf(filtered + i - lag, lag); | |
| 239 | + std_filter[i] = stddevf(filtered + i - lag, lag); | |
| 240 | + } | |
| 241 | +} | |
| 242 | + | |
| 243 | +static int periods(const uint8_t *signal, CodeItem *code, int len) | |
| 244 | +{ | |
| 245 | + int hold = signal[0], cnt = 0; | |
| 246 | + int last = 0; | |
| 247 | + | |
| 248 | + memset(code, 0, len * sizeof(*code)); | |
| 249 | + | |
| 250 | + for (int i = 1; i < len; i++) { | |
| 251 | + if (signal[i] != hold) { | |
| 252 | + code[cnt].size = i - last; | |
| 253 | + code[cnt].bit = hold; | |
| 254 | + hold = signal[i]; | |
| 255 | + last = i; | |
| 256 | + cnt++; | |
| 257 | + } | |
| 258 | + } | |
| 259 | + | |
| 260 | + code[cnt].size = len - last; | |
| 261 | + code[cnt].bit = hold; | |
| 262 | + | |
| 263 | + return cnt + 1; | |
| 264 | +} | |
| 265 | + | |
| 266 | +static void dump_code(AVFilterContext *ctx, int len, int item) | |
| 267 | +{ | |
| 268 | + ReadEIA608Context *s = ctx->priv; | |
| 269 | + | |
| 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); | |
| 273 | + } | |
| 274 | + av_log(ctx, AV_LOG_DEBUG, "\n"); | |
| 275 | +} | |
| 276 | + | |
| 127 | 277 | static void extract_line(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *in, int line) |
| 128 | 278 | { |
| 129 | 279 | ReadEIA608Context *s = ctx->priv; |
| 130 | − int max = 0, min = INT_MAX; | |
| 131 | − int i, ch, range = 0; | |
| 280 | + int i, j, ch, len; | |
| 132 | 281 | const uint8_t *src; |
| 133 | − uint16_t clock[8][2] = { { 0 } }; | |
| 134 | − const int sync_width = s->spw * in->width; | |
| 135 | − int last = 0, peaks = 0, max_peak_diff = 0, dir = RISE; | |
| 136 | − const int width_per_bit = (in->width - sync_width) / 19; | |
| 137 | 282 | uint8_t byte[2] = { 0 }; |
| 138 | − int s1, s2, s3, parity; | |
| 283 | + uint8_t codes[19] = { 0 }; | |
| 284 | + float bit_size = 0.f; | |
| 285 | + int parity; | |
| 139 | 286 | |
| 140 | 287 | src = &in->data[0][line * in->linesize[0]]; |
| 141 | − | |
| 142 | 288 | if (s->lp) { |
| 143 | 289 | uint8_t *dst = s->temp; |
| 144 | 290 | int w = inlink->w - 1; |
| @@ -157,78 +303,60 @@ static void extract_line(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *in · 12 unmodified lines | ||
| 157 | 303 | src = s->temp; |
| 158 | 304 | } |
| 159 | 305 | |
| 160 | − for (i = 0; i < sync_width; i++) { | |
| 161 | − max = FFMAX(max, src[i]); | |
| 162 | − min = FFMIN(min, src[i]); | |
| 163 | − } | |
| 164 | − | |
| 165 | − range = max - min; | |
| 166 | − if (range < s->min_range) | |
| 306 | + build_histogram(s, src, inlink->w); | |
| 307 | + find_black_and_white(s); | |
| 308 | + if (s->white - s->black < 5) | |
| 167 | 309 | return; |
| 168 | 310 | |
| 169 | − for (i = 0; i < sync_width; i++) { | |
| 170 | − int Y = src[i]; | |
| 171 | − | |
| 172 | − if (dir == RISE) { | |
| 173 | − if (Y < last) { | |
| 174 | − dir = FALL; | |
| 175 | − if (last >= s->white) { | |
| 176 | − clock[peaks][0] = last; | |
| 177 | − clock[peaks][1] = i; | |
| 178 | − peaks++; | |
| 179 | − if (peaks > 7) | |
| 180 | − break; | |
| 181 | − } | |
| 182 | − } | |
| 183 | − } else if (dir == FALL) { | |
| 184 | − if (Y > last && last <= s->black) { | |
| 185 | − dir = RISE; | |
| 186 | − } | |
| 187 | − } | |
| 188 | − last = Y; | |
| 189 | − } | |
| 190 | − | |
| 191 | − if (peaks != 7) { | |
| 192 | − av_log(ctx, AV_LOG_DEBUG, "peaks: %d != 7\n", peaks); | |
| 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); | |
| 317 | + if (len < 15 || | |
| 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) { | |
| 193 | 321 | return; |
| 194 | 322 | } |
| 195 | 323 | |
| 196 | − for (i = 1; i < 7; i++) | |
| 197 | − max_peak_diff = FFMAX(max_peak_diff, FFABS(clock[i][0] - clock[i-1][0])); | |
| 198 | − | |
| 199 | − if (max_peak_diff > s->max_peak_diff) { | |
| 200 | − av_log(ctx, AV_LOG_DEBUG, "mhd: %d > %d\n", max_peak_diff, s->max_peak_diff); | |
| 201 | − return; | |
| 324 | + for (i = 14; i < len; i++) { | |
| 325 | + bit_size += s->code[i].size; | |
| 202 | 326 | } |
| 203 | 327 | |
| 204 | − max = 0; min = INT_MAX; | |
| 205 | − for (i = 1; i < 7; i++) { | |
| 206 | − max = FFMAX(max, FFABS(clock[i][1] - clock[i-1][1])); | |
| 207 | − min = FFMIN(min, FFABS(clock[i][1] - clock[i-1][1])); | |
| 328 | + bit_size /= 19.f; | |
| 329 | + for (i = 1; i < 14; i++) { | |
| 330 | + if (s->code[i].size > bit_size * 1.5f) { | |
| 331 | + return; | |
| 332 | + } | |
| 208 | 333 | } |
| 209 | 334 | |
| 210 | − range = max - min; | |
| 211 | − if (range > s->max_period_diff) { | |
| 212 | − av_log(ctx, AV_LOG_DEBUG, "mpd: %d > %d\n", range, s->max_period_diff); | |
| 335 | + if (s->code[15].size / bit_size < 0.45f) { | |
| 213 | 336 | return; |
| 214 | 337 | } |
| 215 | 338 | |
| 216 | − s1 = src[sync_width + width_per_bit * 0 + width_per_bit / 2]; | |
| 217 | − s2 = src[sync_width + width_per_bit * 1 + width_per_bit / 2]; | |
| 218 | − s3 = src[sync_width + width_per_bit * 2 + width_per_bit / 2]; | |
| 339 | + for (j = 0, i = 14; i < len; i++) { | |
| 340 | + int run, bit; | |
| 219 | 341 | |
| 220 | − if (FFABS(s1 - s2) > s->max_start_diff || s1 > s->black || s2 > s->black || s3 < s->white) { | |
| 221 | − av_log(ctx, AV_LOG_DEBUG, "msd: %d > %d\n", FFABS(s1 - s2), s->max_start_diff); | |
| 222 | − return; | |
| 342 | + run = lrintf(s->code[i].size / bit_size); | |
| 343 | + bit = s->code[i].bit; | |
| 344 | + | |
| 345 | + for (int k = 0; j < 19 && k < run; k++) { | |
| 346 | + codes[j++] = bit; | |
| 347 | + } | |
| 348 | + | |
| 349 | + if (j >= 19) | |
| 350 | + break; | |
| 223 | 351 | } |
| 224 | 352 | |
| 225 | 353 | for (ch = 0; ch < 2; ch++) { |
| 226 | 354 | for (parity = 0, i = 0; i < 8; i++) { |
| 227 | − int b = src[sync_width + width_per_bit * (i + 3 + 8 * ch) + width_per_bit / 2]; | |
| 355 | + int b = codes[3 + ch * 8 + i]; | |
| 228 | 356 | |
| 229 | − if (b - s1 > (s3 - s1) * s->bhd) { | |
| 230 | − b = 1; | |
| 357 | + if (b == 255) { | |
| 231 | 358 | parity++; |
| 359 | + b = 1; | |
| 232 | 360 | } else { |
| 233 | 361 | b = 0; |
| 234 | 362 | } |
| @@ -245,6 +373,10 @@ static void extract_line(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *in · 10 unmodified lines | ||
| 245 | 373 | { |
| 246 | 374 | uint8_t key[128], value[128]; |
| 247 | 375 | |
| 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); | |
| 379 | + | |
| 248 | 380 | snprintf(key, sizeof(key), "lavfi.readeia608.%d.cc", s->nb_found); |
| 249 | 381 | snprintf(value, sizeof(value), "0x%02X%02X", byte[0], byte[1]); |
| 250 | 382 | av_dict_set(&in->metadata, key, value, 0); |
| @@ -276,6 +408,12 @@ static av_cold void uninit(AVFilterContext *ctx) · 25 unmodified lines | ||
| 276 | 408 | ReadEIA608Context *s = ctx->priv; |
| 277 | 409 | |
| 278 | 410 | av_freep(&s->temp); |
| 411 | + av_freep(&s->code); | |
| 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); | |
| 279 | 417 | } |
| 280 | 418 | |
| 281 | 419 | static const AVFilterPad readeia608_inputs[] = { |