FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/tty.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 55 65 84.6%
Functions: 5 5 100.0%
Branches: 31 52 59.6%

Line Branch Exec Source
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 "demux.h"
35 #include "internal.h"
36 #include "sauce.h"
37
38 374682 static int isansicode(int x)
39 {
40
10/10
✓ Branch 0 taken 373193 times.
✓ Branch 1 taken 1489 times.
✓ Branch 2 taken 369288 times.
✓ Branch 3 taken 3905 times.
✓ Branch 4 taken 367914 times.
✓ Branch 5 taken 1374 times.
✓ Branch 6 taken 270303 times.
✓ Branch 7 taken 97611 times.
✓ Branch 8 taken 186338 times.
✓ Branch 9 taken 83965 times.
374682 return x == 0x1B || x == 0x0A || x == 0x0D || (x >= 0x20 && x < 0x7f);
41 }
42
43 static const char tty_extensions[31] = "ans,art,asc,diz,ice,nfo,txt,vt";
44
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;
52
53 7125 static int read_probe(const AVProbeData *p)
54 {
55 7125 int cnt = 0;
56
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7125 times.
7125 if (!p->buf_size)
58 return 0;
59
60
3/4
✓ Branch 0 taken 57000 times.
✓ Branch 1 taken 7125 times.
✓ Branch 2 taken 57000 times.
✗ Branch 3 not taken.
64125 for (int i = 0; i < 8 && i < p->buf_size; i++)
61 57000 cnt += !!isansicode(p->buf[i]);
62
63
2/2
✓ Branch 0 taken 6944 times.
✓ Branch 1 taken 181 times.
7125 if (cnt != 8)
64 6944 return 0;
65
66
2/2
✓ Branch 0 taken 317682 times.
✓ Branch 1 taken 181 times.
317863 for (int i = 8; i < p->buf_size; i++)
67 317682 cnt += !!isansicode(p->buf[i]);
68
69 362 return (cnt * 99LL / p->buf_size) * (cnt > 400) *
70 181 !!av_match_ext(p->filename, tty_extensions);
71 }
72
73 /**
74 * Parse EFI header
75 */
76 1 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
77 {
78 1 TtyDemuxContext *s = avctx->priv_data;
79 1 AVIOContext *pb = avctx->pb;
80 char buf[37];
81 int len;
82
83 1 avio_seek(pb, start_pos, SEEK_SET);
84
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (avio_r8(pb) != 0x1A)
85 1 return -1;
86
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); \
94 }
95
96 GET_EFI_META("filename", 12)
97 GET_EFI_META("title", 36)
98
99 s->fsize = start_pos;
100 return 0;
101 }
102
103 2 static int read_header(AVFormatContext *avctx)
104 {
105 2 TtyDemuxContext *s = avctx->priv_data;
106 2 int ret = 0;
107 2 AVStream *st = avformat_new_stream(avctx, NULL);
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!st) {
110 ret = AVERROR(ENOMEM);
111 goto fail;
112 }
113 2 st->codecpar->codec_tag = 0;
114 2 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
115 2 st->codecpar->codec_id = AV_CODEC_ID_ANSI;
116
117 2 st->codecpar->width = s->width;
118 2 st->codecpar->height = s->height;
119 2 avpriv_set_pts_info(st, 60, s->framerate.den, s->framerate.num);
120 2 st->avg_frame_rate = s->framerate;
121
122 /* simulate tty display speed */
123
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
124
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->pb->seekable & AVIO_SEEKABLE_NORMAL) {
126 2 s->fsize = avio_size(avctx->pb);
127 2 st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
128
129
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
130 1 efi_read(avctx, s->fsize - 51);
131
132 2 avio_seek(avctx->pb, 0, SEEK_SET);
133 }
134
135 fail:
136 2 return ret;
137 }
138
139 27 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
140 {
141 27 TtyDemuxContext *s = avctx->priv_data;
142 int n;
143
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (avio_feof(avctx->pb))
145 return AVERROR_EOF;
146
147 27 n = s->chars_per_frame;
148
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (s->fsize) {
149 // ignore metadata buffer
150 27 uint64_t p = avio_tell(avctx->pb);
151
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25 times.
27 if (p == s->fsize)
152 2 return AVERROR_EOF;
153
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 23 times.
25 if (p + s->chars_per_frame > s->fsize)
154 2 n = s->fsize - p;
155 }
156
157 25 pkt->size = av_get_packet(avctx->pb, pkt, n);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (pkt->size < 0)
159 return pkt->size;
160 25 pkt->stream_index = 0;
161 25 pkt->pts = pkt->pos / s->chars_per_frame;
162 25 pkt->flags |= AV_PKT_FLAG_KEY;
163 25 return 0;
164 }
165
166 #define OFFSET(x) offsetof(TtyDemuxContext, x)
167 #define DEC AV_OPT_FLAG_DECODING_PARAM
168 static const AVOption options[] = {
169 { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
170 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
171 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
172 { NULL },
173 };
174
175 static const AVClass tty_demuxer_class = {
176 .class_name = "TTY demuxer",
177 .item_name = av_default_item_name,
178 .option = options,
179 .version = LIBAVUTIL_VERSION_INT,
180 };
181
182 const FFInputFormat ff_tty_demuxer = {
183 .p.name = "tty",
184 .p.long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
185 .p.extensions = tty_extensions,
186 .p.priv_class = &tty_demuxer_class,
187 .p.flags = AVFMT_GENERIC_INDEX,
188 .priv_data_size = sizeof(TtyDemuxContext),
189 .read_probe = read_probe,
190 .read_header = read_header,
191 .read_packet = read_packet,
192 };
193