FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
assdec.c
Go to the documentation of this file.
1 /*
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 <stdint.h>
23 
24 #include "avformat.h"
25 #include "internal.h"
26 #include "subtitles.h"
27 #include "libavcodec/internal.h"
28 #include "libavutil/bprint.h"
29 
30 typedef struct ASSContext {
32 } ASSContext;
33 
34 static int ass_probe(AVProbeData *p)
35 {
36  const char *header = "[Script Info]";
37 
38  if (!memcmp(p->buf, header, strlen(header)) ||
39  !memcmp(p->buf + 3, header, strlen(header)))
40  return AVPROBE_SCORE_MAX;
41 
42  return 0;
43 }
44 
46 {
47  ASSContext *ass = s->priv_data;
49  return 0;
50 }
51 
52 static int read_ts(const uint8_t *p, int64_t *start, int *duration)
53 {
54  int64_t end;
55  int hh1, mm1, ss1, ms1;
56  int hh2, mm2, ss2, ms2;
57 
58  if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
59  &hh1, &mm1, &ss1, &ms1,
60  &hh2, &mm2, &ss2, &ms2) == 8) {
61  end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
62  *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
63  *duration = end - *start;
64  return 0;
65  }
66  return -1;
67 }
68 
69 static int64_t get_line(AVBPrint *buf, AVIOContext *pb)
70 {
71  int64_t pos = avio_tell(pb);
72 
73  av_bprint_clear(buf);
74  for (;;) {
75  char c = avio_r8(pb);
76  if (!c)
77  break;
78  av_bprint_chars(buf, c, 1);
79  if (c == '\n')
80  break;
81  }
82  return pos;
83 }
84 
86 {
87  ASSContext *ass = s->priv_data;
89  int header_remaining, res = 0;
90  AVStream *st;
91 
92  st = avformat_new_stream(s, NULL);
93  if (!st)
94  return AVERROR(ENOMEM);
95  avpriv_set_pts_info(st, 64, 1, 100);
98 
99  header_remaining = INT_MAX;
100 
103 
104  for (;;) {
105  int64_t pos = get_line(&line, s->pb);
106 
107  if (!line.str[0]) // EOF
108  break;
109 
110  if (!memcmp(line.str, "[Events]", 8))
111  header_remaining = 2;
112  else if (line.str[0] == '[')
113  header_remaining = INT_MAX;
114 
115  if (header_remaining) {
116  av_bprintf(&header, "%s", line.str);
117  header_remaining--;
118  } else {
119  int64_t ts_start = AV_NOPTS_VALUE;
120  int duration = -1;
121  AVPacket *sub;
122 
123  if (read_ts(line.str, &ts_start, &duration) < 0)
124  continue;
125  sub = ff_subtitles_queue_insert(&ass->q, line.str, line.len, 0);
126  if (!sub) {
127  res = AVERROR(ENOMEM);
128  goto end;
129  }
130  sub->pos = pos;
131  sub->pts = ts_start;
132  sub->duration = duration;
133  }
134  }
135 
136  av_bprint_finalize(&line, NULL);
137 
138  res = avpriv_bprint_to_extradata(st->codec, &header);
139  if (res < 0)
140  goto end;
141 
143 
144 end:
145  return res;
146 }
147 
149 {
150  ASSContext *ass = s->priv_data;
151  return ff_subtitles_queue_read_packet(&ass->q, pkt);
152 }
153 
154 static int ass_read_seek(AVFormatContext *s, int stream_index,
155  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
156 {
157  ASSContext *ass = s->priv_data;
158  return ff_subtitles_queue_seek(&ass->q, s, stream_index,
159  min_ts, ts, max_ts, flags);
160 }
161 
163  .name = "ass",
164  .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
165  .priv_data_size = sizeof(ASSContext),
170  .read_seek2 = ass_read_seek,
171 };