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);
181 uint32_t back_color = 0;
182 int font_names_total_len = 0;
184 uint8_t buf[30], *p = buf;
219 ass = (
ASS*)
s->ass_ctx;
225 s->font_scale_factor = 1;
246 bytestream_put_be32(&p, 0);
247 bytestream_put_be16(&p, 0x01FF);
248 bytestream_put_be32(&p, back_color);
249 bytestream_put_be64(&p, 0);
251 bytestream_put_be16(&p,
s->d.style_start);
252 bytestream_put_be16(&p,
s->d.style_end);
253 bytestream_put_be16(&p,
s->d.style_fontID);
254 bytestream_put_byte(&p,
s->d.style_flag);
255 bytestream_put_byte(&p,
s->d.style_fontsize);
256 bytestream_put_be32(&p,
s->d.style_color);
268 font_names_total_len += strlen(style->
font_name);
274 for (j = 0; j <
s->font_count; j++) {
291 bytestream_put_be32(&p,
SIZE_ADD + 3 *
s->font_count + font_names_total_len);
292 bytestream_put_be32(&p,
MKBETAG(
'f',
't',
'a',
'b'));
293 bytestream_put_be16(&p,
s->font_count);
297 for (
i = 0;
i <
s->font_count;
i++) {
298 size_t len = strlen(
s->fonts[
i]);
301 bytestream_put_be16(&p,
i + 1);
302 bytestream_put_byte(&p,
len);
350 if (
s->style_attributes_temp.style_start ==
s->text_pos)
353 if (
s->style_attributes_temp.style_flag !=
s->d.style_flag ||
354 s->style_attributes_temp.style_color !=
s->d.style_color ||
355 s->style_attributes_temp.style_fontID !=
s->d.style_fontID ||
356 s->style_attributes_temp.style_fontsize !=
s->d.style_fontsize) {
360 if (
s->count + 1 >
FFMIN(SIZE_MAX /
sizeof(*
s->style_attributes), UINT16_MAX) ||
362 &
s->style_attributes_bytes_allocated,
363 (
s->count + 1) *
sizeof(*
s->style_attributes)))) {
369 s->style_attributes =
tmp;
370 s->style_attributes_temp.style_end =
s->text_pos;
371 s->style_attributes[
s->count++] =
s->style_attributes_temp;
373 s->style_attributes_temp =
s->d;
374 s->style_attributes_temp.style_start =
s->text_pos;
376 s->style_attributes_temp =
s->d;
377 s->style_attributes_temp.style_start =
s->text_pos;
384 uint8_t style_flag = 0;
402 if (!((
s->style_attributes_temp.style_flag & style_flags) ^ style_flags)) {
407 s->style_attributes_temp.style_flag |= style_flags;
415 if (!!(
s->style_attributes_temp.style_flag & style_flag) !=
close) {
421 s->style_attributes_temp.style_flag |= style_flag;
423 s->style_attributes_temp.style_flag &= ~style_flag;
429 if ((
s->style_attributes_temp.style_color & 0xffffff00) ==
color) {
434 s->style_attributes_temp.style_color = (
color & 0xffffff00) |
435 (
s->style_attributes_temp.style_color & 0xff);
445 }
else if (color_id == 2) {
448 s->hclr.color =
s->style_attributes_temp.style_color;
449 if (!(
s->box_flags &
HLIT_BOX) ||
s->hlit.start ==
s->text_pos) {
452 s->hlit.start =
s->text_pos;
453 s->hclr.color =
color | (
s->hclr.color & 0xFF);
456 s->hlit.end =
s->text_pos;
467 if ((
s->style_attributes_temp.style_color & 0xff) ==
alpha) {
472 s->style_attributes_temp.style_color =
473 (
s->style_attributes_temp.style_color & 0xffffff00) |
alpha;
483 else if (alpha_id == 2) {
486 s->hclr.color =
s->style_attributes_temp.style_color;
487 if (!(
s->box_flags &
HLIT_BOX) ||
s->hlit.start ==
s->text_pos) {
490 s->hlit.start =
s->text_pos;
491 s->hclr.color = (
s->hclr.color & 0xffffff00) |
alpha;
494 s->hlit.end =
s->text_pos;
504 for (
int i = 0;
i <
s->font_count;
i++) {
505 if (!strcmp(
name,
s->fonts[
i]))
514 if (
s->style_attributes_temp.style_fontID == fontID) {
519 s->style_attributes_temp.style_fontID = fontID;
530 if (
s->style_attributes_temp.style_fontsize ==
size) {
535 s->style_attributes_temp.style_fontsize =
size;
551 uint8_t style_flags,
alpha;
575 s->ass_dialog_style = style;
584 if (!style_name || !*style_name)
585 style =
s->ass_dialog_style;
594 unsigned i = 0, ret = 0;
599 else if ((
c & 0xE0) == 0xC0)
601 else if ((
c & 0xF0) == 0xE0)
603 else if ((
c & 0xF8) == 0xF0)
618 s->text_pos += utf8_len ? utf8_len :
len;
671 if (
s->buffer.len > UINT16_MAX)
687 length =
s->buffer.len + 2;
692#define OFFSET(x) offsetof(MovTextContext, x)
693#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
700 .class_name =
"MOV text enoder",
707 .p.name =
"mov_text",
const FFCodec ff_movtext_encoder
static av_cold void close(AVCodecParserContext *s)
av_cold 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...
void ff_ass_free_dialog(ASSDialog **dialogp)
Free a dialogue obtained from ff_ass_split_dialog().
int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv, const char *buf)
Split override codes out of a ASS "Dialogue" Text field.
ASSStyle * ff_ass_style_get(ASSSplitContext *ctx, const char *style)
Find an ASSStyle structure by its name.
av_cold void ff_ass_split_free(ASSSplitContext *ctx)
Free all the memory allocated for an ASSSplitContext.
ASSDialog * ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf)
Split one ASS Dialogue line from a string buffer.
Libavcodec external API header.
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define i(width, name, range_min, range_max)
#define FF_CODEC_ENCODE_SUB_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal and external API header
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
Init a print buffer using a pre-existing buffer.
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
Add the pointer to an element to a dynamic array.
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.
#define LIBAVUTIL_VERSION_INT
static const int16_t alpha[]
Macro definitions for various function/variable attributes.
#define MKBETAG(a, b, c, d)
Memory handling functions.
#define STYLE_FLAG_UNDERLINE
static const Box box_types[]
#define STYLE_FLAG_ITALIC
static const size_t box_count
static int encode_sample_description(AVCodecContext *avctx)
#define FONTSIZE_SCALE(s, fs)
static uint8_t mov_text_style_to_flag(const char style)
static void mov_text_font_size_cb(void *priv, int size)
static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
static void mov_text_font_name_cb(void *priv, const char *name)
static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id)
static void mov_text_color_set(MovTextContext *s, uint32_t color)
static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha)
static void encode_hclr(MovTextContext *s)
static void mov_text_end_cb(void *priv)
static void encode_hlit(MovTextContext *s)
static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog)
static void mov_text_text_cb(void *priv, const char *text, int len)
#define DEFAULT_STYLE_FONT_ID
static const ASSCodesCallbacks mov_text_callbacks
static const AVClass mov_text_encoder_class
static void mov_text_cancel_overrides_cb(void *priv, const char *style_name)
static void mov_text_font_name_set(MovTextContext *s, const char *name)
static av_cold int mov_text_encode_close(AVCodecContext *avctx)
static unsigned utf8_strlen(const char *text, int len)
static void mov_text_style_cb(void *priv, const char style, int close)
#define DEFAULT_STYLE_COLOR
static int mov_text_style_start(MovTextContext *s)
static void mov_text_font_size_set(MovTextContext *s, int size)
#define STYLE_RECORD_SIZE
static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style)
static av_cold int mov_text_encode_init(AVCodecContext *avctx)
static void encode_styl(MovTextContext *s)
static void mov_text_new_line_cb(void *priv, int forced)
static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id)
static uint16_t find_font_id(MovTextContext *s, const char *name)
static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
#define DEFAULT_STYLE_FLAG
static void mov_text_cleanup(MovTextContext *s)
#define DEFAULT_STYLE_FONTSIZE
#define av_bprint_append_any(buf, data, size)
#define FF_ARRAY_ELEMS(a)
Set of callback functions corresponding to each override codes that can be encountered in a "Dialogue...
fields extracted from the [Events] section
char * style
name of the ASSStyle to use with this dialog
char * text
actual text which will be displayed as a subtitle, can include style override control codes (see ff_a...
int play_res_y
video height that ASS coords are referring to
This struct can be casted to ASS to access to the split data.
fields extracted from the [V4(+) Styles] section
int primary_color
color that a subtitle will normally appear in
int italic
whether text is italic (1) or not (0)
int bold
whether text is bold (1) or not (0)
int underline
whether text is underlined (1) or not (0)
char * font_name
font face (case sensitive)
int back_color
color of the subtitle outline or shadow
structure containing the whole split ASS data
int styles_count
number of ASSStyle in the styles array
ASSStyle * styles
array of split out styles
ASSScriptInfo script_info
general information about the SSA script
Describe the class of an AVClass context structure.
main external API structure.
uint8_t * subtitle_header
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
char * ass
0 terminated ASS/SSA compatible event line.
void(* encode)(MovTextContext *s)
ASSSplitContext * ass_ctx
StyleBox style_attributes_temp
ASSStyle * ass_dialog_style
StyleBox * style_attributes
unsigned style_attributes_bytes_allocated