blob: 5f71fcb024d489de6af969ac8aa296a2a9e2a7c2 [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
Carl Eugen Hoyos4d8875e2019-03-21 00:18:3736static int ass_probe(const 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
Clément Bœsch6679fcd2015-11-20 13:38:2242 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
43 ff_text_r8(&tr);
44
wm43e842612014-09-02 18:48:4545 ff_text_read(&tr, buf, sizeof(buf));
46
47 if (!memcmp(buf, "[Script Info]", 13))
Michael Niedermayerde3ae182008-09-26 02:19:2148 return AVPROBE_SCORE_MAX;
49
50 return 0;
51}
52
Clément Bœsch069c8972012-11-23 21:47:0953static int ass_read_close(AVFormatContext *s)
Michael Niedermayerde3ae182008-09-26 02:19:2154{
55 ASSContext *ass = s->priv_data;
Clément Bœsche0260e22012-11-23 21:45:0556 ff_subtitles_queue_clean(&ass->q);
Michael Niedermayerde3ae182008-09-26 02:19:2157 return 0;
58}
59
Clément Bœschff5f5cb2014-09-14 07:35:1060static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p,
61 int64_t *start, int *duration)
Michael Niedermayerde3ae182008-09-26 02:19:2162{
Clément Bœsch138902d2014-10-04 10:22:3763 int pos = 0;
Clément Bœsche0260e22012-11-23 21:45:0564 int64_t end;
65 int hh1, mm1, ss1, ms1;
66 int hh2, mm2, ss2, ms2;
Michael Niedermayerde3ae182008-09-26 02:19:2167
Clément Bœschff5f5cb2014-09-14 07:35:1068 if (sscanf(p, "Dialogue: %*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d,%n",
Clément Bœsche0260e22012-11-23 21:45:0569 &hh1, &mm1, &ss1, &ms1,
Clément Bœsch138902d2014-10-04 10:22:3770 &hh2, &mm2, &ss2, &ms2, &pos) >= 8 && pos > 0) {
Clément Bœschff5f5cb2014-09-14 07:35:1071
72 /* This is not part of the sscanf itself in order to handle an actual
73 * number (which would be the Layer) or the form "Marked=N" (which is
Clément Bœschd281a872014-10-11 18:14:3774 * the old SSA field, now replaced by Layer, and will lead to Layer
Clément Bœschff5f5cb2014-09-14 07:35:1075 * being 0 here). */
76 const int layer = atoi(p + 10);
77
Clément Bœsche0260e22012-11-23 21:45:0578 end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
79 *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
80 *duration = end - *start;
Clément Bœschff5f5cb2014-09-14 07:35:1081
82 av_bprint_clear(dst);
83 av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos);
84
85 /* right strip the buffer */
86 while (dst->len > 0 &&
87 dst->str[dst->len - 1] == '\r' ||
88 dst->str[dst->len - 1] == '\n')
89 dst->str[--dst->len] = 0;
Clément Bœsche0260e22012-11-23 21:45:0590 return 0;
91 }
92 return -1;
Michael Niedermayerde3ae182008-09-26 02:19:2193}
94
wm43e842612014-09-02 18:48:4595static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
Michael Niedermayerde3ae182008-09-26 02:19:2196{
wm43e842612014-09-02 18:48:4597 int64_t pos = ff_text_pos(tr);
Clément Bœsche0260e22012-11-23 21:45:0598
99 av_bprint_clear(buf);
100 for (;;) {
wm43e842612014-09-02 18:48:45101 char c = ff_text_r8(tr);
Clément Bœsche0260e22012-11-23 21:45:05102 if (!c)
103 break;
104 av_bprint_chars(buf, c, 1);
105 if (c == '\n')
106 break;
107 }
108 return pos;
Michael Niedermayerde3ae182008-09-26 02:19:21109}
110
Clément Bœsch069c8972012-11-23 21:47:09111static int ass_read_header(AVFormatContext *s)
Michael Niedermayerde3ae182008-09-26 02:19:21112{
Michael Niedermayerde3ae182008-09-26 02:19:21113 ASSContext *ass = s->priv_data;
Clément Bœschff5f5cb2014-09-14 07:35:10114 AVBPrint header, line, rline;
Clément Bœsch9ec52e52014-09-20 17:15:31115 int res = 0;
Michael Niedermayerde3ae182008-09-26 02:19:21116 AVStream *st;
wm43e842612014-09-02 18:48:45117 FFTextReader tr;
Carl Eugen Hoyos19a64312014-10-29 00:32:44118 ff_text_init_avio(s, &tr, s->pb);
Michael Niedermayerde3ae182008-09-26 02:19:21119
Anton Khirnov3b3bbdd2011-06-18 09:43:24120 st = avformat_new_stream(s, NULL);
Michael Niedermayerde3ae182008-09-26 02:19:21121 if (!st)
Clément Bœsch6d2892c2012-11-23 21:47:51122 return AVERROR(ENOMEM);
Anton Khirnovc3f9ebf2011-11-29 18:28:15123 avpriv_set_pts_info(st, 64, 1, 100);
Derek Buitenhuis6f69f7a2016-04-10 19:58:15124 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
125 st->codecpar->codec_id = AV_CODEC_ID_ASS;
Michael Niedermayerde3ae182008-09-26 02:19:21126
Clément Bœsche0260e22012-11-23 21:45:05127 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
128 av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
Clément Bœschff5f5cb2014-09-14 07:35:10129 av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED);
Michael Niedermayerde3ae182008-09-26 02:19:21130
wm4265d2a72015-09-21 11:44:37131 ass->q.keep_duplicates = 1;
132
Clément Bœsche0260e22012-11-23 21:45:05133 for (;;) {
wm43e842612014-09-02 18:48:45134 int64_t pos = get_line(&line, &tr);
Clément Bœsch0aa5c5a2014-09-20 17:31:29135 int64_t ts_start = AV_NOPTS_VALUE;
136 int duration = -1;
137 AVPacket *sub;
Michael Niedermayerde3ae182008-09-26 02:19:21138
Clément Bœsch9ec52e52014-09-20 17:15:31139 if (!line.str[0]) // EOF
140 break;
141
Clément Bœsch0aa5c5a2014-09-20 17:31:29142 if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) {
143 av_bprintf(&header, "%s", line.str);
144 continue;
145 }
146 sub = ff_subtitles_queue_insert(&ass->q, rline.str, rline.len, 0);
147 if (!sub) {
148 res = AVERROR(ENOMEM);
149 goto end;
150 }
151 sub->pos = pos;
152 sub->pts = ts_start;
153 sub->duration = duration;
Michael Niedermayerde3ae182008-09-26 02:19:21154 }
155
Derek Buitenhuis6f69f7a2016-04-10 19:58:15156 res = ff_bprint_to_codecpar_extradata(st->codecpar, &header);
Clément Bœsch36e61e22012-12-29 21:09:59157 if (res < 0)
Clément Bœsche0260e22012-11-23 21:45:05158 goto end;
Michael Niedermayerde3ae182008-09-26 02:19:21159
Clément Bœschaf924fd2015-09-10 19:40:07160 ff_subtitles_queue_finalize(s, &ass->q);
Michael Niedermayerde3ae182008-09-26 02:19:21161
Clément Bœsche0260e22012-11-23 21:45:05162end:
Clément Bœsch71f62752014-09-27 15:49:49163 av_bprint_finalize(&header, NULL);
164 av_bprint_finalize(&line, NULL);
165 av_bprint_finalize(&rline, NULL);
Clément Bœsche0260e22012-11-23 21:45:05166 return res;
Michael Niedermayerde3ae182008-09-26 02:19:21167}
168
Clément Bœsch069c8972012-11-23 21:47:09169static int ass_read_packet(AVFormatContext *s, AVPacket *pkt)
Michael Niedermayerde3ae182008-09-26 02:19:21170{
171 ASSContext *ass = s->priv_data;
Clément Bœsche0260e22012-11-23 21:45:05172 return ff_subtitles_queue_read_packet(&ass->q, pkt);
Michael Niedermayerde3ae182008-09-26 02:19:21173}
174
Clément Bœsch069c8972012-11-23 21:47:09175static int ass_read_seek(AVFormatContext *s, int stream_index,
176 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Aurelien Jacobsac066b82010-07-29 22:10:22177{
178 ASSContext *ass = s->priv_data;
Clément Bœsche0260e22012-11-23 21:45:05179 return ff_subtitles_queue_seek(&ass->q, s, stream_index,
180 min_ts, ts, max_ts, flags);
Aurelien Jacobsac066b82010-07-29 22:10:22181}
182
Andreas Rheinhardtbc706842021-04-19 17:45:24183const AVInputFormat ff_ass_demuxer = {
Aurelien Jacobs87d69d32010-12-04 00:37:53184 .name = "ass",
Diego Biurrun0177b7d2012-07-24 01:23:48185 .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
Andreas Rheinhardtef7eaa42020-03-21 17:31:06186 .flags_internal = FF_FMT_INIT_CLEANUP,
Aurelien Jacobs87d69d32010-12-04 00:37:53187 .priv_data_size = sizeof(ASSContext),
Clément Bœsch069c8972012-11-23 21:47:09188 .read_probe = ass_probe,
189 .read_header = ass_read_header,
190 .read_packet = ass_read_packet,
191 .read_close = ass_read_close,
192 .read_seek2 = ass_read_seek,
Michael Niedermayerde3ae182008-09-26 02:19:21193};