]> git.sesse.net Git - ffmpeg/blob - libavformat/tty.c
970274eb54fc8c937f3899b4c83622ab8bf83a6a
[ffmpeg] / libavformat / tty.c
1 /*
2  * Tele-typewriter demuxer
3  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Tele-typewriter demuxer
25  */
26
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/log.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
32 #include "avformat.h"
33 #include "sauce.h"
34
35 typedef struct {
36     AVClass *class;
37     int chars_per_frame;
38     uint64_t fsize;  /**< file size less metadata buffer */
39     char *video_size;/**< A string describing video size, set by a private option. */
40 } TtyDemuxContext;
41
42 /**
43  * Parse EFI header
44  */
45 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
46 {
47     TtyDemuxContext *s = avctx->priv_data;
48     AVIOContext *pb = avctx->pb;
49     char buf[37];
50     int len;
51
52     avio_seek(pb, start_pos, SEEK_SET);
53     if (avio_r8(pb) != 0x1A)
54         return -1;
55
56 #define GET_EFI_META(name,size) \
57     len = avio_r8(pb); \
58     if (len < 1 || len > size) \
59         return -1; \
60     if (avio_read(pb, buf, size) == size) { \
61         buf[len] = 0; \
62         av_metadata_set2(&avctx->metadata, name, buf, 0); \
63     }
64
65     GET_EFI_META("filename", 12)
66     GET_EFI_META("title",    36)
67
68     s->fsize = start_pos;
69     return 0;
70 }
71
72 static int read_header(AVFormatContext *avctx,
73                        AVFormatParameters *ap)
74 {
75     TtyDemuxContext *s = avctx->priv_data;
76     int width = 0, height = 0, ret = 0;
77     AVStream *st = av_new_stream(avctx, 0);
78
79     if (!st) {
80         ret = AVERROR(ENOMEM);
81         goto fail;
82     }
83     st->codec->codec_tag   = 0;
84     st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
85     st->codec->codec_id    = CODEC_ID_ANSI;
86
87     if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
88         av_log (avctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
89         goto fail;
90     }
91 #if FF_API_FORMAT_PARAMETERS
92     if (ap->width > 0)
93         width = ap->width;
94     if (ap->height > 0)
95         height = ap->height;
96 #endif
97     st->codec->width  = width;
98     st->codec->height = height;
99
100     if (!ap->time_base.num) {
101         av_set_pts_info(st, 60, 1, 25);
102     } else {
103         av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
104     }
105
106     /* simulate tty display speed */
107 #if FF_API_FORMAT_PARAMETERS
108     if (ap->sample_rate)
109         s->chars_per_frame = ap->sample_rate;
110 #endif
111     s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
112
113     if (avctx->pb->seekable) {
114         s->fsize = avio_size(avctx->pb);
115         st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
116
117         if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
118             efi_read(avctx, s->fsize - 51);
119
120         avio_seek(avctx->pb, 0, SEEK_SET);
121     }
122
123 fail:
124     return ret;
125 }
126
127 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
128 {
129     TtyDemuxContext *s = avctx->priv_data;
130     int n;
131
132     if (avctx->pb->eof_reached)
133         return AVERROR_EOF;
134
135     n = s->chars_per_frame;
136     if (s->fsize) {
137         // ignore metadata buffer
138         uint64_t p = avio_tell(avctx->pb);
139         if (p + s->chars_per_frame > s->fsize)
140             n = s->fsize - p;
141     }
142
143     pkt->size = av_get_packet(avctx->pb, pkt, n);
144     if (pkt->size <= 0)
145         return AVERROR(EIO);
146     pkt->flags |= AV_PKT_FLAG_KEY;
147     return 0;
148 }
149
150 #define OFFSET(x) offsetof(TtyDemuxContext, x)
151 #define DEC AV_OPT_FLAG_DECODING_PARAM
152 static const AVOption options[] = {
153     { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), FF_OPT_TYPE_INT, {.dbl = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
154     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
155     { NULL },
156 };
157
158 static const AVClass tty_demuxer_class = {
159     .class_name     = "TTY demuxer",
160     .item_name      = av_default_item_name,
161     .option         = options,
162     .version        = LIBAVUTIL_VERSION_INT,
163 };
164
165 AVInputFormat ff_tty_demuxer = {
166     .name           = "tty",
167     .long_name      = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
168     .priv_data_size = sizeof(TtyDemuxContext),
169     .read_header    = read_header,
170     .read_packet    = read_packet,
171     .extensions     = "ans,art,asc,diz,ice,nfo,txt,vt",
172     .priv_class     = &tty_demuxer_class,
173 };