FFmpeg
Loading...
Searching...
No Matches
ass.c
Go to the documentation of this file.
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
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{
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",
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 if (!avctx->subtitle_header)
79 return AVERROR(ENOMEM);
80 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
81 return 0;
82}
83
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 return ff_ass_subtitle_header_full(avctx,
92 font, font_size, color, color,
93 back_color, back_color,
94 bold, italic, underline,
95 border_style, alignment);
96}
97
110
111char *ff_ass_get_dialog(int readorder, int layer, const char *style,
112 const char *speaker, const char *text)
113{
114 return av_asprintf("%d,%d,%s,%s,0,0,0,,%s",
115 readorder, layer, style ? style : "Default",
116 speaker ? speaker : "", text);
117}
118
119int 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 AVSubtitleRect **rects = sub->rects, *rect;
124 char *ass_str;
125 uint64_t new_nb = 0;
126
127 if (sub->num_rects >= UINT_MAX)
128 return AVERROR(ENOMEM);
129
130 if (nb_rect_allocated && *nb_rect_allocated <= sub->num_rects) {
131 if (sub->num_rects < UINT_MAX / 17 * 16) {
132 new_nb = sub->num_rects + sub->num_rects/16 + 1;
133 } else
134 new_nb = UINT_MAX;
135 } else if (!nb_rect_allocated)
136 new_nb = sub->num_rects + 1;
137
138 if (new_nb) {
139 rects = av_realloc_array(rects, new_nb, sizeof(*sub->rects));
140 if (!rects)
141 return AVERROR(ENOMEM);
142 if (nb_rect_allocated)
143 *nb_rect_allocated = new_nb;
144 sub->rects = rects;
145 }
146
147 rect = av_mallocz(sizeof(*rect));
148 if (!rect)
149 return AVERROR(ENOMEM);
150 rects[sub->num_rects++] = rect;
151 rect->type = SUBTITLE_ASS;
152 ass_str = ff_ass_get_dialog(readorder, layer, style, speaker, dialog);
153 if (!ass_str)
154 return AVERROR(ENOMEM);
155 rect->ass = ass_str;
156 return 0;
157}
158
159int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
160 int readorder, int layer, const char *style,
161 const char *speaker)
162{
163 return ff_ass_add_rect2(sub, dialog, readorder, layer, style, speaker, NULL);
164}
165
167{
169 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
170 s->readorder = 0;
171}
172
173void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
174 const char *linebreaks, int keep_ass_markup)
175{
176 const char *p_end = p + size;
177
178 for (; p < p_end && *p; p++) {
179
180 /* forced custom line breaks, not accounted as "normal" EOL */
181 if (linebreaks && strchr(linebreaks, *p)) {
182 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 } 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 } 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 } else if (p[0] == '\n') {
207 /* some stuff left so we can insert a line break */
208 if (p < p_end - 1)
209 av_bprintf(buf, "\\N");
210 } 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 av_bprint_chars(buf, *p, 1);
218 }
219 }
220}
av_cold void ff_ass_decoder_flush(AVCodecContext *avctx)
Helper to flush a text subtitles decoder making use of the FFASSDecoderContext.
Definition ass.c:166
int ff_ass_subtitle_header_full(AVCodecContext *avctx, int play_res_x, int play_res_y, const char *font, int font_size, int primary_color, int secondary_color, int outline_color, int back_color, int bold, int italic, int underline, int border_style, int alignment)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS.
Definition ass.c:29
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
Definition ass.c:98
int ff_ass_subtitle_header(AVCodecContext *avctx, const char *font, int font_size, int color, int back_color, int bold, int italic, int underline, int border_style, int alignment)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS.
Definition ass.c:84
char * ff_ass_get_dialog(int readorder, int layer, const char *style, const char *speaker, const char *text)
Craft an ASS dialog string.
Definition ass.c:111
int ff_ass_add_rect2(AVSubtitle *sub, const char *dialog, int readorder, int layer, const char *style, const char *speaker, unsigned *nb_rect_allocated)
Add an ASS dialog to a subtitle.
Definition ass.c:119
void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size, const char *linebreaks, int keep_ass_markup)
Escape a text subtitle using ASS syntax into an AVBPrint buffer.
Definition ass.c:173
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int readorder, int layer, const char *style, const char *speaker)
Add an ASS dialog to a subtitle.
Definition ass.c:159
#define ASS_DEFAULT_PLAYRESY
Definition ass.h:29
#define ASS_DEFAULT_ALIGNMENT
Definition ass.h:42
#define ASS_DEFAULT_PLAYRESX
Definition ass.h:28
#define ASS_DEFAULT_FONT_SIZE
Definition ass.h:36
#define ASS_DEFAULT_COLOR
Definition ass.h:37
#define ASS_DEFAULT_BORDERSTYLE
Definition ass.h:43
#define ASS_DEFAULT_BACK_COLOR
Definition ass.h:38
#define ASS_DEFAULT_FONT
Definition ass.h:35
#define ASS_DEFAULT_BOLD
Definition ass.h:39
#define ASS_DEFAULT_UNDERLINE
Definition ass.h:41
#define ASS_DEFAULT_ITALIC
Definition ass.h:40
Libavcodec external API header.
char * av_asprintf(const char *fmt,...)
Definition avstring.c:115
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:122
AVBPrint public header.
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
#define AV_CODEC_FLAG2_RO_FLUSH_NOOP
Do not reset ASS ReadOrder field on flush (subtitles decoding)
Definition avcodec.h:376
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition avcodec.h:2055
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition bprint.c:130
#define AVERROR(e)
Definition error.h:45
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:217
#define AV_STRINGIFY(s)
Definition macros.h:66
Libavcodec version macros.
#define LIBAVCODEC_VERSION
Definition version.h:38
#define av_cold
Definition attributes.h:117
Memory handling functions.
main external API structure.
Definition avcodec.h:443
int subtitle_header_size
Header containing style information for text subtitles.
Definition avcodec.h:1743
int flags2
AV_CODEC_FLAG2_*.
Definition avcodec.h:507
uint8_t * subtitle_header
Definition avcodec.h:1744
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
unsigned num_rects
Definition avcodec.h:2091
AVSubtitleRect ** rects
Definition avcodec.h:2092
#define av_mallocz(s)
int size