FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/srtenc.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 82 138 59.4%
Branches: 29 76 38.2%

Line Branch Exec Source
1 /*
2 * SubRip subtitle encoder
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdarg.h>
23 #include "avcodec.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/bprint.h"
26 #include "ass_split.h"
27 #include "ass.h"
28 #include "internal.h"
29
30
31 #define SRT_STACK_SIZE 64
32
33 typedef struct {
34 AVCodecContext *avctx;
35 ASSSplitContext *ass_ctx;
36 AVBPrint buffer;
37 char stack[SRT_STACK_SIZE];
38 int stack_ptr;
39 int alignment_applied;
40 } SRTContext;
41
42
43 #ifdef __GNUC__
44 __attribute__ ((__format__ (__printf__, 2, 3)))
45 #endif
46 82 static void srt_print(SRTContext *s, const char *str, ...)
47 {
48 va_list vargs;
49 82 va_start(vargs, str);
50 82 av_vbprintf(&s->buffer, str, vargs);
51 82 va_end(vargs);
52 82 }
53
54 3 static int srt_stack_push(SRTContext *s, const char c)
55 {
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->stack_ptr >= SRT_STACK_SIZE)
57 return -1;
58 3 s->stack[s->stack_ptr++] = c;
59 3 return 0;
60 }
61
62 3 static char srt_stack_pop(SRTContext *s)
63 {
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->stack_ptr <= 0)
65 return 0;
66 3 return s->stack[--s->stack_ptr];
67 }
68
69 static int srt_stack_find(SRTContext *s, const char c)
70 {
71 int i;
72 for (i = s->stack_ptr-1; i >= 0; i--)
73 if (s->stack[i] == c)
74 break;
75 return i;
76 }
77
78 3 static void srt_close_tag(SRTContext *s, char tag)
79 {
80
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 srt_print(s, "</%c%s>", tag, tag == 'f' ? "ont" : "");
81 3 }
82
83 3 static void srt_stack_push_pop(SRTContext *s, const char c, int close)
84 {
85
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (close) {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 int i = c ? srt_stack_find(s, c) : 0;
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (i < 0)
88 return;
89
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 while (s->stack_ptr != i)
90 3 srt_close_tag(s, srt_stack_pop(s));
91 } else if (srt_stack_push(s, c) < 0)
92 av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
93 }
94
95 3 static void srt_style_apply(SRTContext *s, const char *style)
96 {
97 3 ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
98
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (st) {
99 3 int c = st->primary_color & 0xFFFFFF;
100
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT) ||
101 st->font_size != ASS_DEFAULT_FONT_SIZE ||
102 c != ASS_DEFAULT_COLOR) {
103 3 srt_print(s, "<font");
104
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT))
105 3 srt_print(s, " face=\"%s\"", st->font_name);
106
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (st->font_size != ASS_DEFAULT_FONT_SIZE)
107 3 srt_print(s, " size=\"%d\"", st->font_size);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (c != ASS_DEFAULT_COLOR)
109 srt_print(s, " color=\"#%06x\"",
110 (c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
111 3 srt_print(s, ">");
112 3 srt_stack_push(s, 'f');
113 }
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st->bold != ASS_DEFAULT_BOLD) {
115 srt_print(s, "<b>");
116 srt_stack_push(s, 'b');
117 }
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st->italic != ASS_DEFAULT_ITALIC) {
119 srt_print(s, "<i>");
120 srt_stack_push(s, 'i');
121 }
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st->underline != ASS_DEFAULT_UNDERLINE) {
123 srt_print(s, "<u>");
124 srt_stack_push(s, 'u');
125 }
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st->alignment != ASS_DEFAULT_ALIGNMENT) {
127 srt_print(s, "{\\an%d}", st->alignment);
128 s->alignment_applied = 1;
129 }
130 }
131 3 }
132
133
134 2 static av_cold int srt_encode_init(AVCodecContext *avctx)
135 {
136 2 SRTContext *s = avctx->priv_data;
137 2 s->avctx = avctx;
138 2 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
139 2 av_bprint_init(&s->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
140
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
141 }
142
143 107 static void srt_text_cb(void *priv, const char *text, int len)
144 {
145 107 SRTContext *s = priv;
146 107 av_bprint_append_data(&s->buffer, text, len);
147 107 }
148
149 67 static void srt_new_line_cb(void *priv, int forced)
150 {
151 67 srt_print(priv, "\r\n");
152 67 }
153
154 static void srt_style_cb(void *priv, char style, int close)
155 {
156 srt_stack_push_pop(priv, style, close);
157 if (!close)
158 srt_print(priv, "<%c>", style);
159 }
160
161 static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
162 {
163 if (color_id > 1)
164 return;
165 srt_stack_push_pop(priv, 'f', color == 0xFFFFFFFF);
166 if (color != 0xFFFFFFFF)
167 srt_print(priv, "<font color=\"#%06x\">",
168 (color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
169 }
170
171 static void srt_font_name_cb(void *priv, const char *name)
172 {
173 srt_stack_push_pop(priv, 'f', !name);
174 if (name)
175 srt_print(priv, "<font face=\"%s\">", name);
176 }
177
178 static void srt_font_size_cb(void *priv, int size)
179 {
180 srt_stack_push_pop(priv, 'f', size < 0);
181 if (size >= 0)
182 srt_print(priv, "<font size=\"%d\">", size);
183 }
184
185 static void srt_alignment_cb(void *priv, int alignment)
186 {
187 SRTContext *s = priv;
188 if (!s->alignment_applied && alignment >= 0) {
189 srt_print(s, "{\\an%d}", alignment);
190 s->alignment_applied = 1;
191 }
192 }
193
194 static void srt_cancel_overrides_cb(void *priv, const char *style)
195 {
196 srt_stack_push_pop(priv, 0, 1);
197 srt_style_apply(priv, style);
198 }
199
200 static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2,
201 int t1, int t2)
202 {
203 // TODO: add a AV_PKT_DATA_SUBTITLE_POSITION side data when a new subtitles
204 // encoding API passing the AVPacket is available.
205 }
206
207 3 static void srt_end_cb(void *priv)
208 {
209 3 srt_stack_push_pop(priv, 0, 1);
210 3 }
211
212 static const ASSCodesCallbacks srt_callbacks = {
213 .text = srt_text_cb,
214 .new_line = srt_new_line_cb,
215 .style = srt_style_cb,
216 .color = srt_color_cb,
217 .font_name = srt_font_name_cb,
218 .font_size = srt_font_size_cb,
219 .alignment = srt_alignment_cb,
220 .cancel_overrides = srt_cancel_overrides_cb,
221 .move = srt_move_cb,
222 .end = srt_end_cb,
223 };
224
225 static const ASSCodesCallbacks text_callbacks = {
226 .text = srt_text_cb,
227 .new_line = srt_new_line_cb,
228 };
229
230 40 static int encode_frame(AVCodecContext *avctx,
231 unsigned char *buf, int bufsize, const AVSubtitle *sub,
232 const ASSCodesCallbacks *cb)
233 {
234 40 SRTContext *s = avctx->priv_data;
235 ASSDialog *dialog;
236 int i;
237
238 40 av_bprint_clear(&s->buffer);
239
240
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 40 times.
80 for (i=0; i<sub->num_rects; i++) {
241 40 const char *ass = sub->rects[i]->ass;
242
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (sub->rects[i]->type != SUBTITLE_ASS) {
244 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
245 return AVERROR(EINVAL);
246 }
247
248 40 dialog = ff_ass_split_dialog(s->ass_ctx, ass);
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (!dialog)
250 return AVERROR(ENOMEM);
251 40 s->alignment_applied = 0;
252
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 37 times.
40 if (avctx->codec_id == AV_CODEC_ID_SUBRIP)
253 3 srt_style_apply(s, dialog->style);
254 40 ff_ass_split_override_codes(cb, s, dialog->text);
255 40 ff_ass_free_dialog(&dialog);
256 }
257
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
40 if (!av_bprint_is_complete(&s->buffer))
259 return AVERROR(ENOMEM);
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (!s->buffer.len)
261 return 0;
262
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (s->buffer.len > bufsize) {
264 av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
265 return AVERROR_BUFFER_TOO_SMALL;
266 }
267 40 memcpy(buf, s->buffer.str, s->buffer.len);
268
269 40 return s->buffer.len;
270 }
271
272 3 static int srt_encode_frame(AVCodecContext *avctx,
273 unsigned char *buf, int bufsize, const AVSubtitle *sub)
274 {
275 3 return encode_frame(avctx, buf, bufsize, sub, &srt_callbacks);
276 }
277
278 37 static int text_encode_frame(AVCodecContext *avctx,
279 unsigned char *buf, int bufsize, const AVSubtitle *sub)
280 {
281 37 return encode_frame(avctx, buf, bufsize, sub, &text_callbacks);
282 }
283
284 2 static int srt_encode_close(AVCodecContext *avctx)
285 {
286 2 SRTContext *s = avctx->priv_data;
287 2 ff_ass_split_free(s->ass_ctx);
288 2 av_bprint_finalize(&s->buffer, NULL);
289 2 return 0;
290 }
291
292 #if CONFIG_SRT_ENCODER
293 /* deprecated encoder */
294 const AVCodec ff_srt_encoder = {
295 .name = "srt",
296 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
297 .type = AVMEDIA_TYPE_SUBTITLE,
298 .id = AV_CODEC_ID_SUBRIP,
299 .priv_data_size = sizeof(SRTContext),
300 .init = srt_encode_init,
301 .encode_sub = srt_encode_frame,
302 .close = srt_encode_close,
303 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
304 };
305 #endif
306
307 #if CONFIG_SUBRIP_ENCODER
308 const AVCodec ff_subrip_encoder = {
309 .name = "subrip",
310 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
311 .type = AVMEDIA_TYPE_SUBTITLE,
312 .id = AV_CODEC_ID_SUBRIP,
313 .priv_data_size = sizeof(SRTContext),
314 .init = srt_encode_init,
315 .encode_sub = srt_encode_frame,
316 .close = srt_encode_close,
317 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
318 };
319 #endif
320
321 #if CONFIG_TEXT_ENCODER
322 const AVCodec ff_text_encoder = {
323 .name = "text",
324 .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
325 .type = AVMEDIA_TYPE_SUBTITLE,
326 .id = AV_CODEC_ID_TEXT,
327 .priv_data_size = sizeof(SRTContext),
328 .init = srt_encode_init,
329 .encode_sub = text_encode_frame,
330 .close = srt_encode_close,
331 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
332 };
333 #endif
334