ass.c
Go to the documentation of this file.
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 
30  const char *font, int font_size,
31  int color, int back_color,
32  int bold, int italic, int underline,
33  int alignment)
34 {
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 
55 {
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_add_rect(AVSubtitle *sub, const char *dialog,
81  int ts_start, int duration, int raw)
82 {
83  AVBPrint buf;
84  int ret, dlen;
85  AVSubtitleRect **rects;
86 
88  if (!raw || raw == 2) {
89  long int layer = 0;
90 
91  if (raw == 2) {
92  /* skip ReadOrder */
93  dialog = strchr(dialog, ',');
94  if (!dialog)
95  return AVERROR_INVALIDDATA;
96  dialog++;
97 
98  /* extract Layer or Marked */
99  layer = strtol(dialog, (char**)&dialog, 10);
100  if (*dialog != ',')
101  return AVERROR_INVALIDDATA;
102  dialog++;
103  }
104  av_bprintf(&buf, "Dialogue: %ld,", layer);
105  insert_ts(&buf, ts_start);
106  insert_ts(&buf, duration == -1 ? -1 : ts_start + duration);
107  if (raw != 2)
108  av_bprintf(&buf, "Default,");
109  }
110 
111  dlen = strcspn(dialog, "\n");
112  dlen += dialog[dlen] == '\n';
113 
114  av_bprintf(&buf, "%.*s", dlen, dialog);
115  if (raw == 2)
116  av_bprintf(&buf, "\r\n");
117  if (!av_bprint_is_complete(&buf))
118  return AVERROR(ENOMEM);
119 
120  rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
121  if (!rects)
122  return AVERROR(ENOMEM);
123  sub->rects = rects;
124  sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
125  rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
126  rects[sub->num_rects]->type = SUBTITLE_ASS;
127  ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass);
128  if (ret < 0)
129  return ret;
130  sub->num_rects++;
131  return dlen;
132 }
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
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:93
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:141
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:193
AVSubtitleRect ** rects
#define ASS_DEFAULT_ALIGNMENT
Definition: ass.h:38
window constants for m
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:77
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
Definition: ass.c:54
static int64_t duration
Definition: ffplay.c:294
int ff_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:29
static void insert_ts(AVBPrint *buf, int ts)
Definition: ass.c:66
#define ASS_DEFAULT_BACK_COLOR
Definition: ass.h:34
#define ASS_DEFAULT_UNDERLINE
Definition: ass.h:37
#define AV_BPRINT_SIZE_UNLIMITED
Convenience macros for special values for av_bprint_init() size_max parameter.
Definition: bprint.h:89
#define ASS_DEFAULT_FONT
Definition: ass.h:31
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Init a print buffer.
Definition: bprint.c:68
static int av_bprint_is_complete(AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:166
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:56
external API header
#define ASS_DEFAULT_FONT_SIZE
Definition: ass.h:32
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int ts_start, int duration, int raw)
Add an ASS dialog line to an AVSubtitle as a new AVSubtitleRect.
Definition: ass.c:80
uint32_t end_display_time
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:112
Buffer to print data progressively.
Definition: bprint.h:75
ret
Definition: avfilter.c:821
main external API structure.
void * buf
Definition: avisynth_c.h:594
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
common internal and external API header
#define ASS_DEFAULT_COLOR
Definition: ass.h:33
Formatted text, the ass field must be set by the decoder and is authoritative.
#define ASS_DEFAULT_ITALIC
Definition: ass.h:36
#define ASS_DEFAULT_BOLD
Definition: ass.h:35
char * ass
0 terminated ASS/SSA compatible event line.
enum AVSubtitleType type
uint8_t * subtitle_header
Header containing style information for text subtitles.