FFmpeg
Loading...
Searching...
No Matches
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 "config_components.h"
23
24#include <stdarg.h>
25#include "avcodec.h"
26#include "libavutil/bprint.h"
27#include "ass_split.h"
28#include "ass.h"
29#include "codec_internal.h"
30
31
32#define SRT_STACK_SIZE 64
33
42
43
44static av_printf_format(2, 3) void srt_print(SRTContext *s, const char *str, ...)
45{
46 va_list vargs;
47 va_start(vargs, str);
48 av_vbprintf(&s->buffer, str, vargs);
49 va_end(vargs);
50}
51
52static int srt_stack_push(SRTContext *s, const char c)
53{
54 if (s->stack_ptr >= SRT_STACK_SIZE)
55 return -1;
56 s->stack[s->stack_ptr++] = c;
57 return 0;
58}
59
61{
62 if (s->stack_ptr <= 0)
63 return 0;
64 return s->stack[--s->stack_ptr];
65}
66
67static int srt_stack_find(SRTContext *s, const char c)
68{
69 int i;
70 for (i = s->stack_ptr-1; i >= 0; i--)
71 if (s->stack[i] == c)
72 break;
73 return i;
74}
75
76static void srt_close_tag(SRTContext *s, char tag)
77{
78 srt_print(s, "</%c%s>", tag, tag == 'f' ? "ont" : "");
79}
80
81static void srt_stack_push_pop(SRTContext *s, const char c, int close)
82{
83 if (close) {
84 int i = c ? srt_stack_find(s, c) : 0;
85 if (i < 0)
86 return;
87 while (s->stack_ptr != i)
89 } else if (srt_stack_push(s, c) < 0)
90 av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
91}
92
93static void srt_style_apply(SRTContext *s, const char *style)
94{
95 ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
96 if (st) {
97 int c = st->primary_color & 0xFFFFFF;
98 if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT) ||
100 c != ASS_DEFAULT_COLOR) {
101 srt_print(s, "<font");
102 if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT))
103 srt_print(s, " face=\"%s\"", st->font_name);
105 srt_print(s, " size=\"%d\"", st->font_size);
106 if (c != ASS_DEFAULT_COLOR)
107 srt_print(s, " color=\"#%06x\"",
108 (c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
109 srt_print(s, ">");
110 srt_stack_push(s, 'f');
111 }
112 if (st->bold != ASS_DEFAULT_BOLD) {
113 srt_print(s, "<b>");
114 srt_stack_push(s, 'b');
115 }
116 if (st->italic != ASS_DEFAULT_ITALIC) {
117 srt_print(s, "<i>");
118 srt_stack_push(s, 'i');
119 }
120 if (st->underline != ASS_DEFAULT_UNDERLINE) {
121 srt_print(s, "<u>");
122 srt_stack_push(s, 'u');
123 }
124 if (st->alignment != ASS_DEFAULT_ALIGNMENT) {
125 srt_print(s, "{\\an%d}", st->alignment);
126 s->alignment_applied = 1;
127 }
128 }
129}
130
131
133{
134 SRTContext *s = avctx->priv_data;
135 s->avctx = avctx;
136 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
137 return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
138}
139
140static void srt_text_cb(void *priv, const char *text, int len)
141{
142 SRTContext *s = priv;
143 av_bprint_append_data(&s->buffer, text, len);
144}
145
146static void srt_new_line_cb(void *priv, int forced)
147{
148 srt_print(priv, "\n");
149}
150
151static void srt_style_cb(void *priv, char style, int close)
152{
153 srt_stack_push_pop(priv, style, close);
154 if (!close)
155 srt_print(priv, "<%c>", style);
156}
157
158static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
159{
160 if (color_id > 1)
161 return;
162 srt_stack_push_pop(priv, 'f', color == 0xFFFFFFFF);
163 if (color != 0xFFFFFFFF)
164 srt_print(priv, "<font color=\"#%06x\">",
165 (color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
166}
167
168static void srt_font_name_cb(void *priv, const char *name)
169{
170 srt_stack_push_pop(priv, 'f', !name);
171 if (name)
172 srt_print(priv, "<font face=\"%s\">", name);
173}
174
175static void srt_font_size_cb(void *priv, int size)
176{
177 srt_stack_push_pop(priv, 'f', size < 0);
178 if (size >= 0)
179 srt_print(priv, "<font size=\"%d\">", size);
180}
181
182static void srt_alignment_cb(void *priv, int alignment)
183{
184 SRTContext *s = priv;
185 if (!s->alignment_applied && alignment >= 0) {
186 srt_print(s, "{\\an%d}", alignment);
187 s->alignment_applied = 1;
188 }
189}
190
191static void srt_cancel_overrides_cb(void *priv, const char *style)
192{
193 srt_stack_push_pop(priv, 0, 1);
194 srt_style_apply(priv, style);
195}
196
197static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2,
198 int t1, int t2)
199{
200 // TODO: add a AV_PKT_DATA_SUBTITLE_POSITION side data when a new subtitles
201 // encoding API passing the AVPacket is available.
202}
203
204static void srt_end_cb(void *priv)
205{
206 srt_stack_push_pop(priv, 0, 1);
207}
208
210 .text = srt_text_cb,
211 .new_line = srt_new_line_cb,
212 .style = srt_style_cb,
213 .color = srt_color_cb,
214 .font_name = srt_font_name_cb,
215 .font_size = srt_font_size_cb,
216 .alignment = srt_alignment_cb,
217 .cancel_overrides = srt_cancel_overrides_cb,
218 .move = srt_move_cb,
219 .end = srt_end_cb,
220};
221
223 .text = srt_text_cb,
224 .new_line = srt_new_line_cb,
225};
226
227static int encode_frame(AVCodecContext *avctx,
228 unsigned char *buf, int bufsize, const AVSubtitle *sub,
229 const ASSCodesCallbacks *cb)
230{
231 SRTContext *s = avctx->priv_data;
232 ASSDialog *dialog;
233 int i;
234
235 av_bprint_init_for_buffer(&s->buffer, buf, bufsize);
236
237 for (i=0; i<sub->num_rects; i++) {
238 const char *ass = sub->rects[i]->ass;
239
240 if (sub->rects[i]->type != SUBTITLE_ASS) {
241 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
242 return AVERROR(EINVAL);
243 }
244
245 dialog = ff_ass_split_dialog(s->ass_ctx, ass);
246 if (!dialog)
247 return AVERROR(ENOMEM);
248 s->alignment_applied = 0;
249 if (avctx->codec_id == AV_CODEC_ID_SUBRIP)
250 srt_style_apply(s, dialog->style);
252 ff_ass_free_dialog(&dialog);
253 }
254
255 if (!s->buffer.len)
256 return 0;
257
258 if (!av_bprint_is_complete(&s->buffer)) {
259 av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
261 }
262
263 return s->buffer.len;
264}
265
267 unsigned char *buf, int bufsize, const AVSubtitle *sub)
268{
269 return encode_frame(avctx, buf, bufsize, sub, &srt_callbacks);
270}
271
273 unsigned char *buf, int bufsize, const AVSubtitle *sub)
274{
275 return encode_frame(avctx, buf, bufsize, sub, &text_callbacks);
276}
277
279{
280 SRTContext *s = avctx->priv_data;
281 ff_ass_split_free(s->ass_ctx);
282 return 0;
283}
284
285#if CONFIG_SRT_ENCODER
286/* deprecated encoder */
287const FFCodec ff_srt_encoder = {
288 .p.name = "srt",
289 CODEC_LONG_NAME("SubRip subtitle"),
290 .p.type = AVMEDIA_TYPE_SUBTITLE,
291 .p.id = AV_CODEC_ID_SUBRIP,
292 .priv_data_size = sizeof(SRTContext),
295 .close = srt_encode_close,
296};
297#endif
298
299#if CONFIG_SUBRIP_ENCODER
301 .p.name = "subrip",
302 CODEC_LONG_NAME("SubRip subtitle"),
303 .p.type = AVMEDIA_TYPE_SUBTITLE,
304 .p.id = AV_CODEC_ID_SUBRIP,
305 .priv_data_size = sizeof(SRTContext),
308 .close = srt_encode_close,
309};
310#endif
311
312#if CONFIG_TEXT_ENCODER
313const FFCodec ff_text_encoder = {
314 .p.name = "text",
315 CODEC_LONG_NAME("Raw text subtitle"),
316 .p.type = AVMEDIA_TYPE_SUBTITLE,
317 .p.id = AV_CODEC_ID_TEXT,
318 .priv_data_size = sizeof(SRTContext),
321 .close = srt_encode_close,
322};
323#endif
const FFCodec ff_text_encoder
const FFCodec ff_subrip_encoder
const FFCodec ff_srt_encoder
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define ASS_DEFAULT_ALIGNMENT
Definition ass.h:42
#define ASS_DEFAULT_FONT_SIZE
Definition ass.h:36
#define ASS_DEFAULT_COLOR
Definition ass.h:37
#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
av_cold 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:382
void ff_ass_free_dialog(ASSDialog **dialogp)
Free a dialogue obtained from ff_ass_split_dialog().
Definition ass_split.c:421
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:483
ASSStyle * ff_ass_style_get(ASSSplitContext *ctx, const char *style)
Find an ASSStyle structure by its name.
Definition ass_split.c:578
av_cold void ff_ass_split_free(ASSSplitContext *ctx)
Free all the memory allocated for an ASSSplitContext.
Definition ass_split.c:470
ASSDialog * ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf)
Split one ASS Dialogue line from a string buffer.
Definition ass_split.c:433
Libavcodec external API header.
AVBPrint public header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_ENCODE_SUB_CB(func)
#define CODEC_LONG_NAME(str)
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition avcodec.h:2055
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition codec_id.h:568
@ AV_CODEC_ID_SUBRIP
Definition codec_id.h:583
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition bprint.h:218
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition bprint.c:148
void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
Append a formatted string to a print buffer.
Definition bprint.c:99
void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
Init a print buffer using a pre-existing buffer.
Definition bprint.c:85
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition error.h:53
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
static void srt_alignment_cb(void *priv, int alignment)
Definition srtenc.c:182
static av_cold int srt_encode_close(AVCodecContext *avctx)
Definition srtenc.c:278
static int text_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
Definition srtenc.c:272
static void srt_cancel_overrides_cb(void *priv, const char *style)
Definition srtenc.c:191
static void srt_close_tag(SRTContext *s, char tag)
Definition srtenc.c:76
static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2, int t1, int t2)
Definition srtenc.c:197
static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
Definition srtenc.c:158
static void srt_style_apply(SRTContext *s, const char *style)
Definition srtenc.c:93
static int srt_stack_find(SRTContext *s, const char c)
Definition srtenc.c:67
static void srt_font_name_cb(void *priv, const char *name)
Definition srtenc.c:168
static const ASSCodesCallbacks srt_callbacks
Definition srtenc.c:209
static void srt_new_line_cb(void *priv, int forced)
Definition srtenc.c:146
#define SRT_STACK_SIZE
Definition srtenc.c:32
static int srt_stack_push(SRTContext *s, const char c)
Definition srtenc.c:52
static void srt_text_cb(void *priv, const char *text, int len)
Definition srtenc.c:140
static char srt_stack_pop(SRTContext *s)
Definition srtenc.c:60
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub, const ASSCodesCallbacks *cb)
Definition srtenc.c:227
static int srt_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
Definition srtenc.c:266
static const ASSCodesCallbacks text_callbacks
Definition srtenc.c:222
static void srt_font_size_cb(void *priv, int size)
Definition srtenc.c:175
static void srt_style_cb(void *priv, char style, int close)
Definition srtenc.c:151
static av_cold int srt_encode_init(AVCodecContext *avctx)
Definition srtenc.c:132
static void srt_stack_push_pop(SRTContext *s, const char c, int close)
Definition srtenc.c:81
static void srt_end_cb(void *priv)
Definition srtenc.c:204
#define av_printf_format(fmtpos, attrpos)
Definition attributes.h:218
#define av_cold
Definition attributes.h:117
uint32_t tag
Definition movenc.c:2073
const char * name
Definition qsvenc.c:142
Set of callback functions corresponding to each override codes that can be encountered in a "Dialogue...
Definition ass_split.h:138
fields extracted from the [Events] section
Definition ass_split.h:71
char * style
name of the ASSStyle to use with this dialog
Definition ass_split.h:76
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
This struct can be casted to ASS to access to the split data.
Definition ass_split.c:205
fields extracted from the [V4(+) Styles] section
Definition ass_split.h:39
int primary_color
color that a subtitle will normally appear in
Definition ass_split.h:43
int italic
whether text is italic (1) or not (0)
Definition ass_split.h:48
int bold
whether text is bold (1) or not (0)
Definition ass_split.h:47
int underline
whether text is underlined (1) or not (0)
Definition ass_split.h:49
char * font_name
font face (case sensitive)
Definition ass_split.h:41
int alignment
position of the text (left, center, top...), defined after the layout of the numpad (1-3 sub,...
Definition ass_split.h:58
int font_size
font height
Definition ass_split.h:42
main external API structure.
Definition avcodec.h:443
uint8_t * subtitle_header
Definition avcodec.h:1744
enum AVCodecID codec_id
Definition avcodec.h:453
void * priv_data
Definition avcodec.h:470
char * ass
0 terminated ASS/SSA compatible event line.
Definition avcodec.h:2084
enum AVSubtitleType type
Definition avcodec.h:2075
unsigned num_rects
Definition avcodec.h:2091
AVSubtitleRect ** rects
Definition avcodec.h:2092
char stack[SRT_STACK_SIZE]
Definition srtenc.c:38
ASSSplitContext * ass_ctx
Definition srtenc.c:36
AVBPrint buffer
Definition srtenc.c:37
AVCodecContext * avctx
Definition srtenc.c:35
int stack_ptr
Definition srtenc.c:39
int alignment_applied
Definition srtenc.c:40
#define av_log(a,...)
int size
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
int len
static double c[64]