2 * SSA/ASS common funtions
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/avstring.h"
25 #include "libavutil/common.h"
28 * Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS.
30 * @param avctx pointer to the AVCodecContext
31 * @param font name of the default font face to use
32 * @param font_size default font size to use
33 * @param color default text color to use (ABGR)
34 * @param back_color default background color to use (ABGR)
35 * @param bold 1 for bold text, 0 for normal text
36 * @param italic 1 for italic text, 0 for normal text
37 * @param underline 1 for underline text, 0 for normal text
38 * @param alignment position of the text (left, center, top...), defined after
39 * the layout of the numpad (1-3 sub, 4-6 mid, 7-9 top)
40 * @return >= 0 on success otherwise an error code <0
42 static int ff_ass_subtitle_header(AVCodecContext
*avctx
,
43 const char *font
, int font_size
,
44 int color
, int back_color
,
45 int bold
, int italic
, int underline
,
50 snprintf(header
, sizeof(header
),
52 "ScriptType: v4.00+\r\n"
55 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
56 "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"
59 "Format: Layer, Start, End, Text\r\n",
60 font
, font_size
, color
, color
, back_color
, back_color
,
61 -bold
, -italic
, -underline
, alignment
);
63 avctx
->subtitle_header
= av_strdup(header
);
64 if (!avctx
->subtitle_header
)
65 return AVERROR(ENOMEM
);
66 avctx
->subtitle_header_size
= strlen(avctx
->subtitle_header
);
70 int ff_ass_subtitle_header_default(AVCodecContext
*avctx
)
72 return ff_ass_subtitle_header(avctx
, ASS_DEFAULT_FONT
,
73 ASS_DEFAULT_FONT_SIZE
,
75 ASS_DEFAULT_BACK_COLOR
,
78 ASS_DEFAULT_UNDERLINE
,
79 ASS_DEFAULT_ALIGNMENT
);
82 void ff_ass_init(AVSubtitle
*sub
)
84 memset(sub
, 0, sizeof(*sub
));
87 static int ts_to_string(char *str
, int strlen
, int ts
)
90 h
= ts
/360000; ts
-= 360000*h
;
91 m
= ts
/ 6000; ts
-= 6000*m
;
92 s
= ts
/ 100; ts
-= 100*s
;
93 return snprintf(str
, strlen
, "%d:%02d:%02d.%02d", h
, m
, s
, ts
);
96 int ff_ass_add_rect(AVSubtitle
*sub
, const char *dialog
,
97 int ts_start
, int ts_end
, int raw
)
99 int len
= 0, dlen
, duration
= ts_end
- ts_start
;
100 char s_start
[16], s_end
[16], header
[48] = {0};
101 AVSubtitleRect
**rects
;
104 ts_to_string(s_start
, sizeof(s_start
), ts_start
);
105 ts_to_string(s_end
, sizeof(s_end
), ts_end
);
106 len
= snprintf(header
, sizeof(header
), "Dialogue: 0,%s,%s,",
110 dlen
= strcspn(dialog
, "\n");
111 dlen
+= dialog
[dlen
] == '\n';
113 rects
= av_realloc(sub
->rects
, (sub
->num_rects
+1) * sizeof(*sub
->rects
));
115 return AVERROR(ENOMEM
);
117 sub
->end_display_time
= FFMAX(sub
->end_display_time
, 10 * duration
);
118 rects
[sub
->num_rects
] = av_mallocz(sizeof(*rects
[0]));
119 rects
[sub
->num_rects
]->type
= SUBTITLE_ASS
;
120 rects
[sub
->num_rects
]->ass
= av_malloc(len
+ dlen
+ 1);
121 strcpy (rects
[sub
->num_rects
]->ass
, header
);
122 av_strlcpy(rects
[sub
->num_rects
]->ass
+ len
, dialog
, dlen
+ 1);