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>
28
29
30 #define SRT_STACK_SIZE 64
31
40
41
42 #ifdef __GNUC__
43 __attribute__ ((__format__ (__printf__, 2, 3)))
44 #endif
46 {
47 va_list vargs;
48 va_start(vargs, str);
50 va_end(vargs);
51 }
52
54 {
56 return -1;
58 return 0;
59 }
60
62 {
64 return 0;
66 }
67
69 {
70 int i;
73 break;
74 return i;
75 }
76
78 {
79 srt_print(s,
"</%c%s>", tag, tag ==
'f' ?
"ont" :
"");
80 }
81
83 {
84 if (close) {
86 if (i < 0)
87 return;
92 }
93
95 {
97 if (st) {
109 (c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
112 }
116 }
120 }
124 }
128 }
129 }
130 }
131
132
134 {
140 }
141
143 {
146 }
147
149 {
151 }
152
154 {
156 if (!close)
158 }
159
161 {
162 if (color_id > 1)
163 return;
165 if (color != 0xFFFFFFFF)
166 srt_print(priv,
"<font color=\"#%06x\">",
167 (color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
168 }
169
171 {
173 if (name)
174 srt_print(priv,
"<font face=\"%s\">", name);
175 }
176
178 {
180 if (size >= 0)
181 srt_print(priv,
"<font size=\"%d\">", size);
182 }
183
185 {
190 }
191 }
192
194 {
197 }
198
199 static void srt_move_cb(
void *priv,
int x1,
int y1,
int x2,
int y2,
201 {
202 // TODO: add a AV_PKT_DATA_SUBTITLE_POSITION side data when a new subtitles
203 // encoding API passing the AVPacket is available.
204 }
205
207 {
209 }
210
222 };
223
227 };
228
232 {
235 int i;
236
238
240 const char *ass = sub->
rects[i]->
ass;
241
245 }
246
247 #if FF_API_ASS_TIMING
248 if (!strncmp(ass, "Dialogue: ", 10)) {
249 int num;
251 for (; dialog && num--; dialog++) {
256 }
257 } else {
258 #endif
260 if (!dialog)
267 #if FF_API_ASS_TIMING
268 }
269 #endif
270 }
271
275 return 0;
276
277 if (s->
buffer.len > bufsize) {
279 return -1;
280 }
282
284 }
285
288 {
289 return encode_frame(avctx, buf, bufsize, sub, &srt_callbacks);
290 }
291
294 {
295 return encode_frame(avctx, buf, bufsize, sub, &text_callbacks);
296 }
297
299 {
303 return 0;
304 }
305
306 #if CONFIG_SRT_ENCODER
307 /* deprecated encoder */
317 };
318 #endif
319
320 #if CONFIG_SUBRIP_ENCODER
330 };
331 #endif
332
333 #if CONFIG_TEXT_ENCODER
343 };
344 #endif
char * style
name of the ASSStyle to use with this dialog
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int text_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
fields extracted from the [Events] section
static av_cold int init(AVCodecContext *avctx)
ASSDialog * ff_ass_split_dialog2(ASSSplitContext *ctx, const char *buf)
Split one ASS Dialogue line from a string buffer.
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...
static const ASSCodesCallbacks srt_callbacks
static av_cold int srt_encode_init(AVCodecContext *avctx)
static int srt_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
char * text
actual text which will be displayed as a subtitle, can include style override control codes (see ff_a...
int alignment
position of the text (left, center, top...), defined after the layout of the numpad (1-3 sub...
static double cb(void *priv, double x, double y)
This struct can be casted to ASS to access to the split data.
#define ASS_DEFAULT_ALIGNMENT
static char srt_stack_pop(SRTContext *s)
void ff_ass_free_dialog(ASSDialog **dialogp)
Free a dialogue obtained from ff_ass_split_dialog2().
static void srt_font_size_cb(void *priv, int size)
int bold
whether text is bold (1) or not (0)
int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv, const char *buf)
Split override codes out of a ASS "Dialogue" Text field.
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
static int srt_encode_close(AVCodecContext *avctx)
ASSDialog * ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf, int cache, int *number)
Split one or several ASS "Dialogue" lines from a string buffer and store them in an already initializ...
#define ASS_DEFAULT_UNDERLINE
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_BPRINT_SIZE_UNLIMITED
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
const char * name
Name of the codec implementation.
#define ASS_DEFAULT_FONT_SIZE
char stack[SRT_STACK_SIZE]
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub, const ASSCodesCallbacks *cb)
static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
static void srt_close_tag(SRTContext *s, char tag)
void(* text)(void *priv, const char *text, int len)
static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2, int t1, int t2)
static int srt_stack_push(SRTContext *s, const char c)
int italic
whether text is italic (1) or not (0)
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Set of callback functions corresponding to each override codes that can be encountered in a "Dialogue...
static void srt_style_apply(SRTContext *s, const char *style)
int primary_color
color that a subtitle will normally appear in
Libavcodec external API header.
main external API structure.
ASSSplitContext * ass_ctx
AVCodec ff_subrip_encoder
void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
Append a formatted string to a print buffer.
static void srt_cancel_overrides_cb(void *priv, const char *style)
char * font_name
font face (case sensitive)
fields extracted from the [V4(+) Styles] section
ASSStyle * ff_ass_style_get(ASSSplitContext *ctx, const char *style)
Find an ASSStyle structure by its name.
static void srt_style_cb(void *priv, char style, int close)
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
static void srt_font_name_cb(void *priv, const char *name)
#define ASS_DEFAULT_COLOR
static void srt_new_line_cb(void *priv, int forced)
Formatted text, the ass field must be set by the decoder and is authoritative.
#define ASS_DEFAULT_ITALIC
static int srt_stack_find(SRTContext *s, const char c)
static void srt_print(SRTContext *s, const char *str,...)
char * ass
0 terminated ASS/SSA compatible event line.
static void srt_alignment_cb(void *priv, int alignment)
void ff_ass_split_free(ASSSplitContext *ctx)
Free all the memory allocated for an ASSSplitContext.
static void srt_text_cb(void *priv, const char *text, int len)
static void srt_end_cb(void *priv)
int underline
whether text is underlined (1) or not (0)
static void srt_stack_push_pop(SRTContext *s, const char c, int close)
static const ASSCodesCallbacks text_callbacks
uint8_t * subtitle_header
Header containing style information for text subtitles.