FFmpeg
tty.c
Go to the documentation of this file.
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 static int isansicode(int x)
39 {
40  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;
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. */
52 
53 static int read_probe(const AVProbeData *p)
54 {
55  int cnt = 0;
56 
57  if (!p->buf_size)
58  return 0;
59 
60  for (int i = 0; i < 8 && i < p->buf_size; i++)
61  cnt += !!isansicode(p->buf[i]);
62 
63  if (cnt != 8)
64  return 0;
65 
66  for (int i = 8; i < p->buf_size; i++)
67  cnt += !!isansicode(p->buf[i]);
68 
69  return (cnt * 99LL / p->buf_size) * (cnt > 400) *
71 }
72 
73 /**
74  * Parse EFI header
75  */
76 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
77 {
78  TtyDemuxContext *s = avctx->priv_data;
79  AVIOContext *pb = avctx->pb;
80  char buf[37];
81  int len;
82 
83  avio_seek(pb, start_pos, SEEK_SET);
84  if (avio_r8(pb) != 0x1A)
85  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 static int read_header(AVFormatContext *avctx)
104 {
105  TtyDemuxContext *s = avctx->priv_data;
106  int ret = 0;
107  AVStream *st = avformat_new_stream(avctx, NULL);
108 
109  if (!st) {
110  ret = AVERROR(ENOMEM);
111  goto fail;
112  }
113  st->codecpar->codec_tag = 0;
116 
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;
121 
122  /* simulate tty display speed */
123  s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
124 
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;
130 
131  if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
132  efi_read(avctx, s->fsize - 51);
133 
134  avio_seek(avctx->pb, 0, SEEK_SET);
135  }
136  }
137 
138 fail:
139  return ret;
140 }
141 
143 {
144  TtyDemuxContext *s = avctx->priv_data;
145  int n;
146 
147  if (avio_feof(avctx->pb))
148  return AVERROR_EOF;
149 
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;
158  }
159 
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;
166  return 0;
167 }
168 
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 },
176 };
177 
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,
183 };
184 
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),
195 };
AV_CODEC_ID_ANSI
@ AV_CODEC_ID_ANSI
Definition: codec_id.h:194
TtyDemuxContext::width
int width
Definition: tty.c:49
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition: opt.h:315
int64_t
long long int64_t
Definition: coverity.c:34
TtyDemuxContext::fsize
uint64_t fsize
file size less metadata buffer
Definition: tty.c:48
AVOption
AVOption.
Definition: opt.h:429
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:837
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:323
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:867
TtyDemuxContext::framerate
AVRational framerate
Set by a private option.
Definition: tty.c:50
fail
#define fail()
Definition: checkasm.h:193
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:807
pkt
AVPacket * pkt
Definition: movenc.c:60
read_packet
static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
Definition: tty.c:142
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
TtyDemuxContext::height
int height
Set by a private option.
Definition: tty.c:49
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
AVProbeData::filename
const char * filename
Definition: avformat.h:452
av_match_ext
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition: format.c:41
fsize
static int64_t fsize(FILE *f)
Definition: audiomatch.c:29
if
if(ret)
Definition: filter_design.txt:179
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
framerate
float framerate
Definition: av1_levels.c:29
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:787
NULL
#define NULL
Definition: coverity.c:32
efi_read
static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
Parse EFI header.
Definition: tty.c:76
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition: opt.h:303
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
options
static const AVOption options[]
Definition: tty.c:171
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1342
parseutils.h
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
options
Definition: swscale.c:42
ff_tty_demuxer
const FFInputFormat ff_tty_demuxer
Definition: tty.c:185
GET_EFI_META
#define GET_EFI_META(name, size)
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
tty_extensions
static const char tty_extensions[31]
Definition: tty.c:43
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
read_probe
static int read_probe(const AVProbeData *p)
Definition: tty.c:53
ff_sauce_read
int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
Definition: sauce.c:32
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
tty_demuxer_class
static const AVClass tty_demuxer_class
Definition: tty.c:178
AVCodecParameters::height
int height
Definition: codec_par.h:135
TtyDemuxContext::chars_per_frame
int chars_per_frame
Definition: tty.c:47
demux.h
len
int len
Definition: vorbis_enc_data.h:426
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
read_header
static int read_header(AVFormatContext *avctx)
Definition: tty.c:103
avformat.h
dict.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
DEC
#define DEC
Definition: tty.c:170
AVPacket::stream_index
int stream_index
Definition: packet.h:541
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition: opt.h:356
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
TtyDemuxContext
Definition: tty.c:45
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
sauce.h
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
isansicode
static int isansicode(int x)
Definition: tty.c:38
avstring.h
width
#define width
Definition: dsp.h:85
OFFSET
#define OFFSET(x)
Definition: tty.c:169
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346