FFmpeg  2.8.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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  int v;
35  char buf[64], *pbuf;
36  FFTextReader tr;
37 
38  ff_text_init_buf(&tr, p->buf, p->buf_size);
39 
40  while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
41  ff_text_r8(&tr);
42 
43  /* Check if the first non-empty line is a number. We do not check what the
44  * number is because in practice it can be anything.
45  * Also, that number can be followed by random garbage, so we can not
46  * unfortunately check that we only have a number. */
47  if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0 ||
48  strtol(buf, &pbuf, 10) < 0 || pbuf == buf)
49  return 0;
50 
51  /* Check if the next line matches a SRT timestamp */
52  if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
53  return 0;
54  if (buf[0] >= '0' && buf[0] <= '9' && strstr(buf, " --> ")
55  && sscanf(buf, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
56  return AVPROBE_SCORE_MAX;
57 
58  return 0;
59 }
60 
61 static int64_t get_pts(const char **buf, int *duration,
62  int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
63 {
64  int i;
65 
66  for (i=0; i<2; i++) {
67  int hh1, mm1, ss1, ms1;
68  int hh2, mm2, ss2, ms2;
69  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
70  "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
71  &hh1, &mm1, &ss1, &ms1,
72  &hh2, &mm2, &ss2, &ms2,
73  x1, x2, y1, y2) >= 8) {
74  int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
75  int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
76  *duration = end - start;
77  *buf += ff_subtitles_next_line(*buf);
78  return start;
79  }
80  *buf += ff_subtitles_next_line(*buf);
81  }
82  return AV_NOPTS_VALUE;
83 }
84 
86 {
87  SRTContext *srt = s->priv_data;
88  AVBPrint buf;
90  int res = 0;
91  FFTextReader tr;
92  ff_text_init_avio(s, &tr, s->pb);
93 
94  if (!st)
95  return AVERROR(ENOMEM);
96  avpriv_set_pts_info(st, 64, 1, 1000);
99 
101 
102  while (!ff_text_eof(&tr)) {
103  ff_subtitles_read_text_chunk(&tr, &buf);
104 
105  if (buf.len) {
106  int64_t pos = ff_text_pos(&tr);
107  int64_t pts;
108  int duration;
109  const char *ptr = buf.str;
110  int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
111  AVPacket *sub;
112 
113  pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
114  if (pts != AV_NOPTS_VALUE) {
115  int len = buf.len - (ptr - buf.str);
116  if (len <= 0)
117  continue;
118  sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
119  if (!sub) {
120  res = AVERROR(ENOMEM);
121  goto end;
122  }
123  sub->pos = pos;
124  sub->pts = pts;
125  sub->duration = duration;
126  if (x1 != -1) {
128  if (p) {
129  AV_WL32(p, x1);
130  AV_WL32(p + 4, y1);
131  AV_WL32(p + 8, x2);
132  AV_WL32(p + 12, y2);
133  }
134  }
135  }
136  }
137  }
138 
140 
141 end:
142  av_bprint_finalize(&buf, NULL);
143  return res;
144 }
145 
147 {
148  SRTContext *srt = s->priv_data;
149  return ff_subtitles_queue_read_packet(&srt->q, pkt);
150 }
151 
152 static int srt_read_seek(AVFormatContext *s, int stream_index,
153  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
154 {
155  SRTContext *srt = s->priv_data;
156  return ff_subtitles_queue_seek(&srt->q, s, stream_index,
157  min_ts, ts, max_ts, flags);
158 }
159 
161 {
162  SRTContext *srt = s->priv_data;
164  return 0;
165 }
166 
168  .name = "srt",
169  .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
170  .priv_data_size = sizeof(SRTContext),
174  .read_seek2 = srt_read_seek,
176 };
#define NULL
Definition: coverity.c:32
float v
const char * s
Definition: avisynth_c.h:631
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1458
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:4093
Subtitle event position.
Definition: avcodec.h:1341
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
Remove and destroy all the subtitles packets.
Definition: subtitles.c:270
FFDemuxSubtitlesQueue q
Definition: srtdec.c:29
int ff_text_peek_r8(FFTextReader *r)
Like ff_text_r8(), but don't remove the byte from the buffer.
Definition: subtitles.c:97
static int srt_read_close(AVFormatContext *s)
Definition: srtdec.c:160
static AVPacket pkt
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
Format I/O context.
Definition: avformat.h:1285
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:181
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3756
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
static int64_t duration
Definition: ffplay.c:326
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1451
#define AV_BPRINT_SIZE_UNLIMITED
int ff_text_eof(FFTextReader *r)
Return non-zero if EOF was reached.
Definition: subtitles.c:92
#define AVERROR(e)
Definition: error.h:43
ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
Read a line of text.
Definition: subtitles.c:379
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
Initialize the FFTextReader from the given AVIOContext.
Definition: subtitles.c:27
static int srt_probe(AVProbeData *p)
Definition: srtdec.c:32
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:873
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
int64_t ff_text_pos(FFTextReader *r)
Return the byte position of the next byte returned by ff_text_r8().
Definition: subtitles.c:60
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int32_t
void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
Set missing durations and sort subtitles by PTS, and then byte position.
Definition: subtitles.c:169
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:634
int ff_text_r8(FFTextReader *r)
Return the next byte.
Definition: subtitles.c:65
Stream structure.
Definition: avformat.h:854
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
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:218
enum AVMediaType codec_type
Definition: avcodec.h:1520
enum AVCodecID codec_id
Definition: avcodec.h:1529
AVIOContext * pb
I/O context.
Definition: avformat.h:1327
void ff_subtitles_read_text_chunk(FFTextReader *tr, AVBPrint *buf)
Read a subtitles chunk from FFTextReader.
Definition: subtitles.c:328
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:61
void * buf
Definition: avisynth_c.h:553
AVInputFormat ff_srt_demuxer
Definition: srtdec.c:167
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
static int64_t pts
Global timestamp for the audio frames.
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:185
static int flags
Definition: cpu.c:47
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
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:152
void ff_text_init_buf(FFTextReader *r, void *buf, size_t size)
Similar to ff_text_init_avio(), but sets it up to read from a bounded buffer.
Definition: subtitles.c:53
static int srt_read_header(AVFormatContext *s)
Definition: srtdec.c:85
static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: srtdec.c:146
int len
void * priv_data
Format private data.
Definition: avformat.h:1313
void INT64 start
Definition: avisynth_c.h:553
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:640
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:303
AVPacket * ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, const uint8_t *event, size_t len, int merge)
Insert a new subtitle event.
Definition: subtitles.c:111
This structure stores compressed data.
Definition: avcodec.h:1410
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
#define AV_WL32(p, v)
Definition: intreadwrite.h:426