]> git.sesse.net Git - ffmpeg/blob - libavcodec/ass.c
avcodec/dcadec: use a constant instead of assuming every compiler can optimize pow...
[ffmpeg] / libavcodec / ass.c
1 /*
2  * SSA/ASS common functions
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 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/bprint.h"
27 #include "libavutil/common.h"
28
29 int ff_ass_subtitle_header(AVCodecContext *avctx,
30                            const char *font, int font_size,
31                            int color, int back_color,
32                            int bold, int italic, int underline,
33                            int alignment)
34 {
35     avctx->subtitle_header = av_asprintf(
36              "[Script Info]\r\n"
37              "ScriptType: v4.00+\r\n"
38              "\r\n"
39              "[V4+ Styles]\r\n"
40              "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
41              "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"
42              "\r\n"
43              "[Events]\r\n"
44              "Format: Layer, Start, End, Style, Text\r\n",
45              font, font_size, color, color, back_color, back_color,
46              -bold, -italic, -underline, alignment);
47
48     if (!avctx->subtitle_header)
49         return AVERROR(ENOMEM);
50     avctx->subtitle_header_size = strlen(avctx->subtitle_header);
51     return 0;
52 }
53
54 int ff_ass_subtitle_header_default(AVCodecContext *avctx)
55 {
56     return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
57                                ASS_DEFAULT_FONT_SIZE,
58                                ASS_DEFAULT_COLOR,
59                                ASS_DEFAULT_BACK_COLOR,
60                                ASS_DEFAULT_BOLD,
61                                ASS_DEFAULT_ITALIC,
62                                ASS_DEFAULT_UNDERLINE,
63                                ASS_DEFAULT_ALIGNMENT);
64 }
65
66 static void insert_ts(AVBPrint *buf, int ts)
67 {
68     if (ts == -1) {
69         av_bprintf(buf, "9:59:59.99,");
70     } else {
71         int h, m, s;
72
73         h = ts/360000;  ts -= 360000*h;
74         m = ts/  6000;  ts -=   6000*m;
75         s = ts/   100;  ts -=    100*s;
76         av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
77     }
78 }
79
80 int ff_ass_bprint_dialog(AVBPrint *buf, const char *dialog,
81                          int ts_start, int duration, int raw)
82 {
83     int dlen;
84
85     if (!raw || raw == 2) {
86         long int layer = 0;
87
88         if (raw == 2) {
89             /* skip ReadOrder */
90             dialog = strchr(dialog, ',');
91             if (!dialog)
92                 return AVERROR_INVALIDDATA;
93             dialog++;
94
95             /* extract Layer or Marked */
96             layer = strtol(dialog, (char**)&dialog, 10);
97             if (*dialog != ',')
98                 return AVERROR_INVALIDDATA;
99             dialog++;
100         }
101         av_bprintf(buf, "Dialogue: %ld,", layer);
102         insert_ts(buf, ts_start);
103         insert_ts(buf, duration == -1 ? -1 : ts_start + duration);
104         if (raw != 2)
105             av_bprintf(buf, "Default,");
106     }
107
108     dlen = strcspn(dialog, "\n");
109     dlen += dialog[dlen] == '\n';
110
111     av_bprintf(buf, "%.*s", dlen, dialog);
112     if (raw == 2)
113         av_bprintf(buf, "\r\n");
114
115     return dlen;
116 }
117
118 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
119                     int ts_start, int duration, int raw)
120 {
121     AVBPrint buf;
122     int ret, dlen;
123     AVSubtitleRect **rects;
124
125     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
126     if ((ret = ff_ass_bprint_dialog(&buf, dialog, ts_start, duration, raw)) < 0)
127         goto err;
128     dlen = ret;
129     if (!av_bprint_is_complete(&buf))
130         goto errnomem;
131
132     rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
133     if (!rects)
134         goto errnomem;
135     sub->rects = rects;
136     sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
137     rects[sub->num_rects]       = av_mallocz(sizeof(*rects[0]));
138     rects[sub->num_rects]->type = SUBTITLE_ASS;
139     ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass);
140     if (ret < 0)
141         goto err;
142     sub->num_rects++;
143     return dlen;
144
145 errnomem:
146     ret = AVERROR(ENOMEM);
147 err:
148     av_bprint_finalize(&buf, NULL);
149     return ret;
150 }
151
152 void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
153                              const char *linebreaks, int keep_ass_markup)
154 {
155     const char *p_end = p + size;
156
157     for (; p < p_end && *p; p++) {
158
159         /* forced custom line breaks, not accounted as "normal" EOL */
160         if (linebreaks && strchr(linebreaks, *p)) {
161             av_bprintf(buf, "\\N");
162
163         /* standard ASS escaping so random characters don't get mis-interpreted
164          * as ASS */
165         } else if (!keep_ass_markup && strchr("{}\\", *p)) {
166             av_bprintf(buf, "\\%c", *p);
167
168         /* some packets might end abruptly (no \0 at the end, like for example
169          * in some cases of demuxing from a classic video container), some
170          * might be terminated with \n or \r\n which we have to remove (for
171          * consistency with those who haven't), and we also have to deal with
172          * evil cases such as \r at the end of the buffer (and no \0 terminated
173          * character) */
174         } else if (p[0] == '\n') {
175             /* some stuff left so we can insert a line break */
176             if (p < p_end - 1)
177                 av_bprintf(buf, "\\N");
178         } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
179             /* \r followed by a \n, we can skip it. We don't insert the \N yet
180              * because we don't know if it is followed by more text */
181             continue;
182
183         /* finally, a sane character */
184         } else {
185             av_bprint_chars(buf, *p, 1);
186         }
187     }
188     av_bprintf(buf, "\r\n");
189 }