Statistics
| Branch: | Revision:

ffmpeg / libavformat / tty.c @ b9f9e59a

History | View | Annotate | Download (3.69 KB)

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
#include <strings.h>
32

    
33
#define LINE_RATE 6000 /* characters per second */
34

    
35
typedef struct {
36
    int chars_per_frame;
37
    uint64_t fsize;  /**< file size less metadata buffer */
38
} TtyDemuxContext;
39

    
40
/**
41
 * Parse EFI header
42
 */
43
static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
44
{
45
    TtyDemuxContext *s = avctx->priv_data;
46
    ByteIOContext *pb = avctx->pb;
47
    char buf[37];
48
    int len;
49

    
50
    url_fseek(pb, start_pos, SEEK_SET);
51
    if (get_byte(pb) != 0x1A)
52
        return -1;
53

    
54
#define GET_EFI_META(name,size) \
55
    len = get_byte(pb); \
56
    if (len < 1 || len > size) \
57
        return -1; \
58
    if (get_buffer(pb, buf, size) == size) { \
59
        buf[len] = 0; \
60
        av_metadata_set2(&avctx->metadata, name, buf, 0); \
61
    }
62

    
63
    GET_EFI_META("filename", 12)
64
    GET_EFI_META("title",    36)
65

    
66
    s->fsize = start_pos;
67
    return 0;
68
}
69

    
70
static int read_header(AVFormatContext *avctx,
71
                       AVFormatParameters *ap)
72
{
73
    TtyDemuxContext *s = avctx->priv_data;
74
    AVStream *st = av_new_stream(avctx, 0);
75
    if (!st)
76
        return AVERROR(ENOMEM);
77
    st->codec->codec_tag   = 0;
78
    st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
79
    st->codec->codec_id    = CODEC_ID_ANSI;
80
    if (ap->width)  st->codec->width  = ap->width;
81
    if (ap->height) st->codec->height = ap->height;
82

    
83
    if (!ap->time_base.num) {
84
        av_set_pts_info(st, 60, 1, 25);
85
    } else {
86
        av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
87
    }
88

    
89
    /* simulate tty display speed */
90
    s->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
91

    
92
    if (!url_is_streamed(avctx->pb)) {
93
        s->fsize = url_fsize(avctx->pb);
94
        st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
95

    
96
        if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
97
            efi_read(avctx, s->fsize - 51);
98

    
99
        url_fseek(avctx->pb, 0, SEEK_SET);
100
    }
101

    
102
    return 0;
103
}
104

    
105
static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
106
{
107
    TtyDemuxContext *s = avctx->priv_data;
108
    int n;
109

    
110
    if (url_feof(avctx->pb))
111
        return AVERROR_EOF;
112

    
113
    n = s->chars_per_frame;
114
    if (s->fsize) {
115
        // ignore metadata buffer
116
        uint64_t p = url_ftell(avctx->pb);
117
        if (p + s->chars_per_frame > s->fsize)
118
            n = s->fsize - p;
119
    }
120

    
121
    pkt->size = av_get_packet(avctx->pb, pkt, n);
122
    if (pkt->size <= 0)
123
        return AVERROR(EIO);
124
    pkt->flags |= PKT_FLAG_KEY;
125
    return 0;
126
}
127

    
128
AVInputFormat tty_demuxer = {
129
    .name           = "tty",
130
    .long_name      = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
131
    .priv_data_size = sizeof(TtyDemuxContext),
132
    .read_header    = read_header,
133
    .read_packet    = read_packet,
134
    .extensions     = "ans,art,asc,diz,ice,nfo,txt,vt",
135
};