X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Ftty.c;h=fbea3196fadc076c18fd8fbf67d5b6e282e73761;hb=bc70684e74a185d7b80c8b80bdedda659cb581b8;hp=8d48f2c45c12d3a58f6c754300e039ab42ac5ea8;hpb=c2631dfd0a0a12050cc1765fd41702c5e93abee5;p=ffmpeg diff --git a/libavformat/tty.c b/libavformat/tty.c index 8d48f2c45c1..fbea3196fad 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 == 0x0A || x == 0x0D || (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,26 @@ typedef struct TtyDemuxContext { AVRational framerate; /**< Set by a private option. */ } TtyDemuxContext; +static int read_probe(const AVProbeData *p) +{ + int cnt = 0; + + if (!p->buf_size) + return 0; + + for (int i = 0; i < 8 && i < p->buf_size; i++) + cnt += !!isansicode(p->buf[i]); + + if (cnt != 8) + return 0; + + for (int i = 8; i < p->buf_size; i++) + cnt += !!isansicode(p->buf[i]); + + return (cnt * 99LL / p->buf_size) * (cnt > 400) * + !!av_match_ext(p->filename, tty_extensions); +} + /** * Parse EFI header */ @@ -129,6 +156,8 @@ static int read_packet(AVFormatContext *avctx, AVPacket *pkt) pkt->size = av_get_packet(avctx->pb, pkt, n); if (pkt->size < 0) return pkt->size; + pkt->stream_index = 0; + pkt->pts = pkt->pos / s->chars_per_frame; pkt->flags |= AV_PKT_FLAG_KEY; return 0; } @@ -149,12 +178,14 @@ static const AVClass tty_demuxer_class = { .version = LIBAVUTIL_VERSION_INT, }; -AVInputFormat ff_tty_demuxer = { +const 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, + .flags = AVFMT_GENERIC_INDEX, };