00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "ass.h"
00024
00040 static int ff_ass_subtitle_header(AVCodecContext *avctx,
00041 const char *font, int font_size,
00042 int color, int back_color,
00043 int bold, int italic, int underline,
00044 int alignment)
00045 {
00046 char header[512];
00047
00048 snprintf(header, sizeof(header),
00049 "[Script Info]\r\n"
00050 "ScriptType: v4.00+\r\n"
00051 "\r\n"
00052 "[V4+ Styles]\r\n"
00053 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
00054 "Style: Default,%s,%d,&H%x,&H%x,&H%x,&H%x,%d,%d,%d,1,1,0,%d,10,10,10,0,0\r\n"
00055 "\r\n"
00056 "[Events]\r\n"
00057 "Format: Layer, Start, End, Text\r\n",
00058 font, font_size, color, color, back_color, back_color,
00059 -bold, -italic, -underline, alignment);
00060
00061 avctx->subtitle_header = av_strdup(header);
00062 if (!avctx->subtitle_header)
00063 return AVERROR(ENOMEM);
00064 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
00065 return 0;
00066 }
00067
00068 int ff_ass_subtitle_header_default(AVCodecContext *avctx)
00069 {
00070 return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
00071 ASS_DEFAULT_FONT_SIZE,
00072 ASS_DEFAULT_COLOR,
00073 ASS_DEFAULT_BACK_COLOR,
00074 ASS_DEFAULT_BOLD,
00075 ASS_DEFAULT_ITALIC,
00076 ASS_DEFAULT_UNDERLINE,
00077 ASS_DEFAULT_ALIGNMENT);
00078 }
00079
00080 void ff_ass_init(AVSubtitle *sub)
00081 {
00082 memset(sub, 0, sizeof(*sub));
00083 }
00084
00085 static int ts_to_string(char *str, int strlen, int ts)
00086 {
00087 int h, m, s;
00088 h = ts/360000; ts -= 360000*h;
00089 m = ts/ 6000; ts -= 6000*m;
00090 s = ts/ 100; ts -= 100*s;
00091 return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
00092 }
00093
00094 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
00095 int ts_start, int ts_end, int raw)
00096 {
00097 int len = 0, dlen, duration = ts_end - ts_start;
00098 char s_start[16], s_end[16], header[48] = {0};
00099 AVSubtitleRect **rects;
00100
00101 if (!raw) {
00102 ts_to_string(s_start, sizeof(s_start), ts_start);
00103 ts_to_string(s_end, sizeof(s_end), ts_end );
00104 len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,",
00105 s_start, s_end);
00106 }
00107
00108 dlen = strcspn(dialog, "\n");
00109 dlen += dialog[dlen] == '\n';
00110
00111 rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
00112 if (!rects)
00113 return AVERROR(ENOMEM);
00114 sub->rects = rects;
00115 sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
00116 rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
00117 rects[sub->num_rects]->type = SUBTITLE_ASS;
00118 rects[sub->num_rects]->ass = av_malloc(len + dlen + 1);
00119 strcpy (rects[sub->num_rects]->ass , header);
00120 strncpy(rects[sub->num_rects]->ass + len, dialog, dlen);
00121 rects[sub->num_rects]->ass[len+dlen] = 0;
00122 sub->num_rects++;
00123 return dlen;
00124 }