2 * Tele-typewriter demuxer
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
5 * This file is part of Libav.
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.
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.
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
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"
40 uint64_t fsize
; /**< file size less metadata buffer */
41 char *video_size
;/**< A string describing video size, set by a private option. */
42 char *framerate
; /**< Set by a private option. */
48 static int efi_read(AVFormatContext
*avctx
, uint64_t start_pos
)
50 TtyDemuxContext
*s
= avctx
->priv_data
;
51 AVIOContext
*pb
= avctx
->pb
;
55 avio_seek(pb
, start_pos
, SEEK_SET
);
56 if (avio_r8(pb
) != 0x1A)
59 #define GET_EFI_META(name,size) \
61 if (len < 1 || len > size) \
63 if (avio_read(pb, buf, size) == size) { \
65 av_dict_set(&avctx->metadata, name, buf, 0); \
68 GET_EFI_META("filename", 12)
69 GET_EFI_META("title", 36)
75 static int read_header(AVFormatContext
*avctx
)
77 TtyDemuxContext
*s
= avctx
->priv_data
;
78 int width
= 0, height
= 0, ret
= 0;
79 AVStream
*st
= avformat_new_stream(avctx
, NULL
);
83 ret
= AVERROR(ENOMEM
);
86 st
->codec
->codec_tag
= 0;
87 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
88 st
->codec
->codec_id
= AV_CODEC_ID_ANSI
;
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");
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
);
98 st
->codec
->width
= width
;
99 st
->codec
->height
= height
;
100 avpriv_set_pts_info(st
, 60, framerate
.den
, framerate
.num
);
102 /* simulate tty display speed */
103 s
->chars_per_frame
= FFMAX(av_q2d(st
->time_base
)*s
->chars_per_frame
, 1);
105 if (avctx
->pb
->seekable
) {
106 s
->fsize
= avio_size(avctx
->pb
);
107 st
->duration
= (s
->fsize
+ s
->chars_per_frame
- 1) / s
->chars_per_frame
;
109 if (ff_sauce_read(avctx
, &s
->fsize
, 0, 0) < 0)
110 efi_read(avctx
, s
->fsize
- 51);
112 avio_seek(avctx
->pb
, 0, SEEK_SET
);
119 static int read_packet(AVFormatContext
*avctx
, AVPacket
*pkt
)
121 TtyDemuxContext
*s
= avctx
->priv_data
;
124 if (avctx
->pb
->eof_reached
)
127 n
= s
->chars_per_frame
;
129 // ignore metadata buffer
130 uint64_t p
= avio_tell(avctx
->pb
);
131 if (p
+ s
->chars_per_frame
> s
->fsize
)
135 pkt
->size
= av_get_packet(avctx
->pb
, pkt
, n
);
138 pkt
->flags
|= AV_PKT_FLAG_KEY
;
142 #define OFFSET(x) offsetof(TtyDemuxContext, x)
143 #define DEC AV_OPT_FLAG_DECODING_PARAM
144 static const AVOption options
[] = {
145 { "chars_per_frame", "", offsetof(TtyDemuxContext
, chars_per_frame
), AV_OPT_TYPE_INT
, {.i64
= 6000}, 1, INT_MAX
, AV_OPT_FLAG_DECODING_PARAM
},
146 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, DEC
},
147 { "framerate", "", OFFSET(framerate
), AV_OPT_TYPE_STRING
, {.str
= "25"}, 0, 0, DEC
},
151 static const AVClass tty_demuxer_class
= {
152 .class_name
= "TTY demuxer",
153 .item_name
= av_default_item_name
,
155 .version
= LIBAVUTIL_VERSION_INT
,
158 AVInputFormat ff_tty_demuxer
= {
160 .long_name
= NULL_IF_CONFIG_SMALL("Tele-typewriter"),
161 .priv_data_size
= sizeof(TtyDemuxContext
),
162 .read_header
= read_header
,
163 .read_packet
= read_packet
,
164 .extensions
= "ans,art,asc,diz,ice,nfo,txt,vt",
165 .priv_class
= &tty_demuxer_class
,