Add a filter to scan the top lines of video frames for vertical interval
timecode (VITC) information and attach it as metadata keys.
Signed-off-by: Tobias Rapp <t.rapp@noa-archive.com>
... | ... |
@@ -385,6 +385,7 @@ Filters: |
385 | 385 |
vf_neighbor.c Paul B Mahol |
386 | 386 |
vf_psnr.c Paul B Mahol |
387 | 387 |
vf_random.c Paul B Mahol |
388 |
+ vf_readvitc.c Tobias Rapp (CC t.rapp at noa-archive dot com) |
|
388 | 389 |
vf_scale.c Michael Niedermayer |
389 | 390 |
vf_separatefields.c Paul B Mahol |
390 | 391 |
vf_ssim.c Paul B Mahol |
... | ... |
@@ -10618,6 +10618,43 @@ less than @code{0}, the filter will try to use a good random seed on a |
10618 | 10618 |
best effort basis. |
10619 | 10619 |
@end table |
10620 | 10620 |
|
10621 |
+@section readvitc |
|
10622 |
+ |
|
10623 |
+Read vertical interval timecode (VITC) information from the top lines of a |
|
10624 |
+video frame. |
|
10625 |
+ |
|
10626 |
+The filter adds frame metadata key @code{lavfi.readvitc.tc_str} with the |
|
10627 |
+timecode value, if a valid timecode has been detected. Further metadata key |
|
10628 |
+@code{lavfi.readvitc.found} is set to 0/1 depending on whether |
|
10629 |
+timecode data has been found or not. |
|
10630 |
+ |
|
10631 |
+This filter accepts the following options: |
|
10632 |
+ |
|
10633 |
+@table @option |
|
10634 |
+@item scan_max |
|
10635 |
+Set the maximum number of lines to scan for VITC data. If the value is set to |
|
10636 |
+@code{-1} the full video frame is scanned. Default is @code{45}. |
|
10637 |
+ |
|
10638 |
+@item thr_b |
|
10639 |
+Set the luma threshold for black. Accepts float numbers in the range [0.0,1.0], |
|
10640 |
+default value is @code{0.2}. The value must be equal or less than @code{thr_w}. |
|
10641 |
+ |
|
10642 |
+@item thr_w |
|
10643 |
+Set the luma threshold for white. Accepts float numbers in the range [0.0,1.0], |
|
10644 |
+default value is @code{0.6}. The value must be equal or greater than @code{thr_b}. |
|
10645 |
+@end table |
|
10646 |
+ |
|
10647 |
+@subsection Examples |
|
10648 |
+ |
|
10649 |
+@itemize |
|
10650 |
+@item |
|
10651 |
+Detect and draw VITC data onto the video frame; if no valid VITC is detected, |
|
10652 |
+draw @code{--:--:--:--} as a placeholder: |
|
10653 |
+@example |
|
10654 |
+ffmpeg -i input.avi -filter:v 'readvitc,drawtext=fontfile=FreeMono.ttf:text=%@{metadata\\:lavfi.readvitc.tc_str\\:--\\\\\\:--\\\\\\:--\\\\\\:--@}:x=(w-tw)/2:y=400-ascent' |
|
10655 |
+@end example |
|
10656 |
+@end itemize |
|
10657 |
+ |
|
10621 | 10658 |
@section remap |
10622 | 10659 |
|
10623 | 10660 |
Remap pixels using 2nd: Xmap and 3rd: Ymap input video stream. |
... | ... |
@@ -227,6 +227,7 @@ OBJS-$(CONFIG_PSNR_FILTER) += vf_psnr.o dualinput.o framesync. |
227 | 227 |
OBJS-$(CONFIG_PULLUP_FILTER) += vf_pullup.o |
228 | 228 |
OBJS-$(CONFIG_QP_FILTER) += vf_qp.o |
229 | 229 |
OBJS-$(CONFIG_RANDOM_FILTER) += vf_random.o |
230 |
+OBJS-$(CONFIG_READVITC_FILTER) += vf_readvitc.o |
|
230 | 231 |
OBJS-$(CONFIG_REALTIME_FILTER) += f_realtime.o |
231 | 232 |
OBJS-$(CONFIG_REMAP_FILTER) += vf_remap.o framesync.o |
232 | 233 |
OBJS-$(CONFIG_REMOVEGRAIN_FILTER) += vf_removegrain.o |
... | ... |
@@ -247,6 +247,7 @@ void avfilter_register_all(void) |
247 | 247 |
REGISTER_FILTER(PULLUP, pullup, vf); |
248 | 248 |
REGISTER_FILTER(QP, qp, vf); |
249 | 249 |
REGISTER_FILTER(RANDOM, random, vf); |
250 |
+ REGISTER_FILTER(READVITC, readvitc, vf); |
|
250 | 251 |
REGISTER_FILTER(REALTIME, realtime, vf); |
251 | 252 |
REGISTER_FILTER(REMAP, remap, vf); |
252 | 253 |
REGISTER_FILTER(REMOVEGRAIN, removegrain, vf); |
... | ... |
@@ -30,7 +30,7 @@ |
30 | 30 |
#include "libavutil/version.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 6 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 42 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 43 |
|
34 | 34 |
#define LIBAVFILTER_VERSION_MICRO 100 |
35 | 35 |
|
36 | 36 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
37 | 37 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,258 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2016 Tobias Rapp |
|
2 |
+ * |
|
3 |
+ * This file is part of FFmpeg. |
|
4 |
+ * |
|
5 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+/** |
|
21 |
+ * @file |
|
22 |
+ * Filter for reading the vertical interval timecode (VITC). |
|
23 |
+ * See also https://en.wikipedia.org/wiki/Vertical_interval_timecode |
|
24 |
+ */ |
|
25 |
+ |
|
26 |
+#include "libavutil/common.h" |
|
27 |
+#include "libavutil/internal.h" |
|
28 |
+#include "libavutil/opt.h" |
|
29 |
+#include "libavutil/pixdesc.h" |
|
30 |
+#include "libavutil/timecode.h" |
|
31 |
+#include "avfilter.h" |
|
32 |
+#include "formats.h" |
|
33 |
+#include "internal.h" |
|
34 |
+ |
|
35 |
+#define LINE_DATA_SIZE 9 |
|
36 |
+ |
|
37 |
+typedef struct ReadVitcContext { |
|
38 |
+ const AVClass *class; |
|
39 |
+ |
|
40 |
+ int scan_max; |
|
41 |
+ double thr_b; |
|
42 |
+ double thr_w; |
|
43 |
+ |
|
44 |
+ int threshold_black; |
|
45 |
+ int threshold_white; |
|
46 |
+ int threshold_gray; |
|
47 |
+ int grp_width; |
|
48 |
+ uint8_t line_data[LINE_DATA_SIZE]; |
|
49 |
+ char tcbuf[AV_TIMECODE_STR_SIZE]; |
|
50 |
+} ReadVitcContext; |
|
51 |
+ |
|
52 |
+#define OFFSET(x) offsetof(ReadVitcContext, x) |
|
53 |
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
|
54 |
+ |
|
55 |
+static const AVOption readvitc_options[] = { |
|
56 |
+ { "scan_max", "maximum line numbers to scan for VITC data", OFFSET(scan_max), AV_OPT_TYPE_INT, {.i64 = 45 }, -1, INT_MAX, FLAGS }, |
|
57 |
+ { "thr_b", "black color threshold", OFFSET(thr_b), AV_OPT_TYPE_DOUBLE, {.dbl = 0.2 }, 0, 1.0, FLAGS }, |
|
58 |
+ { "thr_w", "white color threshold", OFFSET(thr_w), AV_OPT_TYPE_DOUBLE, {.dbl = 0.6 }, 0, 1.0, FLAGS }, |
|
59 |
+ { NULL } |
|
60 |
+}; |
|
61 |
+ |
|
62 |
+AVFILTER_DEFINE_CLASS(readvitc); |
|
63 |
+ |
|
64 |
+static uint8_t get_vitc_crc( uint8_t *line ) { |
|
65 |
+ uint8_t crc; |
|
66 |
+ |
|
67 |
+ crc = 0x01 | (line[0] << 2); |
|
68 |
+ crc ^= (line[0] >> 6) | 0x04 | (line[1] << 4); |
|
69 |
+ crc ^= (line[1] >> 4) | 0x10 | (line[2] << 6); |
|
70 |
+ crc ^= (line[2] >> 2) | 0x40; |
|
71 |
+ crc ^= line[3]; |
|
72 |
+ crc ^= 0x01 | (line[4] << 2); |
|
73 |
+ crc ^= (line[4] >> 6) | 0x04 | (line[5] << 4); |
|
74 |
+ crc ^= (line[5] >> 4) | 0x10 | (line[6] << 6); |
|
75 |
+ crc ^= (line[6] >> 2) | 0x40; |
|
76 |
+ crc ^= line[7]; |
|
77 |
+ crc ^= 0x01; |
|
78 |
+ crc = (crc >> 2) | (crc << 6); // rotate byte right by two bits |
|
79 |
+ return crc; |
|
80 |
+} |
|
81 |
+ |
|
82 |
+static inline uint8_t get_pit_avg3( uint8_t *line, int i ) { |
|
83 |
+ return ((line[i-1] + line[i] + line[i+1]) / 3); |
|
84 |
+} |
|
85 |
+ |
|
86 |
+static int read_vitc_line( ReadVitcContext *ctx, uint8_t *src, int line_size, int width, int height ) |
|
87 |
+{ |
|
88 |
+ uint8_t *scan_line; |
|
89 |
+ int grp_index, pit_index; |
|
90 |
+ int grp_start_pos; |
|
91 |
+ uint8_t pit_value; |
|
92 |
+ int x, y, res = 0; |
|
93 |
+ |
|
94 |
+ if (ctx->scan_max >= 0) |
|
95 |
+ height = FFMIN(height, ctx->scan_max); |
|
96 |
+ |
|
97 |
+ // scan lines for VITC data, starting from the top |
|
98 |
+ for (y = 0; y < height; y++) { |
|
99 |
+ scan_line = src; |
|
100 |
+ memset(ctx->line_data, 0, LINE_DATA_SIZE); |
|
101 |
+ grp_index = 0; |
|
102 |
+ x = 0; |
|
103 |
+ while ((x < width) && (grp_index < 9)) { |
|
104 |
+ // search next sync pattern |
|
105 |
+ while ((x < width) && (scan_line[x] < ctx->threshold_white)) |
|
106 |
+ x++; |
|
107 |
+ while ((x < width) && (scan_line[x] > ctx->threshold_black)) |
|
108 |
+ x++; |
|
109 |
+ x = FFMAX(x - ((ctx->grp_width+10) / 20), 1); // step back a half pit |
|
110 |
+ grp_start_pos = x; |
|
111 |
+ if ((grp_start_pos + ctx->grp_width) > width) |
|
112 |
+ break; // not enough pixels for reading a whole pit group |
|
113 |
+ pit_value = get_pit_avg3(scan_line, x); |
|
114 |
+ if (pit_value < ctx->threshold_white) |
|
115 |
+ break; // first sync bit mismatch |
|
116 |
+ x = grp_start_pos + ((ctx->grp_width) / 10); |
|
117 |
+ pit_value = get_pit_avg3(scan_line, x); |
|
118 |
+ if (pit_value > ctx->threshold_black ) |
|
119 |
+ break; // second sync bit mismatch |
|
120 |
+ for (pit_index = 0; pit_index <= 7; pit_index++) { |
|
121 |
+ x = grp_start_pos + (((pit_index+2)*ctx->grp_width) / 10); |
|
122 |
+ pit_value = get_pit_avg3(scan_line, x); |
|
123 |
+ if (pit_value > ctx->threshold_gray) |
|
124 |
+ ctx->line_data[grp_index] |= (1 << pit_index); |
|
125 |
+ } |
|
126 |
+ grp_index++; |
|
127 |
+ } |
|
128 |
+ if ((grp_index == 9) && (get_vitc_crc(ctx->line_data) == ctx->line_data[8])) { |
|
129 |
+ res = 1; |
|
130 |
+ break; |
|
131 |
+ } |
|
132 |
+ src += line_size; |
|
133 |
+ } |
|
134 |
+ |
|
135 |
+ return res; |
|
136 |
+} |
|
137 |
+ |
|
138 |
+static unsigned bcd2uint(uint8_t high, uint8_t low) |
|
139 |
+{ |
|
140 |
+ if (high > 9 || low > 9) |
|
141 |
+ return 0; |
|
142 |
+ return 10*high + low; |
|
143 |
+} |
|
144 |
+ |
|
145 |
+static char *make_vitc_tc_string(char *buf, uint8_t *line) |
|
146 |
+{ |
|
147 |
+ unsigned hh = bcd2uint(line[7] & 0x03, line[6] & 0x0f); // 6-bit hours |
|
148 |
+ unsigned mm = bcd2uint(line[5] & 0x07, line[4] & 0x0f); // 7-bit minutes |
|
149 |
+ unsigned ss = bcd2uint(line[3] & 0x07, line[2] & 0x0f); // 7-bit seconds |
|
150 |
+ unsigned ff = bcd2uint(line[1] & 0x03, line[0] & 0x0f); // 6-bit frames |
|
151 |
+ unsigned drop = (line[1] & 0x04); // 1-bit drop flag |
|
152 |
+ snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u", |
|
153 |
+ hh, mm, ss, drop ? ';' : ':', ff); |
|
154 |
+ return buf; |
|
155 |
+} |
|
156 |
+ |
|
157 |
+static av_cold int init(AVFilterContext *ctx) |
|
158 |
+{ |
|
159 |
+ ReadVitcContext *s = ctx->priv; |
|
160 |
+ |
|
161 |
+ s->threshold_black = s->thr_b * UINT8_MAX; |
|
162 |
+ s->threshold_white = s->thr_w * UINT8_MAX; |
|
163 |
+ if (s->threshold_black > s->threshold_white) { |
|
164 |
+ av_log(ctx, AV_LOG_WARNING, "Black color threshold is higher than white color threshold (%g > %g)\n", |
|
165 |
+ s->thr_b, s->thr_w); |
|
166 |
+ return AVERROR(EINVAL); |
|
167 |
+ } |
|
168 |
+ s->threshold_gray = s->threshold_white - ((s->threshold_white - s->threshold_black) / 2); |
|
169 |
+ av_log(ctx, AV_LOG_DEBUG, "threshold_black:%d threshold_white:%d threshold_gray:%d\n", |
|
170 |
+ s->threshold_black, s->threshold_white, s->threshold_gray); |
|
171 |
+ |
|
172 |
+ return 0; |
|
173 |
+} |
|
174 |
+ |
|
175 |
+static int config_props(AVFilterLink *inlink) |
|
176 |
+{ |
|
177 |
+ AVFilterContext *ctx = inlink->dst; |
|
178 |
+ ReadVitcContext *s = ctx->priv; |
|
179 |
+ |
|
180 |
+ s->grp_width = inlink->w * 5 / 48; |
|
181 |
+ av_log(ctx, AV_LOG_DEBUG, "w:%d h:%d grp_width:%d scan_max:%d\n", |
|
182 |
+ inlink->w, inlink->h, s->grp_width, s->scan_max); |
|
183 |
+ return 0; |
|
184 |
+} |
|
185 |
+ |
|
186 |
+static int query_formats(AVFilterContext *ctx) |
|
187 |
+{ |
|
188 |
+ static const enum AVPixelFormat pixel_fmts[] = { |
|
189 |
+ AV_PIX_FMT_GRAY8, |
|
190 |
+ AV_PIX_FMT_NV12, |
|
191 |
+ AV_PIX_FMT_NV16, |
|
192 |
+ AV_PIX_FMT_NV21, |
|
193 |
+ AV_PIX_FMT_YUV410P, |
|
194 |
+ AV_PIX_FMT_YUV411P, |
|
195 |
+ AV_PIX_FMT_YUV420P, |
|
196 |
+ AV_PIX_FMT_YUV422P, |
|
197 |
+ AV_PIX_FMT_YUV440P, |
|
198 |
+ AV_PIX_FMT_YUV444P, |
|
199 |
+ AV_PIX_FMT_YUVA420P, |
|
200 |
+ AV_PIX_FMT_YUVA422P, |
|
201 |
+ AV_PIX_FMT_YUVA444P, |
|
202 |
+ AV_PIX_FMT_YUVJ411P, |
|
203 |
+ AV_PIX_FMT_YUVJ420P, |
|
204 |
+ AV_PIX_FMT_YUVJ422P, |
|
205 |
+ AV_PIX_FMT_YUVJ440P, |
|
206 |
+ AV_PIX_FMT_YUVJ444P, |
|
207 |
+ AV_PIX_FMT_NONE |
|
208 |
+ }; |
|
209 |
+ AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts); |
|
210 |
+ if (!fmts_list) |
|
211 |
+ return AVERROR(ENOMEM); |
|
212 |
+ return ff_set_common_formats(ctx, fmts_list); |
|
213 |
+} |
|
214 |
+ |
|
215 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame) |
|
216 |
+{ |
|
217 |
+ AVFilterContext *ctx = inlink->dst; |
|
218 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
219 |
+ ReadVitcContext *s = ctx->priv; |
|
220 |
+ int found; |
|
221 |
+ |
|
222 |
+ found = read_vitc_line(s, frame->data[0], frame->linesize[0], inlink->w, inlink->h); |
|
223 |
+ av_dict_set(&frame->metadata, "lavfi.readvitc.found", (found ? "1" : "0"), 0); |
|
224 |
+ if (found) |
|
225 |
+ av_dict_set(&frame->metadata, "lavfi.readvitc.tc_str", make_vitc_tc_string(s->tcbuf, s->line_data), 0); |
|
226 |
+ |
|
227 |
+ return ff_filter_frame(outlink, frame); |
|
228 |
+} |
|
229 |
+ |
|
230 |
+static const AVFilterPad inputs[] = { |
|
231 |
+ { |
|
232 |
+ .name = "default", |
|
233 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
234 |
+ .filter_frame = filter_frame, |
|
235 |
+ .config_props = config_props, |
|
236 |
+ }, |
|
237 |
+ { NULL } |
|
238 |
+}; |
|
239 |
+ |
|
240 |
+static const AVFilterPad outputs[] = { |
|
241 |
+ { |
|
242 |
+ .name = "default", |
|
243 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
244 |
+ }, |
|
245 |
+ { NULL } |
|
246 |
+}; |
|
247 |
+ |
|
248 |
+AVFilter ff_vf_readvitc = { |
|
249 |
+ .name = "readvitc", |
|
250 |
+ .description = NULL_IF_CONFIG_SMALL("Read vertical interval timecode and write it to frame metadata."), |
|
251 |
+ .priv_size = sizeof(ReadVitcContext), |
|
252 |
+ .priv_class = &readvitc_class, |
|
253 |
+ .inputs = inputs, |
|
254 |
+ .outputs = outputs, |
|
255 |
+ .init = init, |
|
256 |
+ .query_formats = query_formats, |
|
257 |
+}; |