annotate ffmpeg/libavformat/srtdec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * SubRip subtitle demuxer
yading@11 3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 #include "avformat.h"
yading@11 23 #include "internal.h"
yading@11 24 #include "subtitles.h"
yading@11 25 #include "libavutil/bprint.h"
yading@11 26 #include "libavutil/intreadwrite.h"
yading@11 27
yading@11 28 typedef struct {
yading@11 29 FFDemuxSubtitlesQueue q;
yading@11 30 } SRTContext;
yading@11 31
yading@11 32 static int srt_probe(AVProbeData *p)
yading@11 33 {
yading@11 34 const unsigned char *ptr = p->buf;
yading@11 35 int i, v, num = 0;
yading@11 36
yading@11 37 if (AV_RB24(ptr) == 0xEFBBBF)
yading@11 38 ptr += 3; /* skip UTF-8 BOM */
yading@11 39
yading@11 40 for (i=0; i<2; i++) {
yading@11 41 if ((num == i || num + 1 == i)
yading@11 42 && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
yading@11 43 return AVPROBE_SCORE_MAX;
yading@11 44 num = atoi(ptr);
yading@11 45 ptr += strcspn(ptr, "\n") + 1;
yading@11 46 }
yading@11 47 return 0;
yading@11 48 }
yading@11 49
yading@11 50 static int64_t get_pts(const char **buf, int *duration,
yading@11 51 int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
yading@11 52 {
yading@11 53 int i;
yading@11 54
yading@11 55 for (i=0; i<2; i++) {
yading@11 56 int hh1, mm1, ss1, ms1;
yading@11 57 int hh2, mm2, ss2, ms2;
yading@11 58 if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
yading@11 59 "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
yading@11 60 &hh1, &mm1, &ss1, &ms1,
yading@11 61 &hh2, &mm2, &ss2, &ms2,
yading@11 62 x1, x2, y1, y2) >= 8) {
yading@11 63 int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
yading@11 64 int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
yading@11 65 *duration = end - start;
yading@11 66 *buf += strcspn(*buf, "\n") + 1;
yading@11 67 return start;
yading@11 68 }
yading@11 69 *buf += strcspn(*buf, "\n") + 1;
yading@11 70 }
yading@11 71 return AV_NOPTS_VALUE;
yading@11 72 }
yading@11 73
yading@11 74 static int srt_read_header(AVFormatContext *s)
yading@11 75 {
yading@11 76 SRTContext *srt = s->priv_data;
yading@11 77 AVBPrint buf;
yading@11 78 AVStream *st = avformat_new_stream(s, NULL);
yading@11 79 int res = 0;
yading@11 80
yading@11 81 if (!st)
yading@11 82 return AVERROR(ENOMEM);
yading@11 83 avpriv_set_pts_info(st, 64, 1, 1000);
yading@11 84 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
yading@11 85 st->codec->codec_id = AV_CODEC_ID_SUBRIP;
yading@11 86
yading@11 87 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@11 88
yading@11 89 while (!url_feof(s->pb)) {
yading@11 90 ff_subtitles_read_chunk(s->pb, &buf);
yading@11 91
yading@11 92 if (buf.len) {
yading@11 93 int64_t pos = avio_tell(s->pb);
yading@11 94 int64_t pts;
yading@11 95 int duration;
yading@11 96 const char *ptr = buf.str;
yading@11 97 int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
yading@11 98 AVPacket *sub;
yading@11 99
yading@11 100 pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
yading@11 101 if (pts != AV_NOPTS_VALUE) {
yading@11 102 int len = buf.len - (ptr - buf.str);
yading@11 103 if (len <= 0)
yading@11 104 continue;
yading@11 105 sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
yading@11 106 if (!sub) {
yading@11 107 res = AVERROR(ENOMEM);
yading@11 108 goto end;
yading@11 109 }
yading@11 110 sub->pos = pos;
yading@11 111 sub->pts = pts;
yading@11 112 sub->duration = duration;
yading@11 113 if (x1 != -1) {
yading@11 114 uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16);
yading@11 115 if (p) {
yading@11 116 AV_WL32(p, x1);
yading@11 117 AV_WL32(p + 4, y1);
yading@11 118 AV_WL32(p + 8, x2);
yading@11 119 AV_WL32(p + 12, y2);
yading@11 120 }
yading@11 121 }
yading@11 122 }
yading@11 123 }
yading@11 124 }
yading@11 125
yading@11 126 ff_subtitles_queue_finalize(&srt->q);
yading@11 127
yading@11 128 end:
yading@11 129 av_bprint_finalize(&buf, NULL);
yading@11 130 return res;
yading@11 131 }
yading@11 132
yading@11 133 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
yading@11 134 {
yading@11 135 SRTContext *srt = s->priv_data;
yading@11 136 return ff_subtitles_queue_read_packet(&srt->q, pkt);
yading@11 137 }
yading@11 138
yading@11 139 static int srt_read_seek(AVFormatContext *s, int stream_index,
yading@11 140 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
yading@11 141 {
yading@11 142 SRTContext *srt = s->priv_data;
yading@11 143 return ff_subtitles_queue_seek(&srt->q, s, stream_index,
yading@11 144 min_ts, ts, max_ts, flags);
yading@11 145 }
yading@11 146
yading@11 147 static int srt_read_close(AVFormatContext *s)
yading@11 148 {
yading@11 149 SRTContext *srt = s->priv_data;
yading@11 150 ff_subtitles_queue_clean(&srt->q);
yading@11 151 return 0;
yading@11 152 }
yading@11 153
yading@11 154 AVInputFormat ff_srt_demuxer = {
yading@11 155 .name = "srt",
yading@11 156 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
yading@11 157 .priv_data_size = sizeof(SRTContext),
yading@11 158 .read_probe = srt_probe,
yading@11 159 .read_header = srt_read_header,
yading@11 160 .read_packet = srt_read_packet,
yading@11 161 .read_seek2 = srt_read_seek,
yading@11 162 .read_close = srt_read_close,
yading@11 163 };