Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * SSA/ASS common functions | ||
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 "avcodec.h" | ||
23 | #include "ass.h" | ||
24 | #include "libavutil/avstring.h" | ||
25 | #include "libavutil/bprint.h" | ||
26 | #include "libavutil/mem.h" | ||
27 | #include "version.h" | ||
28 | |||
29 | 93 | int ff_ass_subtitle_header_full(AVCodecContext *avctx, | |
30 | int play_res_x, int play_res_y, | ||
31 | const char *font, int font_size, | ||
32 | int primary_color, int secondary_color, | ||
33 | int outline_color, int back_color, | ||
34 | int bold, int italic, int underline, | ||
35 | int border_style, int alignment) | ||
36 | { | ||
37 | 93 | avctx->subtitle_header = av_asprintf( | |
38 | "[Script Info]\n" | ||
39 | "; Script generated by FFmpeg/Lavc%s\n" | ||
40 | "ScriptType: v4.00+\n" | ||
41 | "PlayResX: %d\n" | ||
42 | "PlayResY: %d\n" | ||
43 | "ScaledBorderAndShadow: yes\n" | ||
44 | "YCbCr Matrix: None\n" | ||
45 | "\n" | ||
46 | "[V4+ Styles]\n" | ||
47 | |||
48 | /* ASS (v4+) header */ | ||
49 | "Format: Name, " | ||
50 | "Fontname, Fontsize, " | ||
51 | "PrimaryColour, SecondaryColour, OutlineColour, BackColour, " | ||
52 | "Bold, Italic, Underline, StrikeOut, " | ||
53 | "ScaleX, ScaleY, " | ||
54 | "Spacing, Angle, " | ||
55 | "BorderStyle, Outline, Shadow, " | ||
56 | "Alignment, MarginL, MarginR, MarginV, " | ||
57 | "Encoding\n" | ||
58 | |||
59 | "Style: " | ||
60 | "Default," /* Name */ | ||
61 | "%s,%d," /* Font{name,size} */ | ||
62 | "&H%x,&H%x,&H%x,&H%x," /* {Primary,Secondary,Outline,Back}Colour */ | ||
63 | "%d,%d,%d,0," /* Bold, Italic, Underline, StrikeOut */ | ||
64 | "100,100," /* Scale{X,Y} */ | ||
65 | "0,0," /* Spacing, Angle */ | ||
66 | "%d,1,0," /* BorderStyle, Outline, Shadow */ | ||
67 | "%d,10,10,10," /* Alignment, Margin[LRV] */ | ||
68 | "1\n" /* Encoding */ | ||
69 | |||
70 | "\n" | ||
71 | "[Events]\n" | ||
72 | "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n", | ||
73 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 44 times.
|
93 | !(avctx->flags & AV_CODEC_FLAG_BITEXACT) ? AV_STRINGIFY(LIBAVCODEC_VERSION) : "", |
74 | play_res_x, play_res_y, font, font_size, | ||
75 | primary_color, secondary_color, outline_color, back_color, | ||
76 | -bold, -italic, -underline, border_style, alignment); | ||
77 | |||
78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (!avctx->subtitle_header) |
79 | ✗ | return AVERROR(ENOMEM); | |
80 | 93 | avctx->subtitle_header_size = strlen(avctx->subtitle_header); | |
81 | 93 | return 0; | |
82 | } | ||
83 | |||
84 | 87 | int ff_ass_subtitle_header(AVCodecContext *avctx, | |
85 | const char *font, int font_size, | ||
86 | int color, int back_color, | ||
87 | int bold, int italic, int underline, | ||
88 | int border_style, int alignment) | ||
89 | { | ||
90 | 87 | return ff_ass_subtitle_header_full(avctx, | |
91 | ASS_DEFAULT_PLAYRESX, ASS_DEFAULT_PLAYRESY, | ||
92 | font, font_size, color, color, | ||
93 | back_color, back_color, | ||
94 | bold, italic, underline, | ||
95 | border_style, alignment); | ||
96 | } | ||
97 | |||
98 | 68 | int ff_ass_subtitle_header_default(AVCodecContext *avctx) | |
99 | { | ||
100 | 68 | return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT, | |
101 | ASS_DEFAULT_FONT_SIZE, | ||
102 | ASS_DEFAULT_COLOR, | ||
103 | ASS_DEFAULT_BACK_COLOR, | ||
104 | ASS_DEFAULT_BOLD, | ||
105 | ASS_DEFAULT_ITALIC, | ||
106 | ASS_DEFAULT_UNDERLINE, | ||
107 | ASS_DEFAULT_BORDERSTYLE, | ||
108 | ASS_DEFAULT_ALIGNMENT); | ||
109 | } | ||
110 | |||
111 | 692 | char *ff_ass_get_dialog(int readorder, int layer, const char *style, | |
112 | const char *speaker, const char *text) | ||
113 | { | ||
114 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 692 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 692 times.
|
692 | return av_asprintf("%d,%d,%s,%s,0,0,0,,%s", |
115 | readorder, layer, style ? style : "Default", | ||
116 | speaker ? speaker : "", text); | ||
117 | } | ||
118 | |||
119 | 692 | int ff_ass_add_rect2(AVSubtitle *sub, const char *dialog, | |
120 | int readorder, int layer, const char *style, | ||
121 | const char *speaker, unsigned *nb_rect_allocated) | ||
122 | { | ||
123 | 692 | AVSubtitleRect **rects = sub->rects, *rect; | |
124 | char *ass_str; | ||
125 | 692 | uint64_t new_nb = 0; | |
126 | |||
127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 692 times.
|
692 | if (sub->num_rects >= UINT_MAX) |
128 | ✗ | return AVERROR(ENOMEM); | |
129 | |||
130 |
3/4✓ Branch 0 taken 121 times.
✓ Branch 1 taken 571 times.
✓ Branch 2 taken 121 times.
✗ Branch 3 not taken.
|
692 | if (nb_rect_allocated && *nb_rect_allocated <= sub->num_rects) { |
131 |
1/2✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
|
121 | if (sub->num_rects < UINT_MAX / 17 * 16) { |
132 | 121 | new_nb = sub->num_rects + sub->num_rects/16 + 1; | |
133 | } else | ||
134 | ✗ | new_nb = UINT_MAX; | |
135 |
1/2✓ Branch 0 taken 571 times.
✗ Branch 1 not taken.
|
571 | } else if (!nb_rect_allocated) |
136 | 571 | new_nb = sub->num_rects + 1; | |
137 | |||
138 |
1/2✓ Branch 0 taken 692 times.
✗ Branch 1 not taken.
|
692 | if (new_nb) { |
139 | 692 | rects = av_realloc_array(rects, new_nb, sizeof(*sub->rects)); | |
140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 692 times.
|
692 | if (!rects) |
141 | ✗ | return AVERROR(ENOMEM); | |
142 |
2/2✓ Branch 0 taken 121 times.
✓ Branch 1 taken 571 times.
|
692 | if (nb_rect_allocated) |
143 | 121 | *nb_rect_allocated = new_nb; | |
144 | 692 | sub->rects = rects; | |
145 | } | ||
146 | |||
147 | 692 | rect = av_mallocz(sizeof(*rect)); | |
148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 692 times.
|
692 | if (!rect) |
149 | ✗ | return AVERROR(ENOMEM); | |
150 | 692 | rects[sub->num_rects++] = rect; | |
151 | 692 | rect->type = SUBTITLE_ASS; | |
152 | 692 | ass_str = ff_ass_get_dialog(readorder, layer, style, speaker, dialog); | |
153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 692 times.
|
692 | if (!ass_str) |
154 | ✗ | return AVERROR(ENOMEM); | |
155 | 692 | rect->ass = ass_str; | |
156 | 692 | return 0; | |
157 | } | ||
158 | |||
159 | 571 | int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, | |
160 | int readorder, int layer, const char *style, | ||
161 | const char *speaker) | ||
162 | { | ||
163 | 571 | return ff_ass_add_rect2(sub, dialog, readorder, layer, style, speaker, NULL); | |
164 | } | ||
165 | |||
166 | ✗ | void ff_ass_decoder_flush(AVCodecContext *avctx) | |
167 | { | ||
168 | ✗ | FFASSDecoderContext *s = avctx->priv_data; | |
169 | ✗ | if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP)) | |
170 | ✗ | s->readorder = 0; | |
171 | ✗ | } | |
172 | |||
173 | 132 | void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size, | |
174 | const char *linebreaks, int keep_ass_markup) | ||
175 | { | ||
176 | 132 | const char *p_end = p + size; | |
177 | |||
178 |
4/4✓ Branch 0 taken 4554 times.
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 4544 times.
✓ Branch 3 taken 10 times.
|
4676 | for (; p < p_end && *p; p++) { |
179 | |||
180 | /* forced custom line breaks, not accounted as "normal" EOL */ | ||
181 |
4/4✓ Branch 0 taken 1339 times.
✓ Branch 1 taken 3205 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 1321 times.
|
4544 | if (linebreaks && strchr(linebreaks, *p)) { |
182 | 18 | av_bprintf(buf, "\\N"); | |
183 | |||
184 | /* cancel curly brackets to avoid bogus override tag blocks | ||
185 | * hiding text. Standard ASS has no character escapes, | ||
186 | * though (only) libass provides \{ and \}. | ||
187 | * Unpaired closing brackets don't need escaping at all though and | ||
188 | * to make the situation less bad in standard ASS insert an empty block | ||
189 | */ | ||
190 |
2/4✓ Branch 0 taken 4526 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4526 times.
|
4526 | } else if (!keep_ass_markup && *p == '{') { |
191 | ✗ | av_bprintf(buf, "\\{{}"); | |
192 | |||
193 | /* append word-joiner U+2060 as UTF-8 to break up sequences like \N */ | ||
194 |
2/4✓ Branch 0 taken 4526 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4526 times.
|
4526 | } else if (!keep_ass_markup && *p == '\\') { |
195 | ✗ | if (p_end - p <= 3 || strncmp(p + 1, "\xe2\x81\xa0", 3)) | |
196 | ✗ | av_bprintf(buf, "\\\xe2\x81\xa0"); | |
197 | else | ||
198 | ✗ | av_bprintf(buf, "\\"); | |
199 | |||
200 | /* some packets might end abruptly (no \0 at the end, like for example | ||
201 | * in some cases of demuxing from a classic video container), some | ||
202 | * might be terminated with \n or \r\n which we have to remove (for | ||
203 | * consistency with those who haven't), and we also have to deal with | ||
204 | * evil cases such as \r at the end of the buffer (and no \0 terminated | ||
205 | * character) */ | ||
206 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 4448 times.
|
4526 | } else if (p[0] == '\n') { |
207 | /* some stuff left so we can insert a line break */ | ||
208 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 43 times.
|
78 | if (p < p_end - 1) |
209 | 35 | av_bprintf(buf, "\\N"); | |
210 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 4448 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
4448 | } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') { |
211 | /* \r followed by a \n, we can skip it. We don't insert the \N yet | ||
212 | * because we don't know if it is followed by more text */ | ||
213 | ✗ | continue; | |
214 | |||
215 | /* finally, a sane character */ | ||
216 | } else { | ||
217 | 4448 | av_bprint_chars(buf, *p, 1); | |
218 | } | ||
219 | } | ||
220 | 132 | } | |
221 |