libavformat/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 "avformat.h"
23 #include "internal.h"
24 #include "subtitles.h"
25 #include "libavcodec/internal.h"
26 #include "libavutil/bprint.h"
27 
28 typedef struct ASSContext{
30 }ASSContext;
31 
32 static int ass_probe(AVProbeData *p)
33 {
34  const char *header= "[Script Info]";
35 
36  if( !memcmp(p->buf , header, strlen(header))
37  || !memcmp(p->buf+3, header, strlen(header)))
38  return AVPROBE_SCORE_MAX;
39 
40  return 0;
41 }
42 
44 {
45  ASSContext *ass = s->priv_data;
47  return 0;
48 }
49 
50 static int read_ts(const uint8_t *p, int64_t *start, int *duration)
51 {
52  int64_t end;
53  int hh1, mm1, ss1, ms1;
54  int hh2, mm2, ss2, ms2;
55 
56  if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
57  &hh1, &mm1, &ss1, &ms1,
58  &hh2, &mm2, &ss2, &ms2) == 8) {
59  end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
60  *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
61  *duration = end - *start;
62  return 0;
63  }
64  return -1;
65 }
66 
67 static int64_t get_line(AVBPrint *buf, AVIOContext *pb)
68 {
69  int64_t pos = avio_tell(pb);
70 
71  av_bprint_clear(buf);
72  for (;;) {
73  char c = avio_r8(pb);
74  if (!c)
75  break;
76  av_bprint_chars(buf, c, 1);
77  if (c == '\n')
78  break;
79  }
80  return pos;
81 }
82 
84 {
85  ASSContext *ass = s->priv_data;
86  AVBPrint header, line;
87  int header_remaining, res = 0;
88  AVStream *st;
89 
90  st = avformat_new_stream(s, NULL);
91  if (!st)
92  return AVERROR(ENOMEM);
93  avpriv_set_pts_info(st, 64, 1, 100);
96 
97  header_remaining= INT_MAX;
98 
101 
102  for (;;) {
103  int64_t pos = get_line(&line, s->pb);
104 
105  if (!line.str[0]) // EOF
106  break;
107 
108  if (!memcmp(line.str, "[Events]", 8))
109  header_remaining= 2;
110  else if (line.str[0]=='[')
111  header_remaining= INT_MAX;
112 
113  if (header_remaining) {
114  av_bprintf(&header, "%s", line.str);
115  header_remaining--;
116  } else {
117  int64_t ts_start = AV_NOPTS_VALUE;
118  int duration = -1;
119  AVPacket *sub;
120 
121  if (read_ts(line.str, &ts_start, &duration) < 0)
122  continue;
123  sub = ff_subtitles_queue_insert(&ass->q, line.str, line.len, 0);
124  if (!sub) {
125  res = AVERROR(ENOMEM);
126  goto end;
127  }
128  sub->pos = pos;
129  sub->pts = ts_start;
130  sub->duration = duration;
131  }
132  }
133 
134  av_bprint_finalize(&line, NULL);
135 
136  res = avpriv_bprint_to_extradata(st->codec, &header);
137  if (res < 0)
138  goto end;
139 
141 
142 end:
143  return res;
144 }
145 
147 {
148  ASSContext *ass = s->priv_data;
149  return ff_subtitles_queue_read_packet(&ass->q, pkt);
150 }
151 
152 static int ass_read_seek(AVFormatContext *s, int stream_index,
153  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
154 {
155  ASSContext *ass = s->priv_data;
156  return ff_subtitles_queue_seek(&ass->q, s, stream_index,
157  min_ts, ts, max_ts, flags);
158 }
159 
161  .name = "ass",
162  .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
163  .priv_data_size = sizeof(ASSContext),
168  .read_seek2 = ass_read_seek,
169 };
Definition: start.py:1
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:93
static int64_t get_line(AVBPrint *buf, AVIOContext *pb)
int64_t pos
byte position in stream, -1 if unknown
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.
AVInputFormat ff_ass_demuxer
static int ass_read_close(AVFormatContext *s)
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
Remove and destroy all the subtitles packets.
Definition: subtitles.c:134
AVPacket * ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, const uint8_t *event, int len, int merge)
Insert a new subtitle event.
Definition: subtitles.c:26
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:193
Format I/O context.
Definition: avformat.h:944
static int ass_read_header(AVFormatContext *s)
uint8_t
int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
Generic read_packet() callback for subtitles demuxers using this queue system.
Definition: subtitles.c:82
static AVPacket pkt
Definition: demuxing.c:56
end end
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
static int64_t duration
Definition: ffplay.c:294
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
Finalize buf into extradata and set its size appropriately.
static int ass_read_seek(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
#define AV_BPRINT_SIZE_UNLIMITED
Convenience macros for special values for av_bprint_init() size_max parameter.
Definition: bprint.h:89
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Init a print buffer.
Definition: bprint.c:68
FFDemuxSubtitlesQueue q
static int ass_read_packet(AVFormatContext *s, AVPacket *pkt)
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
Buffer to print data progressively.
Definition: bprint.h:75
static int read_probe(AVProbeData *pd)
void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
Set missing durations and sort subtitles by PTS, and then byte position.
Definition: subtitles.c:72
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Update current_sub_idx to emulate a seek.
Definition: subtitles.c:95
enum AVMediaType codec_type
enum AVCodecID codec_id
AVIOContext * pb
I/O context.
Definition: avformat.h:977
void * buf
Definition: avisynth_c.h:594
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
static int read_ts(const uint8_t *p, int64_t *start, int *duration)
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
struct ASSContext ASSContext
static int flags
Definition: cpu.c:23
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:185
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
Main libavformat public API header.
common internal api header.
static double c[64]
void * priv_data
Format private data.
Definition: avformat.h:964
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip please avoid problems through this extra level of scrutiny For cosmetics only commits you should e g by running git config global user name My Name git config global user email my email which is either set in your personal configuration file through git config core editor or set by one of the following environment VISUAL or EDITOR Log messages should be concise but descriptive Explain why you made a what you did will be obvious from the changes themselves most of the time Saying just bug fix or is bad Remember that people of varying skill levels look at and educate themselves while reading through your code Don t include filenames in log Git provides that information Possibly make the commit message have a descriptive first line
Definition: git-howto.txt:153
static int ass_probe(AVProbeData *p)
void INT64 start
Definition: avisynth_c.h:594
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:116