diff mbox

[FFmpeg-devel] avfilter: add EIA-608 line extractor

Message ID 20170117194338.11992-1-onemda@gmail.com
State Accepted
Headers show

Commit Message

Paul B Mahol Jan. 17, 2017, 7:43 p.m. UTC
Signed-off-by: Dave Rice <dave@dericed.com>
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 doc/filters.texi            |  70 ++++++++++++
 libavfilter/Makefile        |   1 +
 libavfilter/allfilters.c    |   1 +
 libavfilter/vf_readeia608.c | 268 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 340 insertions(+)
 create mode 100644 libavfilter/vf_readeia608.c

Comments

Kieran Kunhya Jan. 17, 2017, 8:17 p.m. UTC | #1
On Tue, 17 Jan 2017 at 19:44 Paul B Mahol <onemda@gmail.com> wrote:

> Signed-off-by: Dave Rice <dave@dericed.com>
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>

Is this better than libzvbi?

Kieran
Moritz Barsnick Jan. 17, 2017, 8:46 p.m. UTC | #2
On Tue, Jan 17, 2017 at 20:43:38 +0100, Paul B Mahol wrote:
> +    .description   = NULL_IF_CONFIG_SMALL("Read EIA-608 Closed Caption codes from input video and write it to frame metadata."),
                                                                                                           ^ them (as "codes" is plural)

I can't really find anything else, but haven't had too much of a
functional look, mostly at style and strings and doc.

Moritz
Paul B Mahol Jan. 17, 2017, 9:04 p.m. UTC | #3
On 1/17/17, Kieran Kunhya <kierank@obe.tv> wrote:
> On Tue, 17 Jan 2017 at 19:44 Paul B Mahol <onemda@gmail.com> wrote:
>
>> Signed-off-by: Dave Rice <dave@dericed.com>
>> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>>
>
> Is this better than libzvbi?

How I can test libzvbi?
Paul B Mahol Jan. 17, 2017, 10:31 p.m. UTC | #4
On 1/17/17, Kieran Kunhya <kierank@obe.tv> wrote:
> On Tue, 17 Jan 2017 at 19:44 Paul B Mahol <onemda@gmail.com> wrote:
>
>> Signed-off-by: Dave Rice <dave@dericed.com>
>> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>>
>
> Is this better than libzvbi?

The only code that does similar thing is contrib/ntsc-cc.c

So its not part of library anyway.
Kieran Kunhya Jan. 17, 2017, 11:53 p.m. UTC | #5
On Tue, 17 Jan 2017, 22:31 Paul B Mahol, <onemda@gmail.com> wrote:

> On 1/17/17, Kieran Kunhya <kierank@obe.tv> wrote:
> > On Tue, 17 Jan 2017 at 19:44 Paul B Mahol <onemda@gmail.com> wrote:
> >
> >> Signed-off-by: Dave Rice <dave@dericed.com>
> >> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> >>
> >
> > Is this better than libzvbi?
>
> The only code that does similar thing is contrib/ntsc-cc.c
>
> So its not part of library anyway.
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


The API supports VBI extraction (and creation).

Kieran

>
>
Tobias Rapp Jan. 18, 2017, 7:50 a.m. UTC | #6
On 17.01.2017 20:43, Paul B Mahol wrote:
> Signed-off-by: Dave Rice <dave@dericed.com>
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  doc/filters.texi            |  70 ++++++++++++
>  libavfilter/Makefile        |   1 +
>  libavfilter/allfilters.c    |   1 +
>  libavfilter/vf_readeia608.c | 268 ++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 340 insertions(+)
>  create mode 100644 libavfilter/vf_readeia608.c
>
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 94548b1..57170e3 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -11249,6 +11249,76 @@ less than @code{0}, the filter will try to use a good random seed on a
>  best effort basis.
>  @end table
>
> +@section readeia608
> +
> +Read closed captioning (EIA-608) information from the top lines of a video frame.
> +
> +This filter adds frame metadata for @code{lavfi.readeia608.cc.X} and
> +@code{lavfi.readeia608.line.X}, where @code{X} is the number of the identified line
> +with EIA-608 data (starting from 0). A description of each metadata value follows:
> +
> +@table @option
> +@item lavfi.readeia608.cc.X
> +The two bytes stored as EIA-608 data (printed in hexadecimal).
> +
> +@item lavfi.readeia608.line.X
> +The number of the line on which the EIA-608 data was identified and read.
> +@end table

 From what I see this is a pair of values that belong together.

lavfi.readeia608.X.cc
lavfi.readeia608.X.line

would indicate that better, IMHO.

> +
> +This filter accepts the following options:
> +
> +@table @option
> +@item start
> +Set the line to start scanning for EIA-608 data. Default is @code{0}.
> +
> +@item end
> +Set the line to end scanning for EIA-608 data. Default is @code{29}.

What about naming them "scan_min/scan_max" for alignment with "readvitc" 
filter?

> +
> +@item mac
> +Set minimal acceptable amplitude change for sync codes detection.
> +Default is @code{0.2}. Allowed range is @code{[0.001 - 1]}.
> +
> +@item spw
> +Set the ratio of width reserved for sync code detection.
> +Default is @code{0.27}. Allowed range is @code{[0.01 - 0.7]}.
> +
> +@item mhd
> +Set the max peaks height difference for sync code detection.
> +Default is @code{0.1}. Allowed range is @code{[0.0 - 0.5]}.
> +
> +@item mpd
> +Set max peaks period difference for sync code detection.
> +Default is @code{0.1}. Allowed range is @code{[0.0 - 0.5]}.
> +
> +@item msd
> +Set the first two max start code bits differences.
> +Default is @code{0.02}. Allowed range is @code{[0.0 - 0.5]}.
> +
> +@item bhd
> +Set the minimum ratio of bits height compared to 3rd start code bit.
> +Default is @code{0.75}. Allowed range is @code{[0.01 - 1]}.
> +
> +@item wth
> +Set the white color threshold. Default is @code{0.35}. Allowed range is @code{[0.1 - 1]}.
> +
> +@item bth
> +Set the black color threshold. Default is @code{0.15}. Allowed range is @code{[0.0 - 0.5]}.

"thr_w/thr_b"?

> +
> +@item chp
> +Enable checking the parity bit. In the event of a parity error, the filter will output
> +@code{0x00} for that character. Default is false.
> +@end table
> +
> +@subsection Examples
> +
> +@itemize
> +@item
> +Output a csv with presentation time and the first two lines of identified EIA-608 captioning data.
> +@example
> +ffprobe -f lavfi -i movie=captioned_video.mov,readeia608 -show_entries frame=pkt_pts_time:frame_tags=lavfi.readeia608.cc.0,lavfi.readeia608.cc.1 -of csv
> +@end example
> +@end itemize
> +
>  @section readvitc
>
>  Read vertical interval timecode (VITC) information from the top lines of a
>
> [...]

BTW: Do you have some example file available for testing?

Regards,
Tobias
Paul B Mahol Jan. 18, 2017, 8:23 a.m. UTC | #7
On 1/18/17, Tobias Rapp <t.rapp@noa-archive.com> wrote:
> On 17.01.2017 20:43, Paul B Mahol wrote:
>> Signed-off-by: Dave Rice <dave@dericed.com>
>> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>> ---
>>  doc/filters.texi            |  70 ++++++++++++
>>  libavfilter/Makefile        |   1 +
>>  libavfilter/allfilters.c    |   1 +
>>  libavfilter/vf_readeia608.c | 268
>> ++++++++++++++++++++++++++++++++++++++++++++
>>  4 files changed, 340 insertions(+)
>>  create mode 100644 libavfilter/vf_readeia608.c
>>
>> diff --git a/doc/filters.texi b/doc/filters.texi
>> index 94548b1..57170e3 100644
>> --- a/doc/filters.texi
>> +++ b/doc/filters.texi
>> @@ -11249,6 +11249,76 @@ less than @code{0}, the filter will try to use a
>> good random seed on a
>>  best effort basis.
>>  @end table
>>
>> +@section readeia608
>> +
>> +Read closed captioning (EIA-608) information from the top lines of a
>> video frame.
>> +
>> +This filter adds frame metadata for @code{lavfi.readeia608.cc.X} and
>> +@code{lavfi.readeia608.line.X}, where @code{X} is the number of the
>> identified line
>> +with EIA-608 data (starting from 0). A description of each metadata value
>> follows:
>> +
>> +@table @option
>> +@item lavfi.readeia608.cc.X
>> +The two bytes stored as EIA-608 data (printed in hexadecimal).
>> +
>> +@item lavfi.readeia608.line.X
>> +The number of the line on which the EIA-608 data was identified and read.
>> +@end table
>
>  From what I see this is a pair of values that belong together.
>
> lavfi.readeia608.X.cc
> lavfi.readeia608.X.line
>
> would indicate that better, IMHO.

Changed.

>
>> +
>> +This filter accepts the following options:
>> +
>> +@table @option
>> +@item start
>> +Set the line to start scanning for EIA-608 data. Default is @code{0}.
>> +
>> +@item end
>> +Set the line to end scanning for EIA-608 data. Default is @code{29}.
>
> What about naming them "scan_min/scan_max" for alignment with "readvitc"
> filter?

OK.

>
>> +
>> +@item mac
>> +Set minimal acceptable amplitude change for sync codes detection.
>> +Default is @code{0.2}. Allowed range is @code{[0.001 - 1]}.
>> +
>> +@item spw
>> +Set the ratio of width reserved for sync code detection.
>> +Default is @code{0.27}. Allowed range is @code{[0.01 - 0.7]}.
>> +
>> +@item mhd
>> +Set the max peaks height difference for sync code detection.
>> +Default is @code{0.1}. Allowed range is @code{[0.0 - 0.5]}.
>> +
>> +@item mpd
>> +Set max peaks period difference for sync code detection.
>> +Default is @code{0.1}. Allowed range is @code{[0.0 - 0.5]}.
>> +
>> +@item msd
>> +Set the first two max start code bits differences.
>> +Default is @code{0.02}. Allowed range is @code{[0.0 - 0.5]}.
>> +
>> +@item bhd
>> +Set the minimum ratio of bits height compared to 3rd start code bit.
>> +Default is @code{0.75}. Allowed range is @code{[0.01 - 1]}.
>> +
>> +@item wth
>> +Set the white color threshold. Default is @code{0.35}. Allowed range is
>> @code{[0.1 - 1]}.
>> +
>> +@item bth
>> +Set the black color threshold. Default is @code{0.15}. Allowed range is
>> @code{[0.0 - 0.5]}.
>
> "thr_w/thr_b"?

Could change...

>
>> +
>> +@item chp
>> +Enable checking the parity bit. In the event of a parity error, the
>> filter will output
>> +@code{0x00} for that character. Default is false.
>> +@end table
>> +
>> +@subsection Examples
>> +
>> +@itemize
>> +@item
>> +Output a csv with presentation time and the first two lines of identified
>> EIA-608 captioning data.
>> +@example
>> +ffprobe -f lavfi -i movie=captioned_video.mov,readeia608 -show_entries
>> frame=pkt_pts_time:frame_tags=lavfi.readeia608.cc.0,lavfi.readeia608.cc.1
>> -of csv
>> +@end example
>> +@end itemize
>> +
>>  @section readvitc
>>
>>  Read vertical interval timecode (VITC) information from the top lines of
>> a
>>
>> [...]
>
> BTW: Do you have some example file available for testing?

Yes, but I'm afraid I can not share them.

Here is one available:

https://archive.org/details/cc_sample
Paul B Mahol Jan. 18, 2017, 8:40 a.m. UTC | #8
On 1/18/17, Kieran Kunhya <kierank@obe.tv> wrote:
> On Tue, 17 Jan 2017, 22:31 Paul B Mahol, <onemda@gmail.com> wrote:
>
>> On 1/17/17, Kieran Kunhya <kierank@obe.tv> wrote:
>> > On Tue, 17 Jan 2017 at 19:44 Paul B Mahol <onemda@gmail.com> wrote:
>> >
>> >> Signed-off-by: Dave Rice <dave@dericed.com>
>> >> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>> >>
>> >
>> > Is this better than libzvbi?
>>
>> The only code that does similar thing is contrib/ntsc-cc.c
>>
>> So its not part of library anyway.
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
>
> The API supports VBI extraction (and creation).

I'm not aware of it. It works only with devices.
Kieran Kunhya Jan. 18, 2017, 5:35 p.m. UTC | #9
On Wed, 18 Jan 2017 at 08:40 Paul B Mahol <onemda@gmail.com> wrote:

> On 1/18/17, Kieran Kunhya <kierank@obe.tv> wrote:
> > On Tue, 17 Jan 2017, 22:31 Paul B Mahol, <onemda@gmail.com> wrote:
> >
> >> On 1/17/17, Kieran Kunhya <kierank@obe.tv> wrote:
> >> > On Tue, 17 Jan 2017 at 19:44 Paul B Mahol <onemda@gmail.com> wrote:
> >> >
> >> >> Signed-off-by: Dave Rice <dave@dericed.com>
> >> >> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> >> >>
> >> >
> >> > Is this better than libzvbi?
> >>
> >> The only code that does similar thing is contrib/ntsc-cc.c
> >>
> >> So its not part of library anyway.
> >> _______________________________________________
> >> ffmpeg-devel mailing list
> >> ffmpeg-devel@ffmpeg.org
> >> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> >
> > The API supports VBI extraction (and creation).
>
> I'm not aware of it. It works only with devices.
>

Here is an example for WSS:

https://github.com/kierank/libzvbi-obe-dev/blob/master/examples/wss.c#L175

CC is similar.

Kieran
Paul B Mahol Jan. 23, 2017, 3:11 p.m. UTC | #10
On 1/17/17, Paul B Mahol <onemda@gmail.com> wrote:
> Signed-off-by: Dave Rice <dave@dericed.com>
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  doc/filters.texi            |  70 ++++++++++++
>  libavfilter/Makefile        |   1 +
>  libavfilter/allfilters.c    |   1 +
>  libavfilter/vf_readeia608.c | 268
> ++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 340 insertions(+)
>  create mode 100644 libavfilter/vf_readeia608.c
>

Gonna push this soon.
Marton Balint Jan. 23, 2017, 10 p.m. UTC | #11
On Mon, 23 Jan 2017, Paul B Mahol wrote:

> On 1/17/17, Paul B Mahol <onemda@gmail.com> wrote:
>> Signed-off-by: Dave Rice <dave@dericed.com>
>> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>> ---
>>  doc/filters.texi            |  70 ++++++++++++
>>  libavfilter/Makefile        |   1 +
>>  libavfilter/allfilters.c    |   1 +
>>  libavfilter/vf_readeia608.c | 268
>> ++++++++++++++++++++++++++++++++++++++++++++
>>  4 files changed, 340 insertions(+)
>>  create mode 100644 libavfilter/vf_readeia608.c
>>
>
> Gonna push this soon.

You may consider using the bit slicer only from libzvbi, I have used it 
for teletext in libavdevice/decklink_dec.cpp, in the 
teletext_data_unit_from_vbi_data function.

I have copied the initialization variables from the zvbi source 
raw_decoder.c, maybe you can do the same with CC.

Regards,
Marton
Paul B Mahol Jan. 24, 2017, 8:26 a.m. UTC | #12
On 1/23/17, Marton Balint <cus@passwd.hu> wrote:
>
> On Mon, 23 Jan 2017, Paul B Mahol wrote:
>
>> On 1/17/17, Paul B Mahol <onemda@gmail.com> wrote:
>>> Signed-off-by: Dave Rice <dave@dericed.com>
>>> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>>> ---
>>>  doc/filters.texi            |  70 ++++++++++++
>>>  libavfilter/Makefile        |   1 +
>>>  libavfilter/allfilters.c    |   1 +
>>>  libavfilter/vf_readeia608.c | 268
>>> ++++++++++++++++++++++++++++++++++++++++++++
>>>  4 files changed, 340 insertions(+)
>>>  create mode 100644 libavfilter/vf_readeia608.c
>>>
>>
>> Gonna push this soon.
>
> You may consider using the bit slicer only from libzvbi, I have used it
> for teletext in libavdevice/decklink_dec.cpp, in the
> teletext_data_unit_from_vbi_data function.
>
> I have copied the initialization variables from the zvbi source
> raw_decoder.c, maybe you can do the same with CC.

IMHO, its too limited.
diff mbox

Patch

diff --git a/doc/filters.texi b/doc/filters.texi
index 94548b1..57170e3 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -11249,6 +11249,76 @@  less than @code{0}, the filter will try to use a good random seed on a
 best effort basis.
 @end table
 
+@section readeia608
+
+Read closed captioning (EIA-608) information from the top lines of a video frame.
+
+This filter adds frame metadata for @code{lavfi.readeia608.cc.X} and
+@code{lavfi.readeia608.line.X}, where @code{X} is the number of the identified line
+with EIA-608 data (starting from 0). A description of each metadata value follows:
+
+@table @option
+@item lavfi.readeia608.cc.X
+The two bytes stored as EIA-608 data (printed in hexadecimal).
+
+@item lavfi.readeia608.line.X
+The number of the line on which the EIA-608 data was identified and read.
+@end table
+
+This filter accepts the following options:
+
+@table @option
+@item start
+Set the line to start scanning for EIA-608 data. Default is @code{0}.
+
+@item end
+Set the line to end scanning for EIA-608 data. Default is @code{29}.
+
+@item mac
+Set minimal acceptable amplitude change for sync codes detection.
+Default is @code{0.2}. Allowed range is @code{[0.001 - 1]}.
+
+@item spw
+Set the ratio of width reserved for sync code detection.
+Default is @code{0.27}. Allowed range is @code{[0.01 - 0.7]}.
+
+@item mhd
+Set the max peaks height difference for sync code detection.
+Default is @code{0.1}. Allowed range is @code{[0.0 - 0.5]}.
+
+@item mpd
+Set max peaks period difference for sync code detection.
+Default is @code{0.1}. Allowed range is @code{[0.0 - 0.5]}.
+
+@item msd
+Set the first two max start code bits differences.
+Default is @code{0.02}. Allowed range is @code{[0.0 - 0.5]}.
+
+@item bhd
+Set the minimum ratio of bits height compared to 3rd start code bit.
+Default is @code{0.75}. Allowed range is @code{[0.01 - 1]}.
+
+@item wth
+Set the white color threshold. Default is @code{0.35}. Allowed range is @code{[0.1 - 1]}.
+
+@item bth
+Set the black color threshold. Default is @code{0.15}. Allowed range is @code{[0.0 - 0.5]}.
+
+@item chp
+Enable checking the parity bit. In the event of a parity error, the filter will output
+@code{0x00} for that character. Default is false.
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Output a csv with presentation time and the first two lines of identified EIA-608 captioning data.
+@example
+ffprobe -f lavfi -i movie=captioned_video.mov,readeia608 -show_entries frame=pkt_pts_time:frame_tags=lavfi.readeia608.cc.0,lavfi.readeia608.cc.1 -of csv
+@end example
+@end itemize
+
 @section readvitc
 
 Read vertical interval timecode (VITC) information from the top lines of a
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 9ab65eb..5afb72a 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -247,6 +247,7 @@  OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o framesync.
 OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
 OBJS-$(CONFIG_QP_FILTER)                     += vf_qp.o
 OBJS-$(CONFIG_RANDOM_FILTER)                 += vf_random.o
+OBJS-$(CONFIG_READEIA608_FILTER)             += vf_readeia608.o
 OBJS-$(CONFIG_READVITC_FILTER)               += vf_readvitc.o
 OBJS-$(CONFIG_REALTIME_FILTER)               += f_realtime.o
 OBJS-$(CONFIG_REMAP_FILTER)                  += vf_remap.o framesync.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 2c37818..4664245 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -262,6 +262,7 @@  void avfilter_register_all(void)
     REGISTER_FILTER(PULLUP,         pullup,         vf);
     REGISTER_FILTER(QP,             qp,             vf);
     REGISTER_FILTER(RANDOM,         random,         vf);
+    REGISTER_FILTER(READEIA608,     readeia608,     vf);
     REGISTER_FILTER(READVITC,       readvitc,       vf);
     REGISTER_FILTER(REALTIME,       realtime,       vf);
     REGISTER_FILTER(REMAP,          remap,          vf);
diff --git a/libavfilter/vf_readeia608.c b/libavfilter/vf_readeia608.c
new file mode 100644
index 0000000..4fa1446
--- /dev/null
+++ b/libavfilter/vf_readeia608.c
@@ -0,0 +1,268 @@ 
+/*
+ * Copyright (c) 2017 Paul B Mahol
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Filter for reading closed captioning data (EIA-608).
+ * See also https://en.wikipedia.org/wiki/EIA-608
+ */
+
+#include <string.h>
+
+#include "libavutil/internal.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/timestamp.h"
+
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+#define FALL 0
+#define RISE 1
+
+typedef struct ReadEIA608Context {
+    const AVClass *class;
+    int start, end;
+    int min_range;
+    int max_peak_diff;
+    int max_period_diff;
+    int max_start_diff;
+    int nb_found;
+    int white;
+    int black;
+    float mpd, mhd, msd, mac, spw, bhd, wth, bth;
+    int chp;
+} ReadEIA608Context;
+
+#define OFFSET(x) offsetof(ReadEIA608Context, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption readeia608_options[] = {
+    { "start", "set from which line to scan for codes",                            OFFSET(start), AV_OPT_TYPE_INT,   {.i64=0},       0, INT_MAX, FLAGS },
+    { "end",   "set to which line to scan for codes",                              OFFSET(end),   AV_OPT_TYPE_INT,   {.i64=29},      0, INT_MAX, FLAGS },
+    { "mac",   "set minimal acceptable amplitude change for sync codes detection", OFFSET(mac),   AV_OPT_TYPE_FLOAT, {.dbl=.2},  0.001,       1, FLAGS },
+    { "spw",   "set ratio of width reserved for sync code detection",              OFFSET(spw),   AV_OPT_TYPE_FLOAT, {.dbl=.27},   0.1,     0.7, FLAGS },
+    { "mhd",   "set max peaks height difference for sync code detection",          OFFSET(mhd),   AV_OPT_TYPE_FLOAT, {.dbl=.1},      0,     0.5, FLAGS },
+    { "mpd",   "set max peaks period difference for sync code detection",          OFFSET(mpd),   AV_OPT_TYPE_FLOAT, {.dbl=.1},      0,     0.5, FLAGS },
+    { "msd",   "set first two max start code bits differences",                    OFFSET(msd),   AV_OPT_TYPE_FLOAT, {.dbl=.02},     0,     0.5, FLAGS },
+    { "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 },
+    { "wth",   "set white color threshold",                                        OFFSET(wth),   AV_OPT_TYPE_FLOAT, {.dbl=.35},   0.1,       1, FLAGS },
+    { "bth",   "set black color threshold",                                        OFFSET(bth),   AV_OPT_TYPE_FLOAT, {.dbl=.15},     0,     0.5, FLAGS },
+    { "chp",   "check and apply parity bit",                                       OFFSET(chp),   AV_OPT_TYPE_BOOL,  {.i64= 0},      0,       1, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(readeia608);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pixel_fmts[] = {
+        AV_PIX_FMT_GRAY8,
+        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_NONE
+    };
+    AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, formats);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    AVFilterContext *ctx = inlink->dst;
+    ReadEIA608Context *s = ctx->priv;
+    int depth = desc->comp[0].depth;
+
+    if (s->end >= inlink->h) {
+        av_log(ctx, AV_LOG_WARNING, "Last line to scan too large, clipping.\n");
+        s->end = inlink->h - 1;
+    }
+
+    if (s->start > s->end) {
+        av_log(ctx, AV_LOG_ERROR, "Invalid range.\n");
+        return AVERROR(EINVAL);
+    }
+
+    s->min_range = s->mac * ((1 << depth) - 1);
+    s->max_peak_diff = s->mhd * ((1 << depth) - 1);
+    s->max_period_diff = s->mpd * ((1 << depth) - 1);
+    s->max_start_diff = s->msd * ((1 << depth) - 1);
+    s->white = s->wth * ((1 << depth) - 1);
+    s->black = s->bth * ((1 << depth) - 1);
+
+    return 0;
+}
+
+static void extract_line(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *in, int line)
+{
+    ReadEIA608Context *s = ctx->priv;
+    int max = 0, min = INT_MAX;
+    int i, ch, range = 0;
+    const uint8_t *src;
+    uint16_t clock[8][2] = { { 0 } };
+    const int sync_width = s->spw * in->width;
+    int last = 0, peaks = 0, max_peak_diff = 0, dir = RISE;
+    const int width_per_bit = (in->width - sync_width) / 19;
+    uint8_t byte[2] = { 0 };
+    int s1, s2, s3, parity;
+
+    src = &in->data[0][line * in->linesize[0]];
+    for (i = 0; i < sync_width; i++) {
+        max = FFMAX(max, src[i]);
+        min = FFMIN(min, src[i]);
+    }
+
+    range = max - min;
+    if (range < s->min_range)
+        return;
+
+    for (i = 0; i < sync_width; i++) {
+        int Y = src[i];
+
+        if (dir == RISE) {
+            if (Y < last) {
+                dir = FALL;
+                if (last >= s->white) {
+                    clock[peaks][0] = last;
+                    clock[peaks][1] = i;
+                    peaks++;
+                    if (peaks > 7)
+                        break;
+                }
+            }
+        } else if (dir == FALL) {
+            if (Y > last && last <= s->black) {
+                dir = RISE;
+            }
+        }
+        last = Y;
+    }
+
+    if (peaks != 7)
+        return;
+
+    for (i = 1; i < 7; i++)
+        max_peak_diff = FFMAX(max_peak_diff, FFABS(clock[i][0] - clock[i-1][0]));
+
+    if (max_peak_diff > s->max_peak_diff)
+        return;
+
+    max = 0; min = INT_MAX;
+    for (i = 1; i < 7; i++) {
+        max = FFMAX(max, FFABS(clock[i][1] - clock[i-1][1]));
+        min = FFMIN(min, FFABS(clock[i][1] - clock[i-1][1]));
+    }
+
+    range = max - min;
+    if (range > s->max_period_diff)
+        return;
+
+    s1 = src[sync_width + width_per_bit * 0 + width_per_bit / 2];
+    s2 = src[sync_width + width_per_bit * 1 + width_per_bit / 2];
+    s3 = src[sync_width + width_per_bit * 2 + width_per_bit / 2];
+
+    if (FFABS(s1 - s2) > s->max_start_diff || s1 > s->black || s2 > s->black || s3 < s->white)
+        return;
+
+    for (ch = 0; ch < 2; ch++) {
+        for (parity = 0, i = 0; i < 8; i++) {
+            int b = src[sync_width + width_per_bit * (i + 3 + 8 * ch) + width_per_bit / 2];
+
+            if (b - s1 > (s3 - s1) * s->bhd) {
+                b = 1;
+                parity++;
+            } else {
+                b = 0;
+            }
+            byte[ch] |= b << i;
+        }
+
+        if (s->chp) {
+            if (!(parity & 1)) {
+                byte[ch] = 0;
+            }
+        }
+    }
+
+    {
+        uint8_t key[128], value[128];
+
+        snprintf(key, sizeof(key), "lavfi.readeia608.cc.%d", s->nb_found);
+        snprintf(value, sizeof(value), "0x%02X%02X", byte[0], byte[1]);
+        av_dict_set(avpriv_frame_get_metadatap(in), key, value, 0);
+
+        snprintf(key, sizeof(key), "lavfi.readeia608.line.%d", s->nb_found);
+        snprintf(value, sizeof(value), "%d", line);
+        av_dict_set(avpriv_frame_get_metadatap(in), key, value, 0);
+    }
+
+    s->nb_found++;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx  = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    ReadEIA608Context *s = ctx->priv;
+    int i;
+
+    s->nb_found = 0;
+    for (i = s->start; i <= s->end; i++)
+        extract_line(ctx, inlink, in, i);
+
+    return ff_filter_frame(outlink, in);
+}
+
+static const AVFilterPad readeia608_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad readeia608_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_readeia608 = {
+    .name          = "readeia608",
+    .description   = NULL_IF_CONFIG_SMALL("Read EIA-608 Closed Caption codes from input video and write it to frame metadata."),
+    .priv_size     = sizeof(ReadEIA608Context),
+    .priv_class    = &readeia608_class,
+    .query_formats = query_formats,
+    .inputs        = readeia608_inputs,
+    .outputs       = readeia608_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};