annotate ffmpeg/libavformat/assdec.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 * SSA/ASS demuxer
yading@11 3 * Copyright (c) 2008 Michael Niedermayer
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 "libavcodec/internal.h"
yading@11 26 #include "libavutil/bprint.h"
yading@11 27
yading@11 28 typedef struct ASSContext{
yading@11 29 FFDemuxSubtitlesQueue q;
yading@11 30 }ASSContext;
yading@11 31
yading@11 32 static int ass_probe(AVProbeData *p)
yading@11 33 {
yading@11 34 const char *header= "[Script Info]";
yading@11 35
yading@11 36 if( !memcmp(p->buf , header, strlen(header))
yading@11 37 || !memcmp(p->buf+3, header, strlen(header)))
yading@11 38 return AVPROBE_SCORE_MAX;
yading@11 39
yading@11 40 return 0;
yading@11 41 }
yading@11 42
yading@11 43 static int ass_read_close(AVFormatContext *s)
yading@11 44 {
yading@11 45 ASSContext *ass = s->priv_data;
yading@11 46 ff_subtitles_queue_clean(&ass->q);
yading@11 47 return 0;
yading@11 48 }
yading@11 49
yading@11 50 static int read_ts(const uint8_t *p, int64_t *start, int *duration)
yading@11 51 {
yading@11 52 int64_t end;
yading@11 53 int hh1, mm1, ss1, ms1;
yading@11 54 int hh2, mm2, ss2, ms2;
yading@11 55
yading@11 56 if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
yading@11 57 &hh1, &mm1, &ss1, &ms1,
yading@11 58 &hh2, &mm2, &ss2, &ms2) == 8) {
yading@11 59 end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
yading@11 60 *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
yading@11 61 *duration = end - *start;
yading@11 62 return 0;
yading@11 63 }
yading@11 64 return -1;
yading@11 65 }
yading@11 66
yading@11 67 static int64_t get_line(AVBPrint *buf, AVIOContext *pb)
yading@11 68 {
yading@11 69 int64_t pos = avio_tell(pb);
yading@11 70
yading@11 71 av_bprint_clear(buf);
yading@11 72 for (;;) {
yading@11 73 char c = avio_r8(pb);
yading@11 74 if (!c)
yading@11 75 break;
yading@11 76 av_bprint_chars(buf, c, 1);
yading@11 77 if (c == '\n')
yading@11 78 break;
yading@11 79 }
yading@11 80 return pos;
yading@11 81 }
yading@11 82
yading@11 83 static int ass_read_header(AVFormatContext *s)
yading@11 84 {
yading@11 85 ASSContext *ass = s->priv_data;
yading@11 86 AVBPrint header, line;
yading@11 87 int header_remaining, res = 0;
yading@11 88 AVStream *st;
yading@11 89
yading@11 90 st = avformat_new_stream(s, NULL);
yading@11 91 if (!st)
yading@11 92 return AVERROR(ENOMEM);
yading@11 93 avpriv_set_pts_info(st, 64, 1, 100);
yading@11 94 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
yading@11 95 st->codec->codec_id= AV_CODEC_ID_SSA;
yading@11 96
yading@11 97 header_remaining= INT_MAX;
yading@11 98
yading@11 99 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@11 100 av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@11 101
yading@11 102 for (;;) {
yading@11 103 int64_t pos = get_line(&line, s->pb);
yading@11 104
yading@11 105 if (!line.str[0]) // EOF
yading@11 106 break;
yading@11 107
yading@11 108 if (!memcmp(line.str, "[Events]", 8))
yading@11 109 header_remaining= 2;
yading@11 110 else if (line.str[0]=='[')
yading@11 111 header_remaining= INT_MAX;
yading@11 112
yading@11 113 if (header_remaining) {
yading@11 114 av_bprintf(&header, "%s", line.str);
yading@11 115 header_remaining--;
yading@11 116 } else {
yading@11 117 int64_t ts_start = AV_NOPTS_VALUE;
yading@11 118 int duration = -1;
yading@11 119 AVPacket *sub;
yading@11 120
yading@11 121 if (read_ts(line.str, &ts_start, &duration) < 0)
yading@11 122 continue;
yading@11 123 sub = ff_subtitles_queue_insert(&ass->q, line.str, line.len, 0);
yading@11 124 if (!sub) {
yading@11 125 res = AVERROR(ENOMEM);
yading@11 126 goto end;
yading@11 127 }
yading@11 128 sub->pos = pos;
yading@11 129 sub->pts = ts_start;
yading@11 130 sub->duration = duration;
yading@11 131 }
yading@11 132 }
yading@11 133
yading@11 134 av_bprint_finalize(&line, NULL);
yading@11 135
yading@11 136 res = avpriv_bprint_to_extradata(st->codec, &header);
yading@11 137 if (res < 0)
yading@11 138 goto end;
yading@11 139
yading@11 140 ff_subtitles_queue_finalize(&ass->q);
yading@11 141
yading@11 142 end:
yading@11 143 return res;
yading@11 144 }
yading@11 145
yading@11 146 static int ass_read_packet(AVFormatContext *s, AVPacket *pkt)
yading@11 147 {
yading@11 148 ASSContext *ass = s->priv_data;
yading@11 149 return ff_subtitles_queue_read_packet(&ass->q, pkt);
yading@11 150 }
yading@11 151
yading@11 152 static int ass_read_seek(AVFormatContext *s, int stream_index,
yading@11 153 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
yading@11 154 {
yading@11 155 ASSContext *ass = s->priv_data;
yading@11 156 return ff_subtitles_queue_seek(&ass->q, s, stream_index,
yading@11 157 min_ts, ts, max_ts, flags);
yading@11 158 }
yading@11 159
yading@11 160 AVInputFormat ff_ass_demuxer = {
yading@11 161 .name = "ass",
yading@11 162 .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
yading@11 163 .priv_data_size = sizeof(ASSContext),
yading@11 164 .read_probe = ass_probe,
yading@11 165 .read_header = ass_read_header,
yading@11 166 .read_packet = ass_read_packet,
yading@11 167 .read_close = ass_read_close,
yading@11 168 .read_seek2 = ass_read_seek,
yading@11 169 };