FFmpeg
Loading...
Searching...
No Matches
webvttenc.c
Go to the documentation of this file.
1/*
2 * WebVTT subtitle encoder
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4 * Copyright (c) 2014 Aman Gupta <ffmpeg@tmm1.net>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <stdarg.h>
24#include "avcodec.h"
25#include "libavutil/bprint.h"
26#include "ass_split.h"
27#include "ass.h"
28#include "codec_internal.h"
29
30#define WEBVTT_STACK_SIZE 64
45
46static av_printf_format(2, 3) void webvtt_print(WebVTTContext *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
54static int webvtt_stack_push(WebVTTContext *s, const char c)
55{
56 if (s->stack_ptr >= WEBVTT_STACK_SIZE)
57 return -1;
58 s->stack[s->stack_ptr++] = c;
59 return 0;
60}
61
63{
64 if (s->stack_ptr <= 0)
65 return 0;
66 return s->stack[--s->stack_ptr];
67}
68
69static int webvtt_stack_find(WebVTTContext *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
79{
80 webvtt_print(s, "</%c>", tag);
81}
82
83static void webvtt_stack_push_pop(WebVTTContext *s, const char c, int close)
84{
85 if (close) {
86 int i = c ? webvtt_stack_find(s, c) : 0;
87 if (i < 0)
88 return;
89 while (s->stack_ptr != i)
91 } else if (webvtt_stack_push(s, c) < 0)
92 av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
93}
94
95static void webvtt_style_apply(WebVTTContext *s, const char *style)
96{
97 ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
98 if (st) {
99 if (st->bold != ASS_DEFAULT_BOLD) {
100 webvtt_print(s, "<b>");
101 webvtt_stack_push(s, 'b');
102 }
103 if (st->italic != ASS_DEFAULT_ITALIC) {
104 webvtt_print(s, "<i>");
105 webvtt_stack_push(s, 'i');
106 }
107 if (st->underline != ASS_DEFAULT_UNDERLINE) {
108 webvtt_print(s, "<u>");
109 webvtt_stack_push(s, 'u');
110 }
111 }
112}
113
115{
116 int64_t sec = millisec / 1000;
117 millisec %= 1000;
118 int64_t min = sec / 60;
119 sec %= 60;
120 int64_t hour = min / 60;
121 min %= 60;
122 if (hour > 0) {
123 webvtt_print(s, "<%02"PRId64":%02"PRId64":%02"PRId64".%03"PRId64">", hour, min, sec, millisec);
124 } else {
125 webvtt_print(s, "<%02"PRId64":%02"PRId64".%03"PRId64">", min, sec, millisec);
126 }
127}
128
130{
131 if (s->pending_duration_ms <= 0)
132 return;
133
134 int64_t next_ts = s->current_time_ms <= INT64_MAX - s->pending_duration_ms ?
135 s->current_time_ms + s->pending_duration_ms : INT64_MAX;
136 if (next_ts > s->cue_start_ms && next_ts < s->cue_end_ms &&
137 next_ts > s->last_timestamp_ms) {
138 webvtt_print_timestamp(s, next_ts);
139 s->last_timestamp_ms = next_ts;
140 }
141 s->current_time_ms = next_ts;
142 s->pending_duration_ms = 0;
143}
144
145static void webvtt_text_cb(void *priv, const char *text, int len)
146{
147 WebVTTContext *s = priv;
148 av_bprint_append_data(&s->buffer, text, len);
149}
150
151static void webvtt_new_line_cb(void *priv, int forced)
152{
153 WebVTTContext *s = priv;
154 webvtt_print(s, "\n");
155}
156
157static void webvtt_karaoke_cb(void *priv, unsigned int duration)
158{
159 WebVTTContext *s = priv;
160
162 s->pending_duration_ms = (int64_t)duration * 10;
163}
164
165static void webvtt_style_cb(void *priv, char style, int close)
166{
167 if (style == 's') // strikethrough unsupported
168 return;
169
170 webvtt_stack_push_pop(priv, style, close);
171 if (!close)
172 webvtt_print(priv, "<%c>", style);
173}
174
175static void webvtt_cancel_overrides_cb(void *priv, const char *style)
176{
177 webvtt_stack_push_pop(priv, 0, 1);
178 webvtt_style_apply(priv, style);
179}
180
181static void webvtt_end_cb(void *priv)
182{
183 WebVTTContext *s = priv;
185 webvtt_stack_push_pop(priv, 0, 1);
186}
187
189 .text = webvtt_text_cb,
190 .new_line = webvtt_new_line_cb,
191 .style = webvtt_style_cb,
192 .color = NULL,
193 .font_name = NULL,
194 .font_size = NULL,
195 .alignment = NULL,
196 .cancel_overrides = webvtt_cancel_overrides_cb,
197 .move = NULL,
198 .karaoke = webvtt_karaoke_cb,
199 .end = webvtt_end_cb,
200};
201
203 unsigned char *buf, int bufsize, const AVSubtitle *sub)
204{
205 WebVTTContext *s = avctx->priv_data;
206 ASSDialog *dialog;
207 int i;
208
209 av_bprint_init_for_buffer(&s->buffer, buf, bufsize);
210
211 /* Multiple rectangles are independent overlays. One monotonic WebVTT
212 * cue cannot represent them, so keep the previous plain-text path. */
213 if (sub->pts != AV_NOPTS_VALUE && sub->num_rects == 1) {
214 s->cue_start_ms = av_rescale_q(sub->pts, AV_TIME_BASE_Q, (AVRational){ 1, 1000 });
215 s->cue_end_ms = s->cue_start_ms + sub->end_display_time;
216 } else {
217 s->cue_start_ms = s->cue_end_ms = 0;
218 }
219 s->current_time_ms = s->cue_start_ms;
220 s->last_timestamp_ms = -1;
221 s->pending_duration_ms = 0;
222
223 for (i=0; i<sub->num_rects; i++) {
224 const char *ass = sub->rects[i]->ass;
225
226 if (sub->rects[i]->type != SUBTITLE_ASS) {
227 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
228 return AVERROR(EINVAL);
229 }
230
231 dialog = ff_ass_split_dialog(s->ass_ctx, ass);
232 if (!dialog)
233 return AVERROR(ENOMEM);
234 webvtt_style_apply(s, dialog->style);
236 ff_ass_free_dialog(&dialog);
237 }
238
239 if (!s->buffer.len)
240 return 0;
241
242 if (!av_bprint_is_complete(&s->buffer)) {
243 av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
245 }
246
247 return s->buffer.len;
248}
249
251{
252 WebVTTContext *s = avctx->priv_data;
253 ff_ass_split_free(s->ass_ctx);
254 return 0;
255}
256
258{
259 WebVTTContext *s = avctx->priv_data;
260 s->avctx = avctx;
261 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
262 return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
263}
264
266 .p.name = "webvtt",
267 CODEC_LONG_NAME("WebVTT subtitle"),
268 .p.type = AVMEDIA_TYPE_SUBTITLE,
269 .p.id = AV_CODEC_ID_WEBVTT,
270 .priv_data_size = sizeof(WebVTTContext),
273 .close = webvtt_encode_close,
274};
const FFCodec ff_webvtt_encoder
Definition webvttenc.c:265
#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:583
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)
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define min(a, b)
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static int64_t duration
Definition ffplay.c:359
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition avcodec.h:2072
@ AV_CODEC_ID_WEBVTT
Definition codec_id.h:589
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:147
void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
Append a formatted string to a print buffer.
Definition bprint.c:98
void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
Init a print buffer using a pre-existing buffer.
Definition bprint.c:84
#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
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
static void webvtt_karaoke_cb(void *priv, unsigned int duration)
Definition webvttenc.c:157
static void webvtt_new_line_cb(void *priv, int forced)
Definition webvttenc.c:151
static int webvtt_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
Definition webvttenc.c:202
static av_cold int webvtt_encode_init(AVCodecContext *avctx)
Definition webvttenc.c:257
static void webvtt_flush_karaoke_timestamp(WebVTTContext *s)
Definition webvttenc.c:129
static av_cold int webvtt_encode_close(AVCodecContext *avctx)
Definition webvttenc.c:250
static char webvtt_stack_pop(WebVTTContext *s)
Definition webvttenc.c:62
#define WEBVTT_STACK_SIZE
Definition webvttenc.c:30
static void webvtt_print_timestamp(WebVTTContext *s, int64_t millisec)
Definition webvttenc.c:114
static int webvtt_stack_find(WebVTTContext *s, const char c)
Definition webvttenc.c:69
static void webvtt_style_cb(void *priv, char style, int close)
Definition webvttenc.c:165
static void webvtt_end_cb(void *priv)
Definition webvttenc.c:181
static void webvtt_close_tag(WebVTTContext *s, char tag)
Definition webvttenc.c:78
static void webvtt_text_cb(void *priv, const char *text, int len)
Definition webvttenc.c:145
static void webvtt_style_apply(WebVTTContext *s, const char *style)
Definition webvttenc.c:95
static void webvtt_cancel_overrides_cb(void *priv, const char *style)
Definition webvttenc.c:175
static int webvtt_stack_push(WebVTTContext *s, const char c)
Definition webvttenc.c:54
static const ASSCodesCallbacks webvtt_callbacks
Definition webvttenc.c:188
static void webvtt_stack_push_pop(WebVTTContext *s, const char c, int close)
Definition webvttenc.c:83
#define av_printf_format(fmtpos, attrpos)
Definition attributes.h:218
#define av_cold
Definition attributes.h:117
uint32_t tag
Definition movenc.c:2128
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 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
main external API structure.
Definition avcodec.h:443
uint8_t * subtitle_header
Definition avcodec.h:1751
void * priv_data
Definition avcodec.h:470
Rational number (pair of numerator and denominator).
Definition rational.h:58
char * ass
0 terminated ASS/SSA compatible event line.
Definition avcodec.h:2101
enum AVSubtitleType type
Definition avcodec.h:2092
uint32_t end_display_time
Definition avcodec.h:2107
unsigned num_rects
Definition avcodec.h:2108
AVSubtitleRect ** rects
Definition avcodec.h:2109
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition avcodec.h:2110
int64_t last_timestamp_ms
Definition webvttenc.c:40
int64_t current_time_ms
Definition webvttenc.c:39
int64_t cue_end_ms
Definition webvttenc.c:38
char stack[WEBVTT_STACK_SIZE]
Definition webvttenc.c:42
ASSSplitContext * ass_ctx
Definition webvttenc.c:33
AVBPrint buffer
Definition webvttenc.c:34
int64_t cue_start_ms
Definition webvttenc.c:37
int64_t pending_duration_ms
Definition webvttenc.c:41
AVCodecContext * avctx
Definition webvttenc.c:32
unsigned timestamp_end
Definition webvttenc.c:35
#define av_log(a,...)
int len
static double c[64]