00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdarg.h>
00023 #include "avcodec.h"
00024 #include "libavutil/avstring.h"
00025 #include "ass_split.h"
00026 #include "ass.h"
00027
00028
00029 #define SRT_STACK_SIZE 64
00030
00031 typedef struct {
00032 AVCodecContext *avctx;
00033 ASSSplitContext *ass_ctx;
00034 char buffer[2048];
00035 char *ptr;
00036 char *end;
00037 char *dialog_start;
00038 int count;
00039 char stack[SRT_STACK_SIZE];
00040 int stack_ptr;
00041 int alignment_applied;
00042 } SRTContext;
00043
00044
00045 #ifdef __GNUC__
00046 __attribute__ ((__format__ (__printf__, 2, 3)))
00047 #endif
00048 static void srt_print(SRTContext *s, const char *str, ...)
00049 {
00050 va_list vargs;
00051 va_start(vargs, str);
00052 s->ptr += vsnprintf(s->ptr, s->end - s->ptr, str, vargs);
00053 va_end(vargs);
00054 }
00055
00056 static int srt_stack_push(SRTContext *s, const char c)
00057 {
00058 if (s->stack_ptr >= SRT_STACK_SIZE)
00059 return -1;
00060 s->stack[s->stack_ptr++] = c;
00061 return 0;
00062 }
00063
00064 static char srt_stack_pop(SRTContext *s)
00065 {
00066 if (s->stack_ptr <= 0)
00067 return 0;
00068 return s->stack[--s->stack_ptr];
00069 }
00070
00071 static int srt_stack_find(SRTContext *s, const char c)
00072 {
00073 int i;
00074 for (i = s->stack_ptr-1; i >= 0; i--)
00075 if (s->stack[i] == c)
00076 break;
00077 return i;
00078 }
00079
00080 static void srt_close_tag(SRTContext *s, char tag)
00081 {
00082 srt_print(s, "</%c%s>", tag, tag == 'f' ? "ont" : "");
00083 }
00084
00085 static void srt_stack_push_pop(SRTContext *s, const char c, int close)
00086 {
00087 if (close) {
00088 int i = c ? srt_stack_find(s, c) : 0;
00089 if (i < 0)
00090 return;
00091 while (s->stack_ptr != i)
00092 srt_close_tag(s, srt_stack_pop(s));
00093 } else if (srt_stack_push(s, c) < 0)
00094 av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
00095 }
00096
00097 static void srt_style_apply(SRTContext *s, const char *style)
00098 {
00099 ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
00100 if (st) {
00101 int c = st->primary_color & 0xFFFFFF;
00102 if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT) ||
00103 st->font_size != ASS_DEFAULT_FONT_SIZE ||
00104 c != ASS_DEFAULT_COLOR) {
00105 srt_print(s, "<font");
00106 if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT))
00107 srt_print(s, " face=\"%s\"", st->font_name);
00108 if (st->font_size != ASS_DEFAULT_FONT_SIZE)
00109 srt_print(s, " size=\"%d\"", st->font_size);
00110 if (c != ASS_DEFAULT_COLOR)
00111 srt_print(s, " color=\"#%06x\"",
00112 (c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
00113 srt_print(s, ">");
00114 srt_stack_push(s, 'f');
00115 }
00116 if (st->bold != ASS_DEFAULT_BOLD) {
00117 srt_print(s, "<b>");
00118 srt_stack_push(s, 'b');
00119 }
00120 if (st->italic != ASS_DEFAULT_ITALIC) {
00121 srt_print(s, "<i>");
00122 srt_stack_push(s, 'i');
00123 }
00124 if (st->underline != ASS_DEFAULT_UNDERLINE) {
00125 srt_print(s, "<u>");
00126 srt_stack_push(s, 'u');
00127 }
00128 if (st->alignment != ASS_DEFAULT_ALIGNMENT) {
00129 srt_print(s, "{\\an%d}", st->alignment);
00130 s->alignment_applied = 1;
00131 }
00132 }
00133 }
00134
00135
00136 static av_cold int srt_encode_init(AVCodecContext *avctx)
00137 {
00138 SRTContext *s = avctx->priv_data;
00139 s->avctx = avctx;
00140 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
00141 return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
00142 }
00143
00144 static void srt_text_cb(void *priv, const char *text, int len)
00145 {
00146 SRTContext *s = priv;
00147 av_strlcpy(s->ptr, text, FFMIN(s->end-s->ptr, len+1));
00148 s->ptr += len;
00149 }
00150
00151 static void srt_new_line_cb(void *priv, int forced)
00152 {
00153 srt_print(priv, "\r\n");
00154 }
00155
00156 static void srt_style_cb(void *priv, char style, int close)
00157 {
00158 srt_stack_push_pop(priv, style, close);
00159 if (!close)
00160 srt_print(priv, "<%c>", style);
00161 }
00162
00163 static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
00164 {
00165 if (color_id > 1)
00166 return;
00167 srt_stack_push_pop(priv, 'f', color == 0xFFFFFFFF);
00168 if (color != 0xFFFFFFFF)
00169 srt_print(priv, "<font color=\"#%06x\">",
00170 (color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
00171 }
00172
00173 static void srt_font_name_cb(void *priv, const char *name)
00174 {
00175 srt_stack_push_pop(priv, 'f', !name);
00176 if (name)
00177 srt_print(priv, "<font face=\"%s\">", name);
00178 }
00179
00180 static void srt_font_size_cb(void *priv, int size)
00181 {
00182 srt_stack_push_pop(priv, 'f', size < 0);
00183 if (size >= 0)
00184 srt_print(priv, "<font size=\"%d\">", size);
00185 }
00186
00187 static void srt_alignment_cb(void *priv, int alignment)
00188 {
00189 SRTContext *s = priv;
00190 if (!s->alignment_applied && alignment >= 0) {
00191 srt_print(s, "{\\an%d}", alignment);
00192 s->alignment_applied = 1;
00193 }
00194 }
00195
00196 static void srt_cancel_overrides_cb(void *priv, const char *style)
00197 {
00198 srt_stack_push_pop(priv, 0, 1);
00199 srt_style_apply(priv, style);
00200 }
00201
00202 static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2,
00203 int t1, int t2)
00204 {
00205 SRTContext *s = priv;
00206 char buffer[32];
00207 int len = snprintf(buffer, sizeof(buffer),
00208 " X1:%03u X2:%03u Y1:%03u Y2:%03u", x1, x2, y1, y2);
00209 if (s->end - s->ptr > len) {
00210 memmove(s->dialog_start+len, s->dialog_start, s->ptr-s->dialog_start+1);
00211 memcpy(s->dialog_start, buffer, len);
00212 s->ptr += len;
00213 }
00214 }
00215
00216 static void srt_end_cb(void *priv)
00217 {
00218 srt_stack_push_pop(priv, 0, 1);
00219 srt_print(priv, "\r\n\r\n");
00220 }
00221
00222 static const ASSCodesCallbacks srt_callbacks = {
00223 .text = srt_text_cb,
00224 .new_line = srt_new_line_cb,
00225 .style = srt_style_cb,
00226 .color = srt_color_cb,
00227 .font_name = srt_font_name_cb,
00228 .font_size = srt_font_size_cb,
00229 .alignment = srt_alignment_cb,
00230 .cancel_overrides = srt_cancel_overrides_cb,
00231 .move = srt_move_cb,
00232 .end = srt_end_cb,
00233 };
00234
00235 static int srt_encode_frame(AVCodecContext *avctx,
00236 unsigned char *buf, int bufsize, const AVSubtitle *sub)
00237 {
00238 SRTContext *s = avctx->priv_data;
00239 ASSDialog *dialog;
00240 int i, len, num;
00241
00242 s->ptr = s->buffer;
00243 s->end = s->ptr + sizeof(s->buffer);
00244
00245 for (i=0; i<sub->num_rects; i++) {
00246
00247 if (sub->rects[i]->type != SUBTITLE_ASS) {
00248 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
00249 return AVERROR(ENOSYS);
00250 }
00251
00252 dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num);
00253 for (; dialog && num--; dialog++) {
00254 if (avctx->codec->id == CODEC_ID_SRT) {
00255 int sh, sm, ss, sc = 10 * dialog->start;
00256 int eh, em, es, ec = 10 * dialog->end;
00257 sh = sc/3600000; sc -= 3600000*sh;
00258 sm = sc/ 60000; sc -= 60000*sm;
00259 ss = sc/ 1000; sc -= 1000*ss;
00260 eh = ec/3600000; ec -= 3600000*eh;
00261 em = ec/ 60000; ec -= 60000*em;
00262 es = ec/ 1000; ec -= 1000*es;
00263 srt_print(s,"%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
00264 ++s->count, sh, sm, ss, sc, eh, em, es, ec);
00265 }
00266 s->alignment_applied = 0;
00267 s->dialog_start = s->ptr - 2;
00268 srt_style_apply(s, dialog->style);
00269 ff_ass_split_override_codes(&srt_callbacks, s, dialog->text);
00270 }
00271 }
00272
00273 if (s->ptr == s->buffer)
00274 return 0;
00275
00276 len = av_strlcpy(buf, s->buffer, bufsize);
00277
00278 if (len > bufsize-1) {
00279 av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
00280 return -1;
00281 }
00282
00283 return len;
00284 }
00285
00286 static int srt_encode_close(AVCodecContext *avctx)
00287 {
00288 SRTContext *s = avctx->priv_data;
00289 ff_ass_split_free(s->ass_ctx);
00290 return 0;
00291 }
00292
00293 #if CONFIG_SRT_ENCODER
00294 AVCodec ff_srt_encoder = {
00295 .name = "srt",
00296 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle with embedded timing"),
00297 .type = AVMEDIA_TYPE_SUBTITLE,
00298 .id = AV_CODEC_ID_SRT,
00299 .priv_data_size = sizeof(SRTContext),
00300 .init = srt_encode_init,
00301 .encode_sub = srt_encode_frame,
00302 .close = srt_encode_close,
00303 };
00304 #endif
00305
00306 #if CONFIG_SUBRIP_ENCODER
00307 AVCodec ff_subrip_encoder = {
00308 .name = "subrip",
00309 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
00310 .type = AVMEDIA_TYPE_SUBTITLE,
00311 .id = AV_CODEC_ID_SUBRIP,
00312 .priv_data_size = sizeof(SRTContext),
00313 .init = srt_encode_init,
00314 .encode_sub = srt_encode_frame,
00315 .close = srt_encode_close,
00316 };
00317 #endif