avformat/mxfdec: Check edit unit for overflow in mxf_set_current_edit_unit()
[FFMpeg-mirror.git] / libavformat / tty.c
blobc3956ccf34ee9072b2e9b201de05fa5a23eb19e5
1 /*
2 * Tele-typewriter demuxer
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file
24 * Tele-typewriter demuxer
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 "demux.h"
35 #include "internal.h"
36 #include "sauce.h"
38 static int isansicode(int x)
40 return x == 0x1B || x == 0x0A || x == 0x0D || (x >= 0x20 && x < 0x7f);
43 static const char tty_extensions[31] = "ans,art,asc,diz,ice,nfo,txt,vt";
45 typedef struct TtyDemuxContext {
46 AVClass *class;
47 int chars_per_frame;
48 uint64_t fsize; /**< file size less metadata buffer */
49 int width, height; /**< Set by a private option. */
50 AVRational framerate; /**< Set by a private option. */
51 } TtyDemuxContext;
53 static int read_probe(const AVProbeData *p)
55 int cnt = 0;
57 if (!p->buf_size)
58 return 0;
60 for (int i = 0; i < 8 && i < p->buf_size; i++)
61 cnt += !!isansicode(p->buf[i]);
63 if (cnt != 8)
64 return 0;
66 for (int i = 8; i < p->buf_size; i++)
67 cnt += !!isansicode(p->buf[i]);
69 return (cnt * 99LL / p->buf_size) * (cnt > 400) *
70 !!av_match_ext(p->filename, tty_extensions);
73 /**
74 * Parse EFI header
76 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
78 TtyDemuxContext *s = avctx->priv_data;
79 AVIOContext *pb = avctx->pb;
80 char buf[37];
81 int len;
83 avio_seek(pb, start_pos, SEEK_SET);
84 if (avio_r8(pb) != 0x1A)
85 return -1;
87 #define GET_EFI_META(name,size) \
88 len = avio_r8(pb); \
89 if (len < 1 || len > size) \
90 return -1; \
91 if (avio_read(pb, buf, size) == size) { \
92 buf[len] = 0; \
93 av_dict_set(&avctx->metadata, name, buf, 0); \
96 GET_EFI_META("filename", 12)
97 GET_EFI_META("title", 36)
99 s->fsize = start_pos;
100 return 0;
103 static int read_header(AVFormatContext *avctx)
105 TtyDemuxContext *s = avctx->priv_data;
106 int ret = 0;
107 AVStream *st = avformat_new_stream(avctx, NULL);
109 if (!st) {
110 ret = AVERROR(ENOMEM);
111 goto fail;
113 st->codecpar->codec_tag = 0;
114 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
115 st->codecpar->codec_id = AV_CODEC_ID_ANSI;
117 st->codecpar->width = s->width;
118 st->codecpar->height = s->height;
119 avpriv_set_pts_info(st, 60, s->framerate.den, s->framerate.num);
120 st->avg_frame_rate = s->framerate;
122 /* simulate tty display speed */
123 s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
125 if (avctx->pb->seekable & AVIO_SEEKABLE_NORMAL) {
126 int64_t fsize = avio_size(avctx->pb);
127 if (fsize > 0) {
128 s->fsize = fsize;
129 st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
131 if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
132 efi_read(avctx, s->fsize - 51);
134 avio_seek(avctx->pb, 0, SEEK_SET);
138 fail:
139 return ret;
142 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
144 TtyDemuxContext *s = avctx->priv_data;
145 int n;
147 if (avio_feof(avctx->pb))
148 return AVERROR_EOF;
150 n = s->chars_per_frame;
151 if (s->fsize) {
152 // ignore metadata buffer
153 uint64_t p = avio_tell(avctx->pb);
154 if (p == s->fsize)
155 return AVERROR_EOF;
156 if (p + s->chars_per_frame > s->fsize)
157 n = s->fsize - p;
160 pkt->size = av_get_packet(avctx->pb, pkt, n);
161 if (pkt->size < 0)
162 return pkt->size;
163 pkt->stream_index = 0;
164 pkt->pts = pkt->pos / s->chars_per_frame;
165 pkt->flags |= AV_PKT_FLAG_KEY;
166 return 0;
169 #define OFFSET(x) offsetof(TtyDemuxContext, x)
170 #define DEC AV_OPT_FLAG_DECODING_PARAM
171 static const AVOption options[] = {
172 { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
173 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
174 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
175 { NULL },
178 static const AVClass tty_demuxer_class = {
179 .class_name = "TTY demuxer",
180 .item_name = av_default_item_name,
181 .option = options,
182 .version = LIBAVUTIL_VERSION_INT,
185 const FFInputFormat ff_tty_demuxer = {
186 .p.name = "tty",
187 .p.long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
188 .p.extensions = tty_extensions,
189 .p.priv_class = &tty_demuxer_class,
190 .p.flags = AVFMT_GENERIC_INDEX,
191 .priv_data_size = sizeof(TtyDemuxContext),
192 .read_probe = read_probe,
193 .read_header = read_header,
194 .read_packet = read_packet,