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
00025 int ff_ass_subtitle_header(AVCodecContext *avctx,
00026 const char *font, int font_size,
00027 int color, int back_color,
00028 int bold, int italic, int underline,
00029 int alignment)
00030 {
00031 char header[512];
00032
00033 snprintf(header, sizeof(header),
00034 "[Script Info]\r\n"
00035 "ScriptType: v4.00+\r\n"
00036 "\r\n"
00037 "[V4+ Styles]\r\n"
00038 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
00039 "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"
00040 "\r\n"
00041 "[Events]\r\n"
00042 "Format: Layer, Start, End, Text\r\n",
00043 font, font_size, color, color, back_color, back_color,
00044 -bold, -italic, -underline, alignment);
00045
00046 avctx->subtitle_header = av_strdup(header);
00047 if (!avctx->subtitle_header)
00048 return AVERROR(ENOMEM);
00049 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
00050 return 0;
00051 }
00052
00053 int ff_ass_subtitle_header_default(AVCodecContext *avctx)
00054 {
00055 return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
00056 ASS_DEFAULT_FONT_SIZE,
00057 ASS_DEFAULT_COLOR,
00058 ASS_DEFAULT_BACK_COLOR,
00059 ASS_DEFAULT_BOLD,
00060 ASS_DEFAULT_ITALIC,
00061 ASS_DEFAULT_UNDERLINE,
00062 ASS_DEFAULT_ALIGNMENT);
00063 }
00064
00065 void ff_ass_init(AVSubtitle *sub)
00066 {
00067 memset(sub, 0, sizeof(*sub));
00068 }
00069
00070 static int ts_to_string(char *str, int strlen, int ts)
00071 {
00072 int h, m, s;
00073 h = ts/360000; ts -= 360000*h;
00074 m = ts/ 6000; ts -= 6000*m;
00075 s = ts/ 100; ts -= 100*s;
00076 return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
00077 }
00078
00079 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
00080 int ts_start, int ts_end, int raw)
00081 {
00082 int len = 0, dlen, duration = ts_end - ts_start;
00083 char s_start[16], s_end[16], header[48] = {0};
00084 AVSubtitleRect **rects;
00085
00086 if (!raw) {
00087 ts_to_string(s_start, sizeof(s_start), ts_start);
00088 ts_to_string(s_end, sizeof(s_end), ts_end );
00089 len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,",
00090 s_start, s_end);
00091 }
00092
00093 dlen = strcspn(dialog, "\n");
00094 dlen += dialog[dlen] == '\n';
00095
00096 rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
00097 if (!rects)
00098 return AVERROR(ENOMEM);
00099 sub->rects = rects;
00100 sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
00101 rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
00102 rects[sub->num_rects]->type = SUBTITLE_ASS;
00103 rects[sub->num_rects]->ass = av_malloc(len + dlen + 1);
00104 strcpy (rects[sub->num_rects]->ass , header);
00105 strncpy(rects[sub->num_rects]->ass + len, dialog, dlen);
00106 rects[sub->num_rects]->ass[len+dlen] = 0;
00107 sub->num_rects++;
00108 return dlen;
00109 }