blob: bf7b8a73a2465ff13a22f993715924d3d0678c3f [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œsche0260e22012-11-23 21:45:0528#include "libavutil/bprint.h"
Michael Niedermayerde3ae182008-09-26 02:19:2129
Patrice Clement5c9c3052014-02-28 17:31:2930typedef struct ASSContext {
Clément Bœsche0260e22012-11-23 21:45:0531 FFDemuxSubtitlesQueue q;
Clément Bœschff5f5cb2014-09-14 07:35:1032 unsigned readorder;
Patrice Clement5c9c3052014-02-28 17:31:2933} ASSContext;
Michael Niedermayerde3ae182008-09-26 02:19:2134
Carl Eugen Hoyos4d8875e2019-03-21 00:18:3735static int ass_probe(const AVProbeData *p)
Michael Niedermayerde3ae182008-09-26 02:19:2136{
wm43e842612014-09-02 18:48:4537 char buf[13];
38 FFTextReader tr;
39 ff_text_init_buf(&tr, p->buf, p->buf_size);
Michael Niedermayerde3ae182008-09-26 02:19:2140
Clément Bœsch6679fcd2015-11-20 13:38:2241 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
42 ff_text_r8(&tr);
43
wm43e842612014-09-02 18:48:4544 ff_text_read(&tr, buf, sizeof(buf));
45
46 if (!memcmp(buf, "[Script Info]", 13))
Michael Niedermayerde3ae182008-09-26 02:19:2147 return AVPROBE_SCORE_MAX;
48
49 return 0;
50}
51
Clément Bœschff5f5cb2014-09-14 07:35:1052static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p,
53 int64_t *start, int *duration)
Michael Niedermayerde3ae182008-09-26 02:19:2154{
Clément Bœsch138902d2014-10-04 10:22:3755 int pos = 0;
Clément Bœsche0260e22012-11-23 21:45:0556 int64_t end;
57 int hh1, mm1, ss1, ms1;
58 int hh2, mm2, ss2, ms2;
Michael Niedermayerde3ae182008-09-26 02:19:2159
Clément Bœschff5f5cb2014-09-14 07:35:1060 if (sscanf(p, "Dialogue: %*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d,%n",
Clément Bœsche0260e22012-11-23 21:45:0561 &hh1, &mm1, &ss1, &ms1,
Clément Bœsch138902d2014-10-04 10:22:3762 &hh2, &mm2, &ss2, &ms2, &pos) >= 8 && pos > 0) {
Clément Bœschff5f5cb2014-09-14 07:35:1063
64 /* This is not part of the sscanf itself in order to handle an actual
65 * number (which would be the Layer) or the form "Marked=N" (which is
Clément Bœschd281a872014-10-11 18:14:3766 * the old SSA field, now replaced by Layer, and will lead to Layer
Clément Bœschff5f5cb2014-09-14 07:35:1067 * being 0 here). */
68 const int layer = atoi(p + 10);
69
Clément Bœsche0260e22012-11-23 21:45:0570 end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
71 *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
72 *duration = end - *start;
Clément Bœschff5f5cb2014-09-14 07:35:1073
74 av_bprint_clear(dst);
75 av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos);
Reimar Döffingerc0f867b2023-06-21 18:06:0976 if (!av_bprint_is_complete(dst))
77 return AVERROR(ENOMEM);
Clément Bœschff5f5cb2014-09-14 07:35:1078
79 /* right strip the buffer */
80 while (dst->len > 0 &&
81 dst->str[dst->len - 1] == '\r' ||
82 dst->str[dst->len - 1] == '\n')
83 dst->str[--dst->len] = 0;
Clément Bœsche0260e22012-11-23 21:45:0584 return 0;
85 }
86 return -1;
Michael Niedermayerde3ae182008-09-26 02:19:2187}
88
wm43e842612014-09-02 18:48:4589static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
Michael Niedermayerde3ae182008-09-26 02:19:2190{
wm43e842612014-09-02 18:48:4591 int64_t pos = ff_text_pos(tr);
Clément Bœsche0260e22012-11-23 21:45:0592
93 av_bprint_clear(buf);
94 for (;;) {
wm43e842612014-09-02 18:48:4595 char c = ff_text_r8(tr);
Clément Bœsche0260e22012-11-23 21:45:0596 if (!c)
97 break;
98 av_bprint_chars(buf, c, 1);
99 if (c == '\n')
100 break;
101 }
102 return pos;
Michael Niedermayerde3ae182008-09-26 02:19:21103}
104
Clément Bœsch069c8972012-11-23 21:47:09105static int ass_read_header(AVFormatContext *s)
Michael Niedermayerde3ae182008-09-26 02:19:21106{
Michael Niedermayerde3ae182008-09-26 02:19:21107 ASSContext *ass = s->priv_data;
Clément Bœschff5f5cb2014-09-14 07:35:10108 AVBPrint header, line, rline;
Clément Bœsch9ec52e52014-09-20 17:15:31109 int res = 0;
Michael Niedermayerde3ae182008-09-26 02:19:21110 AVStream *st;
wm43e842612014-09-02 18:48:45111 FFTextReader tr;
Carl Eugen Hoyos19a64312014-10-29 00:32:44112 ff_text_init_avio(s, &tr, s->pb);
Michael Niedermayerde3ae182008-09-26 02:19:21113
Anton Khirnov3b3bbdd2011-06-18 09:43:24114 st = avformat_new_stream(s, NULL);
Michael Niedermayerde3ae182008-09-26 02:19:21115 if (!st)
Clément Bœsch6d2892c2012-11-23 21:47:51116 return AVERROR(ENOMEM);
Anton Khirnovc3f9ebf2011-11-29 18:28:15117 avpriv_set_pts_info(st, 64, 1, 100);
Derek Buitenhuis6f69f7a2016-04-10 19:58:15118 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
119 st->codecpar->codec_id = AV_CODEC_ID_ASS;
Michael Niedermayerde3ae182008-09-26 02:19:21120
Clément Bœsche0260e22012-11-23 21:45:05121 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
122 av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
Clément Bœschff5f5cb2014-09-14 07:35:10123 av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED);
Michael Niedermayerde3ae182008-09-26 02:19:21124
wm4265d2a72015-09-21 11:44:37125 ass->q.keep_duplicates = 1;
126
Clément Bœsche0260e22012-11-23 21:45:05127 for (;;) {
wm43e842612014-09-02 18:48:45128 int64_t pos = get_line(&line, &tr);
Clément Bœsch0aa5c5a2014-09-20 17:31:29129 int64_t ts_start = AV_NOPTS_VALUE;
130 int duration = -1;
131 AVPacket *sub;
Michael Niedermayerde3ae182008-09-26 02:19:21132
Clément Bœsch9ec52e52014-09-20 17:15:31133 if (!line.str[0]) // EOF
134 break;
135
Clément Bœsch0aa5c5a2014-09-20 17:31:29136 if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) {
137 av_bprintf(&header, "%s", line.str);
138 continue;
139 }
Reimar Döffingerc0f867b2023-06-21 18:06:09140 sub = ff_subtitles_queue_insert_bprint(&ass->q, &rline, 0);
Clément Bœsch0aa5c5a2014-09-20 17:31:29141 if (!sub) {
142 res = AVERROR(ENOMEM);
143 goto end;
144 }
145 sub->pos = pos;
146 sub->pts = ts_start;
147 sub->duration = duration;
Michael Niedermayerde3ae182008-09-26 02:19:21148 }
149
Derek Buitenhuis6f69f7a2016-04-10 19:58:15150 res = ff_bprint_to_codecpar_extradata(st->codecpar, &header);
Clément Bœsch36e61e22012-12-29 21:09:59151 if (res < 0)
Clément Bœsche0260e22012-11-23 21:45:05152 goto end;
Michael Niedermayerde3ae182008-09-26 02:19:21153
Clément Bœschaf924fd2015-09-10 19:40:07154 ff_subtitles_queue_finalize(s, &ass->q);
Michael Niedermayerde3ae182008-09-26 02:19:21155
Clément Bœsche0260e22012-11-23 21:45:05156end:
Clément Bœsch71f62752014-09-27 15:49:49157 av_bprint_finalize(&header, NULL);
158 av_bprint_finalize(&line, NULL);
159 av_bprint_finalize(&rline, NULL);
Clément Bœsche0260e22012-11-23 21:45:05160 return res;
Michael Niedermayerde3ae182008-09-26 02:19:21161}
162
Andreas Rheinhardtbc706842021-04-19 17:45:24163const AVInputFormat ff_ass_demuxer = {
Aurelien Jacobs87d69d32010-12-04 00:37:53164 .name = "ass",
Diego Biurrun0177b7d2012-07-24 01:23:48165 .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
Andreas Rheinhardtef7eaa42020-03-21 17:31:06166 .flags_internal = FF_FMT_INIT_CLEANUP,
Aurelien Jacobs87d69d32010-12-04 00:37:53167 .priv_data_size = sizeof(ASSContext),
Clément Bœsch069c8972012-11-23 21:47:09168 .read_probe = ass_probe,
169 .read_header = ass_read_header,
Andreas Rheinhardtea5bdc82021-07-08 21:05:08170 .read_packet = ff_subtitles_read_packet,
171 .read_close = ff_subtitles_read_close,
172 .read_seek2 = ff_subtitles_read_seek,
Michael Niedermayerde3ae182008-09-26 02:19:21173};