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