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