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 "libavutil/mathematics.h"
23 #include "avformat.h"
24 #include "internal.h"
25 
26 #define MAX_LINESIZE 2000
27 
28 typedef struct ASSContext{
29  uint8_t *event_buffer;
30  uint8_t **event;
31  unsigned int event_count;
32  unsigned int event_index;
33 }ASSContext;
34 
35 static int probe(AVProbeData *p)
36 {
37  const char *header= "[Script Info]";
38 
39  if( !memcmp(p->buf , header, strlen(header))
40  || !memcmp(p->buf+3, header, strlen(header)))
41  return AVPROBE_SCORE_MAX;
42 
43  return 0;
44 }
45 
47 {
48  ASSContext *ass = s->priv_data;
49 
50  av_freep(&ass->event_buffer);
51  av_freep(&ass->event);
52 
53  return 0;
54 }
55 
56 static int64_t get_pts(const uint8_t *p)
57 {
58  int hour, min, sec, hsec;
59 
60  if(sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
61  return AV_NOPTS_VALUE;
62 
63 // av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d [%s]\n", i, hour, min, sec, hsec, p);
64 
65  min+= 60*hour;
66  sec+= 60*min;
67 
68  return sec*100+hsec;
69 }
70 
71 static int event_cmp(uint8_t **a, uint8_t **b)
72 {
73  return get_pts(*a) - get_pts(*b);
74 }
75 
77 {
78  int i, len, header_remaining;
79  ASSContext *ass = s->priv_data;
80  AVIOContext *pb = s->pb;
81  AVStream *st;
82  int allocated[2]={0};
83  uint8_t *p, **dst[2]={0};
84  int pos[2]={0};
85 
86  st = avformat_new_stream(s, NULL);
87  if (!st)
88  return -1;
89  avpriv_set_pts_info(st, 64, 1, 100);
92 
93  header_remaining= INT_MAX;
94  dst[0] = &st->codec->extradata;
95  dst[1] = &ass->event_buffer;
96  while(!pb->eof_reached){
97  uint8_t line[MAX_LINESIZE];
98 
99  len = ff_get_line(pb, line, sizeof(line));
100 
101  if(!memcmp(line, "[Events]", 8))
102  header_remaining= 2;
103  else if(line[0]=='[')
104  header_remaining= INT_MAX;
105 
106  i= header_remaining==0;
107 
108  if(i && get_pts(line) == AV_NOPTS_VALUE)
109  continue;
110 
111  p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i]+MAX_LINESIZE);
112  if(!p)
113  goto fail;
114  *(dst[i])= p;
115  memcpy(p + pos[i], line, len+1);
116  pos[i] += len;
117  if(i) ass->event_count++;
118  else header_remaining--;
119  }
120  st->codec->extradata_size= pos[0];
121 
122  if(ass->event_count >= UINT_MAX / sizeof(*ass->event))
123  goto fail;
124 
125  ass->event= av_malloc(ass->event_count * sizeof(*ass->event));
126  p= ass->event_buffer;
127  for(i=0; i<ass->event_count; i++){
128  ass->event[i]= p;
129  while(*p && *p != '\n')
130  p++;
131  p++;
132  }
133 
134  qsort(ass->event, ass->event_count, sizeof(*ass->event), (void*)event_cmp);
135 
136  return 0;
137 
138 fail:
139  read_close(s);
140 
141  return -1;
142 }
143 
145 {
146  ASSContext *ass = s->priv_data;
147  uint8_t *p, *end;
148 
149  if(ass->event_index >= ass->event_count)
150  return AVERROR(EIO);
151 
152  p= ass->event[ ass->event_index ];
153 
154  end= strchr(p, '\n');
155  av_new_packet(pkt, end ? end-p+1 : strlen(p));
156  pkt->flags |= AV_PKT_FLAG_KEY;
157  pkt->pos= p - ass->event_buffer + s->streams[0]->codec->extradata_size;
158  pkt->pts= pkt->dts= get_pts(p);
159  memcpy(pkt->data, p, pkt->size);
160 
161  ass->event_index++;
162 
163  return 0;
164 }
165 
166 static int read_seek2(AVFormatContext *s, int stream_index,
167  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
168 {
169  ASSContext *ass = s->priv_data;
170 
171  if (flags & AVSEEK_FLAG_BYTE) {
172  return AVERROR(ENOSYS);
173  } else if (flags & AVSEEK_FLAG_FRAME) {
174  if (ts < 0 || ts >= ass->event_count)
175  return AVERROR(ERANGE);
176  ass->event_index = ts;
177  } else {
178  int i, idx = -1;
179  int64_t min_ts_diff = INT64_MAX;
180  if (stream_index == -1) {
181  AVRational time_base = s->streams[0]->time_base;
182  ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
183  min_ts = av_rescale_rnd(min_ts, time_base.den,
184  time_base.num * (int64_t)AV_TIME_BASE,
185  AV_ROUND_UP);
186  max_ts = av_rescale_rnd(max_ts, time_base.den,
187  time_base.num * (int64_t)AV_TIME_BASE,
188  AV_ROUND_DOWN);
189  }
190  /* TODO: ass->event[] is sorted by pts so we could do a binary search */
191  for (i=0; i<ass->event_count; i++) {
192  int64_t pts = get_pts(ass->event[i]);
193  int64_t ts_diff = FFABS(pts - ts);
194  if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
195  min_ts_diff = ts_diff;
196  idx = i;
197  }
198  }
199  if (idx < 0)
200  return AVERROR(ERANGE);
201  ass->event_index = idx;
202  }
203  return 0;
204 }
205 
207  .name = "ass",
208  .long_name = NULL_IF_CONFIG_SMALL("Advanced SubStation Alpha subtitle format"),
209  .priv_data_size = sizeof(ASSContext),
210  .read_probe = probe,
215 };
Bytestream IO Context.
Definition: avio.h:68
unsigned int event_index
Definition: assdec.c:32
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:76
static int64_t get_pts(const uint8_t *p)
Definition: assdec.c:56
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:933
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
AVInputFormat ff_ass_demuxer
Definition: assdec.c:206
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:909
#define b
Definition: swscale.c:1335
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:147
Format I/O context.
Definition: avformat.h:863
unsigned int event_count
Definition: assdec.c:31
Round toward +infinity.
Definition: mathematics.h:70
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
AVStream ** streams
Definition: avformat.h:908
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
static int probe(AVProbeData *p)
Definition: assdec.c:35
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:132
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
Definition: graph2dot.c:39
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
AVCodecContext * codec
codec context
Definition: avformat.h:623
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
static int read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: assdec.c:76
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:277
static int read_probe(AVProbeData *p)
Definition: img2.c:185
uint8_t * event_buffer
Definition: assdec.c:29
#define FFABS(a)
Definition: common.h:50
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:785
#define MAX_LINESIZE
Definition: assdec.c:26
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:283
AVIOContext * pb
Definition: avformat.h:896
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assdec.c:144
#define INT64_MAX
Definition: internal.h:80
int extradata_size
Definition: avcodec.h:1388
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
rational number numerator/denominator
Definition: rational.h:43
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:1619
uint8_t ** event
Definition: assdec.c:30
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
Round toward -infinity.
Definition: mathematics.h:69
struct ASSContext ASSContext
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
Main libavformat public API header.
int den
denominator
Definition: rational.h:45
int eof_reached
true if eof reached
Definition: avio.h:98
int len
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: utils.c:55
void * priv_data
Format private data.
Definition: avformat.h:883
static int event_cmp(uint8_t **a, uint8_t **b)
Definition: assdec.c:71
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition: avformat.h:1621
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:907
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
static int read_close(AVFormatContext *s)
Definition: assdec.c:46
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:652
float min
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:271
static int read_seek2(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: assdec.c:166
enum CodecID codec_id
Definition: avcodec.h:1575