FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/assdec.c
Date: 2026-09-04 04:29:05
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 7961 static int ass_probe(const AVProbeData *p)
37 {
38 char buf[13];
39 FFTextReader tr;
40 7961 ff_text_init_buf(&tr, p->buf, p->buf_size);
41
42
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7964 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 7961 times.
7968 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
43 7 ff_text_r8(&tr);
44
45 7961 ff_text_read(&tr, buf, sizeof(buf));
46
47
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7958 times.
7961 if (!memcmp(buf, "[Script Info]", 13))
48 3 return AVPROBE_SCORE_MAX;
49
50 7958 return 0;
51 }
52
53 208 static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p,
54 int64_t *start, int *duration)
55 {
56 208 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 51 times.
✓ Branch 1 taken 157 times.
208 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 51 times.
✗ Branch 1 not taken.
51 &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 51 const int layer = atoi(p + 10);
70
71 51 end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
72 51 *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
73 51 *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 51 times.
51 if (*duration <= 0)
82 return -1;
83
84 51 av_bprint_clear(dst);
85 51 av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos);
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if (!av_bprint_is_complete(dst))
87 return AVERROR(ENOMEM);
88
89 /* right strip the buffer */
90 51 while (dst->len > 0 &&
91
3/4
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 102 times.
153 dst->str[dst->len - 1] == '\r' ||
92
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 51 times.
102 dst->str[dst->len - 1] == '\n')
93 102 dst->str[--dst->len] = 0;
94 51 return 0;
95 }
96 157 return -1;
97 }
98
99 211 static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
100 {
101 211 int64_t pos = ff_text_pos(tr);
102
103 211 av_bprint_clear(buf);
104 16354 for (;;) {
105 16565 char c = ff_text_r8(tr);
106
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16562 times.
16565 if (!c)
107 3 break;
108 16562 av_bprint_chars(buf, c, 1);
109
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 16354 times.
16562 if (c == '\n')
110 208 break;
111 }
112 211 return pos;
113 }
114
115 3 static int ass_read_header(AVFormatContext *s)
116 {
117 3 ASSContext *ass = s->priv_data;
118 AVBPrint header, line, rline;
119 3 int res = 0;
120 AVStream *st;
121 FFTextReader tr;
122 3 ff_text_init_avio(s, &tr, s->pb);
123
124 3 st = avformat_new_stream(s, NULL);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!st)
126 return AVERROR(ENOMEM);
127 3 avpriv_set_pts_info(st, 64, 1, 100);
128 3 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
129 3 st->codecpar->codec_id = AV_CODEC_ID_ASS;
130
131 3 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
132 3 av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
133 3 av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED);
134
135 3 ass->q.keep_duplicates = 1;
136
137 208 for (;;) {
138 211 int64_t pos = get_line(&line, &tr);
139 211 int64_t ts_start = AV_NOPTS_VALUE;
140 211 int duration = -1;
141 AVPacket *sub;
142
143
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 208 times.
211 if (!line.str[0]) // EOF
144 3 break;
145
146
2/2
✓ Branch 1 taken 157 times.
✓ Branch 2 taken 51 times.
208 if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) {
147 157 av_bprintf(&header, "%s", line.str);
148 157 continue;
149 }
150 51 sub = ff_subtitles_queue_insert_bprint(&ass->q, &rline, 0);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (!sub) {
152 res = AVERROR(ENOMEM);
153 goto end;
154 }
155 51 sub->pos = pos;
156 51 sub->pts = ts_start;
157 51 sub->duration = duration;
158 }
159
160 3 res = ff_bprint_to_codecpar_extradata(st->codecpar, &header);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (res < 0)
162 goto end;
163
164 3 ff_subtitles_queue_finalize(s, &ass->q);
165
166 3 end:
167 3 av_bprint_finalize(&header, NULL);
168 3 av_bprint_finalize(&line, NULL);
169 3 av_bprint_finalize(&rline, NULL);
170 3 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