Libav
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 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 "avformat.h"
23 #include "internal.h"
24 #include "libavutil/intreadwrite.h"
25 
26 static int srt_probe(AVProbeData *p)
27 {
28  unsigned char *ptr = p->buf;
29  int i, v, num = 0;
30 
31  if (AV_RB24(ptr) == 0xEFBBBF)
32  ptr += 3; /* skip UTF-8 BOM */
33 
34  for (i=0; i<2; i++) {
35  if (num == i && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
36  return AVPROBE_SCORE_MAX;
37  num = atoi(ptr);
38  ptr += strcspn(ptr, "\n") + 1;
39  }
40  return 0;
41 }
42 
44 {
46  if (!st)
47  return -1;
48  avpriv_set_pts_info(st, 64, 1, 1000);
51  return 0;
52 }
53 
54 static int64_t get_pts(const char *buf)
55 {
56  int i, v, hour, min, sec, hsec;
57 
58  for (i=0; i<2; i++) {
59  if (sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %*d:%*2d:%*2d%*1[,.]%3d",
60  &hour, &min, &sec, &hsec, &v) == 5) {
61  min += 60*hour;
62  sec += 60*min;
63  return sec*1000+hsec;
64  }
65  buf += strcspn(buf, "\n") + 1;
66  }
67  return AV_NOPTS_VALUE;
68 }
69 
70 static inline int is_eol(char c)
71 {
72  return c == '\r' || c == '\n';
73 }
74 
76 {
77  char buffer[2048], *ptr = buffer, *ptr2;
78  int64_t pos = avio_tell(s->pb);
79  int res = AVERROR_EOF;
80 
81  do {
82  ptr2 = ptr;
83  ptr += ff_get_line(s->pb, ptr, sizeof(buffer)+buffer-ptr);
84  } while (!is_eol(*ptr2) && !s->pb->eof_reached && ptr-buffer<sizeof(buffer)-1);
85 
86  if (buffer[0] && !(res = av_new_packet(pkt, ptr-buffer))) {
87  memcpy(pkt->data, buffer, pkt->size);
88  pkt->flags |= AV_PKT_FLAG_KEY;
89  pkt->pos = pos;
90  pkt->pts = pkt->dts = get_pts(pkt->data);
91  }
92  return res;
93 }
94 
96  .name = "srt",
97  .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
98  .read_probe = srt_probe,
99  .read_header = srt_read_header,
100  .read_packet = srt_read_packet,
101  .flags = AVFMT_GENERIC_INDEX,
102 };
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
static int is_eol(char c)
Definition: srtdec.c:70
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:2821
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int size
Definition: avcodec.h:974
static int64_t get_pts(const char *buf)
Definition: srtdec.c:54
Format I/O context.
Definition: avformat.h:922
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
#define AVERROR_EOF
End of file.
Definition: error.h:51
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2518
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
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
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:417
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
static int srt_probe(AVProbeData *p)
Definition: srtdec.c:26
static char buffer[20]
Definition: seek-test.c:31
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:603
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
AVIOContext * pb
I/O context.
Definition: avformat.h:964
uint8_t * data
Definition: avcodec.h:973
#define AV_RB24(x)
Definition: intreadwrite.h:406
AVInputFormat ff_srt_demuxer
Definition: srtdec.c:95
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
Main libavformat public API header.
static int srt_read_header(AVFormatContext *s)
Definition: srtdec.c:43
static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: srtdec.c:75
int eof_reached
true if eof reached
Definition: avio.h:96
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
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