00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/intreadwrite.h"
00028 #include "libavutil/avstring.h"
00029 #include "libavutil/log.h"
00030 #include "libavutil/dict.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/parseutils.h"
00033 #include "avformat.h"
00034 #include "internal.h"
00035 #include "sauce.h"
00036
00037 typedef struct {
00038 AVClass *class;
00039 int chars_per_frame;
00040 uint64_t fsize;
00041 char *video_size;
00042 char *framerate;
00043 } TtyDemuxContext;
00044
00048 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
00049 {
00050 TtyDemuxContext *s = avctx->priv_data;
00051 AVIOContext *pb = avctx->pb;
00052 char buf[37];
00053 int len;
00054
00055 avio_seek(pb, start_pos, SEEK_SET);
00056 if (avio_r8(pb) != 0x1A)
00057 return -1;
00058
00059 #define GET_EFI_META(name,size) \
00060 len = avio_r8(pb); \
00061 if (len < 1 || len > size) \
00062 return -1; \
00063 if (avio_read(pb, buf, size) == size) { \
00064 buf[len] = 0; \
00065 av_dict_set(&avctx->metadata, name, buf, 0); \
00066 }
00067
00068 GET_EFI_META("filename", 12)
00069 GET_EFI_META("title", 36)
00070
00071 s->fsize = start_pos;
00072 return 0;
00073 }
00074
00075 static int read_header(AVFormatContext *avctx,
00076 AVFormatParameters *ap)
00077 {
00078 TtyDemuxContext *s = avctx->priv_data;
00079 int width = 0, height = 0, ret = 0;
00080 AVStream *st = avformat_new_stream(avctx, NULL);
00081 AVRational framerate;
00082
00083 if (!st) {
00084 ret = AVERROR(ENOMEM);
00085 goto fail;
00086 }
00087 st->codec->codec_tag = 0;
00088 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00089 st->codec->codec_id = CODEC_ID_ANSI;
00090
00091 if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
00092 av_log (avctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
00093 goto fail;
00094 }
00095 if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
00096 av_log(avctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
00097 goto fail;
00098 }
00099 st->codec->width = width;
00100 st->codec->height = height;
00101 avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
00102
00103
00104 s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
00105
00106 if (avctx->pb->seekable) {
00107 s->fsize = avio_size(avctx->pb);
00108 st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
00109
00110 if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
00111 efi_read(avctx, s->fsize - 51);
00112
00113 avio_seek(avctx->pb, 0, SEEK_SET);
00114 }
00115
00116 fail:
00117 return ret;
00118 }
00119
00120 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
00121 {
00122 TtyDemuxContext *s = avctx->priv_data;
00123 int n;
00124
00125 if (url_feof(avctx->pb))
00126 return AVERROR_EOF;
00127
00128 n = s->chars_per_frame;
00129 if (s->fsize) {
00130
00131 uint64_t p = avio_tell(avctx->pb);
00132 if (p + s->chars_per_frame > s->fsize)
00133 n = s->fsize - p;
00134 }
00135
00136 pkt->size = av_get_packet(avctx->pb, pkt, n);
00137 if (pkt->size <= 0)
00138 return AVERROR(EIO);
00139 pkt->flags |= AV_PKT_FLAG_KEY;
00140 return 0;
00141 }
00142
00143 #define OFFSET(x) offsetof(TtyDemuxContext, x)
00144 #define DEC AV_OPT_FLAG_DECODING_PARAM
00145 static const AVOption options[] = {
00146 { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.dbl = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
00147 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00148 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
00149 { NULL },
00150 };
00151
00152 static const AVClass tty_demuxer_class = {
00153 .class_name = "TTY demuxer",
00154 .item_name = av_default_item_name,
00155 .option = options,
00156 .version = LIBAVUTIL_VERSION_INT,
00157 };
00158
00159 AVInputFormat ff_tty_demuxer = {
00160 .name = "tty",
00161 .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
00162 .priv_data_size = sizeof(TtyDemuxContext),
00163 .read_header = read_header,
00164 .read_packet = read_packet,
00165 .extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
00166 .priv_class = &tty_demuxer_class,
00167 };