History

avfilter/vf_readeia608: add >8 depth support

Paul B Mahol committed 5 years ago

Commit df4e2b91 parent a423bc91 file · +72 −32Browse files

Unified · Split

libavfilter/vf_readeia608.c+72−32
@@ -62,7 +62,7 @@ typedef struct ScanItem { · 61 unmodified lines
62 int found;62 int found;
63 int white;63 int white;
64 int black;64 int black;
65 uint64_t histogram[256];65 uint64_t *histogram;
66 uint8_t byte[2];66 uint8_t byte[2];
67 67
68 CodeItem *code;68 CodeItem *code;
@@ -77,8 +77,13 @@ typedef struct ReadEIA608Context { · 8 unmodified lines
77 int chp;77 int chp;
78 int lp;78 int lp;
79 79
80 int depth;
81 int max;
80 int nb_allocated;82 int nb_allocated;
81 ScanItem *scan;83 ScanItem *scan;
84
85 void (*read_line[2])(AVFrame *in, int nb_line,
86 LineItem *line, int lp, int w);
82} ReadEIA608Context;87} ReadEIA608Context;
83 88
84#define OFFSET(x) offsetof(ReadEIA608Context, x)89#define OFFSET(x) offsetof(ReadEIA608Context, x)
@@ -98,13 +103,26 @@ AVFILTER_DEFINE_CLASS(readeia608); · 13 unmodified lines
98static int query_formats(AVFilterContext *ctx)103static int query_formats(AVFilterContext *ctx)
99{104{
100 static const enum AVPixelFormat pixel_fmts[] = {105 static const enum AVPixelFormat pixel_fmts[] = {
101 AV_PIX_FMT_GRAY8,106 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
107 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
108 AV_PIX_FMT_GRAY16,
102 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,109 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
103 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,110 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
104 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,111 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
105 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,112 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
106 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,113 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
107 AV_PIX_FMT_YUVJ411P,114 AV_PIX_FMT_YUVJ411P,
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,
108 AV_PIX_FMT_NONE126 AV_PIX_FMT_NONE
109 };127 };
110 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);128 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
@@ -142,11 +160,13 @@ static int config_filter(AVFilterContext *ctx, int start, int end) · 31 unmodified lines
142 for (int i = 0; i < s->nb_allocated; i++) {160 for (int i = 0; i < s->nb_allocated; i++) {
143 ScanItem *scan = &s->scan[i];161 ScanItem *scan = &s->scan[i];
144 162
163 if (!scan->histogram)
164 scan->histogram = av_calloc(s->max + 1, sizeof(*scan->histogram));
145 if (!scan->line)165 if (!scan->line)
146 scan->line = av_calloc(size, sizeof(*scan->line));166 scan->line = av_calloc(size, sizeof(*scan->line));
147 if (!scan->code)167 if (!scan->code)
148 scan->code = av_calloc(size, sizeof(*scan->code));168 scan->code = av_calloc(size, sizeof(*scan->code));
149 if (!scan->line || !scan->code)169 if (!scan->line || !scan->code || !scan->histogram)
150 return AVERROR(ENOMEM);170 return AVERROR(ENOMEM);
151 }171 }
152 172
@@ -156,17 +176,9 @@ static int config_filter(AVFilterContext *ctx, int start, int end) · 3 unmodified lines
156 return 0;176 return 0;
157}177}
158 178
159static int config_input(AVFilterLink *inlink)
160{
161 AVFilterContext *ctx = inlink->dst;
162 ReadEIA608Context *s = ctx->priv;
163
164 return config_filter(ctx, s->start, s->end);
165}
166
167static void build_histogram(ReadEIA608Context *s, ScanItem *scan, const LineItem *line, int len)179static void build_histogram(ReadEIA608Context *s, ScanItem *scan, const LineItem *line, int len)
168{180{
169 memset(scan->histogram, 0, sizeof(scan->histogram));181 memset(scan->histogram, 0, (s->max + 1) * sizeof(*scan->histogram));
170 182
171 for (int i = LAG; i < len + LAG; i++)183 for (int i = LAG; i < len + LAG; i++)
172 scan->histogram[line[i].input]++;184 scan->histogram[line[i].input]++;
@@ -174,18 +186,19 @@ static void build_histogram(ReadEIA608Context *s, ScanItem *scan, const LineItem · 1 unmodified line
174 186
175static void find_black_and_white(ReadEIA608Context *s, ScanItem *scan)187static void find_black_and_white(ReadEIA608Context *s, ScanItem *scan)
176{188{
189 const int max = s->max;
177 int start = 0, end = 0, middle;190 int start = 0, end = 0, middle;
178 int black = 0, white = 0;191 int black = 0, white = 0;
179 int cnt;192 int cnt;
180 193
181 for (int i = 0; i < 256; i++) {194 for (int i = 0; i <= max; i++) {
182 if (scan->histogram[i]) {195 if (scan->histogram[i]) {
183 start = i;196 start = i;
184 break;197 break;
185 }198 }
186 }199 }
187 200
188 for (int i = 255; i >= 0; i--) {201 for (int i = max; i >= 0; i--) {
189 if (scan->histogram[i]) {202 if (scan->histogram[i]) {
190 end = i;203 end = i;
191 break;204 break;
@@ -308,12 +321,54 @@ static void dump_code(AVFilterContext *ctx, ScanItem *scan, int len, int item) · 116 unmodified lines
308 av_log(ctx, AV_LOG_DEBUG, "\n");321 av_log(ctx, AV_LOG_DEBUG, "\n");
309}322}
310 323
324#define READ_LINE(type, name) \
325static void read_##name(AVFrame *in, int nb_line, LineItem *line, int lp, int w) \
326{ \
327 const type *src = (const type *)(&in->data[0][nb_line * in->linesize[0]]);\
328 \
329 if (lp) { \
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); \
337 \
338 line[LAG + i].input = (src[a] + src[b] + src[c] + src[i] + \
339 src[d] + src[e] + src[f] + 6) / 7; \
340 } \
341 } else { \
342 for (int i = 0; i < w; i++) { \
343 line[LAG + i].input = src[i]; \
344 } \
345 } \
346}
347
348READ_LINE(uint8_t, byte)
349READ_LINE(uint16_t, word)
350
351static int config_input(AVFilterLink *inlink)
352{
353 AVFilterContext *ctx = inlink->dst;
354 ReadEIA608Context *s = ctx->priv;
355 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
356
357 if (!desc)
358 return AVERROR_BUG;
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;
363
364 return config_filter(ctx, s->start, s->end);
365}
366
311static void extract_line(AVFilterContext *ctx, AVFrame *in, ScanItem *scan, int w, int nb_line)367static void extract_line(AVFilterContext *ctx, AVFrame *in, ScanItem *scan, int w, int nb_line)
312{368{
313 ReadEIA608Context *s = ctx->priv;369 ReadEIA608Context *s = ctx->priv;
314 LineItem *line = scan->line;370 LineItem *line = scan->line;
315 int i, j, ch, len;371 int i, j, ch, len;
316 const uint8_t *src;
317 uint8_t codes[19] = { 0 };372 uint8_t codes[19] = { 0 };
318 float bit_size = 0.f;373 float bit_size = 0.f;
319 int parity;374 int parity;
@@ -322,23 +377,7 @@ static void extract_line(AVFilterContext *ctx, AVFrame *in, ScanItem *scan, int · 2 unmodified lines
322 scan->byte[0] = scan->byte[1] = 0;377 scan->byte[0] = scan->byte[1] = 0;
323 scan->found = 0;378 scan->found = 0;
324 379
325 src = &in->data[0][nb_line * in->linesize[0]];380 s->read_line[s->depth > 8](in, nb_line, line, s->lp, w);
326 if (s->lp) {
327 for (i = 0; i < w; i++) {
328 int a = FFMAX(i - 3, 0);
329 int b = FFMAX(i - 2, 0);
330 int c = FFMAX(i - 1, 0);
331 int d = FFMIN(i + 3, w-1);
332 int e = FFMIN(i + 2, w-1);
333 int f = FFMIN(i + 1, w-1);
334
335 line[LAG + i].input = (src[a] + src[b] + src[c] + src[i] + src[d] + src[e] + src[f] + 6) / 7;
336 }
337 } else {
338 for (i = 0; i < w; i++) {
339 line[LAG + i].input = src[i];
340 }
341 }
342 381
343 build_histogram(s, scan, line, w);382 build_histogram(s, scan, line, w);
344 find_black_and_white(s, scan);383 find_black_and_white(s, scan);
@@ -471,6 +510,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 126 unmodified lines
471 for (int i = 0; i < s->nb_allocated; i++) {510 for (int i = 0; i < s->nb_allocated; i++) {
472 ScanItem *scan = &s->scan[i];511 ScanItem *scan = &s->scan[i];
473 512
513 av_freep(&scan->histogram);
474 av_freep(&scan->code);514 av_freep(&scan->code);
475 av_freep(&scan->line);515 av_freep(&scan->line);
476 }516 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t