]> git.sesse.net Git - ffmpeg/blob - libavformat/tty.c
avio: add avio_tell macro as a replacement for url_ftell
[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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "avformat.h"
30 #include "sauce.h"
31
32 #define LINE_RATE 6000 /* characters per second */
33
34 typedef struct {
35     int chars_per_frame;
36     uint64_t fsize;  /**< file size less metadata buffer */
37 } TtyDemuxContext;
38
39 /**
40  * Parse EFI header
41  */
42 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
43 {
44     TtyDemuxContext *s = avctx->priv_data;
45     AVIOContext *pb = avctx->pb;
46     char buf[37];
47     int len;
48
49     avio_seek(pb, start_pos, SEEK_SET);
50     if (avio_r8(pb) != 0x1A)
51         return -1;
52
53 #define GET_EFI_META(name,size) \
54     len = avio_r8(pb); \
55     if (len < 1 || len > size) \
56         return -1; \
57     if (avio_read(pb, buf, size) == size) { \
58         buf[len] = 0; \
59         av_metadata_set2(&avctx->metadata, name, buf, 0); \
60     }
61
62     GET_EFI_META("filename", 12)
63     GET_EFI_META("title",    36)
64
65     s->fsize = start_pos;
66     return 0;
67 }
68
69 static int read_header(AVFormatContext *avctx,
70                        AVFormatParameters *ap)
71 {
72     TtyDemuxContext *s = avctx->priv_data;
73     AVStream *st = av_new_stream(avctx, 0);
74     if (!st)
75         return AVERROR(ENOMEM);
76     st->codec->codec_tag   = 0;
77     st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
78     st->codec->codec_id    = CODEC_ID_ANSI;
79     if (ap->width)  st->codec->width  = ap->width;
80     if (ap->height) st->codec->height = ap->height;
81
82     if (!ap->time_base.num) {
83         av_set_pts_info(st, 60, 1, 25);
84     } else {
85         av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
86     }
87
88     /* simulate tty display speed */
89     s->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
90
91     if (!url_is_streamed(avctx->pb)) {
92         s->fsize = url_fsize(avctx->pb);
93         st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
94
95         if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
96             efi_read(avctx, s->fsize - 51);
97
98         avio_seek(avctx->pb, 0, SEEK_SET);
99     }
100
101     return 0;
102 }
103
104 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
105 {
106     TtyDemuxContext *s = avctx->priv_data;
107     int n;
108
109     if (url_feof(avctx->pb))
110         return AVERROR_EOF;
111
112     n = s->chars_per_frame;
113     if (s->fsize) {
114         // ignore metadata buffer
115         uint64_t p = avio_tell(avctx->pb);
116         if (p + s->chars_per_frame > s->fsize)
117             n = s->fsize - p;
118     }
119
120     pkt->size = av_get_packet(avctx->pb, pkt, n);
121     if (pkt->size <= 0)
122         return AVERROR(EIO);
123     pkt->flags |= AV_PKT_FLAG_KEY;
124     return 0;
125 }
126
127 AVInputFormat ff_tty_demuxer = {
128     .name           = "tty",
129     .long_name      = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
130     .priv_data_size = sizeof(TtyDemuxContext),
131     .read_header    = read_header,
132     .read_packet    = read_packet,
133     .extensions     = "ans,art,asc,diz,ice,nfo,txt,vt",
134 };