FFmpeg  4.1.11
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 "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;
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. */
51 
52 static int read_probe(const AVProbeData *p)
53 {
54  int cnt = 0;
55 
56  for (int i = 0; i < p->buf_size; i++)
57  cnt += !!isansicode(p->buf[i]);
58 
59  return (cnt * 100LL / p->buf_size) * (cnt > 400) *
61 }
62 
63 /**
64  * Parse EFI header
65  */
66 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
67 {
68  TtyDemuxContext *s = avctx->priv_data;
69  AVIOContext *pb = avctx->pb;
70  char buf[37];
71  int len;
72 
73  avio_seek(pb, start_pos, SEEK_SET);
74  if (avio_r8(pb) != 0x1A)
75  return -1;
76 
77 #define GET_EFI_META(name,size) \
78  len = avio_r8(pb); \
79  if (len < 1 || len > size) \
80  return -1; \
81  if (avio_read(pb, buf, size) == size) { \
82  buf[len] = 0; \
83  av_dict_set(&avctx->metadata, name, buf, 0); \
84  }
85 
86  GET_EFI_META("filename", 12)
87  GET_EFI_META("title", 36)
88 
89  s->fsize = start_pos;
90  return 0;
91 }
92 
93 static int read_header(AVFormatContext *avctx)
94 {
95  TtyDemuxContext *s = avctx->priv_data;
96  int ret = 0;
97  AVStream *st = avformat_new_stream(avctx, NULL);
98 
99  if (!st) {
100  ret = AVERROR(ENOMEM);
101  goto fail;
102  }
103  st->codecpar->codec_tag = 0;
106 
107  st->codecpar->width = s->width;
108  st->codecpar->height = s->height;
110  st->avg_frame_rate = s->framerate;
111 
112  /* simulate tty display speed */
114 
115  if (avctx->pb->seekable & AVIO_SEEKABLE_NORMAL) {
116  s->fsize = avio_size(avctx->pb);
117  st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
118 
119  if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
120  efi_read(avctx, s->fsize - 51);
121 
122  avio_seek(avctx->pb, 0, SEEK_SET);
123  }
124 
125 fail:
126  return ret;
127 }
128 
130 {
131  TtyDemuxContext *s = avctx->priv_data;
132  int n;
133 
134  if (avio_feof(avctx->pb))
135  return AVERROR_EOF;
136 
137  n = s->chars_per_frame;
138  if (s->fsize) {
139  // ignore metadata buffer
140  uint64_t p = avio_tell(avctx->pb);
141  if (p == s->fsize)
142  return AVERROR_EOF;
143  if (p + s->chars_per_frame > s->fsize)
144  n = s->fsize - p;
145  }
146 
147  pkt->size = av_get_packet(avctx->pb, pkt, n);
148  if (pkt->size < 0)
149  return pkt->size;
150  pkt->flags |= AV_PKT_FLAG_KEY;
151  return 0;
152 }
153 
154 #define OFFSET(x) offsetof(TtyDemuxContext, x)
155 #define DEC AV_OPT_FLAG_DECODING_PARAM
156 static const AVOption options[] = {
157  { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
158  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
159  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
160  { NULL },
161 };
162 
163 static const AVClass tty_demuxer_class = {
164  .class_name = "TTY demuxer",
165  .item_name = av_default_item_name,
166  .option = options,
167  .version = LIBAVUTIL_VERSION_INT,
168 };
169 
171  .name = "tty",
172  .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
173  .priv_data_size = sizeof(TtyDemuxContext),
177  .extensions = tty_extensions,
178  .priv_class = &tty_demuxer_class,
179 };
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:336
AVOption.
Definition: opt.h:246
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
const char * filename
Definition: avformat.h:449
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4899
uint64_t fsize
file size less metadata buffer
Definition: tty.c:47
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1446
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static AVPacket pkt
AVRational framerate
Set by a private option.
Definition: tty.c:49
Format I/O context.
Definition: avformat.h:1351
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:72
Public dictionary API.
int width
Video only.
Definition: avcodec.h:3966
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4469
#define OFFSET(x)
Definition: tty.c:154
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:313
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:38
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
#define GET_EFI_META(name, size)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
int height
Set by a private option.
Definition: tty.c:48
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
Definition: sauce.c:32
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
static const AVOption options[]
Definition: tty.c:156
static int read_probe(const AVProbeData *p)
Definition: tty.c:52
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:947
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:117
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
#define DEC
Definition: tty.c:155
static int read_header(AVFormatContext *avctx)
Definition: tty.c:93
#define s(width, name)
Definition: cbs_vp9.c:257
int n
Definition: avisynth_c.h:684
if(ret< 0)
Definition: vf_mcdeint.c:279
Stream structure.
Definition: avformat.h:874
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
static const AVClass tty_demuxer_class
Definition: tty.c:163
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
AVInputFormat ff_tty_demuxer
Definition: tty.c:170
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:277
static const char tty_extensions[31]
Definition: tty.c:42
offset must point to AVRational
Definition: opt.h:236
offset must point to two consecutive integers
Definition: opt.h:233
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
misc parsing utilities
SAUCE header parser.
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:923
static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
Parse EFI header.
Definition: tty.c:66
Main libavformat public API header.
int chars_per_frame
Definition: tty.c:46
int den
Denominator.
Definition: rational.h:60
static int isansicode(int x)
Definition: tty.c:37
int len
void * priv_data
Format private data.
Definition: avformat.h:1379
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3904
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
This structure stores compressed data.
Definition: avcodec.h:1422
static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
Definition: tty.c:129
int width
Definition: tty.c:48