Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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 typedef struct {
38  AVClass *class;
40  uint64_t fsize;
41  char *video_size;
42  char *framerate;
44 
48 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
49 {
50  TtyDemuxContext *s = avctx->priv_data;
51  AVIOContext *pb = avctx->pb;
52  char buf[37];
53  int len;
54 
55  avio_seek(pb, start_pos, SEEK_SET);
56  if (avio_r8(pb) != 0x1A)
57  return -1;
58 
59 #define GET_EFI_META(name,size) \
60  len = avio_r8(pb); \
61  if (len < 1 || len > size) \
62  return -1; \
63  if (avio_read(pb, buf, size) == size) { \
64  buf[len] = 0; \
65  av_dict_set(&avctx->metadata, name, buf, 0); \
66  }
67 
68  GET_EFI_META("filename", 12)
69  GET_EFI_META("title", 36)
70 
71  s->fsize = start_pos;
72  return 0;
73 }
74 
75 static int read_header(AVFormatContext *avctx)
76 {
77  TtyDemuxContext *s = avctx->priv_data;
78  int width = 0, height = 0, ret = 0;
79  AVStream *st = avformat_new_stream(avctx, NULL);
80  AVRational framerate;
81 
82  if (!st) {
83  ret = AVERROR(ENOMEM);
84  goto fail;
85  }
86  st->codec->codec_tag = 0;
89 
90  if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
91  av_log (avctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
92  goto fail;
93  }
94  if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
95  av_log(avctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
96  goto fail;
97  }
98  st->codec->width = width;
99  st->codec->height = height;
100  avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
101  st->avg_frame_rate = framerate;
102 
103  /* simulate tty display speed */
105 
106  if (avctx->pb->seekable) {
107  s->fsize = avio_size(avctx->pb);
108  st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
109 
110  if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
111  efi_read(avctx, s->fsize - 51);
112 
113  avio_seek(avctx->pb, 0, SEEK_SET);
114  }
115 
116 fail:
117  return ret;
118 }
119 
120 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
121 {
122  TtyDemuxContext *s = avctx->priv_data;
123  int n;
124 
125  if (avctx->pb->eof_reached)
126  return AVERROR_EOF;
127 
128  n = s->chars_per_frame;
129  if (s->fsize) {
130  // ignore metadata buffer
131  uint64_t p = avio_tell(avctx->pb);
132  if (p + s->chars_per_frame > s->fsize)
133  n = s->fsize - p;
134  }
135 
136  pkt->size = av_get_packet(avctx->pb, pkt, n);
137  if (pkt->size <= 0)
138  return AVERROR(EIO);
139  pkt->flags |= AV_PKT_FLAG_KEY;
140  return 0;
141 }
142 
143 #define OFFSET(x) offsetof(TtyDemuxContext, x)
144 #define DEC AV_OPT_FLAG_DECODING_PARAM
145 static const AVOption options[] = {
146  { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
147  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
148  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
149  { NULL },
150 };
151 
152 static const AVClass tty_demuxer_class = {
153  .class_name = "TTY demuxer",
154  .item_name = av_default_item_name,
155  .option = options,
156  .version = LIBAVUTIL_VERSION_INT,
157 };
158 
160  .name = "tty",
161  .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
162  .priv_data_size = sizeof(TtyDemuxContext),
165  .extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
166  .priv_class = &tty_demuxer_class,
167 };
Bytestream IO Context.
Definition: avio.h:68
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:241
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:122
AVOption.
Definition: opt.h:234
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
Definition: parseutils.c:95
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:2829
uint64_t fsize
file size less metadata buffer
Definition: tty.c:40
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
Format I/O context.
Definition: avformat.h:922
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:38
Public dictionary API.
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
#define OFFSET(x)
Definition: tty.c:143
#define AVERROR_EOF
End of file.
Definition: error.h:51
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:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
#define GET_EFI_META(name, size)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#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:145
int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
Definition: sauce.c:32
static const AVOption options[]
Definition: tty.c:145
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:780
#define FFMAX(a, b)
Definition: common.h:55
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
#define DEC
Definition: tty.c:144
char * video_size
A string describing video size, set by a private option.
Definition: tty.c:41
int width
picture width / height.
Definition: avcodec.h:1229
static int read_header(AVFormatContext *avctx)
Definition: tty.c:75
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
if(ac->has_optimized_func)
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
enum AVMediaType codec_type
Definition: avcodec.h:1058
static const AVClass tty_demuxer_class
Definition: tty.c:152
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVIOContext * pb
I/O context.
Definition: avformat.h:964
AVInputFormat ff_tty_demuxer
Definition: tty.c:159
av_default_item_name
Definition: dnxhdenc.c:52
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
Describe the class of an AVClass context structure.
Definition: log.h:33
rational number numerator/denominator
Definition: rational.h:43
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:265
misc parsing utilities
SAUCE header parser.
int height
Definition: gxfenc.c:72
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:756
static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
Parse EFI header.
Definition: tty.c:48
Main libavformat public API header.
int chars_per_frame
Definition: tty.c:39
int den
denominator
Definition: rational.h:45
int eof_reached
true if eof reached
Definition: avio.h:96
int len
void * priv_data
Format private data.
Definition: avformat.h:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
char * framerate
Set by a private option.
Definition: tty.c:42
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
This structure stores compressed data.
Definition: avcodec.h:950
static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
Definition: tty.c:120