yading@11: /* yading@11: * SSA/ASS demuxer yading@11: * Copyright (c) 2008 Michael Niedermayer yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "subtitles.h" yading@11: #include "libavcodec/internal.h" yading@11: #include "libavutil/bprint.h" yading@11: yading@11: typedef struct ASSContext{ yading@11: FFDemuxSubtitlesQueue q; yading@11: }ASSContext; yading@11: yading@11: static int ass_probe(AVProbeData *p) yading@11: { yading@11: const char *header= "[Script Info]"; yading@11: yading@11: if( !memcmp(p->buf , header, strlen(header)) yading@11: || !memcmp(p->buf+3, header, strlen(header))) yading@11: return AVPROBE_SCORE_MAX; yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int ass_read_close(AVFormatContext *s) yading@11: { yading@11: ASSContext *ass = s->priv_data; yading@11: ff_subtitles_queue_clean(&ass->q); yading@11: return 0; yading@11: } yading@11: yading@11: static int read_ts(const uint8_t *p, int64_t *start, int *duration) yading@11: { yading@11: int64_t end; yading@11: int hh1, mm1, ss1, ms1; yading@11: int hh2, mm2, ss2, ms2; yading@11: yading@11: if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d", yading@11: &hh1, &mm1, &ss1, &ms1, yading@11: &hh2, &mm2, &ss2, &ms2) == 8) { yading@11: end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2; yading@11: *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1; yading@11: *duration = end - *start; yading@11: return 0; yading@11: } yading@11: return -1; yading@11: } yading@11: yading@11: static int64_t get_line(AVBPrint *buf, AVIOContext *pb) yading@11: { yading@11: int64_t pos = avio_tell(pb); yading@11: yading@11: av_bprint_clear(buf); yading@11: for (;;) { yading@11: char c = avio_r8(pb); yading@11: if (!c) yading@11: break; yading@11: av_bprint_chars(buf, c, 1); yading@11: if (c == '\n') yading@11: break; yading@11: } yading@11: return pos; yading@11: } yading@11: yading@11: static int ass_read_header(AVFormatContext *s) yading@11: { yading@11: ASSContext *ass = s->priv_data; yading@11: AVBPrint header, line; yading@11: int header_remaining, res = 0; yading@11: AVStream *st; yading@11: yading@11: st = avformat_new_stream(s, NULL); yading@11: if (!st) yading@11: return AVERROR(ENOMEM); yading@11: avpriv_set_pts_info(st, 64, 1, 100); yading@11: st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; yading@11: st->codec->codec_id= AV_CODEC_ID_SSA; yading@11: yading@11: header_remaining= INT_MAX; yading@11: yading@11: av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED); yading@11: av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED); yading@11: yading@11: for (;;) { yading@11: int64_t pos = get_line(&line, s->pb); yading@11: yading@11: if (!line.str[0]) // EOF yading@11: break; yading@11: yading@11: if (!memcmp(line.str, "[Events]", 8)) yading@11: header_remaining= 2; yading@11: else if (line.str[0]=='[') yading@11: header_remaining= INT_MAX; yading@11: yading@11: if (header_remaining) { yading@11: av_bprintf(&header, "%s", line.str); yading@11: header_remaining--; yading@11: } else { yading@11: int64_t ts_start = AV_NOPTS_VALUE; yading@11: int duration = -1; yading@11: AVPacket *sub; yading@11: yading@11: if (read_ts(line.str, &ts_start, &duration) < 0) yading@11: continue; yading@11: sub = ff_subtitles_queue_insert(&ass->q, line.str, line.len, 0); yading@11: if (!sub) { yading@11: res = AVERROR(ENOMEM); yading@11: goto end; yading@11: } yading@11: sub->pos = pos; yading@11: sub->pts = ts_start; yading@11: sub->duration = duration; yading@11: } yading@11: } yading@11: yading@11: av_bprint_finalize(&line, NULL); yading@11: yading@11: res = avpriv_bprint_to_extradata(st->codec, &header); yading@11: if (res < 0) yading@11: goto end; yading@11: yading@11: ff_subtitles_queue_finalize(&ass->q); yading@11: yading@11: end: yading@11: return res; yading@11: } yading@11: yading@11: static int ass_read_packet(AVFormatContext *s, AVPacket *pkt) yading@11: { yading@11: ASSContext *ass = s->priv_data; yading@11: return ff_subtitles_queue_read_packet(&ass->q, pkt); yading@11: } yading@11: yading@11: static int ass_read_seek(AVFormatContext *s, int stream_index, yading@11: int64_t min_ts, int64_t ts, int64_t max_ts, int flags) yading@11: { yading@11: ASSContext *ass = s->priv_data; yading@11: return ff_subtitles_queue_seek(&ass->q, s, stream_index, yading@11: min_ts, ts, max_ts, flags); yading@11: } yading@11: yading@11: AVInputFormat ff_ass_demuxer = { yading@11: .name = "ass", yading@11: .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"), yading@11: .priv_data_size = sizeof(ASSContext), yading@11: .read_probe = ass_probe, yading@11: .read_header = ass_read_header, yading@11: .read_packet = ass_read_packet, yading@11: .read_close = ass_read_close, yading@11: .read_seek2 = ass_read_seek, yading@11: };