2012-06-06 19:12:24 +03:00
|
|
|
/*
|
|
|
|
* 3GPP TS 26.245 Timed Text encoder
|
|
|
|
* Copyright (c) 2012 Philip Langdale <philipl@overt.org>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "avcodec.h"
|
2020-04-05 23:02:27 +02:00
|
|
|
#include "libavutil/opt.h"
|
2012-08-04 22:19:46 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2015-06-26 22:14:44 +02:00
|
|
|
#include "libavutil/mem.h"
|
|
|
|
#include "libavutil/common.h"
|
2012-06-06 19:12:24 +03:00
|
|
|
#include "ass_split.h"
|
|
|
|
#include "ass.h"
|
2020-10-15 15:29:13 +02:00
|
|
|
#include "bytestream.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2012-06-06 19:12:24 +03:00
|
|
|
|
2015-06-26 22:14:44 +02:00
|
|
|
#define STYLE_FLAG_BOLD (1<<0)
|
|
|
|
#define STYLE_FLAG_ITALIC (1<<1)
|
|
|
|
#define STYLE_FLAG_UNDERLINE (1<<2)
|
|
|
|
#define STYLE_RECORD_SIZE 12
|
|
|
|
#define SIZE_ADD 10
|
|
|
|
|
2015-07-20 20:27:28 +02:00
|
|
|
#define STYL_BOX (1<<0)
|
2015-07-20 20:32:31 +02:00
|
|
|
#define HLIT_BOX (1<<1)
|
|
|
|
#define HCLR_BOX (1<<2)
|
2015-07-20 20:27:28 +02:00
|
|
|
|
2020-04-04 23:23:34 +02:00
|
|
|
#define DEFAULT_STYLE_FONT_ID 0x01
|
|
|
|
#define DEFAULT_STYLE_FONTSIZE 0x12
|
|
|
|
#define DEFAULT_STYLE_COLOR 0xffffffff
|
|
|
|
#define DEFAULT_STYLE_FLAG 0x00
|
|
|
|
|
2020-10-17 05:00:13 +02:00
|
|
|
#define BGR_TO_RGB(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((uint32_t)(c) >> 16) & 0xff))
|
2020-04-05 23:02:27 +02:00
|
|
|
#define FONTSIZE_SCALE(s,fs) ((fs) * (s)->font_scale_factor + 0.5)
|
2015-06-26 22:14:44 +02:00
|
|
|
#define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint16_t style_start;
|
|
|
|
uint16_t style_end;
|
|
|
|
uint8_t style_flag;
|
2020-04-04 23:23:34 +02:00
|
|
|
uint16_t style_fontID;
|
|
|
|
uint8_t style_fontsize;
|
|
|
|
uint32_t style_color;
|
2015-06-26 22:14:44 +02:00
|
|
|
} StyleBox;
|
|
|
|
|
2015-07-20 20:32:31 +02:00
|
|
|
typedef struct {
|
|
|
|
uint16_t start;
|
|
|
|
uint16_t end;
|
|
|
|
} HighlightBox;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t color;
|
|
|
|
} HilightcolorBox;
|
|
|
|
|
2012-06-06 19:12:24 +03:00
|
|
|
typedef struct {
|
2020-04-05 23:02:27 +02:00
|
|
|
AVClass *class;
|
2017-04-23 19:42:25 +02:00
|
|
|
AVCodecContext *avctx;
|
|
|
|
|
2012-06-06 19:12:24 +03:00
|
|
|
ASSSplitContext *ass_ctx;
|
2020-04-05 16:34:45 +02:00
|
|
|
ASSStyle *ass_dialog_style;
|
2020-10-17 03:35:08 +02:00
|
|
|
StyleBox *style_attributes;
|
|
|
|
unsigned count;
|
|
|
|
unsigned style_attributes_bytes_allocated;
|
|
|
|
StyleBox style_attributes_temp;
|
2015-06-26 22:14:44 +02:00
|
|
|
AVBPrint buffer;
|
2015-07-20 20:32:31 +02:00
|
|
|
HighlightBox hlit;
|
|
|
|
HilightcolorBox hclr;
|
2015-07-20 20:27:28 +02:00
|
|
|
uint8_t box_flags;
|
2020-04-04 23:23:34 +02:00
|
|
|
StyleBox d;
|
2015-06-26 22:14:44 +02:00
|
|
|
uint16_t text_pos;
|
2020-10-14 23:27:16 +02:00
|
|
|
char **fonts;
|
2020-04-05 22:26:02 +02:00
|
|
|
int font_count;
|
2020-04-05 23:02:27 +02:00
|
|
|
double font_scale_factor;
|
|
|
|
int frame_height;
|
2012-06-06 19:12:24 +03:00
|
|
|
} MovTextContext;
|
|
|
|
|
2015-07-20 20:27:28 +02:00
|
|
|
typedef struct {
|
2020-10-16 05:02:34 +02:00
|
|
|
void (*encode)(MovTextContext *s);
|
2015-07-20 20:27:28 +02:00
|
|
|
} Box;
|
|
|
|
|
|
|
|
static void mov_text_cleanup(MovTextContext *s)
|
|
|
|
{
|
2020-10-17 03:35:08 +02:00
|
|
|
s->count = 0;
|
|
|
|
s->style_attributes_temp = s->d;
|
2015-07-20 20:27:28 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:02:34 +02:00
|
|
|
static void encode_styl(MovTextContext *s)
|
2015-07-20 20:27:28 +02:00
|
|
|
{
|
2020-04-04 22:43:25 +02:00
|
|
|
if ((s->box_flags & STYL_BOX) && s->count) {
|
2020-10-15 15:29:13 +02:00
|
|
|
uint8_t buf[12], *p = buf;
|
|
|
|
|
|
|
|
bytestream_put_be32(&p, s->count * STYLE_RECORD_SIZE + SIZE_ADD);
|
2020-10-16 05:02:34 +02:00
|
|
|
bytestream_put_be32(&p, MKBETAG('s','t','y','l'));
|
2020-10-15 15:29:13 +02:00
|
|
|
bytestream_put_be16(&p, s->count);
|
2015-07-20 20:27:28 +02:00
|
|
|
/*The above three attributes are hard coded for now
|
|
|
|
but will come from ASS style in the future*/
|
2020-10-15 15:29:13 +02:00
|
|
|
av_bprint_append_any(&s->buffer, buf, 10);
|
2020-10-17 03:35:08 +02:00
|
|
|
for (unsigned j = 0; j < s->count; j++) {
|
2020-10-15 15:29:13 +02:00
|
|
|
const StyleBox *style = &s->style_attributes[j];
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
bytestream_put_be16(&p, style->style_start);
|
|
|
|
bytestream_put_be16(&p, style->style_end);
|
|
|
|
bytestream_put_be16(&p, style->style_fontID);
|
|
|
|
bytestream_put_byte(&p, style->style_flag);
|
|
|
|
bytestream_put_byte(&p, style->style_fontsize);
|
|
|
|
bytestream_put_be32(&p, style->style_color);
|
|
|
|
|
|
|
|
av_bprint_append_any(&s->buffer, buf, 12);
|
2015-07-20 20:27:28 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 22:43:25 +02:00
|
|
|
mov_text_cleanup(s);
|
2015-07-20 20:27:28 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:02:34 +02:00
|
|
|
static void encode_hlit(MovTextContext *s)
|
2015-07-20 20:32:31 +02:00
|
|
|
{
|
|
|
|
if (s->box_flags & HLIT_BOX) {
|
2020-10-15 15:29:13 +02:00
|
|
|
uint8_t buf[12], *p = buf;
|
|
|
|
|
|
|
|
bytestream_put_be32(&p, 12);
|
2020-10-16 05:02:34 +02:00
|
|
|
bytestream_put_be32(&p, MKBETAG('h','l','i','t'));
|
2020-10-15 15:29:13 +02:00
|
|
|
bytestream_put_be16(&p, s->hlit.start);
|
|
|
|
bytestream_put_be16(&p, s->hlit.end);
|
|
|
|
|
|
|
|
av_bprint_append_any(&s->buffer, buf, 12);
|
2015-07-20 20:32:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:02:34 +02:00
|
|
|
static void encode_hclr(MovTextContext *s)
|
2015-07-20 20:32:31 +02:00
|
|
|
{
|
|
|
|
if (s->box_flags & HCLR_BOX) {
|
2020-10-15 15:29:13 +02:00
|
|
|
uint8_t buf[12], *p = buf;
|
|
|
|
|
|
|
|
bytestream_put_be32(&p, 12);
|
2020-10-16 05:02:34 +02:00
|
|
|
bytestream_put_be32(&p, MKBETAG('h','c','l','r'));
|
2020-10-15 15:29:13 +02:00
|
|
|
bytestream_put_be32(&p, s->hclr.color);
|
|
|
|
|
|
|
|
av_bprint_append_any(&s->buffer, buf, 12);
|
2015-07-20 20:32:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 20:27:28 +02:00
|
|
|
static const Box box_types[] = {
|
2020-10-16 05:02:34 +02:00
|
|
|
{ encode_styl },
|
|
|
|
{ encode_hlit },
|
|
|
|
{ encode_hclr },
|
2015-07-20 20:27:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const static size_t box_count = FF_ARRAY_ELEMS(box_types);
|
2012-06-06 19:12:24 +03:00
|
|
|
|
2020-04-05 22:26:02 +02:00
|
|
|
static int mov_text_encode_close(AVCodecContext *avctx)
|
2012-06-06 19:12:24 +03:00
|
|
|
{
|
2020-04-05 22:26:02 +02:00
|
|
|
MovTextContext *s = avctx->priv_data;
|
|
|
|
|
|
|
|
ff_ass_split_free(s->ass_ctx);
|
2020-10-17 03:35:08 +02:00
|
|
|
av_freep(&s->style_attributes);
|
2020-04-05 22:26:02 +02:00
|
|
|
av_freep(&s->fonts);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int encode_sample_description(AVCodecContext *avctx)
|
|
|
|
{
|
2020-10-14 23:27:16 +02:00
|
|
|
ASS *ass;
|
|
|
|
ASSStyle *style;
|
2020-04-05 22:26:02 +02:00
|
|
|
int i, j;
|
2020-10-15 15:29:13 +02:00
|
|
|
uint32_t back_color = 0;
|
2020-04-05 22:26:02 +02:00
|
|
|
int font_names_total_len = 0;
|
|
|
|
MovTextContext *s = avctx->priv_data;
|
2020-10-15 15:29:13 +02:00
|
|
|
uint8_t buf[30], *p = buf;
|
2024-02-18 02:54:45 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
av_bprint_init(&s->buffer, 0, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE + 1);
|
2020-04-05 22:26:02 +02:00
|
|
|
|
2020-10-15 15:29:13 +02:00
|
|
|
// 0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
|
|
|
|
// 0x01, // int8_t horizontal-justification
|
|
|
|
// 0xFF, // int8_t vertical-justification
|
2020-04-05 22:26:02 +02:00
|
|
|
// 0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4]
|
|
|
|
// BoxRecord {
|
2020-10-15 15:29:13 +02:00
|
|
|
// 0x00, 0x00, // int16_t top
|
|
|
|
// 0x00, 0x00, // int16_t left
|
|
|
|
// 0x00, 0x00, // int16_t bottom
|
|
|
|
// 0x00, 0x00, // int16_t right
|
2020-04-05 22:26:02 +02:00
|
|
|
// };
|
|
|
|
// StyleRecord {
|
|
|
|
// 0x00, 0x00, // uint16_t startChar
|
|
|
|
// 0x00, 0x00, // uint16_t endChar
|
|
|
|
// 0x00, 0x01, // uint16_t font-ID
|
|
|
|
// 0x00, // uint8_t face-style-flags
|
|
|
|
// 0x12, // uint8_t font-size
|
|
|
|
// 0xFF, 0xFF, 0xFF, 0xFF, // uint8_t text-color-rgba[4]
|
|
|
|
// };
|
|
|
|
// FontTableBox {
|
|
|
|
// 0x00, 0x00, 0x00, 0x12, // uint32_t size
|
|
|
|
// 'f', 't', 'a', 'b', // uint8_t name[4]
|
|
|
|
// 0x00, 0x01, // uint16_t entry-count
|
|
|
|
// FontRecord {
|
|
|
|
// 0x00, 0x01, // uint16_t font-ID
|
|
|
|
// 0x05, // uint8_t font-name-length
|
|
|
|
// 'S', 'e', 'r', 'i', 'f',// uint8_t font[font-name-length]
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
// Populate sample description from ASS header
|
|
|
|
ass = (ASS*)s->ass_ctx;
|
2020-04-05 23:02:27 +02:00
|
|
|
// Compute font scaling factor based on (optionally) provided
|
|
|
|
// output video height and ASS script play_res_y
|
|
|
|
if (s->frame_height && ass->script_info.play_res_y)
|
|
|
|
s->font_scale_factor = (double)s->frame_height / ass->script_info.play_res_y;
|
|
|
|
else
|
|
|
|
s->font_scale_factor = 1;
|
|
|
|
|
2020-04-05 22:26:02 +02:00
|
|
|
style = ff_ass_style_get(s->ass_ctx, "Default");
|
|
|
|
if (!style && ass->styles_count) {
|
|
|
|
style = &ass->styles[0];
|
|
|
|
}
|
|
|
|
s->d.style_fontID = DEFAULT_STYLE_FONT_ID;
|
|
|
|
s->d.style_fontsize = DEFAULT_STYLE_FONTSIZE;
|
|
|
|
s->d.style_color = DEFAULT_STYLE_COLOR;
|
|
|
|
s->d.style_flag = DEFAULT_STYLE_FLAG;
|
|
|
|
if (style) {
|
2020-04-05 23:02:27 +02:00
|
|
|
s->d.style_fontsize = FONTSIZE_SCALE(s, style->font_size);
|
2020-04-05 22:26:02 +02:00
|
|
|
s->d.style_color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8 |
|
|
|
|
255 - ((uint32_t)style->primary_color >> 24);
|
|
|
|
s->d.style_flag = (!!style->bold * STYLE_FLAG_BOLD) |
|
|
|
|
(!!style->italic * STYLE_FLAG_ITALIC) |
|
|
|
|
(!!style->underline * STYLE_FLAG_UNDERLINE);
|
|
|
|
back_color = (BGR_TO_RGB(style->back_color & 0xffffff) << 8) |
|
|
|
|
(255 - ((uint32_t)style->back_color >> 24));
|
|
|
|
}
|
2012-06-06 19:12:24 +03:00
|
|
|
|
2020-10-15 15:29:13 +02:00
|
|
|
bytestream_put_be32(&p, 0); // displayFlags
|
|
|
|
bytestream_put_be16(&p, 0x01FF); // horizontal/vertical justification (2x int8_t)
|
|
|
|
bytestream_put_be32(&p, back_color);
|
|
|
|
bytestream_put_be64(&p, 0); // BoxRecord - 4xint16_t: top, left, bottom, right
|
2020-04-05 22:26:02 +02:00
|
|
|
// StyleRecord {
|
2020-10-15 15:29:13 +02:00
|
|
|
bytestream_put_be16(&p, s->d.style_start);
|
|
|
|
bytestream_put_be16(&p, s->d.style_end);
|
|
|
|
bytestream_put_be16(&p, s->d.style_fontID);
|
|
|
|
bytestream_put_byte(&p, s->d.style_flag);
|
|
|
|
bytestream_put_byte(&p, s->d.style_fontsize);
|
|
|
|
bytestream_put_be32(&p, s->d.style_color);
|
2020-04-05 22:26:02 +02:00
|
|
|
// };
|
2020-10-15 15:29:13 +02:00
|
|
|
av_bprint_append_any(&s->buffer, buf, 30);
|
2020-04-05 22:26:02 +02:00
|
|
|
|
|
|
|
// Build font table
|
|
|
|
// We can't build a complete font table since that would require
|
|
|
|
// scanning all dialogs first. But we can at least fill in what
|
|
|
|
// is avaiable in the ASS header
|
|
|
|
if (style && ass->styles_count) {
|
|
|
|
// Find unique font names
|
2020-10-16 16:33:23 +02:00
|
|
|
if (style->font_name) {
|
|
|
|
av_dynarray_add(&s->fonts, &s->font_count, style->font_name);
|
|
|
|
font_names_total_len += strlen(style->font_name);
|
|
|
|
}
|
2020-04-05 22:26:02 +02:00
|
|
|
for (i = 0; i < ass->styles_count; i++) {
|
|
|
|
int found = 0;
|
2020-10-16 16:33:23 +02:00
|
|
|
if (!ass->styles[i].font_name)
|
|
|
|
continue;
|
2020-04-05 22:26:02 +02:00
|
|
|
for (j = 0; j < s->font_count; j++) {
|
|
|
|
if (!strcmp(s->fonts[j], ass->styles[i].font_name)) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
av_dynarray_add(&s->fonts, &s->font_count,
|
|
|
|
ass->styles[i].font_name);
|
|
|
|
font_names_total_len += strlen(ass->styles[i].font_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
av_dynarray_add(&s->fonts, &s->font_count, (char*)"Serif");
|
|
|
|
|
|
|
|
// FontTableBox {
|
2020-10-15 15:29:13 +02:00
|
|
|
p = buf;
|
|
|
|
bytestream_put_be32(&p, SIZE_ADD + 3 * s->font_count + font_names_total_len); // Size
|
|
|
|
bytestream_put_be32(&p, MKBETAG('f','t','a','b'));
|
|
|
|
bytestream_put_be16(&p, s->font_count);
|
|
|
|
|
|
|
|
av_bprint_append_any(&s->buffer, buf, 10);
|
2020-04-05 22:26:02 +02:00
|
|
|
// FontRecord {
|
|
|
|
for (i = 0; i < s->font_count; i++) {
|
2020-10-15 15:29:13 +02:00
|
|
|
size_t len = strlen(s->fonts[i]);
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
bytestream_put_be16(&p, i + 1); //fontID
|
|
|
|
bytestream_put_byte(&p, len);
|
|
|
|
|
|
|
|
av_bprint_append_any(&s->buffer, buf, 3);
|
2020-04-05 22:26:02 +02:00
|
|
|
av_bprint_append_any(&s->buffer, s->fonts[i], len);
|
|
|
|
}
|
|
|
|
// };
|
|
|
|
// };
|
2012-06-06 19:12:24 +03:00
|
|
|
|
2020-04-05 22:26:02 +02:00
|
|
|
if (!av_bprint_is_complete(&s->buffer)) {
|
2024-02-18 02:54:45 +02:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
2020-04-04 22:43:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 22:26:02 +02:00
|
|
|
avctx->extradata_size = s->buffer.len;
|
2015-07-27 22:53:16 +02:00
|
|
|
avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
|
2020-04-05 22:26:02 +02:00
|
|
|
if (!avctx->extradata) {
|
2024-02-18 02:54:45 +02:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
2020-04-05 22:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(avctx->extradata, s->buffer.str, avctx->extradata_size);
|
2024-02-18 02:54:45 +02:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
|
|
av_bprint_finalize(&s->buffer, NULL);
|
2020-04-05 22:26:02 +02:00
|
|
|
|
2024-02-18 02:54:45 +02:00
|
|
|
return ret;
|
2020-04-05 22:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int mov_text_encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
MovTextContext *s = avctx->priv_data;
|
|
|
|
s->avctx = avctx;
|
2012-06-06 19:12:24 +03:00
|
|
|
|
|
|
|
s->ass_ctx = ff_ass_split(avctx->subtitle_header);
|
2020-10-17 07:47:43 +02:00
|
|
|
if (!s->ass_ctx)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2020-04-05 22:26:02 +02:00
|
|
|
ret = encode_sample_description(avctx);
|
|
|
|
if (ret < 0)
|
2020-10-17 07:47:43 +02:00
|
|
|
return ret;
|
2020-04-04 23:23:34 +02:00
|
|
|
|
2020-04-05 22:26:02 +02:00
|
|
|
return 0;
|
2012-06-06 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
2020-04-04 22:43:25 +02:00
|
|
|
// Start a new style box if needed
|
|
|
|
static int mov_text_style_start(MovTextContext *s)
|
2015-06-26 22:14:44 +02:00
|
|
|
{
|
2020-04-04 22:43:25 +02:00
|
|
|
// there's an existing style entry
|
2020-10-17 03:35:08 +02:00
|
|
|
if (s->style_attributes_temp.style_start == s->text_pos)
|
2020-04-04 22:43:25 +02:00
|
|
|
// Still at same text pos, use same entry
|
|
|
|
return 1;
|
2020-10-17 03:35:08 +02:00
|
|
|
if (s->style_attributes_temp.style_flag != s->d.style_flag ||
|
|
|
|
s->style_attributes_temp.style_color != s->d.style_color ||
|
|
|
|
s->style_attributes_temp.style_fontID != s->d.style_fontID ||
|
|
|
|
s->style_attributes_temp.style_fontsize != s->d.style_fontsize) {
|
|
|
|
StyleBox *tmp;
|
|
|
|
|
2020-04-04 22:43:25 +02:00
|
|
|
// last style != defaults, end the style entry and start a new one
|
2021-02-21 00:42:04 +02:00
|
|
|
if (s->count + 1 > FFMIN(SIZE_MAX / sizeof(*s->style_attributes), UINT16_MAX) ||
|
2020-10-17 03:35:08 +02:00
|
|
|
!(tmp = av_fast_realloc(s->style_attributes,
|
|
|
|
&s->style_attributes_bytes_allocated,
|
|
|
|
(s->count + 1) * sizeof(*s->style_attributes)))) {
|
2015-07-20 20:27:28 +02:00
|
|
|
mov_text_cleanup(s);
|
2015-06-26 22:14:44 +02:00
|
|
|
av_bprint_clear(&s->buffer);
|
2015-07-20 20:27:28 +02:00
|
|
|
s->box_flags &= ~STYL_BOX;
|
2020-04-04 22:43:25 +02:00
|
|
|
return 0;
|
2015-06-26 22:14:44 +02:00
|
|
|
}
|
2020-10-17 03:35:08 +02:00
|
|
|
s->style_attributes = tmp;
|
|
|
|
s->style_attributes_temp.style_end = s->text_pos;
|
|
|
|
s->style_attributes[s->count++] = s->style_attributes_temp;
|
|
|
|
s->box_flags |= STYL_BOX;
|
|
|
|
s->style_attributes_temp = s->d;
|
|
|
|
s->style_attributes_temp.style_start = s->text_pos;
|
2020-04-04 22:43:25 +02:00
|
|
|
} else { // style entry matches defaults, drop entry
|
2020-10-17 03:35:08 +02:00
|
|
|
s->style_attributes_temp = s->d;
|
|
|
|
s->style_attributes_temp.style_start = s->text_pos;
|
2020-04-04 22:43:25 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t mov_text_style_to_flag(const char style)
|
|
|
|
{
|
|
|
|
uint8_t style_flag = 0;
|
|
|
|
|
|
|
|
switch (style){
|
|
|
|
case 'b':
|
|
|
|
style_flag = STYLE_FLAG_BOLD;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
style_flag = STYLE_FLAG_ITALIC;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
style_flag = STYLE_FLAG_UNDERLINE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return style_flag;
|
|
|
|
}
|
|
|
|
|
2020-04-04 23:23:34 +02:00
|
|
|
static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
|
|
|
|
{
|
2020-10-17 03:35:08 +02:00
|
|
|
if (!((s->style_attributes_temp.style_flag & style_flags) ^ style_flags)) {
|
2020-04-04 23:23:34 +02:00
|
|
|
// setting flags that that are already set
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mov_text_style_start(s))
|
2020-10-17 03:35:08 +02:00
|
|
|
s->style_attributes_temp.style_flag |= style_flags;
|
2020-04-04 23:23:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 22:43:25 +02:00
|
|
|
static void mov_text_style_cb(void *priv, const char style, int close)
|
|
|
|
{
|
|
|
|
MovTextContext *s = priv;
|
|
|
|
uint8_t style_flag = mov_text_style_to_flag(style);
|
|
|
|
|
2020-10-17 03:35:08 +02:00
|
|
|
if (!!(s->style_attributes_temp.style_flag & style_flag) != close) {
|
2020-04-04 22:43:25 +02:00
|
|
|
// setting flag that is already set
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mov_text_style_start(s)) {
|
|
|
|
if (!close)
|
2020-10-17 03:35:08 +02:00
|
|
|
s->style_attributes_temp.style_flag |= style_flag;
|
2020-04-04 22:43:25 +02:00
|
|
|
else
|
2020-10-17 03:35:08 +02:00
|
|
|
s->style_attributes_temp.style_flag &= ~style_flag;
|
2015-06-26 22:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 00:05:18 +02:00
|
|
|
static void mov_text_color_set(MovTextContext *s, uint32_t color)
|
|
|
|
{
|
2020-10-17 03:35:08 +02:00
|
|
|
if ((s->style_attributes_temp.style_color & 0xffffff00) == color) {
|
2020-04-05 00:05:18 +02:00
|
|
|
// color hasn't changed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mov_text_style_start(s))
|
2020-10-17 03:35:08 +02:00
|
|
|
s->style_attributes_temp.style_color = (color & 0xffffff00) |
|
|
|
|
(s->style_attributes_temp.style_color & 0xff);
|
2020-04-05 00:05:18 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 20:32:31 +02:00
|
|
|
static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id)
|
|
|
|
{
|
|
|
|
MovTextContext *s = priv;
|
2020-04-04 21:36:51 +02:00
|
|
|
|
|
|
|
color = BGR_TO_RGB(color) << 8;
|
2020-04-05 00:05:18 +02:00
|
|
|
if (color_id == 1) { //primary color changes
|
|
|
|
mov_text_color_set(s, color);
|
|
|
|
} else if (color_id == 2) { //secondary color changes
|
2020-04-09 21:12:39 +02:00
|
|
|
if (!(s->box_flags & HCLR_BOX))
|
|
|
|
// Highlight alpha not set yet, use current primary alpha
|
2020-10-17 03:35:08 +02:00
|
|
|
s->hclr.color = s->style_attributes_temp.style_color;
|
2020-04-09 21:12:39 +02:00
|
|
|
if (!(s->box_flags & HLIT_BOX) || s->hlit.start == s->text_pos) {
|
2015-07-20 20:32:31 +02:00
|
|
|
s->box_flags |= HCLR_BOX;
|
|
|
|
s->box_flags |= HLIT_BOX;
|
2020-04-04 21:52:44 +02:00
|
|
|
s->hlit.start = s->text_pos;
|
2020-04-09 21:12:39 +02:00
|
|
|
s->hclr.color = color | (s->hclr.color & 0xFF);
|
2015-07-20 20:32:31 +02:00
|
|
|
}
|
2020-04-09 21:12:39 +02:00
|
|
|
else //close tag
|
|
|
|
s->hlit.end = s->text_pos;
|
|
|
|
/* If there are more than one secondary color changes in ASS,
|
|
|
|
take start of first section and end of last section. Movtext
|
|
|
|
allows only one highlight box per sample.
|
|
|
|
*/
|
2015-07-20 20:32:31 +02:00
|
|
|
}
|
2020-04-09 21:12:39 +02:00
|
|
|
// Movtext does not support changes to other color_id (outline, background)
|
2015-07-20 20:32:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 00:34:16 +02:00
|
|
|
static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha)
|
|
|
|
{
|
2020-10-17 03:35:08 +02:00
|
|
|
if ((s->style_attributes_temp.style_color & 0xff) == alpha) {
|
2020-04-05 00:34:16 +02:00
|
|
|
// color hasn't changed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mov_text_style_start(s))
|
2020-10-17 03:35:08 +02:00
|
|
|
s->style_attributes_temp.style_color =
|
|
|
|
(s->style_attributes_temp.style_color & 0xffffff00) | alpha;
|
2020-04-05 00:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id)
|
|
|
|
{
|
|
|
|
MovTextContext *s = priv;
|
|
|
|
|
2020-04-09 21:12:39 +02:00
|
|
|
alpha = 255 - alpha;
|
2020-04-05 00:34:16 +02:00
|
|
|
if (alpha_id == 1) // primary alpha changes
|
2020-04-09 21:12:39 +02:00
|
|
|
mov_text_alpha_set(s, alpha);
|
|
|
|
else if (alpha_id == 2) { //secondary alpha changes
|
|
|
|
if (!(s->box_flags & HCLR_BOX))
|
|
|
|
// Highlight color not set yet, use current primary color
|
2020-10-17 03:35:08 +02:00
|
|
|
s->hclr.color = s->style_attributes_temp.style_color;
|
2020-04-09 21:12:39 +02:00
|
|
|
if (!(s->box_flags & HLIT_BOX) || s->hlit.start == s->text_pos) {
|
|
|
|
s->box_flags |= HCLR_BOX;
|
|
|
|
s->box_flags |= HLIT_BOX;
|
|
|
|
s->hlit.start = s->text_pos;
|
|
|
|
s->hclr.color = (s->hclr.color & 0xffffff00) | alpha;
|
|
|
|
}
|
|
|
|
else //close tag
|
|
|
|
s->hlit.end = s->text_pos;
|
|
|
|
}
|
|
|
|
// Movtext does not support changes to other alpha_id (outline, background)
|
2020-04-05 00:34:16 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 23:27:16 +02:00
|
|
|
static uint16_t find_font_id(MovTextContext *s, const char *name)
|
2020-04-05 22:26:02 +02:00
|
|
|
{
|
2021-12-10 12:20:18 +02:00
|
|
|
if (!name)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (int i = 0; i < s->font_count; i++) {
|
2020-04-05 22:26:02 +02:00
|
|
|
if (!strcmp(name, s->fonts[i]))
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mov_text_font_name_set(MovTextContext *s, const char *name)
|
|
|
|
{
|
|
|
|
int fontID = find_font_id(s, name);
|
2020-10-17 03:35:08 +02:00
|
|
|
if (s->style_attributes_temp.style_fontID == fontID) {
|
2020-04-05 22:26:02 +02:00
|
|
|
// color hasn't changed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mov_text_style_start(s))
|
2020-10-17 03:35:08 +02:00
|
|
|
s->style_attributes_temp.style_fontID = fontID;
|
2020-04-05 22:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mov_text_font_name_cb(void *priv, const char *name)
|
|
|
|
{
|
|
|
|
mov_text_font_name_set((MovTextContext*)priv, name);
|
|
|
|
}
|
|
|
|
|
2020-04-05 00:46:40 +02:00
|
|
|
static void mov_text_font_size_set(MovTextContext *s, int size)
|
|
|
|
{
|
2020-04-05 23:02:27 +02:00
|
|
|
size = FONTSIZE_SCALE(s, size);
|
2020-10-17 03:35:08 +02:00
|
|
|
if (s->style_attributes_temp.style_fontsize == size) {
|
2020-04-05 00:46:40 +02:00
|
|
|
// color hasn't changed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mov_text_style_start(s))
|
2020-10-17 03:35:08 +02:00
|
|
|
s->style_attributes_temp.style_fontsize = size;
|
2020-04-05 00:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mov_text_font_size_cb(void *priv, int size)
|
|
|
|
{
|
|
|
|
mov_text_font_size_set((MovTextContext*)priv, size);
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:52:00 +02:00
|
|
|
static void mov_text_end_cb(void *priv)
|
|
|
|
{
|
|
|
|
// End of text, close any open style record
|
|
|
|
mov_text_style_start((MovTextContext*)priv);
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:34:45 +02:00
|
|
|
static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style)
|
2020-04-04 23:23:34 +02:00
|
|
|
{
|
2020-04-05 00:34:16 +02:00
|
|
|
uint8_t style_flags, alpha;
|
2020-04-05 00:05:18 +02:00
|
|
|
uint32_t color;
|
2020-04-04 23:23:34 +02:00
|
|
|
|
|
|
|
if (style) {
|
|
|
|
style_flags = (!!style->bold * STYLE_FLAG_BOLD) |
|
|
|
|
(!!style->italic * STYLE_FLAG_ITALIC) |
|
|
|
|
(!!style->underline * STYLE_FLAG_UNDERLINE);
|
|
|
|
mov_text_style_set(s, style_flags);
|
2020-04-05 00:05:18 +02:00
|
|
|
color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8;
|
|
|
|
mov_text_color_set(s, color);
|
2020-04-05 00:34:16 +02:00
|
|
|
alpha = 255 - ((uint32_t)style->primary_color >> 24);
|
|
|
|
mov_text_alpha_set(s, alpha);
|
2020-04-05 00:46:40 +02:00
|
|
|
mov_text_font_size_set(s, style->font_size);
|
2020-04-05 22:26:02 +02:00
|
|
|
mov_text_font_name_set(s, style->font_name);
|
2020-04-05 16:34:45 +02:00
|
|
|
} else {
|
|
|
|
// End current style record, go back to defaults
|
|
|
|
mov_text_style_start(s);
|
2020-04-04 23:23:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:34:45 +02:00
|
|
|
static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog)
|
|
|
|
{
|
2020-10-14 23:27:16 +02:00
|
|
|
ASSStyle *style = ff_ass_style_get(s->ass_ctx, dialog->style);
|
2020-04-05 16:34:45 +02:00
|
|
|
|
|
|
|
s->ass_dialog_style = style;
|
|
|
|
mov_text_ass_style_set(s, style);
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:27:16 +02:00
|
|
|
static void mov_text_cancel_overrides_cb(void *priv, const char *style_name)
|
2020-04-05 16:34:45 +02:00
|
|
|
{
|
|
|
|
MovTextContext *s = priv;
|
2020-10-14 23:27:16 +02:00
|
|
|
ASSStyle *style;
|
2020-04-05 16:34:45 +02:00
|
|
|
|
|
|
|
if (!style_name || !*style_name)
|
|
|
|
style = s->ass_dialog_style;
|
|
|
|
else
|
|
|
|
style= ff_ass_style_get(s->ass_ctx, style_name);
|
|
|
|
|
|
|
|
mov_text_ass_style_set(s, style);
|
|
|
|
}
|
|
|
|
|
2021-11-29 18:41:16 +02:00
|
|
|
static unsigned utf8_strlen(const char *text, int len)
|
2018-03-28 04:55:24 +02:00
|
|
|
{
|
2021-11-29 18:41:16 +02:00
|
|
|
unsigned i = 0, ret = 0;
|
2018-03-28 04:55:24 +02:00
|
|
|
while (i < len) {
|
|
|
|
char c = text[i];
|
|
|
|
if ((c & 0x80) == 0)
|
|
|
|
i += 1;
|
|
|
|
else if ((c & 0xE0) == 0xC0)
|
|
|
|
i += 2;
|
|
|
|
else if ((c & 0xF0) == 0xE0)
|
|
|
|
i += 3;
|
|
|
|
else if ((c & 0xF8) == 0xF0)
|
|
|
|
i += 4;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-06-06 19:12:24 +03:00
|
|
|
static void mov_text_text_cb(void *priv, const char *text, int len)
|
|
|
|
{
|
2021-11-29 18:41:16 +02:00
|
|
|
unsigned utf8_len = utf8_strlen(text, len);
|
2012-06-06 19:12:24 +03:00
|
|
|
MovTextContext *s = priv;
|
2015-06-26 22:14:44 +02:00
|
|
|
av_bprint_append_data(&s->buffer, text, len);
|
2018-03-28 04:55:24 +02:00
|
|
|
// If it's not utf-8, just use the byte length
|
|
|
|
s->text_pos += utf8_len ? utf8_len : len;
|
2012-06-06 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mov_text_new_line_cb(void *priv, int forced)
|
|
|
|
{
|
|
|
|
MovTextContext *s = priv;
|
2015-06-26 22:14:44 +02:00
|
|
|
s->text_pos += 1;
|
2021-11-30 12:08:33 +02:00
|
|
|
av_bprint_chars(&s->buffer, '\n', 1);
|
2012-06-06 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const ASSCodesCallbacks mov_text_callbacks = {
|
2020-04-05 16:34:45 +02:00
|
|
|
.text = mov_text_text_cb,
|
|
|
|
.new_line = mov_text_new_line_cb,
|
|
|
|
.style = mov_text_style_cb,
|
|
|
|
.color = mov_text_color_cb,
|
|
|
|
.alpha = mov_text_alpha_cb,
|
2020-04-05 22:26:02 +02:00
|
|
|
.font_name = mov_text_font_name_cb,
|
2020-04-05 16:34:45 +02:00
|
|
|
.font_size = mov_text_font_size_cb,
|
|
|
|
.cancel_overrides = mov_text_cancel_overrides_cb,
|
|
|
|
.end = mov_text_end_cb,
|
2012-06-06 19:12:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
|
2012-09-18 15:30:31 +03:00
|
|
|
int bufsize, const AVSubtitle *sub)
|
2012-06-06 19:12:24 +03:00
|
|
|
{
|
|
|
|
MovTextContext *s = avctx->priv_data;
|
|
|
|
ASSDialog *dialog;
|
2016-02-21 13:58:36 +02:00
|
|
|
int i, length;
|
2012-06-06 19:12:24 +03:00
|
|
|
|
2024-02-18 02:54:45 +02:00
|
|
|
if (bufsize < 3)
|
|
|
|
goto too_small;
|
|
|
|
|
2015-06-26 22:14:44 +02:00
|
|
|
s->text_pos = 0;
|
|
|
|
s->count = 0;
|
2015-07-20 20:27:28 +02:00
|
|
|
s->box_flags = 0;
|
2024-02-18 02:54:45 +02:00
|
|
|
|
|
|
|
av_bprint_init_for_buffer(&s->buffer, buf + 2, bufsize - 2);
|
2012-06-06 19:12:24 +03:00
|
|
|
for (i = 0; i < sub->num_rects; i++) {
|
2016-01-06 14:43:23 +02:00
|
|
|
const char *ass = sub->rects[i]->ass;
|
2012-06-06 19:12:24 +03:00
|
|
|
|
|
|
|
if (sub->rects[i]->type != SUBTITLE_ASS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
|
2020-04-10 20:30:10 +02:00
|
|
|
return AVERROR(EINVAL);
|
2012-06-06 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
2021-08-07 23:52:42 +02:00
|
|
|
dialog = ff_ass_split_dialog(s->ass_ctx, ass);
|
2021-02-25 21:33:44 +02:00
|
|
|
if (!dialog)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
mov_text_dialog(s, dialog);
|
|
|
|
ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
|
|
|
|
ff_ass_free_dialog(&dialog);
|
2012-06-06 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
2021-11-30 12:05:52 +02:00
|
|
|
if (s->buffer.len > UINT16_MAX)
|
2021-11-30 10:21:35 +02:00
|
|
|
return AVERROR(ERANGE);
|
2021-11-30 12:05:52 +02:00
|
|
|
AV_WB16(buf, s->buffer.len);
|
2012-06-06 19:12:24 +03:00
|
|
|
|
2021-11-30 11:02:13 +02:00
|
|
|
for (size_t j = 0; j < box_count; j++)
|
|
|
|
box_types[j].encode(s);
|
|
|
|
|
2021-11-30 10:24:51 +02:00
|
|
|
if (!s->buffer.len)
|
|
|
|
return 0;
|
2015-06-26 22:14:44 +02:00
|
|
|
|
2024-02-18 02:54:45 +02:00
|
|
|
if (!av_bprint_is_complete(&s->buffer)) {
|
|
|
|
too_small:
|
2012-06-06 19:12:24 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
|
2021-11-30 10:24:51 +02:00
|
|
|
return AVERROR_BUFFER_TOO_SMALL;
|
2012-06-06 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
2015-06-26 22:14:44 +02:00
|
|
|
length = s->buffer.len + 2;
|
|
|
|
|
|
|
|
return length;
|
2012-06-06 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
2020-04-05 23:02:27 +02:00
|
|
|
#define OFFSET(x) offsetof(MovTextContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "height", "Frame height, usually video height", OFFSET(frame_height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass mov_text_encoder_class = {
|
|
|
|
.class_name = "MOV text enoder",
|
2024-01-19 14:33:28 +02:00
|
|
|
.item_name = av_default_item_name,
|
2020-04-05 23:02:27 +02:00
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_movtext_encoder = {
|
|
|
|
.p.name = "mov_text",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("3GPP Timed Text subtitle"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_SUBTITLE,
|
|
|
|
.p.id = AV_CODEC_ID_MOV_TEXT,
|
2012-06-06 19:12:24 +03:00
|
|
|
.priv_data_size = sizeof(MovTextContext),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.priv_class = &mov_text_encoder_class,
|
2012-06-06 19:12:24 +03:00
|
|
|
.init = mov_text_encode_init,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_ENCODE_SUB_CB(mov_text_encode_frame),
|
2012-06-06 19:12:24 +03:00
|
|
|
.close = mov_text_encode_close,
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2012-06-06 19:12:24 +03:00
|
|
|
};
|