FFmpeg
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/common.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]\r\n"
39  "; Script generated by FFmpeg/Lavc%s\r\n"
40  "ScriptType: v4.00+\r\n"
41  "PlayResX: %d\r\n"
42  "PlayResY: %d\r\n"
43  "ScaledBorderAndShadow: yes\r\n"
44  "\r\n"
45  "[V4+ Styles]\r\n"
46 
47  /* ASSv4 header */
48  "Format: Name, "
49  "Fontname, Fontsize, "
50  "PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
51  "Bold, Italic, Underline, StrikeOut, "
52  "ScaleX, ScaleY, "
53  "Spacing, Angle, "
54  "BorderStyle, Outline, Shadow, "
55  "Alignment, MarginL, MarginR, MarginV, "
56  "Encoding\r\n"
57 
58  "Style: "
59  "Default," /* Name */
60  "%s,%d," /* Font{name,size} */
61  "&H%x,&H%x,&H%x,&H%x," /* {Primary,Secondary,Outline,Back}Colour */
62  "%d,%d,%d,0," /* Bold, Italic, Underline, StrikeOut */
63  "100,100," /* Scale{X,Y} */
64  "0,0," /* Spacing, Angle */
65  "%d,1,0," /* BorderStyle, Outline, Shadow */
66  "%d,10,10,10," /* Alignment, Margin[LRV] */
67  "0\r\n" /* Encoding */
68 
69  "\r\n"
70  "[Events]\r\n"
71  "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
73  play_res_x, play_res_y, font, font_size,
74  primary_color, secondary_color, outline_color, back_color,
75  -bold, -italic, -underline, border_style, alignment);
76 
77  if (!avctx->subtitle_header)
78  return AVERROR(ENOMEM);
79  avctx->subtitle_header_size = strlen(avctx->subtitle_header);
80  return 0;
81 }
82 
84  const char *font, int font_size,
85  int color, int back_color,
86  int bold, int italic, int underline,
87  int border_style, int alignment)
88 {
89  return ff_ass_subtitle_header_full(avctx,
91  font, font_size, color, color,
92  back_color, back_color,
93  bold, italic, underline,
94  border_style, alignment);
95 }
96 
98 {
108 }
109 
110 char *ff_ass_get_dialog(int readorder, int layer, const char *style,
111  const char *speaker, const char *text)
112 {
113  return av_asprintf("%d,%d,%s,%s,0,0,0,,%s",
114  readorder, layer, style ? style : "Default",
115  speaker ? speaker : "", text);
116 }
117 
118 int ff_ass_add_rect2(AVSubtitle *sub, const char *dialog,
119  int readorder, int layer, const char *style,
120  const char *speaker, unsigned *nb_rect_allocated)
121 {
122  AVSubtitleRect **rects = sub->rects, *rect;
123  char *ass_str;
124  uint64_t new_nb = 0;
125 
126  if (sub->num_rects >= UINT_MAX)
127  return AVERROR(ENOMEM);
128 
129  if (nb_rect_allocated && *nb_rect_allocated <= sub->num_rects) {
130  if (sub->num_rects < UINT_MAX / 17 * 16) {
131  new_nb = sub->num_rects + sub->num_rects/16 + 1;
132  } else
133  new_nb = UINT_MAX;
134  } else if (!nb_rect_allocated)
135  new_nb = sub->num_rects + 1;
136 
137  if (new_nb) {
138  rects = av_realloc_array(rects, new_nb, sizeof(*sub->rects));
139  if (!rects)
140  return AVERROR(ENOMEM);
141  if (nb_rect_allocated)
142  *nb_rect_allocated = new_nb;
143  sub->rects = rects;
144  }
145 
146  rect = av_mallocz(sizeof(*rect));
147  if (!rect)
148  return AVERROR(ENOMEM);
149  rects[sub->num_rects++] = rect;
150  rect->type = SUBTITLE_ASS;
151  ass_str = ff_ass_get_dialog(readorder, layer, style, speaker, dialog);
152  if (!ass_str)
153  return AVERROR(ENOMEM);
154  rect->ass = ass_str;
155  return 0;
156 }
157 
158 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
159  int readorder, int layer, const char *style,
160  const char *speaker)
161 {
162  return ff_ass_add_rect2(sub, dialog, readorder, layer, style, speaker, NULL);
163 }
164 
166 {
167  FFASSDecoderContext *s = avctx->priv_data;
168  if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
169  s->readorder = 0;
170 }
171 
172 void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
173  const char *linebreaks, int keep_ass_markup)
174 {
175  const char *p_end = p + size;
176 
177  for (; p < p_end && *p; p++) {
178 
179  /* forced custom line breaks, not accounted as "normal" EOL */
180  if (linebreaks && strchr(linebreaks, *p)) {
181  av_bprintf(buf, "\\N");
182 
183  /* standard ASS escaping so random characters don't get mis-interpreted
184  * as ASS */
185  } else if (!keep_ass_markup && strchr("{}\\", *p)) {
186  av_bprintf(buf, "\\%c", *p);
187 
188  /* some packets might end abruptly (no \0 at the end, like for example
189  * in some cases of demuxing from a classic video container), some
190  * might be terminated with \n or \r\n which we have to remove (for
191  * consistency with those who haven't), and we also have to deal with
192  * evil cases such as \r at the end of the buffer (and no \0 terminated
193  * character) */
194  } else if (p[0] == '\n') {
195  /* some stuff left so we can insert a line break */
196  if (p < p_end - 1)
197  av_bprintf(buf, "\\N");
198  } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
199  /* \r followed by a \n, we can skip it. We don't insert the \N yet
200  * because we don't know if it is followed by more text */
201  continue;
202 
203  /* finally, a sane character */
204  } else {
205  av_bprint_chars(buf, *p, 1);
206  }
207  }
208 }
LIBAVCODEC_VERSION
#define LIBAVCODEC_VERSION
Definition: version.h:38
ff_ass_subtitle_header
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:83
AVSubtitle
Definition: avcodec.h:2308
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_ass_subtitle_header_default
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
Definition: ass.c:97
color
Definition: vf_paletteuse.c:601
sub
static float sub(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:31
rect
Definition: f_ebur128.c:76
AVSubtitleRect
Definition: avcodec.h:2280
av_asprintf
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:116
ASS_DEFAULT_ALIGNMENT
#define ASS_DEFAULT_ALIGNMENT
Definition: ass.h:42
ff_ass_add_rect
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:158
AVCodecContext::subtitle_header
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:1708
version.h
ASS_DEFAULT_BORDERSTYLE
#define ASS_DEFAULT_BORDERSTYLE
Definition: ass.h:43
SUBTITLE_ASS
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition: avcodec.h:2275
ff_ass_add_rect2
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:118
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:478
ass.h
ff_ass_get_dialog
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:110
ASS_DEFAULT_FONT
#define ASS_DEFAULT_FONT
Definition: ass.h:35
s
#define s(width, name)
Definition: cbs_vp9.c:256
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:225
ff_ass_bprint_text_event
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:172
ff_ass_subtitle_header_full
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
ASS_DEFAULT_BACK_COLOR
#define ASS_DEFAULT_BACK_COLOR
Definition: ass.h:38
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::subtitle_header_size
int subtitle_header_size
Definition: avcodec.h:1709
ASS_DEFAULT_PLAYRESY
#define ASS_DEFAULT_PLAYRESY
Definition: ass.h:29
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:485
ASS_DEFAULT_BOLD
#define ASS_DEFAULT_BOLD
Definition: ass.h:39
size
int size
Definition: twinvq_data.h:10344
ASS_DEFAULT_PLAYRESX
#define ASS_DEFAULT_PLAYRESX
Definition: ass.h:28
ASS_DEFAULT_UNDERLINE
#define ASS_DEFAULT_UNDERLINE
Definition: ass.h:41
ff_ass_decoder_flush
void ff_ass_decoder_flush(AVCodecContext *avctx)
Helper to flush a text subtitles decoder making use of the FFASSDecoderContext.
Definition: ass.c:165
bprint.h
common.h
AV_STRINGIFY
#define AV_STRINGIFY(s)
Definition: macros.h:66
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:264
ASS_DEFAULT_ITALIC
#define ASS_DEFAULT_ITALIC
Definition: ass.h:40
ASS_DEFAULT_COLOR
#define ASS_DEFAULT_COLOR
Definition: ass.h:37
avcodec.h
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
ASS_DEFAULT_FONT_SIZE
#define ASS_DEFAULT_FONT_SIZE
Definition: ass.h:36
AVCodecContext
main external API structure.
Definition: avcodec.h:398
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:288
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
FFASSDecoderContext
Definition: ass.h:46
av_bprint_chars
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140
avstring.h
AV_CODEC_FLAG2_RO_FLUSH_NOOP
#define AV_CODEC_FLAG2_RO_FLUSH_NOOP
Do not reset ASS ReadOrder field on flush (subtitles decoding)
Definition: avcodec.h:343