blob: 2ab7fb9efbb3d82ead77f0ba0f5ec7dd6b59688c [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œschff5f5cb2014-09-14 07:35:1053static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p,
54 int64_t *start, int *duration)
Michael Niedermayerde3ae182008-09-26 02:19:2155{
Clément Bœsch138902d2014-10-04 10:22:3756 int pos = 0;
Clément Bœsche0260e22012-11-23 21:45:0557 int64_t end;
58 int hh1, mm1, ss1, ms1;
59 int hh2, mm2, ss2, ms2;
Michael Niedermayerde3ae182008-09-26 02:19:2160
Clément Bœschff5f5cb2014-09-14 07:35:1061 if (sscanf(p, "Dialogue: %*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d,%n",
Clément Bœsche0260e22012-11-23 21:45:0562 &hh1, &mm1, &ss1, &ms1,
Clément Bœsch138902d2014-10-04 10:22:3763 &hh2, &mm2, &ss2, &ms2, &pos) >= 8 && pos > 0) {
Clément Bœschff5f5cb2014-09-14 07:35:1064
65 /* This is not part of the sscanf itself in order to handle an actual
66 * number (which would be the Layer) or the form "Marked=N" (which is
Clément Bœschd281a872014-10-11 18:14:3767 * the old SSA field, now replaced by Layer, and will lead to Layer
Clément Bœschff5f5cb2014-09-14 07:35:1068 * being 0 here). */
69 const int layer = atoi(p + 10);
70
Clément Bœsche0260e22012-11-23 21:45:0571 end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
72 *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
73 *duration = end - *start;
Clément Bœschff5f5cb2014-09-14 07:35:1074
75 av_bprint_clear(dst);
76 av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos);
77
78 /* right strip the buffer */
79 while (dst->len > 0 &&
80 dst->str[dst->len - 1] == '\r' ||
81 dst->str[dst->len - 1] == '\n')
82 dst->str[--dst->len] = 0;
Clément Bœsche0260e22012-11-23 21:45:0583 return 0;
84 }
85 return -1;
Michael Niedermayerde3ae182008-09-26 02:19:2186}
87
wm43e842612014-09-02 18:48:4588static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
Michael Niedermayerde3ae182008-09-26 02:19:2189{
wm43e842612014-09-02 18:48:4590 int64_t pos = ff_text_pos(tr);
Clément Bœsche0260e22012-11-23 21:45:0591
92 av_bprint_clear(buf);
93 for (;;) {
wm43e842612014-09-02 18:48:4594 char c = ff_text_r8(tr);
Clément Bœsche0260e22012-11-23 21:45:0595 if (!c)
96 break;
97 av_bprint_chars(buf, c, 1);
98 if (c == '\n')
99 break;
100 }
101 return pos;
Michael Niedermayerde3ae182008-09-26 02:19:21102}
103
Clément Bœsch069c8972012-11-23 21:47:09104static int ass_read_header(AVFormatContext *s)
Michael Niedermayerde3ae182008-09-26 02:19:21105{
Michael Niedermayerde3ae182008-09-26 02:19:21106 ASSContext *ass = s->priv_data;
Clément Bœschff5f5cb2014-09-14 07:35:10107 AVBPrint header, line, rline;
Clément Bœsch9ec52e52014-09-20 17:15:31108 int res = 0;
Michael Niedermayerde3ae182008-09-26 02:19:21109 AVStream *st;
wm43e842612014-09-02 18:48:45110 FFTextReader tr;
Carl Eugen Hoyos19a64312014-10-29 00:32:44111 ff_text_init_avio(s, &tr, s->pb);
Michael Niedermayerde3ae182008-09-26 02:19:21112
Anton Khirnov3b3bbdd2011-06-18 09:43:24113 st = avformat_new_stream(s, NULL);
Michael Niedermayerde3ae182008-09-26 02:19:21114 if (!st)
Clément Bœsch6d2892c2012-11-23 21:47:51115 return AVERROR(ENOMEM);
Anton Khirnovc3f9ebf2011-11-29 18:28:15116 avpriv_set_pts_info(st, 64, 1, 100);
Derek Buitenhuis6f69f7a2016-04-10 19:58:15117 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
118 st->codecpar->codec_id = AV_CODEC_ID_ASS;
Michael Niedermayerde3ae182008-09-26 02:19:21119
Clément Bœsche0260e22012-11-23 21:45:05120 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
121 av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
Clément Bœschff5f5cb2014-09-14 07:35:10122 av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED);
Michael Niedermayerde3ae182008-09-26 02:19:21123
wm4265d2a72015-09-21 11:44:37124 ass->q.keep_duplicates = 1;
125
Clément Bœsche0260e22012-11-23 21:45:05126 for (;;) {
wm43e842612014-09-02 18:48:45127 int64_t pos = get_line(&line, &tr);
Clément Bœsch0aa5c5a2014-09-20 17:31:29128 int64_t ts_start = AV_NOPTS_VALUE;
129 int duration = -1;
130 AVPacket *sub;
Michael Niedermayerde3ae182008-09-26 02:19:21131
Clément Bœsch9ec52e52014-09-20 17:15:31132 if (!line.str[0]) // EOF
133 break;
134
Clément Bœsch0aa5c5a2014-09-20 17:31:29135 if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) {
136 av_bprintf(&header, "%s", line.str);
137 continue;
138 }
139 sub = ff_subtitles_queue_insert(&ass->q, rline.str, rline.len, 0);
140 if (!sub) {
141 res = AVERROR(ENOMEM);
142 goto end;
143 }
144 sub->pos = pos;
145 sub->pts = ts_start;
146 sub->duration = duration;
Michael Niedermayerde3ae182008-09-26 02:19:21147 }
148
Derek Buitenhuis6f69f7a2016-04-10 19:58:15149 res = ff_bprint_to_codecpar_extradata(st->codecpar, &header);
Clément Bœsch36e61e22012-12-29 21:09:59150 if (res < 0)
Clément Bœsche0260e22012-11-23 21:45:05151 goto end;
Michael Niedermayerde3ae182008-09-26 02:19:21152
Clément Bœschaf924fd2015-09-10 19:40:07153 ff_subtitles_queue_finalize(s, &ass->q);
Michael Niedermayerde3ae182008-09-26 02:19:21154
Clément Bœsche0260e22012-11-23 21:45:05155end:
Clément Bœsch71f62752014-09-27 15:49:49156 av_bprint_finalize(&header, NULL);
157 av_bprint_finalize(&line, NULL);
158 av_bprint_finalize(&rline, NULL);
Clément Bœsche0260e22012-11-23 21:45:05159 return res;
Michael Niedermayerde3ae182008-09-26 02:19:21160}
161
Andreas Rheinhardtbc706842021-04-19 17:45:24162const AVInputFormat ff_ass_demuxer = {
Aurelien Jacobs87d69d32010-12-04 00:37:53163 .name = "ass",
Diego Biurrun0177b7d2012-07-24 01:23:48164 .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
Andreas Rheinhardtef7eaa42020-03-21 17:31:06165 .flags_internal = FF_FMT_INIT_CLEANUP,
Aurelien Jacobs87d69d32010-12-04 00:37:53166 .priv_data_size = sizeof(ASSContext),
Clément Bœsch069c8972012-11-23 21:47:09167 .read_probe = ass_probe,
168 .read_header = ass_read_header,
Andreas Rheinhardtea5bdc82021-07-08 21:05:08169 .read_packet = ff_subtitles_read_packet,
170 .read_close = ff_subtitles_read_close,
171 .read_seek2 = ff_subtitles_read_seek,
Michael Niedermayerde3ae182008-09-26 02:19:21172};