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 
26 #include "avformat.h"
27 #include "internal.h"
28 
29 #define MAX_LINESIZE 2000
30 
31 typedef struct ASSContext {
34  unsigned int event_count;
35  unsigned int event_index;
36 } ASSContext;
37 
38 static int probe(AVProbeData *p)
39 {
40  const char *header = "[Script Info]";
41 
42  if (!memcmp(p->buf, header, strlen(header)) ||
43  !memcmp(p->buf + 3, header, strlen(header)))
44  return AVPROBE_SCORE_MAX;
45 
46  return 0;
47 }
48 
50 {
51  ASSContext *ass = s->priv_data;
52 
53  av_freep(&ass->event_buffer);
54  av_freep(&ass->event);
55 
56  return 0;
57 }
58 
59 static int64_t get_pts(const uint8_t *p)
60 {
61  int hour, min, sec, hsec;
62 
63  if (sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
64  return AV_NOPTS_VALUE;
65 
66  av_dlog(NULL, "%d %d %d %d [%s]\n", hour, min, sec, hsec, p);
67 
68  min += 60 * hour;
69  sec += 60 * min;
70 
71  return sec * 100 + hsec;
72 }
73 
74 static int event_cmp(const void *_a, const void *_b)
75 {
76  const uint8_t *const *a = _a, *const *b = _b;
77  return get_pts(*a) - get_pts(*b);
78 }
79 
81 {
82  int i, len, header_remaining;
83  ASSContext *ass = s->priv_data;
84  AVIOContext *pb = s->pb;
85  AVStream *st;
86  int allocated[2] = { 0 };
87  uint8_t *p, **dst[2] = { 0 };
88  int pos[2] = { 0 };
89 
90  st = avformat_new_stream(s, NULL);
91  if (!st)
92  return -1;
93  avpriv_set_pts_info(st, 64, 1, 100);
96 
97  header_remaining = INT_MAX;
98  dst[0] = &st->codec->extradata;
99  dst[1] = &ass->event_buffer;
100  while (!pb->eof_reached) {
102 
103  len = ff_get_line(pb, line, sizeof(line));
104 
105  if (!memcmp(line, "[Events]", 8))
106  header_remaining = 2;
107  else if (line[0] == '[')
108  header_remaining = INT_MAX;
109 
110  i = header_remaining == 0;
111 
112  if (i && get_pts(line) == AV_NOPTS_VALUE)
113  continue;
114 
115  p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i] + MAX_LINESIZE);
116  if (!p)
117  goto fail;
118  *(dst[i]) = p;
119  memcpy(p + pos[i], line, len + 1);
120  pos[i] += len;
121  if (i)
122  ass->event_count++;
123  else
124  header_remaining--;
125  }
126  st->codec->extradata_size = pos[0];
127 
128  if (ass->event_count >= UINT_MAX / sizeof(*ass->event))
129  goto fail;
130 
131  ass->event = av_malloc(ass->event_count * sizeof(*ass->event));
132  p = ass->event_buffer;
133  for (i = 0; i < ass->event_count; i++) {
134  ass->event[i] = p;
135  while (*p && *p != '\n')
136  p++;
137  p++;
138  }
139 
140  qsort(ass->event, ass->event_count, sizeof(*ass->event), event_cmp);
141 
142  return 0;
143 
144 fail:
145  read_close(s);
146 
147  return -1;
148 }
149 
151 {
152  ASSContext *ass = s->priv_data;
153  uint8_t *p, *end;
154  int ret;
155 
156  if (ass->event_index >= ass->event_count)
157  return AVERROR(EIO);
158 
159  p = ass->event[ass->event_index];
160 
161  end = strchr(p, '\n');
162  ret = av_new_packet(pkt, end ? end - p + 1 : strlen(p));
163  if (ret < 0)
164  return ret;
165  pkt->flags |= AV_PKT_FLAG_KEY;
166  pkt->pos = p - ass->event_buffer + s->streams[0]->codec->extradata_size;
167  pkt->pts = pkt->dts = get_pts(p);
168  memcpy(pkt->data, p, pkt->size);
169 
170  ass->event_index++;
171 
172  return 0;
173 }
174 
175 static int read_seek2(AVFormatContext *s, int stream_index,
176  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
177 {
178  ASSContext *ass = s->priv_data;
179 
180  if (flags & AVSEEK_FLAG_BYTE) {
181  return AVERROR(ENOSYS);
182  } else if (flags & AVSEEK_FLAG_FRAME) {
183  if (ts < 0 || ts >= ass->event_count)
184  return AVERROR(ERANGE);
185  ass->event_index = ts;
186  } else {
187  int i, idx = -1;
188  int64_t min_ts_diff = INT64_MAX;
189  if (stream_index == -1) {
190  AVRational time_base = s->streams[0]->time_base;
191  ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
192  min_ts = av_rescale_rnd(min_ts, time_base.den,
193  time_base.num * (int64_t) AV_TIME_BASE,
194  AV_ROUND_UP);
195  max_ts = av_rescale_rnd(max_ts, time_base.den,
196  time_base.num * (int64_t) AV_TIME_BASE,
197  AV_ROUND_DOWN);
198  }
199  /* TODO: ass->event[] is sorted by pts so we could do a binary search */
200  for (i = 0; i < ass->event_count; i++) {
201  int64_t pts = get_pts(ass->event[i]);
202  int64_t ts_diff = FFABS(pts - ts);
203  if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
204  min_ts_diff = ts_diff;
205  idx = i;
206  }
207  }
208  if (idx < 0)
209  return AVERROR(ERANGE);
210  ass->event_index = idx;
211  }
212  return 0;
213 }
214 
216  .name = "ass",
217  .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
218  .priv_data_size = sizeof(ASSContext),
219  .read_probe = probe,
224 };
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:62
Bytestream IO Context.
Definition: avio.h:68
unsigned int event_index
Definition: assdec.c:35
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:61
static int64_t get_pts(const uint8_t *p)
Definition: assdec.c:59
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2829
AVInputFormat ff_ass_demuxer
Definition: assdec.c:215
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
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:198
Format I/O context.
Definition: avformat.h:922
unsigned int event_count
Definition: assdec.c:34
av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
uint8_t
Round toward +infinity.
Definition: mathematics.h:53
#define b
Definition: input.c:52
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
static int event_cmp(const void *_a, const void *_b)
Definition: assdec.c:74
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
static int probe(AVProbeData *p)
Definition: assdec.c:38
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
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: mem.c:369
#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:145
Definition: graph2dot.c:49
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:234
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
uint8_t * event_buffer
Definition: assdec.c:32
#define FFABS(a)
Definition: common.h:52
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:603
#define MAX_LINESIZE
Definition: assdec.c:29
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:240
AVIOContext * pb
I/O context.
Definition: avformat.h:964
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assdec.c:150
int extradata_size
Definition: avcodec.h:1165
rational number numerator/denominator
Definition: rational.h:43
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:1610
uint8_t ** event
Definition: assdec.c:33
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
Round toward -infinity.
Definition: mathematics.h:52
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
Main libavformat public API header.
int den
denominator
Definition: rational.h:45
static int read_header(AVFormatContext *s)
Definition: assdec.c:80
int eof_reached
true if eof reached
Definition: avio.h:96
int len
void * priv_data
Format private data.
Definition: avformat.h:950
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition: avformat.h:1612
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
static int read_close(AVFormatContext *s)
Definition: assdec.c:49
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
float min
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
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:175