mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2024-12-23 12:43:46 +02:00
add SubRip encoder
Signed-off-by: Aurelien Jacobs <aurel@gnuage.org>
This commit is contained in:
parent
efc8c709c9
commit
8e43b6fed9
@ -69,7 +69,7 @@ version <next>:
|
||||
- replace the ocv_smooth filter with a more generic ocv filter
|
||||
- Windows Televison (WTV) demuxer
|
||||
- FFmpeg metadata format muxer and demuxer
|
||||
- SubRip (srt) subtitle decoder
|
||||
- SubRip (srt) subtitle encoder and decoder
|
||||
- floating-point AC-3 encoder added
|
||||
- Lagarith decoder
|
||||
- ffmpeg -copytb option added
|
||||
|
@ -198,7 +198,7 @@ Codecs:
|
||||
smc.c Mike Melanson
|
||||
snow.c Michael Niedermayer, Loren Merritt
|
||||
sonic.c Alex Beregszaszi
|
||||
srtdec.c Aurelien Jacobs
|
||||
srt* Aurelien Jacobs
|
||||
sunrast.c Ivo van Poorten
|
||||
svq3.c Michael Niedermayer
|
||||
targa.c Kostya Shishkov
|
||||
|
@ -700,7 +700,7 @@ performance on systems without hardware floating point support).
|
||||
@item DVB @tab X @tab X @tab X @tab X
|
||||
@item DVD @tab X @tab X @tab X @tab X
|
||||
@item PGS @tab @tab @tab @tab X
|
||||
@item SubRip (SRT) @tab X @tab X @tab @tab X
|
||||
@item SubRip (SRT) @tab X @tab X @tab X @tab X
|
||||
@item XSUB @tab @tab @tab X @tab X
|
||||
@end multitable
|
||||
|
||||
|
@ -344,6 +344,7 @@ OBJS-$(CONFIG_SONIC_ENCODER) += sonic.o
|
||||
OBJS-$(CONFIG_SONIC_LS_ENCODER) += sonic.o
|
||||
OBJS-$(CONFIG_SP5X_DECODER) += sp5xdec.o mjpegdec.o mjpeg.o
|
||||
OBJS-$(CONFIG_SRT_DECODER) += srtdec.o ass.o
|
||||
OBJS-$(CONFIG_SRT_ENCODER) += srtenc.o ass_split.o
|
||||
OBJS-$(CONFIG_SUNRAST_DECODER) += sunrast.o
|
||||
OBJS-$(CONFIG_SVQ1_DECODER) += svq1dec.o svq1.o h263.o \
|
||||
mpegvideo.o error_resilience.o
|
||||
|
@ -357,7 +357,7 @@ void avcodec_register_all(void)
|
||||
REGISTER_ENCDEC (DVBSUB, dvbsub);
|
||||
REGISTER_ENCDEC (DVDSUB, dvdsub);
|
||||
REGISTER_DECODER (PGSSUB, pgssub);
|
||||
REGISTER_DECODER (SRT, srt);
|
||||
REGISTER_ENCDEC (SRT, srt);
|
||||
REGISTER_ENCDEC (XSUB, xsub);
|
||||
|
||||
/* external libraries */
|
||||
|
468
libavcodec/ass_split.c
Normal file
468
libavcodec/ass_split.c
Normal file
@ -0,0 +1,468 @@
|
||||
/*
|
||||
* SSA/ASS spliting functions
|
||||
* Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
|
||||
*
|
||||
* This file is part of FFmpeg.
|
||||
*
|
||||
* FFmpeg is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* FFmpeg is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with FFmpeg; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "avcodec.h"
|
||||
#include "ass_split.h"
|
||||
|
||||
typedef enum {
|
||||
ASS_STR,
|
||||
ASS_INT,
|
||||
ASS_FLT,
|
||||
ASS_COLOR,
|
||||
ASS_TIMESTAMP,
|
||||
ASS_ALGN,
|
||||
} ASSFieldType;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
int type;
|
||||
int offset;
|
||||
} ASSFields;
|
||||
|
||||
typedef struct {
|
||||
const char *section;
|
||||
const char *format_header;
|
||||
const char *fields_header;
|
||||
int size;
|
||||
int offset;
|
||||
int offset_count;
|
||||
ASSFields fields[10];
|
||||
} ASSSection;
|
||||
|
||||
static const ASSSection ass_sections[] = {
|
||||
{ .section = "Script Info",
|
||||
.offset = offsetof(ASS, script_info),
|
||||
.fields = {{"ScriptType", ASS_STR, offsetof(ASSScriptInfo, script_type)},
|
||||
{"Collisions", ASS_STR, offsetof(ASSScriptInfo, collisions) },
|
||||
{"PlayResX", ASS_INT, offsetof(ASSScriptInfo, play_res_x) },
|
||||
{"PlayResY", ASS_INT, offsetof(ASSScriptInfo, play_res_y) },
|
||||
{"Timer", ASS_FLT, offsetof(ASSScriptInfo, timer) },
|
||||
{0},
|
||||
}
|
||||
},
|
||||
{ .section = "V4+ Styles",
|
||||
.format_header = "Format",
|
||||
.fields_header = "Style",
|
||||
.size = sizeof(ASSStyle),
|
||||
.offset = offsetof(ASS, styles),
|
||||
.offset_count = offsetof(ASS, styles_count),
|
||||
.fields = {{"Name", ASS_STR, offsetof(ASSStyle, name) },
|
||||
{"Fontname", ASS_STR, offsetof(ASSStyle, font_name) },
|
||||
{"Fontsize", ASS_INT, offsetof(ASSStyle, font_size) },
|
||||
{"PrimaryColour",ASS_COLOR,offsetof(ASSStyle, primary_color)},
|
||||
{"BackColour", ASS_COLOR,offsetof(ASSStyle, back_color) },
|
||||
{"Bold", ASS_INT, offsetof(ASSStyle, bold) },
|
||||
{"Italic", ASS_INT, offsetof(ASSStyle, italic) },
|
||||
{"Underline", ASS_INT, offsetof(ASSStyle, underline) },
|
||||
{"Alignment", ASS_INT, offsetof(ASSStyle, alignment) },
|
||||
{0},
|
||||
}
|
||||
},
|
||||
{ .section = "V4 Styles",
|
||||
.format_header = "Format",
|
||||
.fields_header = "Style",
|
||||
.size = sizeof(ASSStyle),
|
||||
.offset = offsetof(ASS, styles),
|
||||
.offset_count = offsetof(ASS, styles_count),
|
||||
.fields = {{"Name", ASS_STR, offsetof(ASSStyle, name) },
|
||||
{"Fontname", ASS_STR, offsetof(ASSStyle, font_name) },
|
||||
{"Fontsize", ASS_INT, offsetof(ASSStyle, font_size) },
|
||||
{"PrimaryColour",ASS_COLOR,offsetof(ASSStyle, primary_color)},
|
||||
{"BackColour", ASS_COLOR,offsetof(ASSStyle, back_color) },
|
||||
{"Bold", ASS_INT, offsetof(ASSStyle, bold) },
|
||||
{"Italic", ASS_INT, offsetof(ASSStyle, italic) },
|
||||
{"Alignment", ASS_ALGN, offsetof(ASSStyle, alignment) },
|
||||
{0},
|
||||
}
|
||||
},
|
||||
{ .section = "Events",
|
||||
.format_header = "Format",
|
||||
.fields_header = "Dialogue",
|
||||
.size = sizeof(ASSDialog),
|
||||
.offset = offsetof(ASS, dialogs),
|
||||
.offset_count = offsetof(ASS, dialogs_count),
|
||||
.fields = {{"Layer", ASS_INT, offsetof(ASSDialog, layer) },
|
||||
{"Start", ASS_TIMESTAMP, offsetof(ASSDialog, start) },
|
||||
{"End", ASS_TIMESTAMP, offsetof(ASSDialog, end) },
|
||||
{"Style", ASS_STR, offsetof(ASSDialog, style) },
|
||||
{"Text", ASS_STR, offsetof(ASSDialog, text) },
|
||||
{0},
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
typedef int (*ASSConvertFunc)(void *dest, const char *buf, int len);
|
||||
|
||||
static int convert_str(void *dest, const char *buf, int len)
|
||||
{
|
||||
char *str = av_malloc(len + 1);
|
||||
if (str) {
|
||||
memcpy(str, buf, len);
|
||||
str[len] = 0;
|
||||
if (*(void **)dest)
|
||||
av_free(*(void **)dest);
|
||||
*(char **)dest = str;
|
||||
}
|
||||
return !str;
|
||||
}
|
||||
static int convert_int(void *dest, const char *buf, int len)
|
||||
{
|
||||
return sscanf(buf, "%d", (int *)dest) == 1;
|
||||
}
|
||||
static int convert_flt(void *dest, const char *buf, int len)
|
||||
{
|
||||
return sscanf(buf, "%f", (float *)dest) == 1;
|
||||
}
|
||||
static int convert_color(void *dest, const char *buf, int len)
|
||||
{
|
||||
return sscanf(buf, "&H%8x", (int *)dest) == 1 ||
|
||||
sscanf(buf, "%d", (int *)dest) == 1;
|
||||
}
|
||||
static int convert_timestamp(void *dest, const char *buf, int len)
|
||||
{
|
||||
int c, h, m, s, cs;
|
||||
if ((c = sscanf(buf, "%d:%02d:%02d.%02d", &h, &m, &s, &cs)) == 4)
|
||||
*(int *)dest = 360000*h + 6000*m + 100*s + cs;
|
||||
return c == 4;
|
||||
}
|
||||
static int convert_alignment(void *dest, const char *buf, int len)
|
||||
{
|
||||
int a;
|
||||
if (sscanf(buf, "%d", &a) == 1) {
|
||||
/* convert V4 Style alignment to V4+ Style */
|
||||
*(int *)dest = a + ((a&4) >> 1) - 5*!!(a&8);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const ASSConvertFunc convert_func[] = {
|
||||
[ASS_STR] = convert_str,
|
||||
[ASS_INT] = convert_int,
|
||||
[ASS_FLT] = convert_flt,
|
||||
[ASS_COLOR] = convert_color,
|
||||
[ASS_TIMESTAMP] = convert_timestamp,
|
||||
[ASS_ALGN] = convert_alignment,
|
||||
};
|
||||
|
||||
|
||||
struct ASSSplitContext {
|
||||
ASS ass;
|
||||
int current_section;
|
||||
int field_number[FF_ARRAY_ELEMS(ass_sections)];
|
||||
int *field_order[FF_ARRAY_ELEMS(ass_sections)];
|
||||
};
|
||||
|
||||
|
||||
static uint8_t *realloc_section_array(ASSSplitContext *ctx)
|
||||
{
|
||||
const ASSSection *section = &ass_sections[ctx->current_section];
|
||||
int *count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
|
||||
void **section_ptr = (void **)((uint8_t *)&ctx->ass + section->offset);
|
||||
uint8_t *tmp = av_realloc(*section_ptr, (*count+1)*section->size);
|
||||
if (!tmp)
|
||||
return NULL;
|
||||
*section_ptr = tmp;
|
||||
tmp += *count * section->size;
|
||||
memset(tmp, 0, section->size);
|
||||
(*count)++;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
static inline int is_eol(char buf)
|
||||
{
|
||||
return buf == '\r' || buf == '\n' || buf == 0;
|
||||
}
|
||||
|
||||
static inline const char *skip_space(const char *buf)
|
||||
{
|
||||
while (*buf == ' ')
|
||||
buf++;
|
||||
return buf;
|
||||
}
|
||||
|
||||
static const char *ass_split_section(ASSSplitContext *ctx, const char *buf)
|
||||
{
|
||||
const ASSSection *section = &ass_sections[ctx->current_section];
|
||||
int *number = &ctx->field_number[ctx->current_section];
|
||||
int *order = ctx->field_order[ctx->current_section];
|
||||
int *tmp, i, len;
|
||||
|
||||
while (buf && *buf) {
|
||||
if (buf[0] == '[') {
|
||||
ctx->current_section = -1;
|
||||
break;
|
||||
}
|
||||
if (buf[0] == ';' || (buf[0] == '!' && buf[1] == ':')) {
|
||||
/* skip comments */
|
||||
} else if (section->format_header && !order) {
|
||||
len = strlen(section->format_header);
|
||||
if (strncmp(buf, section->format_header, len) || buf[len] != ':')
|
||||
return NULL;
|
||||
buf += len + 1;
|
||||
while (!is_eol(*buf)) {
|
||||
buf = skip_space(buf);
|
||||
len = strcspn(buf, ", \r\n");
|
||||
if (!(tmp = av_realloc(order, (*number + 1) * sizeof(*order))))
|
||||
return NULL;
|
||||
order = tmp;
|
||||
order[*number] = -1;
|
||||
for (i=0; section->fields[i].name; i++)
|
||||
if (!strncmp(buf, section->fields[i].name, len)) {
|
||||
order[*number] = i;
|
||||
break;
|
||||
}
|
||||
(*number)++;
|
||||
buf = skip_space(buf + len + 1);
|
||||
}
|
||||
ctx->field_order[ctx->current_section] = order;
|
||||
} else if (section->fields_header) {
|
||||
len = strlen(section->fields_header);
|
||||
if (!strncmp(buf, section->fields_header, len) && buf[len] == ':') {
|
||||
uint8_t *ptr, *struct_ptr = realloc_section_array(ctx);
|
||||
if (!struct_ptr) return NULL;
|
||||
buf += len + 1;
|
||||
for (i=0; !is_eol(*buf) && i < *number; i++) {
|
||||
int last = i == *number - 1;
|
||||
buf = skip_space(buf);
|
||||
len = strcspn(buf, last ? "\r\n" : ",\r\n");
|
||||
if (order[i] >= 0) {
|
||||
ASSFieldType type = section->fields[order[i]].type;
|
||||
ptr = struct_ptr + section->fields[order[i]].offset;
|
||||
convert_func[type](ptr, buf, len);
|
||||
}
|
||||
buf = skip_space(buf + len + !last);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
len = strcspn(buf, ":\r\n");
|
||||
if (buf[len] == ':') {
|
||||
for (i=0; section->fields[i].name; i++)
|
||||
if (!strncmp(buf, section->fields[i].name, len)) {
|
||||
ASSFieldType type = section->fields[i].type;
|
||||
uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
|
||||
ptr += section->fields[i].offset;
|
||||
buf = skip_space(buf + len + 1);
|
||||
convert_func[type](ptr, buf, strcspn(buf, "\r\n"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
buf += strcspn(buf, "\n") + 1;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int ass_split(ASSSplitContext *ctx, const char *buf)
|
||||
{
|
||||
char c, section[16];
|
||||
int i;
|
||||
|
||||
if (ctx->current_section >= 0)
|
||||
buf = ass_split_section(ctx, buf);
|
||||
|
||||
while (buf && *buf) {
|
||||
if (sscanf(buf, "[%15[0-9A-Za-z+ ]]%c", section, &c) == 2) {
|
||||
buf += strcspn(buf, "\n") + 1;
|
||||
for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
|
||||
if (!strcmp(section, ass_sections[i].section)) {
|
||||
ctx->current_section = i;
|
||||
buf = ass_split_section(ctx, buf);
|
||||
}
|
||||
} else
|
||||
buf += strcspn(buf, "\n") + 1;
|
||||
}
|
||||
return buf ? 0 : AVERROR_INVALIDDATA;
|
||||
}
|
||||
|
||||
ASSSplitContext *ff_ass_split(const char *buf)
|
||||
{
|
||||
ASSSplitContext *ctx = av_mallocz(sizeof(*ctx));
|
||||
ctx->current_section = -1;
|
||||
if (ass_split(ctx, buf) < 0) {
|
||||
ff_ass_split_free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void free_section(ASSSplitContext *ctx, const ASSSection *section)
|
||||
{
|
||||
uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
|
||||
int i, j, *count, c = 1;
|
||||
|
||||
if (section->format_header) {
|
||||
ptr = *(void **)ptr;
|
||||
count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
|
||||
} else
|
||||
count = &c;
|
||||
|
||||
if (ptr)
|
||||
for (i=0; i<*count; i++, ptr += section->size)
|
||||
for (j=0; section->fields[j].name; j++) {
|
||||
const ASSFields *field = §ion->fields[j];
|
||||
if (field->type == ASS_STR)
|
||||
av_freep(ptr + field->offset);
|
||||
}
|
||||
*count = 0;
|
||||
|
||||
if (section->format_header)
|
||||
av_freep((uint8_t *)&ctx->ass + section->offset);
|
||||
}
|
||||
|
||||
ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
|
||||
int cache, int *number)
|
||||
{
|
||||
ASSDialog *dialog = NULL;
|
||||
int i, count;
|
||||
if (!cache)
|
||||
for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
|
||||
if (!strcmp(ass_sections[i].section, "Events")) {
|
||||
free_section(ctx, &ass_sections[i]);
|
||||
break;
|
||||
}
|
||||
count = ctx->ass.dialogs_count;
|
||||
if (ass_split(ctx, buf) == 0)
|
||||
dialog = ctx->ass.dialogs + count;
|
||||
if (number)
|
||||
*number = ctx->ass.dialogs_count - count;
|
||||
return dialog;
|
||||
}
|
||||
|
||||
void ff_ass_split_free(ASSSplitContext *ctx)
|
||||
{
|
||||
if (ctx) {
|
||||
int i;
|
||||
for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
|
||||
free_section(ctx, &ass_sections[i]);
|
||||
av_free(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
|
||||
const char *buf)
|
||||
{
|
||||
const char *text = NULL;
|
||||
char new_line[2];
|
||||
int text_len = 0;
|
||||
|
||||
while (*buf) {
|
||||
if (text && callbacks->text &&
|
||||
(sscanf(buf, "\\%1[nN]", new_line) == 1 ||
|
||||
!strncmp(buf, "{\\", 2))) {
|
||||
callbacks->text(priv, text, text_len);
|
||||
text = NULL;
|
||||
}
|
||||
if (sscanf(buf, "\\%1[nN]", new_line) == 1) {
|
||||
if (callbacks->new_line)
|
||||
callbacks->new_line(priv, new_line[0] == 'N');
|
||||
buf += 2;
|
||||
} else if (!strncmp(buf, "{\\", 2)) {
|
||||
buf++;
|
||||
while (*buf == '\\') {
|
||||
char style[2], c[2], sep[2], c_num[2] = "0", tmp[128] = {0};
|
||||
unsigned int color = 0xFFFFFFFF;
|
||||
int len, size = -1, an = -1, alpha = -1;
|
||||
int x1, y1, x2, y2, t1 = -1, t2 = -1;
|
||||
if (sscanf(buf, "\\%1[bisu]%1[01\\}]%n", style, c, &len) > 1) {
|
||||
int close = c[0] == '0' ? 1 : c[0] == '1' ? 0 : -1;
|
||||
len += close != -1;
|
||||
if (callbacks->style)
|
||||
callbacks->style(priv, style[0], close);
|
||||
} else if (sscanf(buf, "\\c%1[\\}]%n", sep, &len) > 0 ||
|
||||
sscanf(buf, "\\c&H%X&%1[\\}]%n", &color, sep, &len) > 1 ||
|
||||
sscanf(buf, "\\%1[1234]c%1[\\}]%n", c_num, sep, &len) > 1 ||
|
||||
sscanf(buf, "\\%1[1234]c&H%X&%1[\\}]%n", c_num, &color, sep, &len) > 2) {
|
||||
if (callbacks->color)
|
||||
callbacks->color(priv, color, c_num[0] - '0');
|
||||
} else if (sscanf(buf, "\\alpha%1[\\}]%n", sep, &len) > 0 ||
|
||||
sscanf(buf, "\\alpha&H%2X&%1[\\}]%n", &alpha, sep, &len) > 1 ||
|
||||
sscanf(buf, "\\%1[1234]a%1[\\}]%n", c_num, sep, &len) > 1 ||
|
||||
sscanf(buf, "\\%1[1234]a&H%2X&%1[\\}]%n", c_num, &alpha, sep, &len) > 2) {
|
||||
if (callbacks->alpha)
|
||||
callbacks->alpha(priv, alpha, c_num[0] - '0');
|
||||
} else if (sscanf(buf, "\\fn%1[\\}]%n", sep, &len) > 0 ||
|
||||
sscanf(buf, "\\fn%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
|
||||
if (callbacks->font_name)
|
||||
callbacks->font_name(priv, tmp[0] ? tmp : NULL);
|
||||
} else if (sscanf(buf, "\\fs%1[\\}]%n", sep, &len) > 0 ||
|
||||
sscanf(buf, "\\fs%u%1[\\}]%n", &size, sep, &len) > 1) {
|
||||
if (callbacks->font_size)
|
||||
callbacks->font_size(priv, size);
|
||||
} else if (sscanf(buf, "\\a%1[\\}]%n", sep, &len) > 0 ||
|
||||
sscanf(buf, "\\a%2u%1[\\}]%n", &an, sep, &len) > 1 ||
|
||||
sscanf(buf, "\\an%1[\\}]%n", sep, &len) > 0 ||
|
||||
sscanf(buf, "\\an%1u%1[\\}]%n", &an, sep, &len) > 1) {
|
||||
if (an != -1 && buf[2] != 'n')
|
||||
an = (an&3) + (an&4 ? 6 : an&8 ? 3 : 0);
|
||||
if (callbacks->alignment)
|
||||
callbacks->alignment(priv, an);
|
||||
} else if (sscanf(buf, "\\r%1[\\}]%n", sep, &len) > 0 ||
|
||||
sscanf(buf, "\\r%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
|
||||
if (callbacks->cancel_overrides)
|
||||
callbacks->cancel_overrides(priv, tmp);
|
||||
} else if (sscanf(buf, "\\move(%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, sep, &len) > 4 ||
|
||||
sscanf(buf, "\\move(%d,%d,%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, &t1, &t2, sep, &len) > 6) {
|
||||
if (callbacks->move)
|
||||
callbacks->move(priv, x1, y1, x2, y2, t1, t2);
|
||||
} else if (sscanf(buf, "\\pos(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
|
||||
if (callbacks->move)
|
||||
callbacks->move(priv, x1, y1, x1, y1, -1, -1);
|
||||
} else if (sscanf(buf, "\\org(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
|
||||
if (callbacks->origin)
|
||||
callbacks->origin(priv, x1, y1);
|
||||
} else {
|
||||
len = strcspn(buf+1, "\\}") + 2; /* skip unknown code */
|
||||
}
|
||||
buf += len - 1;
|
||||
}
|
||||
if (*buf++ != '}')
|
||||
return AVERROR_INVALIDDATA;
|
||||
} else {
|
||||
if (!text) {
|
||||
text = buf;
|
||||
text_len = 1;
|
||||
} else
|
||||
text_len++;
|
||||
buf++;
|
||||
}
|
||||
}
|
||||
if (text && callbacks->text)
|
||||
callbacks->text(priv, text, text_len);
|
||||
if (callbacks->end)
|
||||
callbacks->end(priv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSStyle *ass_style_get(ASSSplitContext *ctx, const char *style)
|
||||
{
|
||||
ASS *ass = &ctx->ass;
|
||||
int i;
|
||||
|
||||
if (!style || !*style)
|
||||
style = "Default";
|
||||
for (i=0; i<ass->styles_count; i++)
|
||||
if (!strcmp(ass->styles[i].name, style))
|
||||
return ass->styles + i;
|
||||
return NULL;
|
||||
}
|
172
libavcodec/ass_split.h
Normal file
172
libavcodec/ass_split.h
Normal file
@ -0,0 +1,172 @@
|
||||
/*
|
||||
* SSA/ASS spliting functions
|
||||
* Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
|
||||
*
|
||||
* This file is part of FFmpeg.
|
||||
*
|
||||
* FFmpeg is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* FFmpeg is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with FFmpeg; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef AVCODEC_ASS_SPLIT_H
|
||||
#define AVCODEC_ASS_SPLIT_H
|
||||
|
||||
/**
|
||||
* fields extracted from the [Script Info] section
|
||||
*/
|
||||
typedef struct {
|
||||
char *script_type; /**< SSA script format version (eg. v4.00) */
|
||||
char *collisions; /**< how subtitles are moved to prevent collisions */
|
||||
int play_res_x; /**< video width that ASS coords are referring to */
|
||||
int play_res_y; /**< video height that ASS coords are referring to */
|
||||
float timer; /**< time multiplier to apply to SSA clock (in %) */
|
||||
} ASSScriptInfo;
|
||||
|
||||
/**
|
||||
* fields extracted from the [V4(+) Styles] section
|
||||
*/
|
||||
typedef struct {
|
||||
char *name; /**< name of the tyle (case sensitive) */
|
||||
char *font_name; /**< font face (case sensitive) */
|
||||
int font_size; /**< font height */
|
||||
int primary_color; /**< color that a subtitle will normally appear in */
|
||||
int back_color; /**< color of the subtitle outline or shadow */
|
||||
int bold; /**< whether text is bold (1) or not (0) */
|
||||
int italic; /**< whether text is italic (1) or not (0) */
|
||||
int underline; /**< whether text is underlined (1) or not (0) */
|
||||
int alignment; /**< position of the text (left, center, top...),
|
||||
defined after the layout of the numpad
|
||||
(1-3 sub, 4-6 mid, 7-9 top) */
|
||||
} ASSStyle;
|
||||
|
||||
/**
|
||||
* fields extracted from the [Events] section
|
||||
*/
|
||||
typedef struct {
|
||||
int layer; /**< higher numbered layers are drawn over lower numbered */
|
||||
int start; /**< start time of the dialog in centiseconds */
|
||||
int end; /**< end time of the dialog in centiseconds */
|
||||
char *style; /**< name of the ASSStyle to use with this dialog */
|
||||
char *text; /**< actual text which will be displayed as a subtitle,
|
||||
can include style override control codes (see
|
||||
ff_ass_split_override_codes()) */
|
||||
} ASSDialog;
|
||||
|
||||
/**
|
||||
* structure containing the whole split ASS data
|
||||
*/
|
||||
typedef struct {
|
||||
ASSScriptInfo script_info; /**< general information about the SSA script*/
|
||||
ASSStyle *styles; /**< array of split out styles */
|
||||
int styles_count; /**< number of ASSStyle in the styles array */
|
||||
ASSDialog *dialogs; /**< array of split out dialogs */
|
||||
int dialogs_count; /**< number of ASSDialog in the dialogs array*/
|
||||
} ASS;
|
||||
|
||||
/**
|
||||
* This struct can be casted to ASS to access to the split data.
|
||||
*/
|
||||
typedef struct ASSSplitContext ASSSplitContext;
|
||||
|
||||
/**
|
||||
* Split a full ASS file or a ASS header from a string buffer and store
|
||||
* the split structure in a newly allocated context.
|
||||
*
|
||||
* @param buf String containing the ASS formated data.
|
||||
* @return Newly allocated struct containing split data.
|
||||
*/
|
||||
ASSSplitContext *ff_ass_split(const char *buf);
|
||||
|
||||
/**
|
||||
* Split one or several ASS "Dialogue" lines from a string buffer and store
|
||||
* them in a already initialized context.
|
||||
*
|
||||
* @param ctx Context previously initialized by ff_ass_split().
|
||||
* @param buf String containing the ASS "Dialogue" lines.
|
||||
* @param cache Set to 1 to keep all the previously split ASSDialog in
|
||||
* the context, or set to 0 to free all the previously split
|
||||
* ASSDialog.
|
||||
* @param number If not NULL, the pointed integer will be set to the number
|
||||
* of split ASSDialog.
|
||||
* @return Pointer to the first split ASSDialog.
|
||||
*/
|
||||
ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
|
||||
int cache, int *number);
|
||||
|
||||
/**
|
||||
* Free all the memory allocated for an ASSSplitContext.
|
||||
*
|
||||
* @param ctx Context previously initialized by ff_ass_split().
|
||||
*/
|
||||
void ff_ass_split_free(ASSSplitContext *ctx);
|
||||
|
||||
|
||||
/**
|
||||
* Set of callback functions corresponding to each override codes that can
|
||||
* be encountered in a "Dialogue" Text field.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* @defgroup ass_styles ASS styles
|
||||
* @{
|
||||
*/
|
||||
void (*text)(void *priv, const char *text, int len);
|
||||
void (*new_line)(void *priv, int forced);
|
||||
void (*style)(void *priv, char style, int close);
|
||||
void (*color)(void *priv, unsigned int color, unsigned int color_id);
|
||||
void (*alpha)(void *priv, int alpha, int alpha_id);
|
||||
void (*font_name)(void *priv, const char *name);
|
||||
void (*font_size)(void *priv, int size);
|
||||
void (*alignment)(void *priv, int alignment);
|
||||
void (*cancel_overrides)(void *priv, const char *style);
|
||||
/**< @} */
|
||||
|
||||
/**
|
||||
* @defgroup ass_functions ASS functions
|
||||
* @{
|
||||
*/
|
||||
void (*move)(void *priv, int x1, int y1, int x2, int y2, int t1, int t2);
|
||||
void (*origin)(void *priv, int x, int y);
|
||||
/**< @} */
|
||||
|
||||
/**
|
||||
* @defgroup ass_end end of Dialogue Event
|
||||
* @{
|
||||
*/
|
||||
void (*end)(void *priv);
|
||||
/**< @} */
|
||||
} ASSCodesCallbacks;
|
||||
|
||||
/**
|
||||
* Split override codes out of a ASS "Dialogue" Text field.
|
||||
*
|
||||
* @param callbacks Set of callback functions called for each override code
|
||||
* encountered.
|
||||
* @param priv Opaque pointer passed to the callback functions.
|
||||
* @param buf The ASS "Dialogue" Text field to split.
|
||||
* @return >= 0 on success otherwise an error code <0
|
||||
*/
|
||||
int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
|
||||
const char *buf);
|
||||
|
||||
/**
|
||||
* Find an ASSStyle structure by its name.
|
||||
*
|
||||
* @param ctx Context previously initialized by ff_ass_split().
|
||||
* @param style name of the style to search for.
|
||||
* @return the ASSStyle corresponding to style, or NULL if style can't be found
|
||||
*/
|
||||
ASSStyle *ass_style_get(ASSSplitContext *ctx, const char *style);
|
||||
|
||||
#endif /* AVCODEC_ASS_SPLIT_H */
|
301
libavcodec/srtenc.c
Normal file
301
libavcodec/srtenc.c
Normal file
@ -0,0 +1,301 @@
|
||||
/*
|
||||
* SubRip subtitle encoder
|
||||
* Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
|
||||
*
|
||||
* This file is part of FFmpeg.
|
||||
*
|
||||
* FFmpeg is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* FFmpeg is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with FFmpeg; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include "avcodec.h"
|
||||
#include "libavutil/avstring.h"
|
||||
#include "ass_split.h"
|
||||
#include "ass.h"
|
||||
|
||||
|
||||
#define SRT_STACK_SIZE 64
|
||||
|
||||
typedef struct {
|
||||
AVCodecContext *avctx;
|
||||
ASSSplitContext *ass_ctx;
|
||||
char buffer[2048];
|
||||
char *ptr;
|
||||
char *end;
|
||||
char *dialog_start;
|
||||
int count;
|
||||
char stack[SRT_STACK_SIZE];
|
||||
int stack_ptr;
|
||||
int alignment_applied;
|
||||
} SRTContext;
|
||||
|
||||
|
||||
#ifdef __GNUC__
|
||||
__attribute__ ((__format__ (__printf__, 2, 3)))
|
||||
#endif
|
||||
static void srt_print(SRTContext *s, const char *str, ...)
|
||||
{
|
||||
va_list vargs;
|
||||
va_start(vargs, str);
|
||||
s->ptr += vsnprintf(s->ptr, s->end - s->ptr, str, vargs);
|
||||
va_end(vargs);
|
||||
}
|
||||
|
||||
static int srt_stack_push(SRTContext *s, const char c)
|
||||
{
|
||||
if (s->stack_ptr >= SRT_STACK_SIZE)
|
||||
return -1;
|
||||
s->stack[s->stack_ptr++] = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char srt_stack_pop(SRTContext *s)
|
||||
{
|
||||
if (s->stack_ptr <= 0)
|
||||
return 0;
|
||||
return s->stack[--s->stack_ptr];
|
||||
}
|
||||
|
||||
static int srt_stack_find(SRTContext *s, const char c)
|
||||
{
|
||||
int i;
|
||||
for (i = s->stack_ptr-1; i >= 0; i--)
|
||||
if (s->stack[i] == c)
|
||||
break;
|
||||
return i;
|
||||
}
|
||||
|
||||
static void srt_close_tag(SRTContext *s, char tag)
|
||||
{
|
||||
srt_print(s, "</%c%s>", tag, tag == 'f' ? "ont" : "");
|
||||
}
|
||||
|
||||
static void srt_stack_push_pop(SRTContext *s, const char c, int close)
|
||||
{
|
||||
if (close) {
|
||||
int i = c ? srt_stack_find(s, c) : 0;
|
||||
if (i < 0)
|
||||
return;
|
||||
while (s->stack_ptr != i)
|
||||
srt_close_tag(s, srt_stack_pop(s));
|
||||
} else if (srt_stack_push(s, c) < 0)
|
||||
av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
|
||||
}
|
||||
|
||||
static void srt_style_apply(SRTContext *s, const char *style)
|
||||
{
|
||||
ASSStyle *st = ass_style_get(s->ass_ctx, style);
|
||||
if (st) {
|
||||
int c = st->primary_color & 0xFFFFFF;
|
||||
if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT) ||
|
||||
st->font_size != ASS_DEFAULT_FONT_SIZE ||
|
||||
c != ASS_DEFAULT_COLOR) {
|
||||
srt_print(s, "<font");
|
||||
if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT))
|
||||
srt_print(s, " face=\"%s\"", st->font_name);
|
||||
if (st->font_size != ASS_DEFAULT_FONT_SIZE)
|
||||
srt_print(s, " size=\"%d\"", st->font_size);
|
||||
if (c != ASS_DEFAULT_COLOR)
|
||||
srt_print(s, " color=\"#%06x\"",
|
||||
(c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
|
||||
srt_print(s, ">");
|
||||
srt_stack_push(s, 'f');
|
||||
}
|
||||
if (st->bold != ASS_DEFAULT_BOLD) {
|
||||
srt_print(s, "<b>");
|
||||
srt_stack_push(s, 'b');
|
||||
}
|
||||
if (st->italic != ASS_DEFAULT_ITALIC) {
|
||||
srt_print(s, "<i>");
|
||||
srt_stack_push(s, 'i');
|
||||
}
|
||||
if (st->underline != ASS_DEFAULT_UNDERLINE) {
|
||||
srt_print(s, "<u>");
|
||||
srt_stack_push(s, 'u');
|
||||
}
|
||||
if (st->alignment != ASS_DEFAULT_ALIGNMENT) {
|
||||
srt_print(s, "{\\an%d}", st->alignment);
|
||||
s->alignment_applied = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static av_cold int srt_encode_init(AVCodecContext *avctx)
|
||||
{
|
||||
SRTContext *s = avctx->priv_data;
|
||||
s->avctx = avctx;
|
||||
s->ass_ctx = ff_ass_split(avctx->subtitle_header);
|
||||
return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
|
||||
}
|
||||
|
||||
static void srt_text_cb(void *priv, const char *text, int len)
|
||||
{
|
||||
SRTContext *s = priv;
|
||||
av_strlcpy(s->ptr, text, FFMIN(s->end-s->ptr, len+1));
|
||||
s->ptr += len;
|
||||
}
|
||||
|
||||
static void srt_new_line_cb(void *priv, int forced)
|
||||
{
|
||||
srt_print(priv, "\r\n");
|
||||
}
|
||||
|
||||
static void srt_style_cb(void *priv, char style, int close)
|
||||
{
|
||||
srt_stack_push_pop(priv, style, close);
|
||||
if (!close)
|
||||
srt_print(priv, "<%c>", style);
|
||||
}
|
||||
|
||||
static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
|
||||
{
|
||||
if (color_id > 1)
|
||||
return;
|
||||
srt_stack_push_pop(priv, 'f', color == 0xFFFFFFFF);
|
||||
if (color != 0xFFFFFFFF)
|
||||
srt_print(priv, "<font color=\"#%06x\">",
|
||||
(color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
|
||||
}
|
||||
|
||||
static void srt_font_name_cb(void *priv, const char *name)
|
||||
{
|
||||
srt_stack_push_pop(priv, 'f', !name);
|
||||
if (name)
|
||||
srt_print(priv, "<font face=\"%s\">", name);
|
||||
}
|
||||
|
||||
static void srt_font_size_cb(void *priv, int size)
|
||||
{
|
||||
srt_stack_push_pop(priv, 'f', size < 0);
|
||||
if (size >= 0)
|
||||
srt_print(priv, "<font size=\"%d\">", size);
|
||||
}
|
||||
|
||||
static void srt_alignment_cb(void *priv, int alignment)
|
||||
{
|
||||
SRTContext *s = priv;
|
||||
if (!s->alignment_applied && alignment >= 0) {
|
||||
srt_print(s, "{\\an%d}", alignment);
|
||||
s->alignment_applied = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void srt_cancel_overrides_cb(void *priv, const char *style)
|
||||
{
|
||||
srt_stack_push_pop(priv, 0, 1);
|
||||
srt_style_apply(priv, style);
|
||||
}
|
||||
|
||||
static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2,
|
||||
int t1, int t2)
|
||||
{
|
||||
SRTContext *s = priv;
|
||||
char buffer[32];
|
||||
int len = snprintf(buffer, sizeof(buffer),
|
||||
" X1:%03u X2:%03u Y1:%03u Y2:%03u", x1, x2, y1, y2);
|
||||
if (s->end - s->ptr > len) {
|
||||
memmove(s->dialog_start+len, s->dialog_start, s->ptr-s->dialog_start+1);
|
||||
memcpy(s->dialog_start, buffer, len);
|
||||
s->ptr += len;
|
||||
}
|
||||
}
|
||||
|
||||
static void srt_end_cb(void *priv)
|
||||
{
|
||||
srt_stack_push_pop(priv, 0, 1);
|
||||
srt_print(priv, "\r\n\r\n");
|
||||
}
|
||||
|
||||
static const ASSCodesCallbacks srt_callbacks = {
|
||||
.text = srt_text_cb,
|
||||
.new_line = srt_new_line_cb,
|
||||
.style = srt_style_cb,
|
||||
.color = srt_color_cb,
|
||||
.font_name = srt_font_name_cb,
|
||||
.font_size = srt_font_size_cb,
|
||||
.alignment = srt_alignment_cb,
|
||||
.cancel_overrides = srt_cancel_overrides_cb,
|
||||
.move = srt_move_cb,
|
||||
.end = srt_end_cb,
|
||||
};
|
||||
|
||||
static int srt_encode_frame(AVCodecContext *avctx,
|
||||
unsigned char *buf, int bufsize, void *data)
|
||||
{
|
||||
SRTContext *s = avctx->priv_data;
|
||||
AVSubtitle *sub = data;
|
||||
ASSDialog *dialog;
|
||||
int i, len, num;
|
||||
|
||||
s->ptr = s->buffer;
|
||||
s->end = s->ptr + sizeof(s->buffer);
|
||||
|
||||
for (i=0; i<sub->num_rects; i++) {
|
||||
|
||||
if (sub->rects[i]->type != SUBTITLE_ASS) {
|
||||
av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
|
||||
return AVERROR(ENOSYS);
|
||||
}
|
||||
|
||||
dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num);
|
||||
for (; dialog && num--; dialog++) {
|
||||
int sh, sm, ss, sc = 10 * dialog->start;
|
||||
int eh, em, es, ec = 10 * dialog->end;
|
||||
sh = sc/3600000; sc -= 3600000*sh;
|
||||
sm = sc/ 60000; sc -= 60000*sm;
|
||||
ss = sc/ 1000; sc -= 1000*ss;
|
||||
eh = ec/3600000; ec -= 3600000*eh;
|
||||
em = ec/ 60000; ec -= 60000*em;
|
||||
es = ec/ 1000; ec -= 1000*es;
|
||||
srt_print(s,"%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
|
||||
++s->count, sh, sm, ss, sc, eh, em, es, ec);
|
||||
s->alignment_applied = 0;
|
||||
s->dialog_start = s->ptr - 2;
|
||||
srt_style_apply(s, dialog->style);
|
||||
ff_ass_split_override_codes(&srt_callbacks, s, dialog->text);
|
||||
}
|
||||
}
|
||||
|
||||
if (s->ptr == s->buffer)
|
||||
return 0;
|
||||
|
||||
len = av_strlcpy(buf, s->buffer, bufsize);
|
||||
|
||||
if (len > bufsize-1) {
|
||||
av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static int srt_encode_close(AVCodecContext *avctx)
|
||||
{
|
||||
SRTContext *s = avctx->priv_data;
|
||||
ff_ass_split_free(s->ass_ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
AVCodec ff_srt_encoder = {
|
||||
.name = "srt",
|
||||
.long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
|
||||
.type = AVMEDIA_TYPE_SUBTITLE,
|
||||
.id = CODEC_ID_SRT,
|
||||
.priv_data_size = sizeof(SRTContext),
|
||||
.init = srt_encode_init,
|
||||
.encode = srt_encode_frame,
|
||||
.close = srt_encode_close,
|
||||
};
|
@ -21,7 +21,7 @@
|
||||
#define AVCODEC_VERSION_H
|
||||
|
||||
#define LIBAVCODEC_VERSION_MAJOR 52
|
||||
#define LIBAVCODEC_VERSION_MINOR 115
|
||||
#define LIBAVCODEC_VERSION_MINOR 116
|
||||
#define LIBAVCODEC_VERSION_MICRO 0
|
||||
|
||||
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
|
||||
|
Loading…
Reference in New Issue
Block a user