FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ass.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 36 46 78.3%
Branches: 22 38 57.9%

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/common.h"
27
28 83 int ff_ass_subtitle_header_full(AVCodecContext *avctx,
29 int play_res_x, int play_res_y,
30 const char *font, int font_size,
31 int primary_color, int secondary_color,
32 int outline_color, int back_color,
33 int bold, int italic, int underline,
34 int border_style, int alignment)
35 {
36 83 avctx->subtitle_header = av_asprintf(
37 "[Script Info]\r\n"
38 "; Script generated by FFmpeg/Lavc%s\r\n"
39 "ScriptType: v4.00+\r\n"
40 "PlayResX: %d\r\n"
41 "PlayResY: %d\r\n"
42 "ScaledBorderAndShadow: yes\r\n"
43 "\r\n"
44 "[V4+ Styles]\r\n"
45
46 /* ASSv4 header */
47 "Format: Name, "
48 "Fontname, Fontsize, "
49 "PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
50 "Bold, Italic, Underline, StrikeOut, "
51 "ScaleX, ScaleY, "
52 "Spacing, Angle, "
53 "BorderStyle, Outline, Shadow, "
54 "Alignment, MarginL, MarginR, MarginV, "
55 "Encoding\r\n"
56
57 "Style: "
58 "Default," /* Name */
59 "%s,%d," /* Font{name,size} */
60 "&H%x,&H%x,&H%x,&H%x," /* {Primary,Secondary,Outline,Back}Colour */
61 "%d,%d,%d,0," /* Bold, Italic, Underline, StrikeOut */
62 "100,100," /* Scale{X,Y} */
63 "0,0," /* Spacing, Angle */
64 "%d,1,0," /* BorderStyle, Outline, Shadow */
65 "%d,10,10,10," /* Alignment, Margin[LRV] */
66 "0\r\n" /* Encoding */
67
68 "\r\n"
69 "[Events]\r\n"
70 "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
71
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 39 times.
83 !(avctx->flags & AV_CODEC_FLAG_BITEXACT) ? AV_STRINGIFY(LIBAVCODEC_VERSION) : "",
72 play_res_x, play_res_y, font, font_size,
73 primary_color, secondary_color, outline_color, back_color,
74 -bold, -italic, -underline, border_style, alignment);
75
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!avctx->subtitle_header)
77 return AVERROR(ENOMEM);
78 83 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
79 83 return 0;
80 }
81
82 77 int ff_ass_subtitle_header(AVCodecContext *avctx,
83 const char *font, int font_size,
84 int color, int back_color,
85 int bold, int italic, int underline,
86 int border_style, int alignment)
87 {
88 77 return ff_ass_subtitle_header_full(avctx,
89 ASS_DEFAULT_PLAYRESX, ASS_DEFAULT_PLAYRESY,
90 font, font_size, color, color,
91 back_color, back_color,
92 bold, italic, underline,
93 border_style, alignment);
94 }
95
96 64 int ff_ass_subtitle_header_default(AVCodecContext *avctx)
97 {
98 64 return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
99 ASS_DEFAULT_FONT_SIZE,
100 ASS_DEFAULT_COLOR,
101 ASS_DEFAULT_BACK_COLOR,
102 ASS_DEFAULT_BOLD,
103 ASS_DEFAULT_ITALIC,
104 ASS_DEFAULT_UNDERLINE,
105 ASS_DEFAULT_BORDERSTYLE,
106 ASS_DEFAULT_ALIGNMENT);
107 }
108
109 633 char *ff_ass_get_dialog(int readorder, int layer, const char *style,
110 const char *speaker, const char *text)
111 {
112
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 633 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 633 times.
633 return av_asprintf("%d,%d,%s,%s,0,0,0,,%s",
113 readorder, layer, style ? style : "Default",
114 speaker ? speaker : "", text);
115 }
116
117 633 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
118 int readorder, int layer, const char *style,
119 const char *speaker)
120 {
121 char *ass_str;
122 AVSubtitleRect **rects;
123
124 633 rects = av_realloc_array(sub->rects, sub->num_rects+1, sizeof(*sub->rects));
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 633 times.
633 if (!rects)
126 return AVERROR(ENOMEM);
127 633 sub->rects = rects;
128 633 rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 633 times.
633 if (!rects[sub->num_rects])
130 return AVERROR(ENOMEM);
131 633 rects[sub->num_rects]->type = SUBTITLE_ASS;
132 633 ass_str = ff_ass_get_dialog(readorder, layer, style, speaker, dialog);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 633 times.
633 if (!ass_str)
134 return AVERROR(ENOMEM);
135 633 rects[sub->num_rects]->ass = ass_str;
136 633 sub->num_rects++;
137 633 return 0;
138 }
139
140 void ff_ass_decoder_flush(AVCodecContext *avctx)
141 {
142 FFASSDecoderContext *s = avctx->priv_data;
143 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
144 s->readorder = 0;
145 }
146
147 89 void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
148 const char *linebreaks, int keep_ass_markup)
149 {
150 89 const char *p_end = p + size;
151
152
4/4
✓ Branch 0 taken 3170 times.
✓ Branch 1 taken 79 times.
✓ Branch 2 taken 3160 times.
✓ Branch 3 taken 10 times.
3249 for (; p < p_end && *p; p++) {
153
154 /* forced custom line breaks, not accounted as "normal" EOL */
155
4/4
✓ Branch 0 taken 1339 times.
✓ Branch 1 taken 1821 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 1321 times.
3160 if (linebreaks && strchr(linebreaks, *p)) {
156 18 av_bprintf(buf, "\\N");
157
158 /* standard ASS escaping so random characters don't get mis-interpreted
159 * as ASS */
160
2/4
✓ Branch 0 taken 3142 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3142 times.
3142 } else if (!keep_ass_markup && strchr("{}\\", *p)) {
161 av_bprintf(buf, "\\%c", *p);
162
163 /* some packets might end abruptly (no \0 at the end, like for example
164 * in some cases of demuxing from a classic video container), some
165 * might be terminated with \n or \r\n which we have to remove (for
166 * consistency with those who haven't), and we also have to deal with
167 * evil cases such as \r at the end of the buffer (and no \0 terminated
168 * character) */
169
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 3107 times.
3142 } else if (p[0] == '\n') {
170 /* some stuff left so we can insert a line break */
171
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 if (p < p_end - 1)
172 35 av_bprintf(buf, "\\N");
173
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3107 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3107 } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
174 /* \r followed by a \n, we can skip it. We don't insert the \N yet
175 * because we don't know if it is followed by more text */
176 continue;
177
178 /* finally, a sane character */
179 } else {
180 3107 av_bprint_chars(buf, *p, 1);
181 }
182 }
183 89 }
184