Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 1 | /* |
| 2 | * SSA/ASS demuxer |
| 3 | * Copyright (c) 2008 Michael Niedermayer |
Clément Bœsch | 3a6fa38 | 2014-09-20 17:31:52 | [diff] [blame] | 4 | * Copyright (c) 2014 Clément Bœsch |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 5 | * |
| 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 Biurrun | 8f8bc92 | 2013-11-23 20:32:55 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 25 | #include "avformat.h" |
Aurelien Jacobs | 7c89295 | 2010-07-21 21:39:01 | [diff] [blame] | 26 | #include "internal.h" |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 27 | #include "subtitles.h" |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 28 | #include "libavutil/bprint.h" |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 29 | |
Patrice Clement | 5c9c305 | 2014-02-28 17:31:29 | [diff] [blame] | 30 | typedef struct ASSContext { |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 31 | FFDemuxSubtitlesQueue q; |
Clément Bœsch | ff5f5cb | 2014-09-14 07:35:10 | [diff] [blame] | 32 | unsigned readorder; |
Patrice Clement | 5c9c305 | 2014-02-28 17:31:29 | [diff] [blame] | 33 | } ASSContext; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 34 | |
Carl Eugen Hoyos | 4d8875e | 2019-03-21 00:18:37 | [diff] [blame] | 35 | static int ass_probe(const AVProbeData *p) |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 36 | { |
wm4 | 3e84261 | 2014-09-02 18:48:45 | [diff] [blame] | 37 | char buf[13]; |
| 38 | FFTextReader tr; |
| 39 | ff_text_init_buf(&tr, p->buf, p->buf_size); |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 40 | |
Clément Bœsch | 6679fcd | 2015-11-20 13:38:22 | [diff] [blame] | 41 | while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n') |
| 42 | ff_text_r8(&tr); |
| 43 | |
wm4 | 3e84261 | 2014-09-02 18:48:45 | [diff] [blame] | 44 | ff_text_read(&tr, buf, sizeof(buf)); |
| 45 | |
| 46 | if (!memcmp(buf, "[Script Info]", 13)) |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 47 | return AVPROBE_SCORE_MAX; |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Clément Bœsch | ff5f5cb | 2014-09-14 07:35:10 | [diff] [blame] | 52 | static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p, |
| 53 | int64_t *start, int *duration) |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 54 | { |
Clément Bœsch | 138902d | 2014-10-04 10:22:37 | [diff] [blame] | 55 | int pos = 0; |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 56 | int64_t end; |
| 57 | int hh1, mm1, ss1, ms1; |
| 58 | int hh2, mm2, ss2, ms2; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 59 | |
Clément Bœsch | ff5f5cb | 2014-09-14 07:35:10 | [diff] [blame] | 60 | if (sscanf(p, "Dialogue: %*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d,%n", |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 61 | &hh1, &mm1, &ss1, &ms1, |
Clément Bœsch | 138902d | 2014-10-04 10:22:37 | [diff] [blame] | 62 | &hh2, &mm2, &ss2, &ms2, &pos) >= 8 && pos > 0) { |
Clément Bœsch | ff5f5cb | 2014-09-14 07:35:10 | [diff] [blame] | 63 | |
| 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œsch | d281a87 | 2014-10-11 18:14:37 | [diff] [blame] | 66 | * the old SSA field, now replaced by Layer, and will lead to Layer |
Clément Bœsch | ff5f5cb | 2014-09-14 07:35:10 | [diff] [blame] | 67 | * being 0 here). */ |
| 68 | const int layer = atoi(p + 10); |
| 69 | |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 70 | end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2; |
| 71 | *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1; |
| 72 | *duration = end - *start; |
Clément Bœsch | ff5f5cb | 2014-09-14 07:35:10 | [diff] [blame] | 73 | |
| 74 | av_bprint_clear(dst); |
| 75 | av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos); |
Reimar Döffinger | c0f867b | 2023-06-21 18:06:09 | [diff] [blame] | 76 | if (!av_bprint_is_complete(dst)) |
| 77 | return AVERROR(ENOMEM); |
Clément Bœsch | ff5f5cb | 2014-09-14 07:35:10 | [diff] [blame] | 78 | |
| 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œsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | return -1; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 87 | } |
| 88 | |
wm4 | 3e84261 | 2014-09-02 18:48:45 | [diff] [blame] | 89 | static int64_t get_line(AVBPrint *buf, FFTextReader *tr) |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 90 | { |
wm4 | 3e84261 | 2014-09-02 18:48:45 | [diff] [blame] | 91 | int64_t pos = ff_text_pos(tr); |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 92 | |
| 93 | av_bprint_clear(buf); |
| 94 | for (;;) { |
wm4 | 3e84261 | 2014-09-02 18:48:45 | [diff] [blame] | 95 | char c = ff_text_r8(tr); |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 96 | if (!c) |
| 97 | break; |
| 98 | av_bprint_chars(buf, c, 1); |
| 99 | if (c == '\n') |
| 100 | break; |
| 101 | } |
| 102 | return pos; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 103 | } |
| 104 | |
Clément Bœsch | 069c897 | 2012-11-23 21:47:09 | [diff] [blame] | 105 | static int ass_read_header(AVFormatContext *s) |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 106 | { |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 107 | ASSContext *ass = s->priv_data; |
Clément Bœsch | ff5f5cb | 2014-09-14 07:35:10 | [diff] [blame] | 108 | AVBPrint header, line, rline; |
Clément Bœsch | 9ec52e5 | 2014-09-20 17:15:31 | [diff] [blame] | 109 | int res = 0; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 110 | AVStream *st; |
wm4 | 3e84261 | 2014-09-02 18:48:45 | [diff] [blame] | 111 | FFTextReader tr; |
Carl Eugen Hoyos | 19a6431 | 2014-10-29 00:32:44 | [diff] [blame] | 112 | ff_text_init_avio(s, &tr, s->pb); |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 113 | |
Anton Khirnov | 3b3bbdd | 2011-06-18 09:43:24 | [diff] [blame] | 114 | st = avformat_new_stream(s, NULL); |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 115 | if (!st) |
Clément Bœsch | 6d2892c | 2012-11-23 21:47:51 | [diff] [blame] | 116 | return AVERROR(ENOMEM); |
Anton Khirnov | c3f9ebf | 2011-11-29 18:28:15 | [diff] [blame] | 117 | avpriv_set_pts_info(st, 64, 1, 100); |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 118 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; |
| 119 | st->codecpar->codec_id = AV_CODEC_ID_ASS; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 120 | |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 121 | av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED); |
| 122 | av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED); |
Clément Bœsch | ff5f5cb | 2014-09-14 07:35:10 | [diff] [blame] | 123 | av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED); |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 124 | |
wm4 | 265d2a7 | 2015-09-21 11:44:37 | [diff] [blame] | 125 | ass->q.keep_duplicates = 1; |
| 126 | |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 127 | for (;;) { |
wm4 | 3e84261 | 2014-09-02 18:48:45 | [diff] [blame] | 128 | int64_t pos = get_line(&line, &tr); |
Clément Bœsch | 0aa5c5a | 2014-09-20 17:31:29 | [diff] [blame] | 129 | int64_t ts_start = AV_NOPTS_VALUE; |
| 130 | int duration = -1; |
| 131 | AVPacket *sub; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 132 | |
Clément Bœsch | 9ec52e5 | 2014-09-20 17:15:31 | [diff] [blame] | 133 | if (!line.str[0]) // EOF |
| 134 | break; |
| 135 | |
Clément Bœsch | 0aa5c5a | 2014-09-20 17:31:29 | [diff] [blame] | 136 | if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) { |
| 137 | av_bprintf(&header, "%s", line.str); |
| 138 | continue; |
| 139 | } |
Reimar Döffinger | c0f867b | 2023-06-21 18:06:09 | [diff] [blame] | 140 | sub = ff_subtitles_queue_insert_bprint(&ass->q, &rline, 0); |
Clément Bœsch | 0aa5c5a | 2014-09-20 17:31:29 | [diff] [blame] | 141 | if (!sub) { |
| 142 | res = AVERROR(ENOMEM); |
| 143 | goto end; |
| 144 | } |
| 145 | sub->pos = pos; |
| 146 | sub->pts = ts_start; |
| 147 | sub->duration = duration; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 148 | } |
| 149 | |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 150 | res = ff_bprint_to_codecpar_extradata(st->codecpar, &header); |
Clément Bœsch | 36e61e2 | 2012-12-29 21:09:59 | [diff] [blame] | 151 | if (res < 0) |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 152 | goto end; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 153 | |
Clément Bœsch | af924fd | 2015-09-10 19:40:07 | [diff] [blame] | 154 | ff_subtitles_queue_finalize(s, &ass->q); |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 155 | |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 156 | end: |
Clément Bœsch | 71f6275 | 2014-09-27 15:49:49 | [diff] [blame] | 157 | av_bprint_finalize(&header, NULL); |
| 158 | av_bprint_finalize(&line, NULL); |
| 159 | av_bprint_finalize(&rline, NULL); |
Clément Bœsch | e0260e2 | 2012-11-23 21:45:05 | [diff] [blame] | 160 | return res; |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 161 | } |
| 162 | |
Andreas Rheinhardt | bc70684 | 2021-04-19 17:45:24 | [diff] [blame] | 163 | const AVInputFormat ff_ass_demuxer = { |
Aurelien Jacobs | 87d69d3 | 2010-12-04 00:37:53 | [diff] [blame] | 164 | .name = "ass", |
Diego Biurrun | 0177b7d | 2012-07-24 01:23:48 | [diff] [blame] | 165 | .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"), |
Andreas Rheinhardt | ef7eaa4 | 2020-03-21 17:31:06 | [diff] [blame] | 166 | .flags_internal = FF_FMT_INIT_CLEANUP, |
Aurelien Jacobs | 87d69d3 | 2010-12-04 00:37:53 | [diff] [blame] | 167 | .priv_data_size = sizeof(ASSContext), |
Clément Bœsch | 069c897 | 2012-11-23 21:47:09 | [diff] [blame] | 168 | .read_probe = ass_probe, |
| 169 | .read_header = ass_read_header, |
Andreas Rheinhardt | ea5bdc8 | 2021-07-08 21:05:08 | [diff] [blame] | 170 | .read_packet = ff_subtitles_read_packet, |
| 171 | .read_close = ff_subtitles_read_close, |
| 172 | .read_seek2 = ff_subtitles_read_seek, |
Michael Niedermayer | de3ae18 | 2008-09-26 02:19:21 | [diff] [blame] | 173 | }; |