FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/assdec.c
Date: 2026-09-28 04:46:15
Exec Total Coverage
Lines: 72 78 92.3%
Functions: 4 4 100.0%
Branches: 27 34 79.4%

Line Branch Exec Source
1 /*
2 * SSA/ASS demuxer
3 * Copyright (c) 2008 Michael Niedermayer
4 * Copyright (c) 2014 Clément Bœsch
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
23 #include <stdint.h>
24
25 #include "avformat.h"
26 #include "demux.h"
27 #include "internal.h"
28 #include "subtitles.h"
29 #include "libavutil/bprint.h"
30
31 typedef struct ASSContext {
32 FFDemuxSubtitlesQueue q;
33 unsigned readorder;
34 } ASSContext;
35
36 8076 static int ass_probe(const AVProbeData *p)
37 {
38 char buf[13];
39 FFTextReader tr;
40 8076 ff_text_init_buf(&tr, p->buf, p->buf_size);
41
42
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8079 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 8076 times.
8083 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
43 7 ff_text_r8(&tr);
44
45 8076 ff_text_read(&tr, buf, sizeof(buf));
46
47
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8071 times.
8076 if (!memcmp(buf, "[Script Info]", 13))
48 5 return AVPROBE_SCORE_MAX;
49
50 8071 return 0;
51 }
52
53 238 static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p,
54 int64_t *start, int *duration)
55 {
56 238 int pos = 0;
57 int64_t end;
58 int hh1, mm1, ss1, ms1;
59 int hh2, mm2, ss2, ms2;
60
61
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 179 times.
238 if (sscanf(p, "Dialogue: %*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d,%n",
62 &hh1, &mm1, &ss1, &ms1,
63
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 &hh2, &mm2, &ss2, &ms2, &pos) >= 8 && pos > 0) {
64
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
67 * the old SSA field, now replaced by Layer, and will lead to Layer
68 * being 0 here). */
69 59 const int layer = atoi(p + 10);
70
71 59 end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
72 59 *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
73 59 *duration = end - *start;
74
75 /* Events with duration <= 0 are output by some authoring scripts and
76 * are simply treated as hidden by renderers. We cannot output such
77 * events as actual packets since this would cause their duration to be
78 * guessed by later processing like compute_pkt_fields().
79 * Hence, such events are written to the header like non-dialogue
80 * events like comments are. */
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (*duration <= 0)
82 ✗ return -1;
83
84 59 av_bprint_clear(dst);
85 59 av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos);
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
59 if (!av_bprint_is_complete(dst))
87 ✗ return AVERROR(ENOMEM);
88
89 /* right strip the buffer */
90 59 while (dst->len > 0 &&
91
3/4
✓ Branch 0 taken 169 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 118 times.
169 dst->str[dst->len - 1] == '\r' ||
92
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 59 times.
118 dst->str[dst->len - 1] == '\n')
93 110 dst->str[--dst->len] = 0;
94 59 return 0;
95 }
96 179 return -1;
97 }
98
99 243 static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
100 {
101 243 int64_t pos = ff_text_pos(tr);
102
103 243 av_bprint_clear(buf);
104 18098 for (;;) {
105 18341 char c = ff_text_r8(tr);
106
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18336 times.
18341 if (!c)
107 5 break;
108 18336 av_bprint_chars(buf, c, 1);
109
2/2
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 18098 times.
18336 if (c == '\n')
110 238 break;
111 }
112 243 return pos;
113 }
114
115 5 static int ass_read_header(AVFormatContext *s)
116 {
117 5 ASSContext *ass = s->priv_data;
118 AVBPrint header, line, rline;
119 5 int res = 0;
120 AVStream *st;
121 FFTextReader tr;
122 5 ff_text_init_avio(s, &tr, s->pb);
123
124 5 st = avformat_new_stream(s, NULL);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!st)
126 ✗ return AVERROR(ENOMEM);
127 5 avpriv_set_pts_info(st, 64, 1, 100);
128 5 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
129 5 st->codecpar->codec_id = AV_CODEC_ID_ASS;
130
131 5 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
132 5 av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
133 5 av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED);
134
135 5 ass->q.keep_duplicates = 1;
136
137 238 for (;;) {
138 243 int64_t pos = get_line(&line, &tr);
139 243 int64_t ts_start = AV_NOPTS_VALUE;
140 243 int duration = -1;
141 AVPacket *sub;
142
143
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 238 times.
243 if (!line.str[0]) // EOF
144 5 break;
145
146
2/2
✓ Branch 1 taken 179 times.
✓ Branch 2 taken 59 times.
238 if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) {
147 179 av_bprintf(&header, "%s", line.str);
148 179 continue;
149 }
150 59 sub = ff_subtitles_queue_insert_bprint(&ass->q, &rline, 0);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (!sub) {
152 ✗ res = AVERROR(ENOMEM);
153 ✗ goto end;
154 }
155 59 sub->pos = pos;
156 59 sub->pts = ts_start;
157 59 sub->duration = duration;
158 }
159
160 5 res = ff_bprint_to_codecpar_extradata(st->codecpar, &header);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (res < 0)
162 ✗ goto end;
163
164 5 ff_subtitles_queue_finalize(s, &ass->q);
165
166 5 end:
167 5 av_bprint_finalize(&header, NULL);
168 5 av_bprint_finalize(&line, NULL);
169 5 av_bprint_finalize(&rline, NULL);
170 5 return res;
171 }
172
173 const FFInputFormat ff_ass_demuxer = {
174 .p.name = "ass",
175 .p.long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
176 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
177 .priv_data_size = sizeof(ASSContext),
178 .read_probe = ass_probe,
179 .read_header = ass_read_header,
180 .read_packet = ff_subtitles_read_packet,
181 .read_close = ff_subtitles_read_close,
182 .read_seek2 = ff_subtitles_read_seek,
183 };
184