]> git.sesse.net Git - ffmpeg/blob - libavformat/tty.c
avformat/tty: Fix division by 0 in probe
[ffmpeg] / libavformat / tty.c
1 /*
2  * Tele-typewriter demuxer
3  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Tele-typewriter demuxer
25  */
26
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/log.h"
30 #include "libavutil/dict.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "avformat.h"
34 #include "internal.h"
35 #include "sauce.h"
36
37 static int isansicode(int x)
38 {
39     return x == 0x1B || x == 0x0A || x == 0x0D || (x >= 0x20 && x < 0x7f);
40 }
41
42 static const char tty_extensions[31] = "ans,art,asc,diz,ice,nfo,txt,vt";
43
44 typedef struct TtyDemuxContext {
45     AVClass *class;
46     int chars_per_frame;
47     uint64_t fsize;  /**< file size less metadata buffer */
48     int width, height; /**< Set by a private option. */
49     AVRational framerate; /**< Set by a private option. */
50 } TtyDemuxContext;
51
52 static int read_probe(const AVProbeData *p)
53 {
54     int cnt = 0;
55
56     if (!p->buf_size)
57         return 0;
58
59     for (int i = 0; i < p->buf_size; i++)
60         cnt += !!isansicode(p->buf[i]);
61
62     return (cnt * 100LL / p->buf_size) * (cnt > 400) *
63         !!av_match_ext(p->filename, tty_extensions);
64 }
65
66 /**
67  * Parse EFI header
68  */
69 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
70 {
71     TtyDemuxContext *s = avctx->priv_data;
72     AVIOContext *pb = avctx->pb;
73     char buf[37];
74     int len;
75
76     avio_seek(pb, start_pos, SEEK_SET);
77     if (avio_r8(pb) != 0x1A)
78         return -1;
79
80 #define GET_EFI_META(name,size) \
81     len = avio_r8(pb); \
82     if (len < 1 || len > size) \
83         return -1; \
84     if (avio_read(pb, buf, size) == size) { \
85         buf[len] = 0; \
86         av_dict_set(&avctx->metadata, name, buf, 0); \
87     }
88
89     GET_EFI_META("filename", 12)
90     GET_EFI_META("title",    36)
91
92     s->fsize = start_pos;
93     return 0;
94 }
95
96 static int read_header(AVFormatContext *avctx)
97 {
98     TtyDemuxContext *s = avctx->priv_data;
99     int ret = 0;
100     AVStream *st = avformat_new_stream(avctx, NULL);
101
102     if (!st) {
103         ret = AVERROR(ENOMEM);
104         goto fail;
105     }
106     st->codecpar->codec_tag   = 0;
107     st->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
108     st->codecpar->codec_id    = AV_CODEC_ID_ANSI;
109
110     st->codecpar->width  = s->width;
111     st->codecpar->height = s->height;
112     avpriv_set_pts_info(st, 60, s->framerate.den, s->framerate.num);
113     st->avg_frame_rate = s->framerate;
114
115     /* simulate tty display speed */
116     s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
117
118     if (avctx->pb->seekable & AVIO_SEEKABLE_NORMAL) {
119         s->fsize = avio_size(avctx->pb);
120         st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
121
122         if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
123             efi_read(avctx, s->fsize - 51);
124
125         avio_seek(avctx->pb, 0, SEEK_SET);
126     }
127
128 fail:
129     return ret;
130 }
131
132 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
133 {
134     TtyDemuxContext *s = avctx->priv_data;
135     int n;
136
137     if (avio_feof(avctx->pb))
138         return AVERROR_EOF;
139
140     n = s->chars_per_frame;
141     if (s->fsize) {
142         // ignore metadata buffer
143         uint64_t p = avio_tell(avctx->pb);
144         if (p == s->fsize)
145             return AVERROR_EOF;
146         if (p + s->chars_per_frame > s->fsize)
147             n = s->fsize - p;
148     }
149
150     pkt->size = av_get_packet(avctx->pb, pkt, n);
151     if (pkt->size < 0)
152         return pkt->size;
153     pkt->flags |= AV_PKT_FLAG_KEY;
154     return 0;
155 }
156
157 #define OFFSET(x) offsetof(TtyDemuxContext, x)
158 #define DEC AV_OPT_FLAG_DECODING_PARAM
159 static const AVOption options[] = {
160     { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
161     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
162     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
163     { NULL },
164 };
165
166 static const AVClass tty_demuxer_class = {
167     .class_name     = "TTY demuxer",
168     .item_name      = av_default_item_name,
169     .option         = options,
170     .version        = LIBAVUTIL_VERSION_INT,
171 };
172
173 AVInputFormat ff_tty_demuxer = {
174     .name           = "tty",
175     .long_name      = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
176     .priv_data_size = sizeof(TtyDemuxContext),
177     .read_probe     = read_probe,
178     .read_header    = read_header,
179     .read_packet    = read_packet,
180     .extensions     = tty_extensions,
181     .priv_class     = &tty_demuxer_class,
182 };