diff mbox series

[FFmpeg-devel] avformat/tty: add probe function

Message ID 20200128212741.4890-1-onemda@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel] avformat/tty: add probe function
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork success Applied patch
andriy/ffmpeg-patchwork success Configure finished
andriy/ffmpeg-patchwork success Make finished
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Paul B Mahol Jan. 28, 2020, 9:27 p.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavformat/tty.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

Comments

Carl Eugen Hoyos Jan. 28, 2020, 10:24 p.m. UTC | #1
Am Di., 28. Jan. 2020 um 22:28 Uhr schrieb Paul B Mahol <onemda@gmail.com>:

> +static int isansicode(int x)
> +{
> +    return (x == 0x1B) || (x >= 0x20 && x < 0x7f);

I would have expected at least CR and LF, maybe
also the tabulator, to be valid characters.

Carl Eugen
diff mbox series

Patch

diff --git a/libavformat/tty.c b/libavformat/tty.c
index 8d48f2c45c..69aad64790 100644
--- a/libavformat/tty.c
+++ b/libavformat/tty.c
@@ -34,6 +34,13 @@ 
 #include "internal.h"
 #include "sauce.h"
 
+static int isansicode(int x)
+{
+    return (x == 0x1B) || (x >= 0x20 && x < 0x7f);
+}
+
+static const char tty_extensions[31] = "ans,art,asc,diz,ice,nfo,txt,vt";
+
 typedef struct TtyDemuxContext {
     AVClass *class;
     int chars_per_frame;
@@ -42,6 +49,17 @@  typedef struct TtyDemuxContext {
     AVRational framerate; /**< Set by a private option. */
 } TtyDemuxContext;
 
+static int read_probe(const AVProbeData *p)
+{
+    int cnt = 0;
+
+    for (int i = 0; i < p->buf_size; i++)
+        cnt += !!isansicode(p->buf[i]);
+
+    return (cnt * 100LL / p->buf_size) * (cnt > 400) *
+        !!av_match_ext(p->filename, tty_extensions);
+}
+
 /**
  * Parse EFI header
  */
@@ -153,8 +171,9 @@  AVInputFormat ff_tty_demuxer = {
     .name           = "tty",
     .long_name      = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
     .priv_data_size = sizeof(TtyDemuxContext),
+    .read_probe     = read_probe,
     .read_header    = read_header,
     .read_packet    = read_packet,
-    .extensions     = "ans,art,asc,diz,ice,nfo,txt,vt",
+    .extensions     = tty_extensions,
     .priv_class     = &tty_demuxer_class,
 };