FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/samidec.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 88 101 87.1%
Functions: 4 5 80.0%
Branches: 44 62 71.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Clément Bœsch
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * SAMI subtitle decoder
24 * @see http://msdn.microsoft.com/en-us/library/ms971327.aspx
25 */
26
27 #include "ass.h"
28 #include "libavutil/attributes.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/bprint.h"
31 #include "libavutil/mem.h"
32 #include "codec_internal.h"
33 #include "htmlsubtitles.h"
34
35 typedef struct {
36 AVBPrint source;
37 AVBPrint content;
38 AVBPrint encoded_source;
39 AVBPrint encoded_content;
40 AVBPrint full;
41 int readorder;
42 } SAMIContext;
43
44 167 static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
45 {
46 167 SAMIContext *sami = avctx->priv_data;
47 167 int ret = 0;
48 167 char *tag = NULL;
49 167 char *dupsrc = av_strdup(src);
50 167 char *p = dupsrc;
51 167 AVBPrint *dst_content = &sami->encoded_content;
52 167 AVBPrint *dst_source = &sami->encoded_source;
53
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 167 times.
167 if (!dupsrc)
55 return AVERROR(ENOMEM);
56
57 167 av_bprint_clear(&sami->encoded_content);
58 167 av_bprint_clear(&sami->content);
59 167 av_bprint_clear(&sami->encoded_source);
60 89 for (;;) {
61 256 char *saveptr = NULL;
62 256 int prev_chr_is_space = 0;
63 256 AVBPrint *dst = &sami->content;
64
65 /* parse & extract paragraph tag */
66 256 p = av_stristr(p, "<P");
67
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 168 times.
256 if (!p)
68 88 break;
69
2/4
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 168 times.
168 if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
70 p++;
71 continue;
72 }
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (dst->len) // add a separator with the previous paragraph if there was one
74 av_bprintf(dst, "\\N");
75 168 tag = av_strtok(p, ">", &saveptr);
76
2/4
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
168 if (!tag || !saveptr)
77 break;
78 168 p = saveptr;
79
80 /* check if the current paragraph is the "source" (speaker name) */
81
3/4
✓ Branch 1 taken 166 times.
✓ Branch 2 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 166 times.
168 if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
82 2 dst = &sami->source;
83 2 av_bprint_clear(dst);
84 }
85
86 /* if empty event -> skip subtitle */
87
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 168 times.
245 while (av_isspace(*p))
88 77 p++;
89
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 89 times.
168 if (!strncmp(p, "&nbsp;", 6)) {
90 79 ret = -1;
91 79 goto end;
92 }
93
94 /* extract the text, stripping most of the tags */
95
2/2
✓ Branch 0 taken 6893 times.
✓ Branch 1 taken 88 times.
6981 while (*p) {
96
2/2
✓ Branch 0 taken 316 times.
✓ Branch 1 taken 6577 times.
6893 if (*p == '<') {
97
5/6
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 314 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
316 if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
98 break;
99 }
100
2/2
✓ Branch 1 taken 137 times.
✓ Branch 2 taken 6755 times.
6892 if (!av_strncasecmp(p, "<BR", 3)) {
101 137 av_bprintf(dst, "\\N");
102 137 p++;
103
3/4
✓ Branch 0 taken 411 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 274 times.
✓ Branch 3 taken 137 times.
411 while (*p && *p != '>')
104 274 p++;
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137 times.
137 if (!*p)
106 break;
107
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 if (*p == '>')
108 137 p++;
109 137 continue;
110 }
111
2/2
✓ Branch 0 taken 5951 times.
✓ Branch 1 taken 804 times.
6755 if (!av_isspace(*p))
112 5951 av_bprint_chars(dst, *p, 1);
113
2/2
✓ Branch 0 taken 683 times.
✓ Branch 1 taken 121 times.
804 else if (!prev_chr_is_space)
114 683 av_bprint_chars(dst, ' ', 1);
115 6755 prev_chr_is_space = av_isspace(*p);
116 6755 p++;
117 }
118 }
119
120 88 av_bprint_clear(&sami->full);
121
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 79 times.
88 if (sami->source.len) {
122 9 ret = ff_htmlmarkup_to_ass(avctx, dst_source, sami->source.str);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
124 goto end;
125 9 av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->encoded_source.str);
126 }
127 88 ret = ff_htmlmarkup_to_ass(avctx, dst_content, sami->content.str);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if (ret < 0)
129 goto end;
130 88 av_bprintf(&sami->full, "%s", sami->encoded_content.str);
131
132 167 end:
133 167 av_free(dupsrc);
134 167 return ret;
135 }
136
137 167 static int sami_decode_frame(AVCodecContext *avctx, AVSubtitle *sub,
138 int *got_sub_ptr, const AVPacket *avpkt)
139 {
140 167 const char *ptr = avpkt->data;
141 167 SAMIContext *sami = avctx->priv_data;
142
143
2/4
✓ Branch 0 taken 167 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 167 times.
✗ Branch 3 not taken.
167 if (ptr && avpkt->size > 0) {
144 167 int ret = sami_paragraph_to_ass(avctx, ptr);
145
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 88 times.
167 if (ret < 0)
146 79 return ret;
147 // TODO: pass escaped sami->encoded_source.str as source
148 88 ret = ff_ass_add_rect(sub, sami->full.str, sami->readorder++, 0, NULL, NULL);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if (ret < 0)
150 return ret;
151 }
152 88 *got_sub_ptr = sub->num_rects > 0;
153 88 return avpkt->size;
154 }
155
156 4 static av_cold int sami_init(AVCodecContext *avctx)
157 {
158 4 SAMIContext *sami = avctx->priv_data;
159 4 av_bprint_init(&sami->source, 0, 2048);
160 4 av_bprint_init(&sami->content, 0, 2048);
161 4 av_bprint_init(&sami->encoded_source, 0, 2048);
162 4 av_bprint_init(&sami->encoded_content, 0, 2048);
163 4 av_bprint_init(&sami->full, 0, 2048);
164 4 return ff_ass_subtitle_header_default(avctx);
165 }
166
167 4 static av_cold int sami_close(AVCodecContext *avctx)
168 {
169 4 SAMIContext *sami = avctx->priv_data;
170 4 av_bprint_finalize(&sami->source, NULL);
171 4 av_bprint_finalize(&sami->content, NULL);
172 4 av_bprint_finalize(&sami->encoded_source, NULL);
173 4 av_bprint_finalize(&sami->encoded_content, NULL);
174 4 av_bprint_finalize(&sami->full, NULL);
175 4 return 0;
176 }
177
178 static av_cold void sami_flush(AVCodecContext *avctx)
179 {
180 SAMIContext *sami = avctx->priv_data;
181 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
182 sami->readorder = 0;
183 }
184
185 const FFCodec ff_sami_decoder = {
186 .p.name = "sami",
187 CODEC_LONG_NAME("SAMI subtitle"),
188 .p.type = AVMEDIA_TYPE_SUBTITLE,
189 .p.id = AV_CODEC_ID_SAMI,
190 .priv_data_size = sizeof(SAMIContext),
191 .init = sami_init,
192 .close = sami_close,
193 FF_CODEC_DECODE_SUB_CB(sami_decode_frame),
194 .flush = sami_flush,
195 };
196