Libav
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 #include "libavutil/common.h"
26 
43  const char *font, int font_size,
44  int color, int back_color,
45  int bold, int italic, int underline,
46  int alignment)
47 {
48  char header[512];
49 
50  snprintf(header, sizeof(header),
51  "[Script Info]\r\n"
52  "ScriptType: v4.00+\r\n"
53  "\r\n"
54  "[V4+ Styles]\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"
57  "\r\n"
58  "[Events]\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);
62 
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);
67  return 0;
68 }
69 
71 {
80 }
81 
83 {
84  memset(sub, 0, sizeof(*sub));
85 }
86 
87 static int ts_to_string(char *str, int strlen, int ts)
88 {
89  int h, m, s;
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);
94 }
95 
96 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
97  int ts_start, int ts_end, int raw)
98 {
99  int len = 0, dlen, duration = ts_end - ts_start;
100  char s_start[16], s_end[16], header[48] = {0};
101  AVSubtitleRect **rects;
102 
103  if (!raw) {
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,",
107  s_start, s_end);
108  }
109 
110  dlen = strcspn(dialog, "\n");
111  dlen += dialog[dlen] == '\n';
112 
113  rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
114  if (!rects)
115  return AVERROR(ENOMEM);
116  sub->rects = rects;
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);
123  sub->num_rects++;
124  return dlen;
125 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
unsigned num_rects
Definition: avcodec.h:3081
static int64_t duration
Definition: avplay.c:246
AVSubtitleRect ** rects
Definition: avcodec.h:3082
#define ASS_DEFAULT_ALIGNMENT
Definition: ass.h:38
int subtitle_header_size
Definition: avcodec.h:2732
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
Definition: ass.c:70
#define ASS_DEFAULT_BACK_COLOR
Definition: ass.h:34
#define ASS_DEFAULT_UNDERLINE
Definition: ass.h:37
#define AVERROR(e)
Definition: error.h:43
#define ASS_DEFAULT_FONT
Definition: ass.h:31
void ff_ass_init(AVSubtitle *sub)
Initialize an AVSubtitle structure for use with ff_ass_add_rect().
Definition: ass.c:82
#define FFMAX(a, b)
Definition: common.h:55
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
static int ass_subtitle_header(AVCodecContext *avctx, const char *font, int font_size, int color, int back_color, int bold, int italic, int underline, int alignment)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS.
Definition: ass.c:42
#define ASS_DEFAULT_FONT_SIZE
Definition: ass.h:32
uint32_t end_display_time
Definition: avcodec.h:3080
Libavcodec external API header.
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
main external API structure.
Definition: avcodec.h:1044
static int ts_to_string(char *str, int strlen, int ts)
Definition: ass.c:87
common internal and external API header
#define ASS_DEFAULT_COLOR
Definition: ass.h:33
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
static const uint8_t color[]
Definition: log.c:55
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition: avcodec.h:3047
#define ASS_DEFAULT_ITALIC
Definition: ass.h:36
int len
#define ASS_DEFAULT_BOLD
Definition: ass.h:35
char * ass
0 terminated ASS/SSA compatible event line.
Definition: avcodec.h:3073
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int ts_start, int ts_end, int raw)
Add an ASS dialog line to an AVSubtitle as a new AVSubtitleRect.
Definition: ass.c:96
enum AVSubtitleType type
Definition: avcodec.h:3064
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:2731