annotate assdec.c @ 6314:d907dafa4c5d libavformat

add mime-type for ASS muxer
author aurel
date Sun, 25 Jul 2010 19:36:20 +0000
parents af30d878f7d7
children 8788871cbc99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
1 /*
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
2 * SSA/ASS demuxer
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
3 * Copyright (c) 2008 Michael Niedermayer
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
4 *
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
5 * This file is part of FFmpeg.
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
6 *
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
11 *
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
15 * Lesser General Public License for more details.
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
16 *
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
20 */
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
21
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
22 #include "avformat.h"
6300
e62d23b0547d move ff_get_line to aviobuf.c
aurel
parents: 6299
diff changeset
23 #include "internal.h"
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
24
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
25 #define MAX_LINESIZE 2000
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
26
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
27 typedef struct ASSContext{
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
28 uint8_t *event_buffer;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
29 uint8_t **event;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
30 unsigned int event_count;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
31 unsigned int event_index;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
32 }ASSContext;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
33
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
34 static int probe(AVProbeData *p)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
35 {
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
36 const char *header= "[Script Info]";
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
37
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
38 if( !memcmp(p->buf , header, strlen(header))
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
39 || !memcmp(p->buf+3, header, strlen(header)))
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
40 return AVPROBE_SCORE_MAX;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
41
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
42 return 0;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
43 }
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
44
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
45 static int read_close(AVFormatContext *s)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
46 {
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
47 ASSContext *ass = s->priv_data;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
48
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
49 av_freep(&ass->event_buffer);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
50 av_freep(&ass->event);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
51
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
52 return 0;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
53 }
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
54
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
55 static int64_t get_pts(const uint8_t *p)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
56 {
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
57 int hour, min, sec, hsec;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
58
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
59 if(sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
60 return AV_NOPTS_VALUE;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
61
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
62 // av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d [%s]\n", i, hour, min, sec, hsec, p);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
63
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
64 min+= 60*hour;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
65 sec+= 60*min;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
66
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
67 return sec*100+hsec;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
68 }
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
69
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
70 static int event_cmp(uint8_t **a, uint8_t **b)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
71 {
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
72 return get_pts(*a) - get_pts(*b);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
73 }
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
74
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
75 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
76 {
6303
af30d878f7d7 use return value of ff_get_line()
aurel
parents: 6300
diff changeset
77 int i, len, header_remaining;
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
78 ASSContext *ass = s->priv_data;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
79 ByteIOContext *pb = s->pb;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
80 AVStream *st;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
81 int allocated[2]={0};
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
82 uint8_t *p, **dst[2]={0};
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
83 int pos[2]={0};
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
84
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
85 st = av_new_stream(s, 0);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
86 if (!st)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
87 return -1;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
88 av_set_pts_info(st, 64, 1, 100);
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 4484
diff changeset
89 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
90 st->codec->codec_id= CODEC_ID_SSA;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
91
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
92 header_remaining= INT_MAX;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
93 dst[0] = &st->codec->extradata;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
94 dst[1] = &ass->event_buffer;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
95 while(!url_feof(pb)){
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
96 uint8_t line[MAX_LINESIZE];
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
97
6303
af30d878f7d7 use return value of ff_get_line()
aurel
parents: 6300
diff changeset
98 len = ff_get_line(pb, line, sizeof(line));
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
99
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
100 if(!memcmp(line, "[Events]", 8))
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
101 header_remaining= 2;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
102 else if(line[0]=='[')
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
103 header_remaining= INT_MAX;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
104
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
105 i= header_remaining==0;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
106
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
107 if(i && get_pts(line) == AV_NOPTS_VALUE)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
108 continue;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
109
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
110 p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i]+MAX_LINESIZE);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
111 if(!p)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
112 goto fail;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
113 *(dst[i])= p;
6303
af30d878f7d7 use return value of ff_get_line()
aurel
parents: 6300
diff changeset
114 memcpy(p + pos[i], line, len+1);
af30d878f7d7 use return value of ff_get_line()
aurel
parents: 6300
diff changeset
115 pos[i] += len;
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
116 if(i) ass->event_count++;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
117 else header_remaining--;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
118 }
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
119 st->codec->extradata_size= pos[0];
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
120
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
121 if(ass->event_count >= UINT_MAX / sizeof(*ass->event))
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
122 goto fail;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
123
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
124 ass->event= av_malloc(ass->event_count * sizeof(*ass->event));
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
125 p= ass->event_buffer;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
126 for(i=0; i<ass->event_count; i++){
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
127 ass->event[i]= p;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
128 while(*p && *p != '\n')
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
129 p++;
3943
b250956a6f68 Do not lose the '\n'.
michael
parents: 3942
diff changeset
130 p++;
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
131 }
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
132
4484
da64b6d7a2d9 Silence "assdec.c:146: warning: passing argument 4 of ¡Æqsort¡Ç from incompatible pointer type"
michael
parents: 3950
diff changeset
133 qsort(ass->event, ass->event_count, sizeof(*ass->event), (void*)event_cmp);
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
134
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
135 return 0;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
136
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
137 fail:
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
138 read_close(s);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
139
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
140 return -1;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
141 }
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
142
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
143 static int read_packet(AVFormatContext *s, AVPacket *pkt)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
144 {
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
145 ASSContext *ass = s->priv_data;
3943
b250956a6f68 Do not lose the '\n'.
michael
parents: 3942
diff changeset
146 uint8_t *p, *end;
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
147
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
148 if(ass->event_index >= ass->event_count)
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
149 return AVERROR(EIO);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
150
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
151 p= ass->event[ ass->event_index ];
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
152
3943
b250956a6f68 Do not lose the '\n'.
michael
parents: 3942
diff changeset
153 end= strchr(p, '\n');
b250956a6f68 Do not lose the '\n'.
michael
parents: 3942
diff changeset
154 av_new_packet(pkt, end ? end-p+1 : strlen(p));
5913
11bb10c37225 Replace all occurences of PKT_FLAG_KEY with AV_PKT_FLAG_KEY.
cehoyos
parents: 5910
diff changeset
155 pkt->flags |= AV_PKT_FLAG_KEY;
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
156 pkt->pos= p - ass->event_buffer + s->streams[0]->codec->extradata_size;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
157 pkt->pts= pkt->dts= get_pts(p);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
158 memcpy(pkt->data, p, pkt->size);
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
159
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
160 ass->event_index++;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
161
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
162 return 0;
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
163 }
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
164
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
165 AVInputFormat ass_demuxer = {
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
166 "ass",
6241
a7e674f70016 imporve long_name for ass (de)muxer
aurel
parents: 5913
diff changeset
167 NULL_IF_CONFIG_SMALL("Advanced SubStation Alpha subtitle format"),
3942
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
168 sizeof(ASSContext),
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
169 probe,
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
170 read_header,
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
171 read_packet,
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
172 read_close,
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
173 // read_seek,
ffb410d82ad3 2 in 1, an ASS and SSA demuxer.
michael
parents:
diff changeset
174 };