Make ff_ass_subtitle_header static to ass.c
[libav.git] / libavcodec / ass.c
CommitLineData
5952be07
AJ
1/*
2 * SSA/ASS common funtions
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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.
11 *
12 * FFmpeg 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avcodec.h"
23#include "ass.h"
24
cf1d794a
DEP
25/**
26 * Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS.
27 *
28 * @param avctx pointer to the AVCodecContext
29 * @param font name of the default font face to use
30 * @param font_size default font size to use
31 * @param color default text color to use (ABGR)
32 * @param back_color default background color to use (ABGR)
33 * @param bold 1 for bold text, 0 for normal text
34 * @param italic 1 for italic text, 0 for normal text
35 * @param underline 1 for underline text, 0 for normal text
36 * @param alignment position of the text (left, center, top...), defined after
37 * the layout of the numpad (1-3 sub, 4-6 mid, 7-9 top)
38 * @return >= 0 on success otherwise an error code <0
39 */
40static int ff_ass_subtitle_header(AVCodecContext *avctx,
41 const char *font, int font_size,
42 int color, int back_color,
43 int bold, int italic, int underline,
44 int alignment)
2c77c906
AJ
45{
46 char header[512];
47
48 snprintf(header, sizeof(header),
49 "[Script Info]\r\n"
50 "ScriptType: v4.00+\r\n"
51 "\r\n"
52 "[V4+ Styles]\r\n"
53 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
54 "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"
55 "\r\n"
56 "[Events]\r\n"
57 "Format: Layer, Start, End, Text\r\n",
58 font, font_size, color, color, back_color, back_color,
59 -bold, -italic, -underline, alignment);
60
61 avctx->subtitle_header = av_strdup(header);
62 if (!avctx->subtitle_header)
63 return AVERROR(ENOMEM);
64 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
65 return 0;
66}
67
68int ff_ass_subtitle_header_default(AVCodecContext *avctx)
69{
70 return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
71 ASS_DEFAULT_FONT_SIZE,
72 ASS_DEFAULT_COLOR,
73 ASS_DEFAULT_BACK_COLOR,
74 ASS_DEFAULT_BOLD,
75 ASS_DEFAULT_ITALIC,
76 ASS_DEFAULT_UNDERLINE,
77 ASS_DEFAULT_ALIGNMENT);
78}
79
5952be07
AJ
80void ff_ass_init(AVSubtitle *sub)
81{
82 memset(sub, 0, sizeof(*sub));
83}
84
85static int ts_to_string(char *str, int strlen, int ts)
86{
87 int h, m, s;
88 h = ts/360000; ts -= 360000*h;
89 m = ts/ 6000; ts -= 6000*m;
90 s = ts/ 100; ts -= 100*s;
91 return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
92}
93
94int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
95 int ts_start, int ts_end, int raw)
96{
97 int len = 0, dlen, duration = ts_end - ts_start;
98 char s_start[16], s_end[16], header[48] = {0};
99 AVSubtitleRect **rects;
100
101 if (!raw) {
102 ts_to_string(s_start, sizeof(s_start), ts_start);
103 ts_to_string(s_end, sizeof(s_end), ts_end );
104 len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,",
105 s_start, s_end);
106 }
107
108 dlen = strcspn(dialog, "\n");
109 dlen += dialog[dlen] == '\n';
110
111 rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
112 if (!rects)
113 return AVERROR(ENOMEM);
114 sub->rects = rects;
115 sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
116 rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
117 rects[sub->num_rects]->type = SUBTITLE_ASS;
118 rects[sub->num_rects]->ass = av_malloc(len + dlen + 1);
119 strcpy (rects[sub->num_rects]->ass , header);
120 strncpy(rects[sub->num_rects]->ass + len, dialog, dlen);
4ab4d65f 121 rects[sub->num_rects]->ass[len+dlen] = 0;
5952be07
AJ
122 sub->num_rects++;
123 return dlen;
124}