FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/assdec.c
Date: 2023-09-22 12:20:07
Exec Total Coverage
Lines: 71 76 93.4%
Functions: 4 4 100.0%
Branches: 26 32 81.2%

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 "internal.h"
27 #include "subtitles.h"
28 #include "libavutil/bprint.h"
29
30 typedef struct ASSContext {
31 FFDemuxSubtitlesQueue q;
32 unsigned readorder;
33 } ASSContext;
34
35 6938 static int ass_probe(const AVProbeData *p)
36 {
37 char buf[13];
38 FFTextReader tr;
39 6938 ff_text_init_buf(&tr, p->buf, p->buf_size);
40
41
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6941 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 6938 times.
6945 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
42 7 ff_text_r8(&tr);
43
44 6938 ff_text_read(&tr, buf, sizeof(buf));
45
46
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6935 times.
6938 if (!memcmp(buf, "[Script Info]", 13))
47 3 return AVPROBE_SCORE_MAX;
48
49 6935 return 0;
50 }
51
52 208 static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p,
53 int64_t *start, int *duration)
54 {
55 208 int pos = 0;
56 int64_t end;
57 int hh1, mm1, ss1, ms1;
58 int hh2, mm2, ss2, ms2;
59
60
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",
61 &hh1, &mm1, &ss1, &ms1,
62
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 &hh2, &mm2, &ss2, &ms2, &pos) >= 8 && pos > 0) {
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
66 * the old SSA field, now replaced by Layer, and will lead to Layer
67 * being 0 here). */
68 51 const int layer = atoi(p + 10);
69
70 51 end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
71 51 *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
72 51 *duration = end - *start;
73
74 51 av_bprint_clear(dst);
75 51 av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos);
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if (!av_bprint_is_complete(dst))
77 return AVERROR(ENOMEM);
78
79 /* right strip the buffer */
80 51 while (dst->len > 0 &&
81
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' ||
82
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 51 times.
102 dst->str[dst->len - 1] == '\n')
83 102 dst->str[--dst->len] = 0;
84 51 return 0;
85 }
86 157 return -1;
87 }
88
89 211 static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
90 {
91 211 int64_t pos = ff_text_pos(tr);
92
93 211 av_bprint_clear(buf);
94 16354 for (;;) {
95 16565 char c = ff_text_r8(tr);
96
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16562 times.
16565 if (!c)
97 3 break;
98 16562 av_bprint_chars(buf, c, 1);
99
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 16354 times.
16562 if (c == '\n')
100 208 break;
101 }
102 211 return pos;
103 }
104
105 3 static int ass_read_header(AVFormatContext *s)
106 {
107 3 ASSContext *ass = s->priv_data;
108 AVBPrint header, line, rline;
109 3 int res = 0;
110 AVStream *st;
111 FFTextReader tr;
112 3 ff_text_init_avio(s, &tr, s->pb);
113
114 3 st = avformat_new_stream(s, NULL);
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!st)
116 return AVERROR(ENOMEM);
117 3 avpriv_set_pts_info(st, 64, 1, 100);
118 3 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
119 3 st->codecpar->codec_id = AV_CODEC_ID_ASS;
120
121 3 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
122 3 av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
123 3 av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED);
124
125 3 ass->q.keep_duplicates = 1;
126
127 208 for (;;) {
128 211 int64_t pos = get_line(&line, &tr);
129 211 int64_t ts_start = AV_NOPTS_VALUE;
130 211 int duration = -1;
131 AVPacket *sub;
132
133
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 208 times.
211 if (!line.str[0]) // EOF
134 3 break;
135
136
2/2
✓ Branch 1 taken 157 times.
✓ Branch 2 taken 51 times.
208 if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) {
137 157 av_bprintf(&header, "%s", line.str);
138 157 continue;
139 }
140 51 sub = ff_subtitles_queue_insert_bprint(&ass->q, &rline, 0);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (!sub) {
142 res = AVERROR(ENOMEM);
143 goto end;
144 }
145 51 sub->pos = pos;
146 51 sub->pts = ts_start;
147 51 sub->duration = duration;
148 }
149
150 3 res = ff_bprint_to_codecpar_extradata(st->codecpar, &header);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (res < 0)
152 goto end;
153
154 3 ff_subtitles_queue_finalize(s, &ass->q);
155
156 3 end:
157 3 av_bprint_finalize(&header, NULL);
158 3 av_bprint_finalize(&line, NULL);
159 3 av_bprint_finalize(&rline, NULL);
160 3 return res;
161 }
162
163 const AVInputFormat ff_ass_demuxer = {
164 .name = "ass",
165 .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
166 .flags_internal = FF_FMT_INIT_CLEANUP,
167 .priv_data_size = sizeof(ASSContext),
168 .read_probe = ass_probe,
169 .read_header = ass_read_header,
170 .read_packet = ff_subtitles_read_packet,
171 .read_close = ff_subtitles_read_close,
172 .read_seek2 = ff_subtitles_read_seek,
173 };
174