Commit 37ab5e2e authored by John Stebbins's avatar John Stebbins Committed by Philip Langdale

lavc/movtextenc: init style record from ASS dialog style

Signed-off-by: 's avatarPhilip Langdale <philipl@overt.org>
parent a129cc9e
...@@ -39,6 +39,11 @@ ...@@ -39,6 +39,11 @@
#define HLIT_BOX (1<<1) #define HLIT_BOX (1<<1)
#define HCLR_BOX (1<<2) #define HCLR_BOX (1<<2)
#define DEFAULT_STYLE_FONT_ID 0x01
#define DEFAULT_STYLE_FONTSIZE 0x12
#define DEFAULT_STYLE_COLOR 0xffffffff
#define DEFAULT_STYLE_FLAG 0x00
#define BGR_TO_RGB(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((c) >> 16) & 0xff)) #define BGR_TO_RGB(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((c) >> 16) & 0xff))
#define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size) #define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size)
...@@ -46,6 +51,9 @@ typedef struct { ...@@ -46,6 +51,9 @@ typedef struct {
uint16_t style_start; uint16_t style_start;
uint16_t style_end; uint16_t style_end;
uint8_t style_flag; uint8_t style_flag;
uint16_t style_fontID;
uint8_t style_fontsize;
uint32_t style_color;
} StyleBox; } StyleBox;
typedef struct { typedef struct {
...@@ -68,9 +76,7 @@ typedef struct { ...@@ -68,9 +76,7 @@ typedef struct {
HilightcolorBox hclr; HilightcolorBox hclr;
int count; int count;
uint8_t box_flags; uint8_t box_flags;
uint16_t style_fontID; StyleBox d;
uint8_t style_fontsize;
uint32_t style_color;
uint16_t text_pos; uint16_t text_pos;
uint16_t byte_count; uint16_t byte_count;
} MovTextContext; } MovTextContext;
...@@ -104,25 +110,26 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type) ...@@ -104,25 +110,26 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type)
tsmb_size = s->count * STYLE_RECORD_SIZE + SIZE_ADD; tsmb_size = s->count * STYLE_RECORD_SIZE + SIZE_ADD;
tsmb_size = AV_RB32(&tsmb_size); tsmb_size = AV_RB32(&tsmb_size);
style_entries = AV_RB16(&s->count); style_entries = AV_RB16(&s->count);
s->style_fontID = 0x00 | 0x01<<8;
s->style_fontsize = 0x12;
s->style_color = MKTAG(0xFF, 0xFF, 0xFF, 0xFF);
/*The above three attributes are hard coded for now /*The above three attributes are hard coded for now
but will come from ASS style in the future*/ but will come from ASS style in the future*/
av_bprint_append_any(&s->buffer, &tsmb_size, 4); av_bprint_append_any(&s->buffer, &tsmb_size, 4);
av_bprint_append_any(&s->buffer, &tsmb_type, 4); av_bprint_append_any(&s->buffer, &tsmb_type, 4);
av_bprint_append_any(&s->buffer, &style_entries, 2); av_bprint_append_any(&s->buffer, &style_entries, 2);
for (j = 0; j < s->count; j++) { for (j = 0; j < s->count; j++) {
uint16_t style_start, style_end; uint16_t style_start, style_end, style_fontID;
uint32_t style_color;
style_start = AV_RB16(&s->style_attributes[j]->style_start); style_start = AV_RB16(&s->style_attributes[j]->style_start);
style_end = AV_RB16(&s->style_attributes[j]->style_end); style_end = AV_RB16(&s->style_attributes[j]->style_end);
style_color = AV_RB32(&s->d.style_color);
style_fontID = AV_RB16(&s->d.style_fontID);
av_bprint_append_any(&s->buffer, &style_start, 2); av_bprint_append_any(&s->buffer, &style_start, 2);
av_bprint_append_any(&s->buffer, &style_end, 2); av_bprint_append_any(&s->buffer, &style_end, 2);
av_bprint_append_any(&s->buffer, &s->style_fontID, 2); av_bprint_append_any(&s->buffer, &style_fontID, 2);
av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_flag, 1); av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_flag, 1);
av_bprint_append_any(&s->buffer, &s->style_fontsize, 1); av_bprint_append_any(&s->buffer, &s->d.style_fontsize, 1);
av_bprint_append_any(&s->buffer, &s->style_color, 4); av_bprint_append_any(&s->buffer, &style_color, 4);
} }
} }
mov_text_cleanup(s); mov_text_cleanup(s);
...@@ -220,6 +227,13 @@ static av_cold int mov_text_encode_init(AVCodecContext *avctx) ...@@ -220,6 +227,13 @@ static av_cold int mov_text_encode_init(AVCodecContext *avctx)
memcpy(avctx->extradata, text_sample_entry, avctx->extradata_size); memcpy(avctx->extradata, text_sample_entry, avctx->extradata_size);
s->ass_ctx = ff_ass_split(avctx->subtitle_header); s->ass_ctx = ff_ass_split(avctx->subtitle_header);
// TODO: Initialize from ASS style record
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;
return s->ass_ctx ? 0 : AVERROR_INVALIDDATA; return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
} }
...@@ -270,6 +284,17 @@ static uint8_t mov_text_style_to_flag(const char style) ...@@ -270,6 +284,17 @@ static uint8_t mov_text_style_to_flag(const char style)
return style_flag; return style_flag;
} }
static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
{
if (!s->style_attributes_temp ||
!((s->style_attributes_temp->style_flag & style_flags) ^ style_flags)) {
// setting flags that that are already set
return;
}
if (mov_text_style_start(s))
s->style_attributes_temp->style_flag |= style_flags;
}
static void mov_text_style_cb(void *priv, const char style, int close) static void mov_text_style_cb(void *priv, const char style, int close)
{ {
MovTextContext *s = priv; MovTextContext *s = priv;
...@@ -315,6 +340,19 @@ static void mov_text_end_cb(void *priv) ...@@ -315,6 +340,19 @@ static void mov_text_end_cb(void *priv)
mov_text_style_start((MovTextContext*)priv); mov_text_style_start((MovTextContext*)priv);
} }
static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog)
{
ASSStyle * style = ff_ass_style_get(s->ass_ctx, dialog->style);
uint8_t style_flags;
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);
}
}
static uint16_t utf8_strlen(const char *text, int len) static uint16_t utf8_strlen(const char *text, int len)
{ {
uint16_t i = 0, ret = 0; uint16_t i = 0, ret = 0;
...@@ -386,6 +424,7 @@ static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, ...@@ -386,6 +424,7 @@ static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
int num; int num;
dialog = ff_ass_split_dialog(s->ass_ctx, ass, 0, &num); dialog = ff_ass_split_dialog(s->ass_ctx, ass, 0, &num);
for (; dialog && num--; dialog++) { for (; dialog && num--; dialog++) {
mov_text_dialog(s, dialog);
ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text); ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
} }
} else { } else {
...@@ -393,6 +432,7 @@ static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, ...@@ -393,6 +432,7 @@ static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
dialog = ff_ass_split_dialog2(s->ass_ctx, ass); dialog = ff_ass_split_dialog2(s->ass_ctx, ass);
if (!dialog) if (!dialog)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
mov_text_dialog(s, dialog);
ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text); ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
ff_ass_free_dialog(&dialog); ff_ass_free_dialog(&dialog);
#if FF_API_ASS_TIMING #if FF_API_ASS_TIMING
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment