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/avassert.h"
00025 #include "libavutil/avstring.h"
00026 #include "libavutil/common.h"
00027 
00028 int ff_ass_subtitle_header(AVCodecContext *avctx,
00029                            const char *font, int font_size,
00030                            int color, int back_color,
00031                            int bold, int italic, int underline,
00032                            int alignment)
00033 {
00034     avctx->subtitle_header = av_asprintf(
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, Style, Text\r\n",
00044              font, font_size, color, color, back_color, back_color,
00045              -bold, -italic, -underline, alignment);
00046 
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 static int ts_to_string(char *str, int strlen, int ts)
00066 {
00067     int h, m, s;
00068     h = ts/360000;  ts -= 360000*h;
00069     m = ts/  6000;  ts -=   6000*m;
00070     s = ts/   100;  ts -=    100*s;
00071     return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
00072 }
00073 
00074 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
00075                     int ts_start, int duration, int raw)
00076 {
00077     int len = 0, dlen;
00078     char s_start[16], s_end[16], header[48] = {0};
00079     AVSubtitleRect **rects;
00080 
00081     if (!raw) {
00082         ts_to_string(s_start, sizeof(s_start), ts_start);
00083         if (duration == -1)
00084             snprintf(s_end, sizeof(s_end), "9:59:59.99");
00085         else
00086             ts_to_string(s_end, sizeof(s_end), ts_start + duration);
00087         len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,Default,",
00088                        s_start, s_end);
00089         av_assert0(len < sizeof(header));
00090     }
00091 
00092     dlen = strcspn(dialog, "\n");
00093     dlen += dialog[dlen] == '\n';
00094 
00095     rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
00096     if (!rects)
00097         return AVERROR(ENOMEM);
00098     sub->rects = rects;
00099     sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
00100     rects[sub->num_rects]       = av_mallocz(sizeof(*rects[0]));
00101     rects[sub->num_rects]->type = SUBTITLE_ASS;
00102     rects[sub->num_rects]->ass  = av_malloc(len + dlen + 1);
00103     strcpy (rects[sub->num_rects]->ass      , header);
00104     av_strlcpy(rects[sub->num_rects]->ass + len, dialog, dlen + 1);
00105     sub->num_rects++;
00106     return dlen;
00107 }