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