From patchwork Mon Apr 6 17:52:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18729 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id D126C4495FB for ; Mon, 6 Apr 2020 20:58:37 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BC5BE68B4F5; Mon, 6 Apr 2020 20:58:37 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.jetheaddev.com (mail.jetheaddev.com [70.164.99.34]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 91A1468B4C0 for ; Mon, 6 Apr 2020 20:58:31 +0300 (EEST) Received: from creator.alpe-d-promontory.fun (192.168.13.165) by cas.jetheaddev.com (192.168.13.27) with Microsoft SMTP Server (TLS) id 14.3.351.0; Mon, 6 Apr 2020 10:58:30 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:11 -0600 Message-ID: <20200406175218.1299994-17-jstebbins@jetheaddev.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200406175218.1299994-1-jstebbins@jetheaddev.com> References: <20200406175218.1299994-1-jstebbins@jetheaddev.com> MIME-Version: 1.0 X-Originating-IP: [192.168.13.165] Subject: [FFmpeg-devel] [PATCH 16/23] lavc/movtextenc: init style record from ASS dialog style X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavcodec/movtextenc.c | 60 ++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index d389111419..4e7d55efcb 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -39,6 +39,11 @@ #define HLIT_BOX (1<<1) #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 av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size) @@ -46,6 +51,9 @@ typedef struct { uint16_t style_start; uint16_t style_end; uint8_t style_flag; + uint16_t style_fontID; + uint8_t style_fontsize; + uint32_t style_color; } StyleBox; typedef struct { @@ -68,9 +76,7 @@ typedef struct { HilightcolorBox hclr; int count; uint8_t box_flags; - uint16_t style_fontID; - uint8_t style_fontsize; - uint32_t style_color; + StyleBox d; uint16_t text_pos; uint16_t byte_count; } MovTextContext; @@ -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 = AV_RB32(&tsmb_size); 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 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_type, 4); av_bprint_append_any(&s->buffer, &style_entries, 2); 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_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_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_fontsize, 1); - av_bprint_append_any(&s->buffer, &s->style_color, 4); + av_bprint_append_any(&s->buffer, &s->d.style_fontsize, 1); + av_bprint_append_any(&s->buffer, &style_color, 4); } } mov_text_cleanup(s); @@ -220,6 +227,13 @@ static av_cold int mov_text_encode_init(AVCodecContext *avctx) memcpy(avctx->extradata, text_sample_entry, avctx->extradata_size); 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; } @@ -270,6 +284,17 @@ static uint8_t mov_text_style_to_flag(const char style) 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) { MovTextContext *s = priv; @@ -315,6 +340,19 @@ static void mov_text_end_cb(void *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) { uint16_t i = 0, ret = 0; @@ -386,6 +424,7 @@ static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, int num; dialog = ff_ass_split_dialog(s->ass_ctx, ass, 0, &num); for (; dialog && num--; dialog++) { + mov_text_dialog(s, dialog); ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text); } } else { @@ -393,6 +432,7 @@ static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, dialog = ff_ass_split_dialog2(s->ass_ctx, ass); 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); #if FF_API_ASS_TIMING