Commit 8e43b6fe authored by Aurelien Jacobs's avatar Aurelien Jacobs

add SubRip encoder

Signed-off-by: 's avatarAurelien Jacobs <aurel@gnuage.org>
parent efc8c709
......@@ -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 */
......
This diff is collapsed.
/*
* 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 */
/*
* 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, \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment