diff mbox

[FFmpeg-devel] avformat/bintext: Implement bin_probe()

Message ID 20180131142229.14055-1-michael@niedermayer.cc
State New
Headers show

Commit Message

Michael Niedermayer Jan. 31, 2018, 2:22 p.m. UTC
Fixes misdetection of sbQ9.bin

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavformat/bintext.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

Comments

Carl Eugen Hoyos Jan. 31, 2018, 4:43 p.m. UTC | #1
2018-01-31 15:22 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
> Fixes misdetection of sbQ9.bin
>
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavformat/bintext.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 48 insertions(+), 1 deletion(-)
>
> diff --git a/libavformat/bintext.c b/libavformat/bintext.c
> index 12e3bfde4d..722a40c3e5 100644
> --- a/libavformat/bintext.c
> +++ b/libavformat/bintext.c
> @@ -126,6 +126,53 @@ static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
>          par->width = fsize > 4000 ? (160<<3) : (80<<3);
>  }
>
> +static int bin_probe(AVProbeData *p)
> +{
> +    const uint8_t *d = p->buf;
> +    int magic = 0, sauce = 0;
> +    int invisible = 0;
> +    int i;
> +
> +    if (p->buf_size > 256)
> +        magic = !memcmp(d + p->buf_size - 256, next_magic, sizeof(next_magic));
> +    if (p->buf_size > 128)
> +        sauce = !memcmp(d + p->buf_size - 128, "SAUCE00", 7);
> +
> +    if (magic)
> +        return AVPROBE_SCORE_MAX / 2;

This seems too low to me or am I wrong?

> +
> +    if (av_match_ext(p->filename, "bin")) {
> +        AVCodecParameters par;
> +        int got_width = 0;
> +        par.width = par.height = 0;

> +        if (sauce)
> +            return AVPROBE_SCORE_MAX / 2;

Same here.

> +
> +        predict_width(&par, p->buf_size, got_width);
> +        if (par.width <= 0)
> +            return 0;
> +        calculate_height(&par, p->buf_size);
> +        if (par.height <= 0)
> +            return 0;
> +
> +        for (i = 0; i < p->buf_size - 256;  i+=2) {
> +            if ((d[i+1] & 15) == (d[i+1] >> 4) && d[i] && d[i] != 0xFF && d[i] != ' ') {

> +                invisible ++;

This looks unused.

> +            }
> +        }
> +
> +        if (par.width * par.height * 2 / (8*16) == p->buf_size)
> +            return AVPROBE_SCORE_MAX / 2;

Shouldn't this also be a higher score?

Thank you for looking into this issue, Carl Eugen
Michael Niedermayer Feb. 1, 2018, 1:19 a.m. UTC | #2
On Wed, Jan 31, 2018 at 05:43:24PM +0100, Carl Eugen Hoyos wrote:
> 2018-01-31 15:22 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
> > Fixes misdetection of sbQ9.bin
> >
> > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> > ---
> >  libavformat/bintext.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
> >  1 file changed, 48 insertions(+), 1 deletion(-)
> >
> > diff --git a/libavformat/bintext.c b/libavformat/bintext.c
> > index 12e3bfde4d..722a40c3e5 100644
> > --- a/libavformat/bintext.c
> > +++ b/libavformat/bintext.c
> > @@ -126,6 +126,53 @@ static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
> >          par->width = fsize > 4000 ? (160<<3) : (80<<3);
> >  }
> >
> > +static int bin_probe(AVProbeData *p)
> > +{
> > +    const uint8_t *d = p->buf;
> > +    int magic = 0, sauce = 0;
> > +    int invisible = 0;
> > +    int i;
> > +
> > +    if (p->buf_size > 256)
> > +        magic = !memcmp(d + p->buf_size - 256, next_magic, sizeof(next_magic));
> > +    if (p->buf_size > 128)
> > +        sauce = !memcmp(d + p->buf_size - 128, "SAUCE00", 7);
> > +
> > +    if (magic)
> > +        return AVPROBE_SCORE_MAX / 2;
> 
> This seems too low to me or am I wrong?
> 
> > +
> > +    if (av_match_ext(p->filename, "bin")) {
> > +        AVCodecParameters par;
> > +        int got_width = 0;
> > +        par.width = par.height = 0;
> 
> > +        if (sauce)
> > +            return AVPROBE_SCORE_MAX / 2;
> 
> Same here.

was thinking the same but that was what the filename based detection
used before.
I kind of just left it where it was, the main goal of the patch was
just to fix the misdetection not to raise the score.
But we can surely raise these.
WHat would you think is a good value ?


> 
> > +
> > +        predict_width(&par, p->buf_size, got_width);
> > +        if (par.width <= 0)
> > +            return 0;
> > +        calculate_height(&par, p->buf_size);
> > +        if (par.height <= 0)
> > +            return 0;
> > +
> > +        for (i = 0; i < p->buf_size - 256;  i+=2) {
> > +            if ((d[i+1] & 15) == (d[i+1] >> 4) && d[i] && d[i] != 0xFF && d[i] != ' ') {
> 
> > +                invisible ++;
> 
> This looks unused.

yes, iam aware, ive added it thinking it would be needed but all my test files
where detected correctly so it wasnt needed
i can remove it if people prefer ?
or it can be left so next time something gets probed wrong the code is
already there to check the next level of information.


> 
> > +            }
> > +        }
> > +
> > +        if (par.width * par.height * 2 / (8*16) == p->buf_size)
> > +            return AVPROBE_SCORE_MAX / 2;
> 
> Shouldn't this also be a higher score?

This one here, no. This is a rather weak test. It looks stronger than it
is
Its weak as width and height are computed currently from the size


> 
> Thank you for looking into this issue, Carl Eugen
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Carl Eugen Hoyos Feb. 5, 2018, 1:02 p.m. UTC | #3
2018-02-01 2:19 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
> On Wed, Jan 31, 2018 at 05:43:24PM +0100, Carl Eugen Hoyos wrote:
>> 2018-01-31 15:22 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
>> > Fixes misdetection of sbQ9.bin
>> >
>> > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
>> > ---
>> >  libavformat/bintext.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
>> >  1 file changed, 48 insertions(+), 1 deletion(-)
>> >
>> > diff --git a/libavformat/bintext.c b/libavformat/bintext.c
>> > index 12e3bfde4d..722a40c3e5 100644
>> > --- a/libavformat/bintext.c
>> > +++ b/libavformat/bintext.c
>> > @@ -126,6 +126,53 @@ static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
>> >          par->width = fsize > 4000 ? (160<<3) : (80<<3);
>> >  }
>> >
>> > +static int bin_probe(AVProbeData *p)
>> > +{
>> > +    const uint8_t *d = p->buf;
>> > +    int magic = 0, sauce = 0;
>> > +    int invisible = 0;
>> > +    int i;
>> > +
>> > +    if (p->buf_size > 256)
>> > +        magic = !memcmp(d + p->buf_size - 256, next_magic, sizeof(next_magic));
>> > +    if (p->buf_size > 128)
>> > +        sauce = !memcmp(d + p->buf_size - 128, "SAUCE00", 7);
>> > +
>> > +    if (magic)
>> > +        return AVPROBE_SCORE_MAX / 2;
>>
>> This seems too low to me or am I wrong?
>>
>> > +
>> > +    if (av_match_ext(p->filename, "bin")) {
>> > +        AVCodecParameters par;
>> > +        int got_width = 0;
>> > +        par.width = par.height = 0;
>>
>> > +        if (sauce)
>> > +            return AVPROBE_SCORE_MAX / 2;
>>
>> Same here.
>
> was thinking the same but that was what the filename based detection
> used before.
> I kind of just left it where it was, the main goal of the patch was
> just to fix the misdetection not to raise the score.
> But we can surely raise these.
> WHat would you think is a good value ?

I suggest to make it EXTENSION + 1 to have a still low score
but better reflect the high probability.

No more comments, Carl Eugen
Michael Niedermayer Feb. 6, 2018, 12:11 a.m. UTC | #4
On Mon, Feb 05, 2018 at 02:02:56PM +0100, Carl Eugen Hoyos wrote:
> 2018-02-01 2:19 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
> > On Wed, Jan 31, 2018 at 05:43:24PM +0100, Carl Eugen Hoyos wrote:
> >> 2018-01-31 15:22 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
> >> > Fixes misdetection of sbQ9.bin
> >> >
> >> > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> >> > ---
> >> >  libavformat/bintext.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
> >> >  1 file changed, 48 insertions(+), 1 deletion(-)
> >> >
> >> > diff --git a/libavformat/bintext.c b/libavformat/bintext.c
> >> > index 12e3bfde4d..722a40c3e5 100644
> >> > --- a/libavformat/bintext.c
> >> > +++ b/libavformat/bintext.c
> >> > @@ -126,6 +126,53 @@ static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
> >> >          par->width = fsize > 4000 ? (160<<3) : (80<<3);
> >> >  }
> >> >
> >> > +static int bin_probe(AVProbeData *p)
> >> > +{
> >> > +    const uint8_t *d = p->buf;
> >> > +    int magic = 0, sauce = 0;
> >> > +    int invisible = 0;
> >> > +    int i;
> >> > +
> >> > +    if (p->buf_size > 256)
> >> > +        magic = !memcmp(d + p->buf_size - 256, next_magic, sizeof(next_magic));
> >> > +    if (p->buf_size > 128)
> >> > +        sauce = !memcmp(d + p->buf_size - 128, "SAUCE00", 7);
> >> > +
> >> > +    if (magic)
> >> > +        return AVPROBE_SCORE_MAX / 2;
> >>
> >> This seems too low to me or am I wrong?
> >>
> >> > +
> >> > +    if (av_match_ext(p->filename, "bin")) {
> >> > +        AVCodecParameters par;
> >> > +        int got_width = 0;
> >> > +        par.width = par.height = 0;
> >>
> >> > +        if (sauce)
> >> > +            return AVPROBE_SCORE_MAX / 2;
> >>
> >> Same here.
> >
> > was thinking the same but that was what the filename based detection
> > used before.
> > I kind of just left it where it was, the main goal of the patch was
> > just to fix the misdetection not to raise the score.
> > But we can surely raise these.
> > WHat would you think is a good value ?
> 
> I suggest to make it EXTENSION + 1 to have a still low score
> but better reflect the high probability.

ok, will push with that

thx

[...]
diff mbox

Patch

diff --git a/libavformat/bintext.c b/libavformat/bintext.c
index 12e3bfde4d..722a40c3e5 100644
--- a/libavformat/bintext.c
+++ b/libavformat/bintext.c
@@ -126,6 +126,53 @@  static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
         par->width = fsize > 4000 ? (160<<3) : (80<<3);
 }
 
+static int bin_probe(AVProbeData *p)
+{
+    const uint8_t *d = p->buf;
+    int magic = 0, sauce = 0;
+    int invisible = 0;
+    int i;
+
+    if (p->buf_size > 256)
+        magic = !memcmp(d + p->buf_size - 256, next_magic, sizeof(next_magic));
+    if (p->buf_size > 128)
+        sauce = !memcmp(d + p->buf_size - 128, "SAUCE00", 7);
+
+    if (magic)
+        return AVPROBE_SCORE_MAX / 2;
+
+    if (av_match_ext(p->filename, "bin")) {
+        AVCodecParameters par;
+        int got_width = 0;
+        par.width = par.height = 0;
+        if (sauce)
+            return AVPROBE_SCORE_MAX / 2;
+
+        predict_width(&par, p->buf_size, got_width);
+        if (par.width <= 0)
+            return 0;
+        calculate_height(&par, p->buf_size);
+        if (par.height <= 0)
+            return 0;
+
+        for (i = 0; i < p->buf_size - 256;  i+=2) {
+            if ((d[i+1] & 15) == (d[i+1] >> 4) && d[i] && d[i] != 0xFF && d[i] != ' ') {
+                invisible ++;
+            }
+        }
+
+        if (par.width * par.height * 2 / (8*16) == p->buf_size)
+            return AVPROBE_SCORE_MAX / 2;
+        return 1;
+    }
+
+    if (sauce)
+        return 1;
+
+    return 0;
+}
+
+
 static int bintext_read_header(AVFormatContext *s)
 {
     BinDemuxContext *bin = s->priv_data;
@@ -343,9 +390,9 @@  AVInputFormat ff_bintext_demuxer = {
     .name           = "bin",
     .long_name      = NULL_IF_CONFIG_SMALL("Binary text"),
     .priv_data_size = sizeof(BinDemuxContext),
+    .read_probe     = bin_probe,
     .read_header    = bintext_read_header,
     .read_packet    = read_packet,
-    .extensions     = "bin",
     .priv_class     = CLASS("Binary text demuxer"),
 };
 #endif