Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "libavutil/mathematics.h"
25 #include "avformat.h"
26 #include "internal.h"
27 
28 #define MAX_LINESIZE 2000
29 
30 typedef struct ASSContext{
33  unsigned int event_count;
34  unsigned int event_index;
35 }ASSContext;
36 
37 static int probe(AVProbeData *p)
38 {
39  const char *header= "[Script Info]";
40 
41  if( !memcmp(p->buf , header, strlen(header))
42  || !memcmp(p->buf+3, header, strlen(header)))
43  return AVPROBE_SCORE_MAX;
44 
45  return 0;
46 }
47 
49 {
50  ASSContext *ass = s->priv_data;
51 
52  av_freep(&ass->event_buffer);
53  av_freep(&ass->event);
54 
55  return 0;
56 }
57 
58 static int64_t get_pts(const uint8_t *p)
59 {
60  int hour, min, sec, hsec;
61 
62  if(sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
63  return AV_NOPTS_VALUE;
64 
65  av_dlog(NULL, "%d %d %d %d [%s]\n", hour, min, sec, hsec, p);
66 
67  min+= 60*hour;
68  sec+= 60*min;
69 
70  return sec*100+hsec;
71 }
72 
73 static int event_cmp(const void *_a, const void *_b)
74 {
75  const uint8_t *const *a = _a, *const *b = _b;
76  return get_pts(*a) - get_pts(*b);
77 }
78 
80 {
81  int i, len, header_remaining;
82  ASSContext *ass = s->priv_data;
83  AVIOContext *pb = s->pb;
84  AVStream *st;
85  int allocated[2]={0};
86  uint8_t *p, **dst[2]={0};
87  int pos[2]={0};
88 
89  st = avformat_new_stream(s, NULL);
90  if (!st)
91  return -1;
92  avpriv_set_pts_info(st, 64, 1, 100);
95 
96  header_remaining= INT_MAX;
97  dst[0] = &st->codec->extradata;
98  dst[1] = &ass->event_buffer;
99  while(!pb->eof_reached){
101 
102  len = ff_get_line(pb, line, sizeof(line));
103 
104  if(!memcmp(line, "[Events]", 8))
105  header_remaining= 2;
106  else if(line[0]=='[')
107  header_remaining= INT_MAX;
108 
109  i= header_remaining==0;
110 
111  if(i && get_pts(line) == AV_NOPTS_VALUE)
112  continue;
113 
114  p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i]+MAX_LINESIZE);
115  if(!p)
116  goto fail;
117  *(dst[i])= p;
118  memcpy(p + pos[i], line, len+1);
119  pos[i] += len;
120  if(i) ass->event_count++;
121  else header_remaining--;
122  }
123  st->codec->extradata_size= pos[0];
124 
125  if(ass->event_count >= UINT_MAX / sizeof(*ass->event))
126  goto fail;
127 
128  ass->event= av_malloc(ass->event_count * sizeof(*ass->event));
129  p= ass->event_buffer;
130  for(i=0; i<ass->event_count; i++){
131  ass->event[i]= p;
132  while(*p && *p != '\n')
133  p++;
134  p++;
135  }
136 
137  qsort(ass->event, ass->event_count, sizeof(*ass->event), event_cmp);
138 
139  return 0;
140 
141 fail:
142  read_close(s);
143 
144  return -1;
145 }
146 
148 {
149  ASSContext *ass = s->priv_data;
150  uint8_t *p, *end;
151 
152  if(ass->event_index >= ass->event_count)
153  return AVERROR(EIO);
154 
155  p= ass->event[ ass->event_index ];
156 
157  end= strchr(p, '\n');
158  av_new_packet(pkt, end ? end-p+1 : strlen(p));
159  pkt->flags |= AV_PKT_FLAG_KEY;
160  pkt->pos= p - ass->event_buffer + s->streams[0]->codec->extradata_size;
161  pkt->pts= pkt->dts= get_pts(p);
162  memcpy(pkt->data, p, pkt->size);
163 
164  ass->event_index++;
165 
166  return 0;
167 }
168 
169 static int read_seek2(AVFormatContext *s, int stream_index,
170  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
171 {
172  ASSContext *ass = s->priv_data;
173 
174  if (flags & AVSEEK_FLAG_BYTE) {
175  return AVERROR(ENOSYS);
176  } else if (flags & AVSEEK_FLAG_FRAME) {
177  if (ts < 0 || ts >= ass->event_count)
178  return AVERROR(ERANGE);
179  ass->event_index = ts;
180  } else {
181  int i, idx = -1;
182  int64_t min_ts_diff = INT64_MAX;
183  if (stream_index == -1) {
184  AVRational time_base = s->streams[0]->time_base;
185  ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
186  min_ts = av_rescale_rnd(min_ts, time_base.den,
187  time_base.num * (int64_t)AV_TIME_BASE,
188  AV_ROUND_UP);
189  max_ts = av_rescale_rnd(max_ts, time_base.den,
190  time_base.num * (int64_t)AV_TIME_BASE,
191  AV_ROUND_DOWN);
192  }
193  /* TODO: ass->event[] is sorted by pts so we could do a binary search */
194  for (i=0; i<ass->event_count; i++) {
195  int64_t pts = get_pts(ass->event[i]);
196  int64_t ts_diff = FFABS(pts - ts);
197  if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
198  min_ts_diff = ts_diff;
199  idx = i;
200  }
201  }
202  if (idx < 0)
203  return AVERROR(ERANGE);
204  ass->event_index = idx;
205  }
206  return 0;
207 }
208 
210  .name = "ass",
211  .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
212  .priv_data_size = sizeof(ASSContext),
213  .read_probe = probe,
218 };