6271
|
1 /*
|
|
2 * @file
|
|
3 * Tele-typewriter demuxer
|
|
4 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
|
|
5 *
|
|
6 * This file is part of FFmpeg.
|
|
7 *
|
|
8 * FFmpeg is free software; you can redistribute it and/or
|
|
9 * modify it under the terms of the GNU Lesser General Public
|
|
10 * License as published by the Free Software Foundation; either
|
|
11 * version 2.1 of the License, or (at your option) any later version.
|
|
12 *
|
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 * Lesser General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU Lesser General Public
|
|
19 * License along with FFmpeg; if not, write to the Free Software
|
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
21 */
|
|
22
|
|
23 #include "libavutil/intreadwrite.h"
|
|
24 #include "libavutil/avstring.h"
|
|
25 #include "avformat.h"
|
|
26 #include "sauce.h"
|
|
27 #include <strings.h>
|
|
28
|
|
29 #define LINE_RATE 6000 /* characters per second */
|
|
30
|
|
31 typedef struct {
|
|
32 int chars_per_frame;
|
|
33 uint64_t fsize; /** file size less metadata buffer */
|
|
34 } TtyDemuxContext;
|
|
35
|
|
36 /**
|
|
37 * Parse EFI header
|
|
38 */
|
|
39 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
|
|
40 {
|
|
41 TtyDemuxContext *s = avctx->priv_data;
|
|
42 ByteIOContext *pb = avctx->pb;
|
|
43 char buf[37];
|
|
44 int len;
|
|
45
|
|
46 url_fseek(pb, start_pos, SEEK_SET);
|
|
47 if (get_byte(pb) != 0x1A)
|
|
48 return -1;
|
|
49
|
|
50 #define GET_EFI_META(name,size) \
|
|
51 len = get_byte(pb); \
|
|
52 if (len < 1 || len > size) \
|
|
53 return -1; \
|
|
54 if (get_buffer(pb, buf, size) == size) { \
|
|
55 buf[len] = 0; \
|
|
56 av_metadata_set2(&avctx->metadata, name, buf, 0); \
|
|
57 }
|
|
58
|
|
59 GET_EFI_META("filename", 12)
|
|
60 GET_EFI_META("title", 36)
|
|
61
|
|
62 s->fsize = start_pos;
|
|
63 return 0;
|
|
64 }
|
|
65
|
|
66 static int read_header(AVFormatContext *avctx,
|
|
67 AVFormatParameters *ap)
|
|
68 {
|
|
69 TtyDemuxContext *s = avctx->priv_data;
|
|
70 AVStream *st = av_new_stream(avctx, 0);
|
|
71 if (!st)
|
|
72 return AVERROR(ENOMEM);
|
|
73 st->codec->codec_tag = 0;
|
|
74 st->codec->codec_type = CODEC_TYPE_VIDEO;
|
|
75 st->codec->codec_id = CODEC_ID_ANSI;
|
|
76 if (ap->width) st->codec->width = ap->width;
|
|
77 if (ap->height) st->codec->height = ap->height;
|
|
78
|
|
79 if (!ap->time_base.num) {
|
|
80 av_set_pts_info(st, 60, 1, 25);
|
|
81 } else {
|
|
82 av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
|
|
83 }
|
|
84
|
|
85 /* simulate tty display speed */
|
|
86 s->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
|
|
87
|
|
88 if (!url_is_streamed(avctx->pb)) {
|
|
89 s->fsize = url_fsize(avctx->pb);
|
|
90 st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
|
|
91
|
|
92 if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
|
|
93 efi_read(avctx, s->fsize - 51);
|
|
94
|
|
95 url_fseek(avctx->pb, 0, SEEK_SET);
|
|
96 }
|
|
97
|
|
98 return 0;
|
|
99 }
|
|
100
|
|
101 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
|
|
102 {
|
|
103 TtyDemuxContext *s = avctx->priv_data;
|
|
104 int n;
|
|
105
|
|
106 if (url_feof(avctx->pb))
|
|
107 return AVERROR_EOF;
|
|
108
|
|
109 n = s->chars_per_frame;
|
|
110 if (s->fsize) {
|
|
111 // ignore metadata buffer
|
|
112 uint64_t p = url_ftell(avctx->pb);
|
|
113 if (p + s->chars_per_frame > s->fsize)
|
|
114 n = s->fsize - p;
|
|
115 }
|
|
116
|
|
117 pkt->size = av_get_packet(avctx->pb, pkt, n);
|
|
118 if (pkt->size <= 0)
|
|
119 return AVERROR(EIO);
|
|
120 pkt->flags |= PKT_FLAG_KEY;
|
|
121 return 0;
|
|
122 }
|
|
123
|
|
124 AVInputFormat tty_demuxer = {
|
|
125 .name = "tty",
|
|
126 .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
|
|
127 .priv_data_size = sizeof(TtyDemuxContext),
|
|
128 .read_header = read_header,
|
|
129 .read_packet = read_packet,
|
|
130 .extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
|
|
131 };
|