blob: 87ce2f25eee673c78029f35cf2e0f05d5e4c773a [file] [log] [blame]
Michael Niedermayerde3ae182008-09-26 02:19:211/*
2 * SSA/ASS demuxer
3 * Copyright (c) 2008 Michael Niedermayer
Clément Bœsch3a6fa382014-09-20 17:31:524 * Copyright (c) 2014 Clément Bœsch
Michael Niedermayerde3ae182008-09-26 02:19:215 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
Diego Biurrun8f8bc922013-11-23 20:32:5523#include <stdint.h>
24
Michael Niedermayerde3ae182008-09-26 02:19:2125#include "avformat.h"
Aurelien Jacobs7c892952010-07-21 21:39:0126#include "internal.h"
Clément Bœsche0260e22012-11-23 21:45:0527#include "subtitles.h"
Clément Bœsch36e61e22012-12-29 21:09:5928#include "libavcodec/internal.h"
Clément Bœsche0260e22012-11-23 21:45:0529#include "libavutil/bprint.h"
Michael Niedermayerde3ae182008-09-26 02:19:2130
Patrice Clement5c9c3052014-02-28 17:31:2931typedef struct ASSContext {
Clément Bœsche0260e22012-11-23 21:45:0532 FFDemuxSubtitlesQueue q;
Clément Bœschff5f5cb2014-09-14 07:35:1033 unsigned readorder;
Patrice Clement5c9c3052014-02-28 17:31:2934} ASSContext;
Michael Niedermayerde3ae182008-09-26 02:19:2135
Clément Bœsch069c8972012-11-23 21:47:0936static int ass_probe(AVProbeData *p)
Michael Niedermayerde3ae182008-09-26 02:19:2137{
wm43e842612014-09-02 18:48:4538 char buf[13];
39 FFTextReader tr;
40 ff_text_init_buf(&tr, p->buf, p->buf_size);
Michael Niedermayerde3ae182008-09-26 02:19:2141
wm43e842612014-09-02 18:48:4542 ff_text_read(&tr, buf, sizeof(buf));
43
44 if (!memcmp(buf, "[Script Info]", 13))
Michael Niedermayerde3ae182008-09-26 02:19:2145 return AVPROBE_SCORE_MAX;
46
47 return 0;
48}
49
Clément Bœsch069c8972012-11-23 21:47:0950static int ass_read_close(AVFormatContext *s)
Michael Niedermayerde3ae182008-09-26 02:19:2151{
52 ASSContext *ass = s->priv_data;
Clément Bœsche0260e22012-11-23 21:45:0553 ff_subtitles_queue_clean(&ass->q);
Michael Niedermayerde3ae182008-09-26 02:19:2154 return 0;
55}
56
Clément Bœschff5f5cb2014-09-14 07:35:1057static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p,
58 int64_t *start, int *duration)
Michael Niedermayerde3ae182008-09-26 02:19:2159{
Clément Bœsch138902d2014-10-04 10:22:3760 int pos = 0;
Clément Bœsche0260e22012-11-23 21:45:0561 int64_t end;
62 int hh1, mm1, ss1, ms1;
63 int hh2, mm2, ss2, ms2;
Michael Niedermayerde3ae182008-09-26 02:19:2164
Clément Bœschff5f5cb2014-09-14 07:35:1065 if (sscanf(p, "Dialogue: %*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d,%n",
Clément Bœsche0260e22012-11-23 21:45:0566 &hh1, &mm1, &ss1, &ms1,
Clément Bœsch138902d2014-10-04 10:22:3767 &hh2, &mm2, &ss2, &ms2, &pos) >= 8 && pos > 0) {
Clément Bœschff5f5cb2014-09-14 07:35:1068
69 /* This is not part of the sscanf itself in order to handle an actual
70 * number (which would be the Layer) or the form "Marked=N" (which is
71 * the old SSA field, now replaced by Layer, and will be lead to Layer
72 * being 0 here). */
73 const int layer = atoi(p + 10);
74
Clément Bœsche0260e22012-11-23 21:45:0575 end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
76 *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
77 *duration = end - *start;
Clément Bœschff5f5cb2014-09-14 07:35:1078
79 av_bprint_clear(dst);
80 av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos);
81
82 /* right strip the buffer */
83 while (dst->len > 0 &&
84 dst->str[dst->len - 1] == '\r' ||
85 dst->str[dst->len - 1] == '\n')
86 dst->str[--dst->len] = 0;
Clément Bœsche0260e22012-11-23 21:45:0587 return 0;
88 }
89 return -1;
Michael Niedermayerde3ae182008-09-26 02:19:2190}
91
wm43e842612014-09-02 18:48:4592static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
Michael Niedermayerde3ae182008-09-26 02:19:2193{
wm43e842612014-09-02 18:48:4594 int64_t pos = ff_text_pos(tr);
Clément Bœsche0260e22012-11-23 21:45:0595
96 av_bprint_clear(buf);
97 for (;;) {
wm43e842612014-09-02 18:48:4598 char c = ff_text_r8(tr);
Clément Bœsche0260e22012-11-23 21:45:0599 if (!c)
100 break;
101 av_bprint_chars(buf, c, 1);
102 if (c == '\n')
103 break;
104 }
105 return pos;
Michael Niedermayerde3ae182008-09-26 02:19:21106}
107
Clément Bœsch069c8972012-11-23 21:47:09108static int ass_read_header(AVFormatContext *s)
Michael Niedermayerde3ae182008-09-26 02:19:21109{
Michael Niedermayerde3ae182008-09-26 02:19:21110 ASSContext *ass = s->priv_data;
Clément Bœschff5f5cb2014-09-14 07:35:10111 AVBPrint header, line, rline;
Clément Bœsch9ec52e52014-09-20 17:15:31112 int res = 0;
Michael Niedermayerde3ae182008-09-26 02:19:21113 AVStream *st;
wm43e842612014-09-02 18:48:45114 FFTextReader tr;
115 ff_text_init_avio(&tr, s->pb);
Michael Niedermayerde3ae182008-09-26 02:19:21116
Anton Khirnov3b3bbdd2011-06-18 09:43:24117 st = avformat_new_stream(s, NULL);
Michael Niedermayerde3ae182008-09-26 02:19:21118 if (!st)
Clément Bœsch6d2892c2012-11-23 21:47:51119 return AVERROR(ENOMEM);
Anton Khirnovc3f9ebf2011-11-29 18:28:15120 avpriv_set_pts_info(st, 64, 1, 100);
Stefano Sabatini72415b22010-03-30 23:30:55121 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
Clément Bœschff5f5cb2014-09-14 07:35:10122 st->codec->codec_id = AV_CODEC_ID_ASS;
Michael Niedermayerde3ae182008-09-26 02:19:21123
Clément Bœsche0260e22012-11-23 21:45:05124 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
125 av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
Clément Bœschff5f5cb2014-09-14 07:35:10126 av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED);
Michael Niedermayerde3ae182008-09-26 02:19:21127
Clément Bœsche0260e22012-11-23 21:45:05128 for (;;) {
wm43e842612014-09-02 18:48:45129 int64_t pos = get_line(&line, &tr);
Clément Bœsch0aa5c5a2014-09-20 17:31:29130 int64_t ts_start = AV_NOPTS_VALUE;
131 int duration = -1;
132 AVPacket *sub;
Michael Niedermayerde3ae182008-09-26 02:19:21133
Clément Bœsch9ec52e52014-09-20 17:15:31134 if (!line.str[0]) // EOF
135 break;
136
Clément Bœsch0aa5c5a2014-09-20 17:31:29137 if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) {
138 av_bprintf(&header, "%s", line.str);
139 continue;
140 }
141 sub = ff_subtitles_queue_insert(&ass->q, rline.str, rline.len, 0);
142 if (!sub) {
143 res = AVERROR(ENOMEM);
144 goto end;
145 }
146 sub->pos = pos;
147 sub->pts = ts_start;
148 sub->duration = duration;
Michael Niedermayerde3ae182008-09-26 02:19:21149 }
150
Clément Bœsch67286fa2012-12-30 21:54:53151 res = avpriv_bprint_to_extradata(st->codec, &header);
Clément Bœsch36e61e22012-12-29 21:09:59152 if (res < 0)
Clément Bœsche0260e22012-11-23 21:45:05153 goto end;
Michael Niedermayerde3ae182008-09-26 02:19:21154
Clément Bœsche0260e22012-11-23 21:45:05155 ff_subtitles_queue_finalize(&ass->q);
Michael Niedermayerde3ae182008-09-26 02:19:21156
Clément Bœsche0260e22012-11-23 21:45:05157end:
Clément Bœsch71f62752014-09-27 15:49:49158 av_bprint_finalize(&header, NULL);
159 av_bprint_finalize(&line, NULL);
160 av_bprint_finalize(&rline, NULL);
Clément Bœsche0260e22012-11-23 21:45:05161 return res;
Michael Niedermayerde3ae182008-09-26 02:19:21162}
163
Clément Bœsch069c8972012-11-23 21:47:09164static int ass_read_packet(AVFormatContext *s, AVPacket *pkt)
Michael Niedermayerde3ae182008-09-26 02:19:21165{
166 ASSContext *ass = s->priv_data;
Clément Bœsche0260e22012-11-23 21:45:05167 return ff_subtitles_queue_read_packet(&ass->q, pkt);
Michael Niedermayerde3ae182008-09-26 02:19:21168}
169
Clément Bœsch069c8972012-11-23 21:47:09170static int ass_read_seek(AVFormatContext *s, int stream_index,
171 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Aurelien Jacobsac066b82010-07-29 22:10:22172{
173 ASSContext *ass = s->priv_data;
Clément Bœsche0260e22012-11-23 21:45:05174 return ff_subtitles_queue_seek(&ass->q, s, stream_index,
175 min_ts, ts, max_ts, flags);
Aurelien Jacobsac066b82010-07-29 22:10:22176}
177
Diego Elio Pettenò66355be2011-01-25 22:03:28178AVInputFormat ff_ass_demuxer = {
Aurelien Jacobs87d69d32010-12-04 00:37:53179 .name = "ass",
Diego Biurrun0177b7d2012-07-24 01:23:48180 .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
Aurelien Jacobs87d69d32010-12-04 00:37:53181 .priv_data_size = sizeof(ASSContext),
Clément Bœsch069c8972012-11-23 21:47:09182 .read_probe = ass_probe,
183 .read_header = ass_read_header,
184 .read_packet = ass_read_packet,
185 .read_close = ass_read_close,
186 .read_seek2 = ass_read_seek,
Michael Niedermayerde3ae182008-09-26 02:19:21187};