Statistics
| Branch: | Revision:

ffmpeg / libavformat / assdec.c @ 470bce2b

History | View | Annotate | Download (4.36 KB)

1 de3ae185 Michael Niedermayer
/*
2
 * SSA/ASS demuxer
3
 * Copyright (c) 2008 Michael Niedermayer
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
#include "avformat.h"
23
24
#define MAX_LINESIZE 2000
25
26
typedef struct ASSContext{
27
    uint8_t *event_buffer;
28
    uint8_t **event;
29
    unsigned int event_count;
30
    unsigned int event_index;
31
}ASSContext;
32
33
static void get_line(ByteIOContext *s, char *buf, int maxlen)
34
{
35
    int i = 0;
36
    char c;
37
38
    do{
39
        c = get_byte(s);
40 7a8b7634 Aurelien Jacobs
        if (i < maxlen-1)
41 de3ae185 Michael Niedermayer
            buf[i++] = c;
42
    }while(c != '\n' && c);
43
44
    buf[i] = 0;
45
}
46
47
static int probe(AVProbeData *p)
48
{
49
    const char *header= "[Script Info]";
50
51
    if(   !memcmp(p->buf  , header, strlen(header))
52
       || !memcmp(p->buf+3, header, strlen(header)))
53
        return AVPROBE_SCORE_MAX;
54
55
    return 0;
56
}
57
58
static int read_close(AVFormatContext *s)
59
{
60
    ASSContext *ass = s->priv_data;
61
62
    av_freep(&ass->event_buffer);
63
    av_freep(&ass->event);
64
65
    return 0;
66
}
67
68
static int64_t get_pts(const uint8_t *p)
69
{
70
    int hour, min, sec, hsec;
71
72
    if(sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
73
        return AV_NOPTS_VALUE;
74
75
//    av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d [%s]\n", i, hour, min, sec, hsec, p);
76
77
    min+= 60*hour;
78
    sec+= 60*min;
79
80
    return sec*100+hsec;
81
}
82
83
static int event_cmp(uint8_t **a, uint8_t **b)
84
{
85
    return get_pts(*a) - get_pts(*b);
86
}
87
88
static int read_header(AVFormatContext *s, AVFormatParameters *ap)
89
{
90
    int i, header_remaining;
91
    ASSContext *ass = s->priv_data;
92
    ByteIOContext *pb = s->pb;
93
    AVStream *st;
94
    int allocated[2]={0};
95
    uint8_t *p, **dst[2]={0};
96
    int pos[2]={0};
97
98
    st = av_new_stream(s, 0);
99
    if (!st)
100
        return -1;
101
    av_set_pts_info(st, 64, 1, 100);
102
    st->codec->codec_type = CODEC_TYPE_SUBTITLE;
103
    st->codec->codec_id= CODEC_ID_SSA;
104
105
    header_remaining= INT_MAX;
106
    dst[0] = &st->codec->extradata;
107
    dst[1] = &ass->event_buffer;
108
    while(!url_feof(pb)){
109
        uint8_t line[MAX_LINESIZE];
110
111
        get_line(pb, line, sizeof(line));
112
113
        if(!memcmp(line, "[Events]", 8))
114
            header_remaining= 2;
115
        else if(line[0]=='[')
116
            header_remaining= INT_MAX;
117
118
        i= header_remaining==0;
119
120
        if(i && get_pts(line) == AV_NOPTS_VALUE)
121
            continue;
122
123
        p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i]+MAX_LINESIZE);
124
        if(!p)
125
            goto fail;
126
        *(dst[i])= p;
127
        memcpy(p + pos[i], line, strlen(line)+1);
128
        pos[i] += strlen(line);
129
        if(i) ass->event_count++;
130
        else  header_remaining--;
131
    }
132
    st->codec->extradata_size= pos[0];
133
134
    if(ass->event_count >= UINT_MAX / sizeof(*ass->event))
135
        goto fail;
136
137
    ass->event= av_malloc(ass->event_count * sizeof(*ass->event));
138
    p= ass->event_buffer;
139
    for(i=0; i<ass->event_count; i++){
140
        ass->event[i]= p;
141
        while(*p && *p != '\n')
142
            p++;
143 5c8a1d19 Michael Niedermayer
        p++;
144 de3ae185 Michael Niedermayer
    }
145
146
    qsort(ass->event, ass->event_count, sizeof(*ass->event), event_cmp);
147
148
    return 0;
149
150
fail:
151
    read_close(s);
152
153
    return -1;
154
}
155
156
static int read_packet(AVFormatContext *s, AVPacket *pkt)
157
{
158
    ASSContext *ass = s->priv_data;
159 5c8a1d19 Michael Niedermayer
    uint8_t *p, *end;
160 de3ae185 Michael Niedermayer
161
    if(ass->event_index >= ass->event_count)
162
        return AVERROR(EIO);
163
164
    p= ass->event[ ass->event_index ];
165
166 5c8a1d19 Michael Niedermayer
    end= strchr(p, '\n');
167
    av_new_packet(pkt, end ? end-p+1 : strlen(p));
168 de3ae185 Michael Niedermayer
    pkt->flags |= PKT_FLAG_KEY;
169
    pkt->pos= p - ass->event_buffer + s->streams[0]->codec->extradata_size;
170
    pkt->pts= pkt->dts= get_pts(p);
171
    memcpy(pkt->data, p, pkt->size);
172
173
    ass->event_index++;
174
175
    return 0;
176
}
177
178
AVInputFormat ass_demuxer = {
179
    "ass",
180
    NULL_IF_CONFIG_SMALL("SSA/ASS format"),
181
    sizeof(ASSContext),
182
    probe,
183
    read_header,
184
    read_packet,
185
    read_close,
186
//    read_seek,
187
};