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 {
00077 TtyDemuxContext *s = avctx->priv_data;
00078 int width = 0, height = 0, ret = 0;
00079 AVStream *st = avformat_new_stream(avctx, NULL);
00080 AVRational framerate;
00081
00082 if (!st) {
00083 ret = AVERROR(ENOMEM);
00084 goto fail;
00085 }
00086 st->codec->codec_tag = 0;
00087 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00088 st->codec->codec_id = CODEC_ID_ANSI;
00089
00090 if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
00091 av_log (avctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
00092 goto fail;
00093 }
00094 if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
00095 av_log(avctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
00096 goto fail;
00097 }
00098 st->codec->width = width;
00099 st->codec->height = height;
00100 avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
00101
00102
00103 s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
00104
00105 if (avctx->pb->seekable) {
00106 s->fsize = avio_size(avctx->pb);
00107 st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
00108
00109 if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
00110 efi_read(avctx, s->fsize - 51);
00111
00112 avio_seek(avctx->pb, 0, SEEK_SET);
00113 }
00114
00115 fail:
00116 return ret;
00117 }
00118
00119 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
00120 {
00121 TtyDemuxContext *s = avctx->priv_data;
00122 int n;
00123
00124 if (url_feof(avctx->pb))
00125 return AVERROR_EOF;
00126
00127 n = s->chars_per_frame;
00128 if (s->fsize) {
00129
00130 uint64_t p = avio_tell(avctx->pb);
00131 if (p + s->chars_per_frame > s->fsize)
00132 n = s->fsize - p;
00133 }
00134
00135 pkt->size = av_get_packet(avctx->pb, pkt, n);
00136 if (pkt->size <= 0)
00137 return AVERROR(EIO);
00138 pkt->flags |= AV_PKT_FLAG_KEY;
00139 return 0;
00140 }
00141
00142 #define OFFSET(x) offsetof(TtyDemuxContext, x)
00143 #define DEC AV_OPT_FLAG_DECODING_PARAM
00144 static const AVOption options[] = {
00145 { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.dbl = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
00146 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00147 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
00148 { NULL },
00149 };
00150
00151 static const AVClass tty_demuxer_class = {
00152 .class_name = "TTY demuxer",
00153 .item_name = av_default_item_name,
00154 .option = options,
00155 .version = LIBAVUTIL_VERSION_INT,
00156 };
00157
00158 AVInputFormat ff_tty_demuxer = {
00159 .name = "tty",
00160 .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
00161 .priv_data_size = sizeof(TtyDemuxContext),
00162 .read_header = read_header,
00163 .read_packet = read_packet,
00164 .extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
00165 .priv_class = &tty_demuxer_class,
00166 };