ass.c
Go to the documentation of this file.
1 /*
2  * SSA/ASS common funtions
3  * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "avcodec.h"
23 #include "ass.h"
24 #include "libavutil/avstring.h"
25 
42  const char *font, int font_size,
43  int color, int back_color,
44  int bold, int italic, int underline,
45  int alignment)
46 {
47  char header[512];
48 
49  snprintf(header, sizeof(header),
50  "[Script Info]\r\n"
51  "ScriptType: v4.00+\r\n"
52  "\r\n"
53  "[V4+ Styles]\r\n"
54  "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
55  "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"
56  "\r\n"
57  "[Events]\r\n"
58  "Format: Layer, Start, End, Text\r\n",
59  font, font_size, color, color, back_color, back_color,
60  -bold, -italic, -underline, alignment);
61 
62  avctx->subtitle_header = av_strdup(header);
63  if (!avctx->subtitle_header)
64  return AVERROR(ENOMEM);
65  avctx->subtitle_header_size = strlen(avctx->subtitle_header);
66  return 0;
67 }
68 
70 {
79 }
80 
82 {
83  memset(sub, 0, sizeof(*sub));
84 }
85 
86 static int ts_to_string(char *str, int strlen, int ts)
87 {
88  int h, m, s;
89  h = ts/360000; ts -= 360000*h;
90  m = ts/ 6000; ts -= 6000*m;
91  s = ts/ 100; ts -= 100*s;
92  return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
93 }
94 
95 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
96  int ts_start, int ts_end, int raw)
97 {
98  int len = 0, dlen, duration = ts_end - ts_start;
99  char s_start[16], s_end[16], header[48] = {0};
100  AVSubtitleRect **rects;
101 
102  if (!raw) {
103  ts_to_string(s_start, sizeof(s_start), ts_start);
104  ts_to_string(s_end, sizeof(s_end), ts_end );
105  len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,",
106  s_start, s_end);
107  }
108 
109  dlen = strcspn(dialog, "\n");
110  dlen += dialog[dlen] == '\n';
111 
112  rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
113  if (!rects)
114  return AVERROR(ENOMEM);
115  sub->rects = rects;
116  sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
117  rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
118  rects[sub->num_rects]->type = SUBTITLE_ASS;
119  rects[sub->num_rects]->ass = av_malloc(len + dlen + 1);
120  strcpy (rects[sub->num_rects]->ass , header);
121  av_strlcpy(rects[sub->num_rects]->ass + len, dialog, dlen + 1);
122  sub->num_rects++;
123  return dlen;
124 }