Commit | Line | Data |
---|---|---|
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 | ||
2c77c906 AJ |
25 | int ff_ass_subtitle_header(AVCodecContext *avctx, |
26 | const char *font, int font_size, | |
27 | int color, int back_color, | |
28 | int bold, int italic, int underline, | |
29 | int alignment) | |
30 | { | |
31 | char header[512]; | |
32 | ||
33 | snprintf(header, sizeof(header), | |
34 | "[Script Info]\r\n" | |
35 | "ScriptType: v4.00+\r\n" | |
36 | "\r\n" | |
37 | "[V4+ Styles]\r\n" | |
38 | "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n" | |
39 | "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" | |
40 | "\r\n" | |
41 | "[Events]\r\n" | |
42 | "Format: Layer, Start, End, Text\r\n", | |
43 | font, font_size, color, color, back_color, back_color, | |
44 | -bold, -italic, -underline, alignment); | |
45 | ||
46 | avctx->subtitle_header = av_strdup(header); | |
47 | if (!avctx->subtitle_header) | |
48 | return AVERROR(ENOMEM); | |
49 | avctx->subtitle_header_size = strlen(avctx->subtitle_header); | |
50 | return 0; | |
51 | } | |
52 | ||
53 | int ff_ass_subtitle_header_default(AVCodecContext *avctx) | |
54 | { | |
55 | return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT, | |
56 | ASS_DEFAULT_FONT_SIZE, | |
57 | ASS_DEFAULT_COLOR, | |
58 | ASS_DEFAULT_BACK_COLOR, | |
59 | ASS_DEFAULT_BOLD, | |
60 | ASS_DEFAULT_ITALIC, | |
61 | ASS_DEFAULT_UNDERLINE, | |
62 | ASS_DEFAULT_ALIGNMENT); | |
63 | } | |
64 | ||
5952be07 AJ |
65 | void ff_ass_init(AVSubtitle *sub) |
66 | { | |
67 | memset(sub, 0, sizeof(*sub)); | |
68 | } | |
69 | ||
70 | static int ts_to_string(char *str, int strlen, int ts) | |
71 | { | |
72 | int h, m, s; | |
73 | h = ts/360000; ts -= 360000*h; | |
74 | m = ts/ 6000; ts -= 6000*m; | |
75 | s = ts/ 100; ts -= 100*s; | |
76 | return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts); | |
77 | } | |
78 | ||
79 | int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, | |
80 | int ts_start, int ts_end, int raw) | |
81 | { | |
82 | int len = 0, dlen, duration = ts_end - ts_start; | |
83 | char s_start[16], s_end[16], header[48] = {0}; | |
84 | AVSubtitleRect **rects; | |
85 | ||
86 | if (!raw) { | |
87 | ts_to_string(s_start, sizeof(s_start), ts_start); | |
88 | ts_to_string(s_end, sizeof(s_end), ts_end ); | |
89 | len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,", | |
90 | s_start, s_end); | |
91 | } | |
92 | ||
93 | dlen = strcspn(dialog, "\n"); | |
94 | dlen += dialog[dlen] == '\n'; | |
95 | ||
96 | rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects)); | |
97 | if (!rects) | |
98 | return AVERROR(ENOMEM); | |
99 | sub->rects = rects; | |
100 | sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration); | |
101 | rects[sub->num_rects] = av_mallocz(sizeof(*rects[0])); | |
102 | rects[sub->num_rects]->type = SUBTITLE_ASS; | |
103 | rects[sub->num_rects]->ass = av_malloc(len + dlen + 1); | |
104 | strcpy (rects[sub->num_rects]->ass , header); | |
105 | strncpy(rects[sub->num_rects]->ass + len, dialog, dlen); | |
4ab4d65f | 106 | rects[sub->num_rects]->ass[len+dlen] = 0; |
5952be07 AJ |
107 | sub->num_rects++; |
108 | return dlen; | |
109 | } |