FFmpeg
srtenc.c
Go to the documentation of this file.
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 <stdarg.h>
23 #include "avcodec.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/bprint.h"
26 #include "ass_split.h"
27 #include "ass.h"
28 #include "internal.h"
29 
30 
31 #define SRT_STACK_SIZE 64
32 
33 typedef struct {
36  AVBPrint buffer;
37  char stack[SRT_STACK_SIZE];
38  int stack_ptr;
40 } SRTContext;
41 
42 
43 #ifdef __GNUC__
44 __attribute__ ((__format__ (__printf__, 2, 3)))
45 #endif
46 static void srt_print(SRTContext *s, const char *str, ...)
47 {
48  va_list vargs;
49  va_start(vargs, str);
50  av_vbprintf(&s->buffer, str, vargs);
51  va_end(vargs);
52 }
53 
54 static int srt_stack_push(SRTContext *s, const char c)
55 {
56  if (s->stack_ptr >= SRT_STACK_SIZE)
57  return -1;
58  s->stack[s->stack_ptr++] = c;
59  return 0;
60 }
61 
62 static char srt_stack_pop(SRTContext *s)
63 {
64  if (s->stack_ptr <= 0)
65  return 0;
66  return s->stack[--s->stack_ptr];
67 }
68 
69 static int srt_stack_find(SRTContext *s, const char c)
70 {
71  int i;
72  for (i = s->stack_ptr-1; i >= 0; i--)
73  if (s->stack[i] == c)
74  break;
75  return i;
76 }
77 
78 static void srt_close_tag(SRTContext *s, char tag)
79 {
80  srt_print(s, "</%c%s>", tag, tag == 'f' ? "ont" : "");
81 }
82 
83 static void srt_stack_push_pop(SRTContext *s, const char c, int close)
84 {
85  if (close) {
86  int i = c ? srt_stack_find(s, c) : 0;
87  if (i < 0)
88  return;
89  while (s->stack_ptr != i)
91  } else if (srt_stack_push(s, c) < 0)
92  av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
93 }
94 
95 static void srt_style_apply(SRTContext *s, const char *style)
96 {
97  ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
98  if (st) {
99  int c = st->primary_color & 0xFFFFFF;
100  if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT) ||
102  c != ASS_DEFAULT_COLOR) {
103  srt_print(s, "<font");
104  if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT))
105  srt_print(s, " face=\"%s\"", st->font_name);
106  if (st->font_size != ASS_DEFAULT_FONT_SIZE)
107  srt_print(s, " size=\"%d\"", st->font_size);
108  if (c != ASS_DEFAULT_COLOR)
109  srt_print(s, " color=\"#%06x\"",
110  (c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
111  srt_print(s, ">");
112  srt_stack_push(s, 'f');
113  }
114  if (st->bold != ASS_DEFAULT_BOLD) {
115  srt_print(s, "<b>");
116  srt_stack_push(s, 'b');
117  }
118  if (st->italic != ASS_DEFAULT_ITALIC) {
119  srt_print(s, "<i>");
120  srt_stack_push(s, 'i');
121  }
122  if (st->underline != ASS_DEFAULT_UNDERLINE) {
123  srt_print(s, "<u>");
124  srt_stack_push(s, 'u');
125  }
126  if (st->alignment != ASS_DEFAULT_ALIGNMENT) {
127  srt_print(s, "{\\an%d}", st->alignment);
128  s->alignment_applied = 1;
129  }
130  }
131 }
132 
133 
135 {
136  SRTContext *s = avctx->priv_data;
137  s->avctx = avctx;
138  s->ass_ctx = ff_ass_split(avctx->subtitle_header);
140  return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
141 }
142 
143 static void srt_text_cb(void *priv, const char *text, int len)
144 {
145  SRTContext *s = priv;
146  av_bprint_append_data(&s->buffer, text, len);
147 }
148 
149 static void srt_new_line_cb(void *priv, int forced)
150 {
151  srt_print(priv, "\r\n");
152 }
153 
154 static void srt_style_cb(void *priv, char style, int close)
155 {
156  srt_stack_push_pop(priv, style, close);
157  if (!close)
158  srt_print(priv, "<%c>", style);
159 }
160 
161 static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
162 {
163  if (color_id > 1)
164  return;
165  srt_stack_push_pop(priv, 'f', color == 0xFFFFFFFF);
166  if (color != 0xFFFFFFFF)
167  srt_print(priv, "<font color=\"#%06x\">",
168  (color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
169 }
170 
171 static void srt_font_name_cb(void *priv, const char *name)
172 {
173  srt_stack_push_pop(priv, 'f', !name);
174  if (name)
175  srt_print(priv, "<font face=\"%s\">", name);
176 }
177 
178 static void srt_font_size_cb(void *priv, int size)
179 {
180  srt_stack_push_pop(priv, 'f', size < 0);
181  if (size >= 0)
182  srt_print(priv, "<font size=\"%d\">", size);
183 }
184 
185 static void srt_alignment_cb(void *priv, int alignment)
186 {
187  SRTContext *s = priv;
188  if (!s->alignment_applied && alignment >= 0) {
189  srt_print(s, "{\\an%d}", alignment);
190  s->alignment_applied = 1;
191  }
192 }
193 
194 static void srt_cancel_overrides_cb(void *priv, const char *style)
195 {
196  srt_stack_push_pop(priv, 0, 1);
197  srt_style_apply(priv, style);
198 }
199 
200 static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2,
201  int t1, int t2)
202 {
203  // TODO: add a AV_PKT_DATA_SUBTITLE_POSITION side data when a new subtitles
204  // encoding API passing the AVPacket is available.
205 }
206 
207 static void srt_end_cb(void *priv)
208 {
209  srt_stack_push_pop(priv, 0, 1);
210 }
211 
213  .text = srt_text_cb,
214  .new_line = srt_new_line_cb,
215  .style = srt_style_cb,
216  .color = srt_color_cb,
217  .font_name = srt_font_name_cb,
218  .font_size = srt_font_size_cb,
219  .alignment = srt_alignment_cb,
220  .cancel_overrides = srt_cancel_overrides_cb,
221  .move = srt_move_cb,
222  .end = srt_end_cb,
223 };
224 
226  .text = srt_text_cb,
227  .new_line = srt_new_line_cb,
228 };
229 
230 static int encode_frame(AVCodecContext *avctx,
231  unsigned char *buf, int bufsize, const AVSubtitle *sub,
232  const ASSCodesCallbacks *cb)
233 {
234  SRTContext *s = avctx->priv_data;
235  ASSDialog *dialog;
236  int i;
237 
238  av_bprint_clear(&s->buffer);
239 
240  for (i=0; i<sub->num_rects; i++) {
241  const char *ass = sub->rects[i]->ass;
242 
243  if (sub->rects[i]->type != SUBTITLE_ASS) {
244  av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
245  return AVERROR(EINVAL);
246  }
247 
248  dialog = ff_ass_split_dialog(s->ass_ctx, ass);
249  if (!dialog)
250  return AVERROR(ENOMEM);
251  s->alignment_applied = 0;
252  if (avctx->codec_id == AV_CODEC_ID_SUBRIP)
253  srt_style_apply(s, dialog->style);
255  ff_ass_free_dialog(&dialog);
256  }
257 
258  if (!av_bprint_is_complete(&s->buffer))
259  return AVERROR(ENOMEM);
260  if (!s->buffer.len)
261  return 0;
262 
263  if (s->buffer.len > bufsize) {
264  av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
266  }
267  memcpy(buf, s->buffer.str, s->buffer.len);
268 
269  return s->buffer.len;
270 }
271 
273  unsigned char *buf, int bufsize, const AVSubtitle *sub)
274 {
275  return encode_frame(avctx, buf, bufsize, sub, &srt_callbacks);
276 }
277 
279  unsigned char *buf, int bufsize, const AVSubtitle *sub)
280 {
281  return encode_frame(avctx, buf, bufsize, sub, &text_callbacks);
282 }
283 
285 {
286  SRTContext *s = avctx->priv_data;
287  ff_ass_split_free(s->ass_ctx);
288  av_bprint_finalize(&s->buffer, NULL);
289  return 0;
290 }
291 
292 #if CONFIG_SRT_ENCODER
293 /* deprecated encoder */
294 const AVCodec ff_srt_encoder = {
295  .name = "srt",
296  .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
297  .type = AVMEDIA_TYPE_SUBTITLE,
298  .id = AV_CODEC_ID_SUBRIP,
299  .priv_data_size = sizeof(SRTContext),
301  .encode_sub = srt_encode_frame,
302  .close = srt_encode_close,
303  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
304 };
305 #endif
306 
307 #if CONFIG_SUBRIP_ENCODER
308 const AVCodec ff_subrip_encoder = {
309  .name = "subrip",
310  .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
311  .type = AVMEDIA_TYPE_SUBTITLE,
312  .id = AV_CODEC_ID_SUBRIP,
313  .priv_data_size = sizeof(SRTContext),
315  .encode_sub = srt_encode_frame,
316  .close = srt_encode_close,
317  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
318 };
319 #endif
320 
321 #if CONFIG_TEXT_ENCODER
322 const AVCodec ff_text_encoder = {
323  .name = "text",
324  .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
325  .type = AVMEDIA_TYPE_SUBTITLE,
326  .id = AV_CODEC_ID_TEXT,
327  .priv_data_size = sizeof(SRTContext),
329  .encode_sub = text_encode_frame,
330  .close = srt_encode_close,
331  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
332 };
333 #endif
srt_new_line_cb
static void srt_new_line_cb(void *priv, int forced)
Definition: srtenc.c:149
AVSubtitle
Definition: avcodec.h:2289
AVCodec
AVCodec.
Definition: codec.h:202
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
srt_stack_find
static int srt_stack_find(SRTContext *s, const char c)
Definition: srtenc.c:69
ASSCodesCallbacks
Set of callback functions corresponding to each override codes that can be encountered in a "Dialogue...
Definition: ass_split.h:138
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
srt_stack_push_pop
static void srt_stack_push_pop(SRTContext *s, const char c, int close)
Definition: srtenc.c:83
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:234
color
Definition: vf_paletteuse.c:599
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:68
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:215
text_callbacks
static const ASSCodesCallbacks text_callbacks
Definition: srtenc.c:225
sub
static float sub(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:31
ASSStyle::alignment
int alignment
position of the text (left, center, top...), defined after the layout of the numpad (1-3 sub,...
Definition: ass_split.h:58
SRT_STACK_SIZE
#define SRT_STACK_SIZE
Definition: srtenc.c:31
srt_alignment_cb
static void srt_alignment_cb(void *priv, int alignment)
Definition: srtenc.c:185
SRTContext
Definition: srtenc.c:33
ff_ass_split_dialog
ASSDialog * ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf)
Split one ASS Dialogue line from a string buffer.
Definition: ass_split.c:427
ASS_DEFAULT_ALIGNMENT
#define ASS_DEFAULT_ALIGNMENT
Definition: ass.h:42
internal.h
ASSStyle::font_size
int font_size
font height
Definition: ass_split.h:42
AVCodecContext::subtitle_header
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:1683
t1
#define t1
Definition: regdef.h:29
av_bprint_append_data
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:157
ff_subrip_encoder
const AVCodec ff_subrip_encoder
srt_encode_init
static av_cold int srt_encode_init(AVCodecContext *avctx)
Definition: srtenc.c:134
ASSDialog::style
char * style
name of the ASSStyle to use with this dialog
Definition: ass_split.h:76
SUBTITLE_ASS
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition: avcodec.h:2256
init
static int init
Definition: av_tx.c:47
srt_encode_frame
static int srt_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
Definition: srtenc.c:272
ASSStyle::font_name
char * font_name
font face (case sensitive)
Definition: ass_split.h:41
ass_split.h
AVERROR_BUFFER_TOO_SMALL
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:53
srt_stack_pop
static char srt_stack_pop(SRTContext *s)
Definition: srtenc.c:62
ass.h
ff_ass_free_dialog
void ff_ass_free_dialog(ASSDialog **dialogp)
Free a dialogue obtained from ff_ass_split_dialog().
Definition: ass_split.c:415
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
ASS_DEFAULT_FONT
#define ASS_DEFAULT_FONT
Definition: ass.h:35
av_cold
#define av_cold
Definition: attributes.h:90
SRTContext::alignment_applied
int alignment_applied
Definition: srtenc.c:39
s
#define s(width, name)
Definition: cbs_vp9.c:257
SRTContext::stack_ptr
int stack_ptr
Definition: srtenc.c:38
srt_font_size_cb
static void srt_font_size_cb(void *priv, int size)
Definition: srtenc.c:178
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:393
av_vbprintf
void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
Append a formatted string to a print buffer.
Definition: bprint.c:116
NULL
#define NULL
Definition: coverity.c:32
ASSStyle::primary_color
int primary_color
color that a subtitle will normally appear in
Definition: ass_split.h:43
srt_stack_push
static int srt_stack_push(SRTContext *s, const char c)
Definition: srtenc.c:54
srt_encode_close
static int srt_encode_close(AVCodecContext *avctx)
Definition: srtenc.c:284
ASSSplitContext
This struct can be casted to ASS to access to the split data.
Definition: ass_split.c:199
ff_ass_split
ASSSplitContext * ff_ass_split(const char *buf)
Split a full ASS file or a ASS header from a string buffer and store the split structure in a newly a...
Definition: ass_split.c:376
srt_cancel_overrides_cb
static void srt_cancel_overrides_cb(void *priv, const char *style)
Definition: srtenc.c:194
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ASSStyle
fields extracted from the [V4(+) Styles] section
Definition: ass_split.h:39
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
srt_close_tag
static void srt_close_tag(SRTContext *s, char tag)
Definition: srtenc.c:78
ASSStyle::underline
int underline
whether text is underlined (1) or not (0)
Definition: ass_split.h:49
ff_ass_split_free
void ff_ass_split_free(ASSSplitContext *ctx)
Free all the memory allocated for an ASSSplitContext.
Definition: ass_split.c:464
ASS_DEFAULT_BOLD
#define ASS_DEFAULT_BOLD
Definition: ass.h:39
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
SRTContext::avctx
AVCodecContext * avctx
Definition: srtenc.c:34
ASSDialog::text
char * text
actual text which will be displayed as a subtitle, can include style override control codes (see ff_a...
Definition: ass_split.h:82
SRTContext::ass_ctx
ASSSplitContext * ass_ctx
Definition: srtenc.c:35
size
int size
Definition: twinvq_data.h:10344
srt_move_cb
static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2, int t1, int t2)
Definition: srtenc.c:200
ASSStyle::italic
int italic
whether text is italic (1) or not (0)
Definition: ass_split.h:48
encode_frame
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub, const ASSCodesCallbacks *cb)
Definition: srtenc.c:230
srt_color_cb
static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
Definition: srtenc.c:161
ASS_DEFAULT_UNDERLINE
#define ASS_DEFAULT_UNDERLINE
Definition: ass.h:41
ff_ass_style_get
ASSStyle * ff_ass_style_get(ASSSplitContext *ctx, const char *style)
Find an ASSStyle structure by its name.
Definition: ass_split.c:572
bprint.h
srt_style_apply
static void srt_style_apply(SRTContext *s, const char *style)
Definition: srtenc.c:95
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AV_CODEC_ID_SUBRIP
@ AV_CODEC_ID_SUBRIP
Definition: codec_id.h:539
srt_print
static void srt_print(SRTContext *s, const char *str,...)
Definition: srtenc.c:46
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
ASS_DEFAULT_ITALIC
#define ASS_DEFAULT_ITALIC
Definition: ass.h:40
len
int len
Definition: vorbis_enc_data.h:426
ASS_DEFAULT_COLOR
#define ASS_DEFAULT_COLOR
Definition: ass.h:37
avcodec.h
tag
uint32_t tag
Definition: movenc.c:1596
SRTContext::buffer
AVBPrint buffer
Definition: srtenc.c:36
AV_CODEC_ID_TEXT
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition: codec_id.h:524
srt_end_cb
static void srt_end_cb(void *priv)
Definition: srtenc.c:207
ASS_DEFAULT_FONT_SIZE
#define ASS_DEFAULT_FONT_SIZE
Definition: ass.h:36
AVCodecContext
main external API structure.
Definition: avcodec.h:383
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:226
t2
#define t2
Definition: regdef.h:30
ff_text_encoder
const AVCodec ff_text_encoder
srt_style_cb
static void srt_style_cb(void *priv, char style, int close)
Definition: srtenc.c:154
ASSDialog
fields extracted from the [Events] section
Definition: ass_split.h:71
srt_text_cb
static void srt_text_cb(void *priv, const char *text, int len)
Definition: srtenc.c:143
srt_font_name_cb
static void srt_font_name_cb(void *priv, const char *name)
Definition: srtenc.c:171
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
convert_header.str
string str
Definition: convert_header.py:20
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_ass_split_override_codes
int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv, const char *buf)
Split override codes out of a ASS "Dialogue" Text field.
Definition: ass_split.c:477
srt_callbacks
static const ASSCodesCallbacks srt_callbacks
Definition: srtenc.c:212
avstring.h
ff_srt_encoder
const AVCodec ff_srt_encoder
text_encode_frame
static int text_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
Definition: srtenc.c:278
ASSStyle::bold
int bold
whether text is bold (1) or not (0)
Definition: ass_split.h:47
ASSCodesCallbacks::text
void(* text)(void *priv, const char *text, int len)
Definition: ass_split.h:143