libavformat/tty.c
5a717094
 /*
  * Tele-typewriter demuxer
  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
07e6384d
 /**
  * @file
  * Tele-typewriter demuxer
  */
 
5a717094
 #include "libavutil/intreadwrite.h"
 #include "libavutil/avstring.h"
67540af7
 #include "libavutil/log.h"
d2d67e42
 #include "libavutil/dict.h"
67540af7
 #include "libavutil/opt.h"
06d8c9e5
 #include "libavutil/parseutils.h"
5a717094
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
5a717094
 #include "sauce.h"
 
 typedef struct {
67540af7
     AVClass *class;
5a717094
     int chars_per_frame;
a20df858
     uint64_t fsize;  /**< file size less metadata buffer */
06d8c9e5
     char *video_size;/**< A string describing video size, set by a private option. */
c1dcbfdd
     char *framerate; /**< Set by a private option. */
5a717094
 } TtyDemuxContext;
 
 /**
  * Parse EFI header
  */
 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
 {
     TtyDemuxContext *s = avctx->priv_data;
471fe57e
     AVIOContext *pb = avctx->pb;
5a717094
     char buf[37];
     int len;
 
f59d8ff8
     avio_seek(pb, start_pos, SEEK_SET);
e63a3628
     if (avio_r8(pb) != 0x1A)
5a717094
         return -1;
 
 #define GET_EFI_META(name,size) \
e63a3628
     len = avio_r8(pb); \
5a717094
     if (len < 1 || len > size) \
         return -1; \
e63a3628
     if (avio_read(pb, buf, size) == size) { \
5a717094
         buf[len] = 0; \
d2d67e42
         av_dict_set(&avctx->metadata, name, buf, 0); \
5a717094
     }
 
     GET_EFI_META("filename", 12)
     GET_EFI_META("title",    36)
 
     s->fsize = start_pos;
     return 0;
 }
 
6e9651d1
 static int read_header(AVFormatContext *avctx)
5a717094
 {
     TtyDemuxContext *s = avctx->priv_data;
8346f60a
     int width = 0, height = 0, ret = 0;
3b3bbdd3
     AVStream *st = avformat_new_stream(avctx, NULL);
c1dcbfdd
     AVRational framerate;
8346f60a
 
     if (!st) {
         ret = AVERROR(ENOMEM);
         goto fail;
     }
5a717094
     st->codec->codec_tag   = 0;
b9f9e59a
     st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
36ef5369
     st->codec->codec_id    = AV_CODEC_ID_ANSI;
06d8c9e5
 
8346f60a
     if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
         av_log (avctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
         goto fail;
06d8c9e5
     }
c1dcbfdd
     if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
         goto fail;
     }
06d8c9e5
     st->codec->width  = width;
     st->codec->height = height;
c3f9ebf7
     avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
5a717094
 
     /* simulate tty display speed */
67540af7
     s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
5a717094
 
8978feda
     if (avctx->pb->seekable) {
db44ea96
         s->fsize = avio_size(avctx->pb);
5a717094
         st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
 
         if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
             efi_read(avctx, s->fsize - 51);
 
f59d8ff8
         avio_seek(avctx->pb, 0, SEEK_SET);
5a717094
     }
 
8346f60a
 fail:
     return ret;
5a717094
 }
 
 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
 {
     TtyDemuxContext *s = avctx->priv_data;
     int n;
 
     if (url_feof(avctx->pb))
         return AVERROR_EOF;
 
     n = s->chars_per_frame;
     if (s->fsize) {
         // ignore metadata buffer
384c9c2f
         uint64_t p = avio_tell(avctx->pb);
bf959ac2
         if (p == s->fsize)
             return AVERROR_EOF;
5a717094
         if (p + s->chars_per_frame > s->fsize)
             n = s->fsize - p;
     }
 
     pkt->size = av_get_packet(avctx->pb, pkt, n);
29948971
     if (pkt->size < 0)
         return pkt->size;
d947bce1
     pkt->flags |= AV_PKT_FLAG_KEY;
5a717094
     return 0;
 }
 
06d8c9e5
 #define OFFSET(x) offsetof(TtyDemuxContext, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
67540af7
 static const AVOption options[] = {
e6153f17
     { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
145f741e
     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
67540af7
     { NULL },
 };
 
 static const AVClass tty_demuxer_class = {
     .class_name     = "TTY demuxer",
     .item_name      = av_default_item_name,
     .option         = options,
     .version        = LIBAVUTIL_VERSION_INT,
 };
 
66355be3
 AVInputFormat ff_tty_demuxer = {
5a717094
     .name           = "tty",
     .long_name      = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
     .priv_data_size = sizeof(TtyDemuxContext),
     .read_header    = read_header,
     .read_packet    = read_packet,
     .extensions     = "ans,art,asc,diz,ice,nfo,txt,vt",
67540af7
     .priv_class     = &tty_demuxer_class,
5a717094
 };