FFmpeg
Loading...
Searching...
No Matches
vf_readvitc.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Tobias Rapp
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21/**
22 * @file
23 * Filter for reading the vertical interval timecode (VITC).
24 * See also https://en.wikipedia.org/wiki/Vertical_interval_timecode
25 */
26
27#include "libavutil/common.h"
28#include "libavutil/internal.h"
29#include "libavutil/opt.h"
30#include "libavutil/timecode.h"
31#include "avfilter.h"
32#include "filters.h"
33#include "video.h"
34
35#define LINE_DATA_SIZE 9
36
51
52#define OFFSET(x) offsetof(ReadVitcContext, x)
53#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54
55static 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
63
64static 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
82static inline uint8_t get_pit_avg3( uint8_t *line, int i ) {
83 return ((line[i-1] + line[i] + line[i+1]) / 3);
84}
85
86static 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
138static 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
145static 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
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
175static 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
207
209{
210 AVFilterContext *ctx = inlink->dst;
211 AVFilterLink *outlink = ctx->outputs[0];
212 ReadVitcContext *s = ctx->priv;
213 int found;
214
215 found = read_vitc_line(s, frame->data[0], frame->linesize[0], inlink->w, inlink->h);
216 av_dict_set(&frame->metadata, "lavfi.readvitc.found", (found ? "1" : "0"), 0);
217 if (found)
218 av_dict_set(&frame->metadata, "lavfi.readvitc.tc_str", make_vitc_tc_string(s->tcbuf, s->line_data), 0);
219
220 return ff_filter_frame(outlink, frame);
221}
222
223static const AVFilterPad inputs[] = {
224 {
225 .name = "default",
226 .type = AVMEDIA_TYPE_VIDEO,
227 .filter_frame = filter_frame,
228 .config_props = config_props,
229 },
230};
231
233 .p.name = "readvitc",
234 .p.description = NULL_IF_CONFIG_SMALL("Read vertical interval timecode and write it to frame metadata."),
235 .p.priv_class = &readvitc_class,
237 .priv_size = sizeof(ReadVitcContext),
241 .init = init,
242};
static const AVFilterPad inputs[]
Definition af_aap.c:299
const FFFilter ff_vf_readvitc
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define ss(width, name, subs,...)
Definition cbs_vp9.c:202
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
common internal and external API header
#define NULL
Definition coverity.c:32
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int high
Definition dovi_rpuenc.c:39
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
AVOptions.
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition pixfmt.h:97
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_NV16
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:198
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define snprintf
Definition snprintf.h:34
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
char tcbuf[AV_TIMECODE_STR_SIZE]
Definition vf_readvitc.c:49
uint8_t line_data[LINE_DATA_SIZE]
Definition vf_readvitc.c:48
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
Timecode helpers header.
#define AV_TIMECODE_STR_SIZE
Definition timecode.h:33
static enum AVPixelFormat pixel_fmts[]
Definition vf_amplify.c:52
#define LINE_DATA_SIZE
Definition vf_readvitc.c:35
static char * make_vitc_tc_string(char *buf, uint8_t *line)
static int read_vitc_line(ReadVitcContext *ctx, uint8_t *src, int line_size, int width, int height)
Definition vf_readvitc.c:86
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static unsigned bcd2uint(uint8_t high, uint8_t low)
static int config_props(AVFilterLink *inlink)
static uint8_t get_vitc_crc(uint8_t *line)
Definition vf_readvitc.c:64
static uint8_t get_pit_avg3(uint8_t *line, int i)
Definition vf_readvitc.c:82
#define OFFSET(x)
Definition vf_readvitc.c:52
static const AVOption readvitc_options[]
Definition vf_readvitc.c:55
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37