Libav 0.7.1
|
00001 /* 00002 * Tele-typewriter demuxer 00003 * Copyright (c) 2010 Peter Ross <pross@xvid.org> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 "sauce.h" 00035 00036 typedef struct { 00037 AVClass *class; 00038 int chars_per_frame; 00039 uint64_t fsize; 00040 char *video_size; 00041 char *framerate; 00042 } TtyDemuxContext; 00043 00047 static int efi_read(AVFormatContext *avctx, uint64_t start_pos) 00048 { 00049 TtyDemuxContext *s = avctx->priv_data; 00050 AVIOContext *pb = avctx->pb; 00051 char buf[37]; 00052 int len; 00053 00054 avio_seek(pb, start_pos, SEEK_SET); 00055 if (avio_r8(pb) != 0x1A) 00056 return -1; 00057 00058 #define GET_EFI_META(name,size) \ 00059 len = avio_r8(pb); \ 00060 if (len < 1 || len > size) \ 00061 return -1; \ 00062 if (avio_read(pb, buf, size) == size) { \ 00063 buf[len] = 0; \ 00064 av_dict_set(&avctx->metadata, name, buf, 0); \ 00065 } 00066 00067 GET_EFI_META("filename", 12) 00068 GET_EFI_META("title", 36) 00069 00070 s->fsize = start_pos; 00071 return 0; 00072 } 00073 00074 static int read_header(AVFormatContext *avctx, 00075 AVFormatParameters *ap) 00076 { 00077 TtyDemuxContext *s = avctx->priv_data; 00078 int width = 0, height = 0, ret = 0; 00079 AVStream *st = av_new_stream(avctx, 0); 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 #if FF_API_FORMAT_PARAMETERS 00099 if (ap->width > 0) 00100 width = ap->width; 00101 if (ap->height > 0) 00102 height = ap->height; 00103 if (ap->time_base.num) 00104 framerate = (AVRational){ap->time_base.den, ap->time_base.num}; 00105 #endif 00106 st->codec->width = width; 00107 st->codec->height = height; 00108 av_set_pts_info(st, 60, framerate.den, framerate.num); 00109 00110 /* simulate tty display speed */ 00111 #if FF_API_FORMAT_PARAMETERS 00112 if (ap->sample_rate) 00113 s->chars_per_frame = ap->sample_rate; 00114 #endif 00115 s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1); 00116 00117 if (avctx->pb->seekable) { 00118 s->fsize = avio_size(avctx->pb); 00119 st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame; 00120 00121 if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0) 00122 efi_read(avctx, s->fsize - 51); 00123 00124 avio_seek(avctx->pb, 0, SEEK_SET); 00125 } 00126 00127 fail: 00128 return ret; 00129 } 00130 00131 static int read_packet(AVFormatContext *avctx, AVPacket *pkt) 00132 { 00133 TtyDemuxContext *s = avctx->priv_data; 00134 int n; 00135 00136 if (avctx->pb->eof_reached) 00137 return AVERROR_EOF; 00138 00139 n = s->chars_per_frame; 00140 if (s->fsize) { 00141 // ignore metadata buffer 00142 uint64_t p = avio_tell(avctx->pb); 00143 if (p + s->chars_per_frame > s->fsize) 00144 n = s->fsize - p; 00145 } 00146 00147 pkt->size = av_get_packet(avctx->pb, pkt, n); 00148 if (pkt->size <= 0) 00149 return AVERROR(EIO); 00150 pkt->flags |= AV_PKT_FLAG_KEY; 00151 return 0; 00152 } 00153 00154 #define OFFSET(x) offsetof(TtyDemuxContext, x) 00155 #define DEC AV_OPT_FLAG_DECODING_PARAM 00156 static const AVOption options[] = { 00157 { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), FF_OPT_TYPE_INT, {.dbl = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM}, 00158 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, 00159 { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC }, 00160 { NULL }, 00161 }; 00162 00163 static const AVClass tty_demuxer_class = { 00164 .class_name = "TTY demuxer", 00165 .item_name = av_default_item_name, 00166 .option = options, 00167 .version = LIBAVUTIL_VERSION_INT, 00168 }; 00169 00170 AVInputFormat ff_tty_demuxer = { 00171 .name = "tty", 00172 .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"), 00173 .priv_data_size = sizeof(TtyDemuxContext), 00174 .read_header = read_header, 00175 .read_packet = read_packet, 00176 .extensions = "ans,art,asc,diz,ice,nfo,txt,vt", 00177 .priv_class = &tty_demuxer_class, 00178 };