This source file includes following definitions.
- ff_ass_subtitle_header
- ff_ass_subtitle_header_default
- insert_ts
- ff_ass_bprint_dialog
- ff_ass_add_rect
- ff_ass_add_rect_bprint
- ff_ass_bprint_text_event
#include "avcodec.h"
#include "ass.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/common.h"
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 alignment)
{
avctx->subtitle_header = av_asprintf(
"[Script Info]\r\n"
"; Script generated by FFmpeg/Lavc%s\r\n"
"ScriptType: v4.00+\r\n"
"PlayResX: 384\r\n"
"PlayResY: 288\r\n"
"\r\n"
"[V4+ Styles]\r\n"
"Format: Name, "
"Fontname, Fontsize, "
"PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
"Bold, Italic, Underline, StrikeOut, "
"ScaleX, ScaleY, "
"Spacing, Angle, "
"BorderStyle, Outline, Shadow, "
"Alignment, MarginL, MarginR, MarginV, "
"Encoding\r\n"
"Style: "
"Default,"
"%s,%d,"
"&H%x,&H%x,&H%x,&H%x,"
"%d,%d,%d,0,"
"100,100,"
"0,0,"
"1,1,0,"
"%d,10,10,10,"
"0\r\n"
"\r\n"
"[Events]\r\n"
"Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
!(avctx->flags & CODEC_FLAG_BITEXACT) ? AV_STRINGIFY(LIBAVCODEC_VERSION) : "",
font, font_size, color, color, back_color, back_color,
-bold, -italic, -underline, alignment);
if (!avctx->subtitle_header)
return AVERROR(ENOMEM);
avctx->subtitle_header_size = strlen(avctx->subtitle_header);
return 0;
}
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
{
return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
ASS_DEFAULT_FONT_SIZE,
ASS_DEFAULT_COLOR,
ASS_DEFAULT_BACK_COLOR,
ASS_DEFAULT_BOLD,
ASS_DEFAULT_ITALIC,
ASS_DEFAULT_UNDERLINE,
ASS_DEFAULT_ALIGNMENT);
}
static void insert_ts(AVBPrint *buf, int ts)
{
if (ts == -1) {
av_bprintf(buf, "9:59:59.99,");
} else {
int h, m, s;
h = ts/360000; ts -= 360000*h;
m = ts/ 6000; ts -= 6000*m;
s = ts/ 100; ts -= 100*s;
av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
}
}
int ff_ass_bprint_dialog(AVBPrint *buf, const char *dialog,
int ts_start, int duration, int raw)
{
int dlen;
if (!raw || raw == 2) {
long int layer = 0;
if (raw == 2) {
dialog = strchr(dialog, ',');
if (!dialog)
return AVERROR_INVALIDDATA;
dialog++;
layer = strtol(dialog, (char**)&dialog, 10);
if (*dialog != ',')
return AVERROR_INVALIDDATA;
dialog++;
}
av_bprintf(buf, "Dialogue: %ld,", layer);
insert_ts(buf, ts_start);
insert_ts(buf, duration == -1 ? -1 : ts_start + duration);
if (raw != 2)
av_bprintf(buf, "Default,,0,0,0,,");
}
dlen = strcspn(dialog, "\n");
dlen += dialog[dlen] == '\n';
av_bprintf(buf, "%.*s", dlen, dialog);
if (raw == 2)
av_bprintf(buf, "\r\n");
return dlen;
}
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
int ts_start, int duration, int raw)
{
AVBPrint buf;
int ret, dlen;
AVSubtitleRect **rects;
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
if ((ret = ff_ass_bprint_dialog(&buf, dialog, ts_start, duration, raw)) < 0)
goto err;
dlen = ret;
if (!av_bprint_is_complete(&buf))
goto errnomem;
rects = av_realloc_array(sub->rects, (sub->num_rects+1), sizeof(*sub->rects));
if (!rects)
goto errnomem;
sub->rects = rects;
sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
rects[sub->num_rects]->type = SUBTITLE_ASS;
ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass);
if (ret < 0)
goto err;
sub->num_rects++;
return dlen;
errnomem:
ret = AVERROR(ENOMEM);
err:
av_bprint_finalize(&buf, NULL);
return ret;
}
int ff_ass_add_rect_bprint(AVSubtitle *sub, AVBPrint *buf,
int ts_start, int duration)
{
av_bprintf(buf, "\r\n");
if (!av_bprint_is_complete(buf))
return AVERROR(ENOMEM);
return ff_ass_add_rect(sub, buf->str, ts_start, duration, 0);
}
void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
const char *linebreaks, int keep_ass_markup)
{
const char *p_end = p + size;
for (; p < p_end && *p; p++) {
if (linebreaks && strchr(linebreaks, *p)) {
av_bprintf(buf, "\\N");
} else if (!keep_ass_markup && strchr("{}\\", *p)) {
av_bprintf(buf, "\\%c", *p);
} else if (p[0] == '\n') {
if (p < p_end - 1)
av_bprintf(buf, "\\N");
} else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
continue;
} else {
av_bprint_chars(buf, *p, 1);
}
}
}