Go to the documentation of this file.
34 #define STYLE_FLAG_BOLD (1<<0)
35 #define STYLE_FLAG_ITALIC (1<<1)
36 #define STYLE_FLAG_UNDERLINE (1<<2)
37 #define STYLE_RECORD_SIZE 12
40 #define STYL_BOX (1<<0)
41 #define HLIT_BOX (1<<1)
42 #define HCLR_BOX (1<<2)
44 #define DEFAULT_STYLE_FONT_ID 0x01
45 #define DEFAULT_STYLE_FONTSIZE 0x12
46 #define DEFAULT_STYLE_COLOR 0xffffffff
47 #define DEFAULT_STYLE_FLAG 0x00
49 #define BGR_TO_RGB(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((uint32_t)(c) >> 16) & 0xff))
50 #define FONTSIZE_SCALE(s,fs) ((fs) * (s)->font_scale_factor + 0.5)
51 #define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size)
100 s->style_attributes_temp =
s->d;
106 uint8_t buf[12], *p = buf;
109 bytestream_put_be32(&p,
MKBETAG(
's',
't',
'y',
'l'));
110 bytestream_put_be16(&p,
s->count);
114 for (
unsigned j = 0; j <
s->count; j++) {
115 const StyleBox *style = &
s->style_attributes[j];
119 bytestream_put_be16(&p, style->
style_end);
134 uint8_t buf[12], *p = buf;
136 bytestream_put_be32(&p, 12);
137 bytestream_put_be32(&p,
MKBETAG(
'h',
'l',
'i',
't'));
138 bytestream_put_be16(&p,
s->hlit.start);
139 bytestream_put_be16(&p,
s->hlit.end);
148 uint8_t buf[12], *p = buf;
150 bytestream_put_be32(&p, 12);
151 bytestream_put_be32(&p,
MKBETAG(
'h',
'c',
'l',
'r'));
152 bytestream_put_be32(&p,
s->hclr.color);
182 uint32_t back_color = 0;
183 int font_names_total_len = 0;
185 uint8_t buf[30], *p = buf;
217 ass = (
ASS*)
s->ass_ctx;
223 s->font_scale_factor = 1;
244 bytestream_put_be32(&p, 0);
245 bytestream_put_be16(&p, 0x01FF);
246 bytestream_put_be32(&p, back_color);
247 bytestream_put_be64(&p, 0);
249 bytestream_put_be16(&p,
s->d.style_start);
250 bytestream_put_be16(&p,
s->d.style_end);
251 bytestream_put_be16(&p,
s->d.style_fontID);
252 bytestream_put_byte(&p,
s->d.style_flag);
253 bytestream_put_byte(&p,
s->d.style_fontsize);
254 bytestream_put_be32(&p,
s->d.style_color);
266 font_names_total_len += strlen(style->
font_name);
272 for (j = 0; j <
s->font_count; j++) {
289 bytestream_put_be32(&p,
SIZE_ADD + 3 *
s->font_count + font_names_total_len);
290 bytestream_put_be32(&p,
MKBETAG(
'f',
't',
'a',
'b'));
291 bytestream_put_be16(&p,
s->font_count);
295 for (
i = 0;
i <
s->font_count;
i++) {
296 size_t len = strlen(
s->fonts[
i]);
299 bytestream_put_be16(&p,
i + 1);
300 bytestream_put_byte(&p,
len);
346 if (
s->style_attributes_temp.style_start ==
s->text_pos)
349 if (
s->style_attributes_temp.style_flag !=
s->d.style_flag ||
350 s->style_attributes_temp.style_color !=
s->d.style_color ||
351 s->style_attributes_temp.style_fontID !=
s->d.style_fontID ||
352 s->style_attributes_temp.style_fontsize !=
s->d.style_fontsize) {
356 if (
s->count + 1 >
FFMIN(SIZE_MAX /
sizeof(*
s->style_attributes), UINT16_MAX) ||
358 &
s->style_attributes_bytes_allocated,
359 (
s->count + 1) *
sizeof(*
s->style_attributes)))) {
365 s->style_attributes =
tmp;
366 s->style_attributes_temp.style_end =
s->text_pos;
367 s->style_attributes[
s->count++] =
s->style_attributes_temp;
369 s->style_attributes_temp =
s->d;
370 s->style_attributes_temp.style_start =
s->text_pos;
372 s->style_attributes_temp =
s->d;
373 s->style_attributes_temp.style_start =
s->text_pos;
380 uint8_t style_flag = 0;
398 if (!((
s->style_attributes_temp.style_flag & style_flags) ^ style_flags)) {
403 s->style_attributes_temp.style_flag |= style_flags;
411 if (!!(
s->style_attributes_temp.style_flag & style_flag) != close) {
417 s->style_attributes_temp.style_flag |= style_flag;
419 s->style_attributes_temp.style_flag &= ~style_flag;
425 if ((
s->style_attributes_temp.style_color & 0xffffff00) ==
color) {
430 s->style_attributes_temp.style_color = (
color & 0xffffff00) |
431 (
s->style_attributes_temp.style_color & 0xff);
441 }
else if (color_id == 2) {
444 s->hclr.color =
s->style_attributes_temp.style_color;
445 if (!(
s->box_flags &
HLIT_BOX) ||
s->hlit.start ==
s->text_pos) {
448 s->hlit.start =
s->text_pos;
449 s->hclr.color =
color | (
s->hclr.color & 0xFF);
452 s->hlit.end =
s->text_pos;
463 if ((
s->style_attributes_temp.style_color & 0xff) ==
alpha) {
468 s->style_attributes_temp.style_color =
469 (
s->style_attributes_temp.style_color & 0xffffff00) |
alpha;
479 else if (alpha_id == 2) {
482 s->hclr.color =
s->style_attributes_temp.style_color;
483 if (!(
s->box_flags &
HLIT_BOX) ||
s->hlit.start ==
s->text_pos) {
486 s->hlit.start =
s->text_pos;
487 s->hclr.color = (
s->hclr.color & 0xffffff00) |
alpha;
490 s->hlit.end =
s->text_pos;
500 for (
int i = 0;
i <
s->font_count;
i++) {
501 if (!strcmp(
name,
s->fonts[
i]))
510 if (
s->style_attributes_temp.style_fontID == fontID) {
515 s->style_attributes_temp.style_fontID = fontID;
526 if (
s->style_attributes_temp.style_fontsize ==
size) {
531 s->style_attributes_temp.style_fontsize =
size;
547 uint8_t style_flags,
alpha;
571 s->ass_dialog_style = style;
580 if (!style_name || !*style_name)
581 style =
s->ass_dialog_style;
590 unsigned i = 0,
ret = 0;
595 else if ((
c & 0xE0) == 0xC0)
597 else if ((
c & 0xF0) == 0xE0)
599 else if ((
c & 0xF8) == 0xF0)
614 s->text_pos += utf8_len ? utf8_len :
len;
663 if (
s->buffer.len > UINT16_MAX)
677 if (
s->buffer.len > bufsize - 3) {
682 memcpy(buf,
s->buffer.str,
s->buffer.len);
683 length =
s->buffer.len + 2;
688 #define OFFSET(x) offsetof(MovTextContext, x)
689 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
703 .
p.
name =
"mov_text",
#define AV_BPRINT_SIZE_UNLIMITED
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
static unsigned utf8_strlen(const char *text, int len)
Set of callback functions corresponding to each override codes that can be encountered in a "Dialogue...
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
static void mov_text_cancel_overrides_cb(void *priv, const char *style_name)
ASSDialog * ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf)
Split one ASS Dialogue line from a string buffer.
static void mov_text_font_name_set(MovTextContext *s, const char *name)
static const AVOption options[]
ASSStyle * styles
array of split out styles
uint8_t * subtitle_header
Header containing style information for text subtitles.
static av_cold int mov_text_encode_init(AVCodecContext *avctx)
const static size_t box_count
char * style
name of the ASSStyle to use with this dialog
static void mov_text_color_set(MovTextContext *s, uint32_t color)
#define DEFAULT_STYLE_FONT_ID
static const AVClass mov_text_encoder_class
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
char * font_name
font face (case sensitive)
AVCodec p
The public AVCodec.
static void mov_text_font_size_set(MovTextContext *s, int size)
static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha)
static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id)
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
#define FONTSIZE_SCALE(s, fs)
char * ass
0 terminated ASS/SSA compatible event line.
void ff_ass_free_dialog(ASSDialog **dialogp)
Free a dialogue obtained from ff_ass_split_dialog().
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
static void mov_text_text_cb(void *priv, const char *text, int len)
static int mov_text_style_start(MovTextContext *s)
static const Box box_types[]
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
static void mov_text_cleanup(MovTextContext *s)
#define STYLE_FLAG_UNDERLINE
#define CODEC_LONG_NAME(str)
int play_res_y
video height that ASS coords are referring to
#define STYLE_FLAG_ITALIC
structure containing the whole split ASS data
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
StyleBox * style_attributes
int primary_color
color that a subtitle will normally appear in
const char * av_default_item_name(void *ptr)
Return the context name.
#define FF_CODEC_ENCODE_SUB_CB(func)
This struct can be casted to ASS to access to the split data.
ASSSplitContext * ff_ass_split(const char *buf)
Split a full ASS file or a ASS header from a string buffer and store the split structure in a newly a...
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
fields extracted from the [V4(+) Styles] section
static uint8_t mov_text_style_to_flag(const char style)
int underline
whether text is underlined (1) or not (0)
void ff_ass_split_free(ASSSplitContext *ctx)
Free all the memory allocated for an ASSSplitContext.
#define DEFAULT_STYLE_COLOR
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
Add the pointer to an element to a dynamic array.
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
char * text
actual text which will be displayed as a subtitle, can include style override control codes (see ff_a...
static void encode_styl(MovTextContext *s)
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
int italic
whether text is italic (1) or not (0)
#define MKBETAG(a, b, c, d)
ASSStyle * ass_dialog_style
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
#define DEFAULT_STYLE_FONTSIZE
StyleBox style_attributes_temp
static int mov_text_encode_close(AVCodecContext *avctx)
static uint16_t find_font_id(MovTextContext *s, const char *name)
static void mov_text_font_name_cb(void *priv, const char *name)
int back_color
color of the subtitle outline or shadow
static const ASSCodesCallbacks mov_text_callbacks
ASSStyle * ff_ass_style_get(ASSSplitContext *ctx, const char *style)
Find an ASSStyle structure by its name.
static int encode_sample_description(AVCodecContext *avctx)
#define STYLE_RECORD_SIZE
#define i(width, name, range_min, range_max)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static void encode_hlit(MovTextContext *s)
const char * name
Name of the codec implementation.
static void mov_text_new_line_cb(void *priv, int forced)
int styles_count
number of ASSStyle in the styles array
static void mov_text_end_cb(void *priv)
static void encode_hclr(MovTextContext *s)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_INPUT_BUFFER_PADDING_SIZE
main external API structure.
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
static void mov_text_font_size_cb(void *priv, int size)
fields extracted from the [Events] section
unsigned style_attributes_bytes_allocated
#define av_bprint_append_any(buf, data, size)
static void mov_text_style_cb(void *priv, const char style, int close)
static const int16_t alpha[]
static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style)
#define DEFAULT_STYLE_FLAG
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv, const char *buf)
Split override codes out of a ASS "Dialogue" Text field.
const FFCodec ff_movtext_encoder
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
ASSSplitContext * ass_ctx
static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id)
int bold
whether text is bold (1) or not (0)
void(* text)(void *priv, const char *text, int len)
ASSScriptInfo script_info
general information about the SSA script