libavformat/srtdec.c
Go to the documentation of this file.
1 /*
2  * SubRip subtitle demuxer
3  * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
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 "libavutil/bprint.h"
26 #include "libavutil/intreadwrite.h"
27 
28 typedef struct {
30 } SRTContext;
31 
32 static int srt_probe(AVProbeData *p)
33 {
34  const unsigned char *ptr = p->buf;
35  int i, v, num = 0;
36 
37  if (AV_RB24(ptr) == 0xEFBBBF)
38  ptr += 3; /* skip UTF-8 BOM */
39 
40  for (i=0; i<2; i++) {
41  if ((num == i || num + 1 == i)
42  && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
43  return AVPROBE_SCORE_MAX;
44  num = atoi(ptr);
45  ptr += strcspn(ptr, "\n") + 1;
46  }
47  return 0;
48 }
49 
50 static int64_t get_pts(const char **buf, int *duration,
52 {
53  int i;
54 
55  for (i=0; i<2; i++) {
56  int hh1, mm1, ss1, ms1;
57  int hh2, mm2, ss2, ms2;
58  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
59  "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
60  &hh1, &mm1, &ss1, &ms1,
61  &hh2, &mm2, &ss2, &ms2,
62  x1, x2, y1, y2) >= 8) {
63  int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
64  int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
65  *duration = end - start;
66  *buf += strcspn(*buf, "\n") + 1;
67  return start;
68  }
69  *buf += strcspn(*buf, "\n") + 1;
70  }
71  return AV_NOPTS_VALUE;
72 }
73 
75 {
76  SRTContext *srt = s->priv_data;
77  AVBPrint buf;
79  int res = 0;
80 
81  if (!st)
82  return AVERROR(ENOMEM);
83  avpriv_set_pts_info(st, 64, 1, 1000);
86 
88 
89  while (!url_feof(s->pb)) {
90  ff_subtitles_read_chunk(s->pb, &buf);
91 
92  if (buf.len) {
93  int64_t pos = avio_tell(s->pb);
94  int64_t pts;
95  int duration;
96  const char *ptr = buf.str;
97  int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
98  AVPacket *sub;
99 
100  pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
101  if (pts != AV_NOPTS_VALUE) {
102  int len = buf.len - (ptr - buf.str);
103  if (len <= 0)
104  continue;
105  sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
106  if (!sub) {
107  res = AVERROR(ENOMEM);
108  goto end;
109  }
110  sub->pos = pos;
111  sub->pts = pts;
112  sub->duration = duration;
113  if (x1 != -1) {
115  if (p) {
116  AV_WL32(p, x1);
117  AV_WL32(p + 4, y1);
118  AV_WL32(p + 8, x2);
119  AV_WL32(p + 12, y2);
120  }
121  }
122  }
123  }
124  }
125 
127 
128 end:
129  av_bprint_finalize(&buf, NULL);
130  return res;
131 }
132 
134 {
135  SRTContext *srt = s->priv_data;
136  return ff_subtitles_queue_read_packet(&srt->q, pkt);
137 }
138 
139 static int srt_read_seek(AVFormatContext *s, int stream_index,
140  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
141 {
142  SRTContext *srt = s->priv_data;
143  return ff_subtitles_queue_seek(&srt->q, s, stream_index,
144  min_ts, ts, max_ts, flags);
145 }
146 
148 {
149  SRTContext *srt = s->priv_data;
151  return 0;
152 }
153 
155  .name = "srt",
156  .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
157  .priv_data_size = sizeof(SRTContext),
161  .read_seek2 = srt_read_seek,
163 };
Definition: start.py:1
float v
const char * s
Definition: avisynth_c.h:668
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.
Subtitle event position.
void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
Read a subtitles chunk.
Definition: subtitles.c:192
y1
Definition: lab5.m:33
#define AV_RB24
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
Remove and destroy all the subtitles packets.
Definition: subtitles.c:134
FFDemuxSubtitlesQueue q
x1
Definition: genspecsines3.m:7
static int srt_read_close(AVFormatContext *s)
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
struct SRTContext SRTContext
Format I/O context.
Definition: avformat.h:944
#define AV_WL32(p, darg)
Definition: intreadwrite.h:282
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.
#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
static int srt_probe(AVProbeData *p)
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)
int32_t
void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
Set missing durations and sort subtitles by PTS, and then byte position.
Definition: subtitles.c:72
y2
Definition: lab5.m:34
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
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
static int64_t get_pts(const char **buf, int *duration, int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
void * buf
Definition: avisynth_c.h:594
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
x2
Definition: genspecsines3.m:8
synthesis window for stochastic i
AVInputFormat ff_srt_demuxer
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
static int flags
Definition: cpu.c:23
#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.
static int srt_read_seek(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
static int srt_read_header(AVFormatContext *s)
static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
int len
void * priv_data
Format private data.
Definition: avformat.h:964
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
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:264
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