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