FFmpeg  2.1.1
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  while (*ptr == '\r' || *ptr == '\n')
41  ptr++;
42  for (i=0; i<2; i++) {
43  if ((num == i || num + 1 == i)
44  && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
45  return AVPROBE_SCORE_MAX;
46  num = atoi(ptr);
47  ptr += ff_subtitles_next_line(ptr);
48  }
49  return 0;
50 }
51 
52 static int64_t get_pts(const char **buf, int *duration,
53  int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
54 {
55  int i;
56 
57  for (i=0; i<2; i++) {
58  int hh1, mm1, ss1, ms1;
59  int hh2, mm2, ss2, ms2;
60  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
61  "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
62  &hh1, &mm1, &ss1, &ms1,
63  &hh2, &mm2, &ss2, &ms2,
64  x1, x2, y1, y2) >= 8) {
65  int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
66  int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
67  *duration = end - start;
68  *buf += ff_subtitles_next_line(*buf);
69  return start;
70  }
71  *buf += ff_subtitles_next_line(*buf);
72  }
73  return AV_NOPTS_VALUE;
74 }
75 
77 {
78  SRTContext *srt = s->priv_data;
79  AVBPrint buf;
80  AVStream *st = avformat_new_stream(s, NULL);
81  int res = 0;
82 
83  if (!st)
84  return AVERROR(ENOMEM);
85  avpriv_set_pts_info(st, 64, 1, 1000);
88 
90 
91  while (!url_feof(s->pb)) {
92  ff_subtitles_read_chunk(s->pb, &buf);
93 
94  if (buf.len) {
95  int64_t pos = avio_tell(s->pb);
96  int64_t pts;
97  int duration;
98  const char *ptr = buf.str;
99  int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
100  AVPacket *sub;
101 
102  pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
103  if (pts != AV_NOPTS_VALUE) {
104  int len = buf.len - (ptr - buf.str);
105  if (len <= 0)
106  continue;
107  sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
108  if (!sub) {
109  res = AVERROR(ENOMEM);
110  goto end;
111  }
112  sub->pos = pos;
113  sub->pts = pts;
114  sub->duration = duration;
115  if (x1 != -1) {
117  if (p) {
118  AV_WL32(p, x1);
119  AV_WL32(p + 4, y1);
120  AV_WL32(p + 8, x2);
121  AV_WL32(p + 12, y2);
122  }
123  }
124  }
125  }
126  }
127 
129 
130 end:
131  av_bprint_finalize(&buf, NULL);
132  return res;
133 }
134 
136 {
137  SRTContext *srt = s->priv_data;
138  return ff_subtitles_queue_read_packet(&srt->q, pkt);
139 }
140 
141 static int srt_read_seek(AVFormatContext *s, int stream_index,
142  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
143 {
144  SRTContext *srt = s->priv_data;
145  return ff_subtitles_queue_seek(&srt->q, s, stream_index,
146  min_ts, ts, max_ts, flags);
147 }
148 
150 {
151  SRTContext *srt = s->priv_data;
153  return 0;
154 }
155 
157  .name = "srt",
158  .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
159  .priv_data_size = sizeof(SRTContext),
163  .read_seek2 = srt_read_seek,
165 };
float v
const char * s
Definition: avisynth_c.h:668
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1092
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:3922
Subtitle event position.
Definition: avcodec.h:996
void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
Read a subtitles chunk.
Definition: subtitles.c:243
#define AV_WL32(p, darg)
Definition: intreadwrite.h:282
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
Remove and destroy all the subtitles packets.
Definition: subtitles.c:185
FFDemuxSubtitlesQueue q
Definition: srtdec.c:29
static int srt_read_close(AVFormatContext *s)
Definition: srtdec.c:149
AVPacket * ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, const uint8_t *event, int len, int merge)
Insert a new subtitle event.
Definition: subtitles.c:26
Format I/O context.
Definition: avformat.h:968
static uint8_t * res
Definition: ffhash.c:43
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:96
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
static int64_t duration
Definition: ffplay.c:306
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
int duration
Duration of this packet in AVStream-&gt;time_base units, 0 if unknown.
Definition: avcodec.h:1085
void * priv_data
Format private data.
Definition: avformat.h:988
#define AV_BPRINT_SIZE_UNLIMITED
Convenience macros for special values for av_bprint_init() size_max parameter.
Definition: bprint.h:91
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:298
static int srt_probe(AVProbeData *p)
Definition: srtdec.c:32
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Init a print buffer.
Definition: bprint.c:69
Buffer to print data progressively.
Definition: bprint.h:77
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:54
int32_t
void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
Set missing durations and sort subtitles by PTS, and then byte position.
Definition: subtitles.c:84
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
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:133
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
static int64_t get_pts(const char **buf, int *duration, int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
Definition: srtdec.c:52
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
void * buf
Definition: avisynth_c.h:594
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
#define AV_RB24(x)
Definition: intreadwrite.h:436
AVInputFormat ff_srt_demuxer
Definition: srtdec.c:156
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
static av_always_inline int ff_subtitles_next_line(const char *ptr)
Get the number of characters to increment to jump to the next line, or to the end of the string...
Definition: subtitles.h:111
static int flags
Definition: cpu.c:45
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:342
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
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)
Definition: srtdec.c:141
static int srt_read_header(AVFormatContext *s)
Definition: srtdec.c:76
static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: srtdec.c:135
int len
#define AVERROR(e)
void INT64 start
Definition: avisynth_c.h:594
static AVPacket pkt
Definition: demuxing.c:52
This structure stores compressed data.
Definition: avcodec.h:1040
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avcodec.h:2278