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