From patchwork Mon Apr 6 17:51:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18714 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 1BA3844BB75 for ; Mon, 6 Apr 2020 20:52:56 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 04DE268B3C7; Mon, 6 Apr 2020 20:52:56 +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 6854F68B31C for ; Mon, 6 Apr 2020 20:52:49 +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:52:47 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:51:56 -0600 Message-ID: <20200406175218.1299994-2-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 01/23] lavc/movtextdec: fix ass header colors 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" A conversion from rgb to bgr is necessary --- libavcodec/movtextdec.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index c38c5edce6..05becaf64d 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -48,6 +48,8 @@ #define TOP_CENTER 8 #define TOP_RIGHT 9 +#define RGB_TO_BGR(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((c) >> 16) & 0xff)) + typedef struct { char *font; int fontsize; @@ -448,10 +450,11 @@ static int mov_text_init(AVCodecContext *avctx) { MovTextContext *m = avctx->priv_data; ret = mov_text_tx3g(avctx, m); if (ret == 0) { - return ff_ass_subtitle_header(avctx, m->d.font, m->d.fontsize, m->d.color, - m->d.back_color, m->d.bold, m->d.italic, - m->d.underline, ASS_DEFAULT_BORDERSTYLE, - m->d.alignment); + return ff_ass_subtitle_header(avctx, m->d.font, m->d.fontsize, + RGB_TO_BGR(m->d.color), + RGB_TO_BGR(m->d.back_color), + m->d.bold, m->d.italic, m->d.underline, + ASS_DEFAULT_BORDERSTYLE, m->d.alignment); } else return ff_ass_subtitle_header_default(avctx); } From patchwork Mon Apr 6 17:51:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18715 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 165E244BD26 for ; Mon, 6 Apr 2020 20:53:38 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id ED68868B393; Mon, 6 Apr 2020 20:53: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 0EB5E68ACFC for ; Mon, 6 Apr 2020 20:53: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:53:29 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:51:57 -0600 Message-ID: <20200406175218.1299994-3-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 02/23] lavc/movtextdec: simplify style record walk 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" It's not necessary to walk the style record list twice per subtitle character. style records are in order and do not overlap. --- libavcodec/movtextdec.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 05becaf64d..47a8401119 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -355,8 +355,9 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, { MovTextContext *m = avctx->priv_data; int i = 0; - int j = 0; int text_pos = 0; + int style_active = 0; + int entry = 0; if (text < text_end && m->box_flags & TWRP_BOX) { if (m->w.wrap_flag == 1) { @@ -369,26 +370,27 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, while (text < text_end) { int len; - if (m->box_flags & STYL_BOX) { - for (i = 0; i < m->style_entries; i++) { - if (m->s[i]->style_flag && text_pos == m->s[i]->style_end) { - av_bprintf(buf, "{\\r}"); + if ((m->box_flags & STYL_BOX) && entry < m->style_entries) { + if (text_pos == m->s[entry]->style_start) { + style_active = 1; + if (m->s[entry]->style_flag & STYLE_FLAG_BOLD) + av_bprintf(buf, "{\\b1}"); + if (m->s[entry]->style_flag & STYLE_FLAG_ITALIC) + av_bprintf(buf, "{\\i1}"); + if (m->s[entry]->style_flag & STYLE_FLAG_UNDERLINE) + av_bprintf(buf, "{\\u1}"); + av_bprintf(buf, "{\\fs%d}", m->s[entry]->fontsize); + for (i = 0; i < m->ftab_entries; i++) { + if (m->s[entry]->style_fontID == m->ftab[i]->fontID) + av_bprintf(buf, "{\\fn%s}", m->ftab[i]->font); } } - for (i = 0; i < m->style_entries; i++) { - if (m->s[i]->style_flag && text_pos == m->s[i]->style_start) { - if (m->s[i]->style_flag & STYLE_FLAG_BOLD) - av_bprintf(buf, "{\\b1}"); - if (m->s[i]->style_flag & STYLE_FLAG_ITALIC) - av_bprintf(buf, "{\\i1}"); - if (m->s[i]->style_flag & STYLE_FLAG_UNDERLINE) - av_bprintf(buf, "{\\u1}"); - av_bprintf(buf, "{\\fs%d}", m->s[i]->fontsize); - for (j = 0; j < m->ftab_entries; j++) { - if (m->s[i]->style_fontID == m->ftab[j]->fontID) - av_bprintf(buf, "{\\fn%s}", m->ftab[j]->font); - } + if (text_pos == m->s[entry]->style_end) { + if (style_active) { + av_bprintf(buf, "{\\r}"); + style_active = 0; } + entry++; } } if (m->box_flags & HLIT_BOX) { From patchwork Mon Apr 6 17:51:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18716 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 F19CE44BD26 for ; Mon, 6 Apr 2020 20:53:45 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DB5C468B3F8; Mon, 6 Apr 2020 20:53:45 +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 C25DF68B035 for ; Mon, 6 Apr 2020 20:53:38 +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:53:37 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:51:58 -0600 Message-ID: <20200406175218.1299994-4-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 03/23] lavc/movtextdec: fix bold, italic, underline flags 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" They should be 0 or 1 so that 0 or -1 is written to the ass header --- libavcodec/movtextdec.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 47a8401119..d6896562c2 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -193,9 +193,9 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) tx3g_ptr += 2; // face-style-flags s_default.style_flag = *tx3g_ptr++; - m->d.bold = s_default.style_flag & STYLE_FLAG_BOLD; - m->d.italic = s_default.style_flag & STYLE_FLAG_ITALIC; - m->d.underline = s_default.style_flag & STYLE_FLAG_UNDERLINE; + m->d.bold = !!(s_default.style_flag & STYLE_FLAG_BOLD); + m->d.italic = !!(s_default.style_flag & STYLE_FLAG_ITALIC); + m->d.underline = !!(s_default.style_flag & STYLE_FLAG_UNDERLINE); // fontsize m->d.fontsize = *tx3g_ptr++; // Primary color From patchwork Mon Apr 6 17:51:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18717 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 E139D44BD3A for ; Mon, 6 Apr 2020 20:54:24 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CD55168B393; Mon, 6 Apr 2020 20:54:24 +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 A5F6668B393 for ; Mon, 6 Apr 2020 20:54:17 +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:54:16 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:51:59 -0600 Message-ID: <20200406175218.1299994-5-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 04/23] lavc/movtextdec: handle changes to default style flags 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" Style flags were only being turned on. If the default was on and style record turned off, style flag remained on. --- libavcodec/movtextdec.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index d6896562c2..a3e37d013d 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -55,9 +55,9 @@ typedef struct { int fontsize; int color; int back_color; - int bold; - int italic; - int underline; + uint8_t bold; + uint8_t italic; + uint8_t underline; int alignment; } MovTextDefault; @@ -70,6 +70,9 @@ typedef struct { uint16_t style_start; uint16_t style_end; uint8_t style_flag; + uint8_t bold; + uint8_t italic; + uint8_t underline; uint8_t fontsize; uint16_t style_fontID; } StyleBox; @@ -313,6 +316,9 @@ static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) m->s_temp->style_fontID = AV_RB16(tsmb); tsmb += 2; m->s_temp->style_flag = AV_RB8(tsmb); + m->s_temp->bold = !!(m->s_temp->style_flag & STYLE_FLAG_BOLD); + m->s_temp->italic = !!(m->s_temp->style_flag & STYLE_FLAG_ITALIC); + m->s_temp->underline = !!(m->s_temp->style_flag & STYLE_FLAG_UNDERLINE); tsmb++; m->s_temp->fontsize = AV_RB8(tsmb); av_dynarray_add(&m->s, &m->count_s, m->s_temp); @@ -373,12 +379,12 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, if ((m->box_flags & STYL_BOX) && entry < m->style_entries) { if (text_pos == m->s[entry]->style_start) { style_active = 1; - if (m->s[entry]->style_flag & STYLE_FLAG_BOLD) - av_bprintf(buf, "{\\b1}"); - if (m->s[entry]->style_flag & STYLE_FLAG_ITALIC) - av_bprintf(buf, "{\\i1}"); - if (m->s[entry]->style_flag & STYLE_FLAG_UNDERLINE) - av_bprintf(buf, "{\\u1}"); + if (m->s[entry]->bold ^ m->d.bold) + av_bprintf(buf, "{\\b%d}", m->s[entry]->bold); + if (m->s[entry]->italic ^ m->d.italic) + av_bprintf(buf, "{\\i%d}", m->s[entry]->italic); + if (m->s[entry]->underline ^ m->d.underline) + av_bprintf(buf, "{\\u%d}", m->s[entry]->underline); av_bprintf(buf, "{\\fs%d}", m->s[entry]->fontsize); for (i = 0; i < m->ftab_entries; i++) { if (m->s[entry]->style_fontID == m->ftab[i]->fontID) From patchwork Mon Apr 6 17:52:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18718 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 B208144BD3A for ; Mon, 6 Apr 2020 20:54:32 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9CF0268B3FF; Mon, 6 Apr 2020 20:54:32 +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 BFD8368B3CB for ; Mon, 6 Apr 2020 20:54:25 +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:54:24 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:00 -0600 Message-ID: <20200406175218.1299994-6-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 05/23] lavc/movtextdec: only write fontsize, fontID tags if not default 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/movtextdec.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index a3e37d013d..6c7d93702e 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -51,8 +51,9 @@ #define RGB_TO_BGR(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((c) >> 16) & 0xff)) typedef struct { + uint16_t fontID; char *font; - int fontsize; + uint8_t fontsize; int color; int back_color; uint8_t bold; @@ -146,7 +147,6 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) uint8_t *tx3g_ptr = avctx->extradata; int i, box_size, font_length; int8_t v_align, h_align; - int style_fontID; StyleBox s_default; m->count_f = 0; @@ -192,7 +192,7 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) // StyleRecord tx3g_ptr += 4; // fontID - style_fontID = AV_RB16(tx3g_ptr); + m->d.fontID = AV_RB16(tx3g_ptr); tx3g_ptr += 2; // face-style-flags s_default.style_flag = *tx3g_ptr++; @@ -252,7 +252,7 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) tx3g_ptr = tx3g_ptr + font_length; } for (i = 0; i < m->ftab_entries; i++) { - if (style_fontID == m->ftab[i]->fontID) + if (m->d.fontID == m->ftab[i]->fontID) m->d.font = m->ftab[i]->font; } return 0; @@ -385,11 +385,13 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, av_bprintf(buf, "{\\i%d}", m->s[entry]->italic); if (m->s[entry]->underline ^ m->d.underline) av_bprintf(buf, "{\\u%d}", m->s[entry]->underline); - av_bprintf(buf, "{\\fs%d}", m->s[entry]->fontsize); - for (i = 0; i < m->ftab_entries; i++) { - if (m->s[entry]->style_fontID == m->ftab[i]->fontID) - av_bprintf(buf, "{\\fn%s}", m->ftab[i]->font); - } + if (m->s[entry]->fontsize != m->d.fontsize) + av_bprintf(buf, "{\\fs%d}", m->s[entry]->fontsize); + if (m->s[entry]->style_fontID != m->d.fontID) + for (i = 0; i < m->ftab_entries; i++) { + if (m->s[entry]->style_fontID == m->ftab[i]->fontID) + av_bprintf(buf, "{\\fn%s}", m->ftab[i]->font); + } } if (text_pos == m->s[entry]->style_end) { if (style_active) { From patchwork Mon Apr 6 17:52:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18719 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 A407944BD3F for ; Mon, 6 Apr 2020 20:55:10 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 88F7068B469; Mon, 6 Apr 2020 20:55:10 +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 402BD68ACB2 for ; Mon, 6 Apr 2020 20:55:09 +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:55:07 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:01 -0600 Message-ID: <20200406175218.1299994-7-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 06/23] lavc/movtextdec: make sure default font name is set 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/movtextdec.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 6c7d93702e..2481c71af6 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -52,7 +52,7 @@ typedef struct { uint16_t fontID; - char *font; + const char *font; uint8_t fontsize; int color; int back_color; @@ -251,6 +251,8 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) m->ftab_temp = NULL; tx3g_ptr = tx3g_ptr + font_length; } + // In case of broken header, init default font + m->d.font = ASS_DEFAULT_FONT; for (i = 0; i < m->ftab_entries; i++) { if (m->d.fontID == m->ftab[i]->fontID) m->d.font = m->ftab[i]->font; From patchwork Mon Apr 6 17:52:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18720 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 DA7E644BD3F for ; Mon, 6 Apr 2020 20:55:13 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C2C8868B472; Mon, 6 Apr 2020 20:55:13 +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 BBC9368B435 for ; Mon, 6 Apr 2020 20:55:11 +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:55:10 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:02 -0600 Message-ID: <20200406175218.1299994-8-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 07/23] lavc/movtextdec: add alpha default to ass header colors 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/movtextdec.c | 14 ++++++++++---- tests/ref/fate/sub-movtext | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 2481c71af6..eb9c7f5755 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -55,7 +55,9 @@ typedef struct { const char *font; uint8_t fontsize; int color; + uint8_t alpha; int back_color; + uint8_t back_alpha; uint8_t bold; uint8_t italic; uint8_t underline; @@ -186,7 +188,9 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) } // Background Color m->d.back_color = AV_RB24(tx3g_ptr); - tx3g_ptr += 4; + tx3g_ptr += 3; + m->d.back_alpha = AV_RB8(tx3g_ptr); + tx3g_ptr += 1; // BoxRecord tx3g_ptr += 8; // StyleRecord @@ -203,7 +207,9 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) m->d.fontsize = *tx3g_ptr++; // Primary color m->d.color = AV_RB24(tx3g_ptr); - tx3g_ptr += 4; + tx3g_ptr += 3; + m->d.alpha = AV_RB8(tx3g_ptr); + tx3g_ptr += 1; // FontRecord // FontRecord Size tx3g_ptr += 4; @@ -463,8 +469,8 @@ static int mov_text_init(AVCodecContext *avctx) { ret = mov_text_tx3g(avctx, m); if (ret == 0) { return ff_ass_subtitle_header(avctx, m->d.font, m->d.fontsize, - RGB_TO_BGR(m->d.color), - RGB_TO_BGR(m->d.back_color), + (255 - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color), + (255 - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color), m->d.bold, m->d.italic, m->d.underline, ASS_DEFAULT_BORDERSTYLE, m->d.alignment); } else diff --git a/tests/ref/fate/sub-movtext b/tests/ref/fate/sub-movtext index 94ed22d318..6047060918 100644 --- a/tests/ref/fate/sub-movtext +++ b/tests/ref/fate/sub-movtext @@ -6,7 +6,7 @@ PlayResY: 288 [V4+ Styles] Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding -Style: Default,Serif,18,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,0 +Style: Default,Serif,18,&Hffffff,&Hffffff,&Hff000000,&Hff000000,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,0 [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text From patchwork Mon Apr 6 17:52:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18721 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 0B92B44BD3F for ; Mon, 6 Apr 2020 20:55:52 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E163468B47B; Mon, 6 Apr 2020 20:55:51 +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 30CF368B3A2 for ; Mon, 6 Apr 2020 20:55:45 +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:55:43 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:03 -0600 Message-ID: <20200406175218.1299994-9-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 08/23] lavc/movtextdec: add color and alpha style tags 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/movtextdec.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index eb9c7f5755..4d5dcdf5e7 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -76,6 +76,8 @@ typedef struct { uint8_t bold; uint8_t italic; uint8_t underline; + int color; + uint8_t alpha; uint8_t fontsize; uint16_t style_fontID; } StyleBox; @@ -329,14 +331,16 @@ static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) m->s_temp->underline = !!(m->s_temp->style_flag & STYLE_FLAG_UNDERLINE); tsmb++; m->s_temp->fontsize = AV_RB8(tsmb); + tsmb++; + m->s_temp->color = AV_RB24(tsmb); + tsmb += 3; + m->s_temp->alpha = AV_RB8(tsmb); + tsmb++; av_dynarray_add(&m->s, &m->count_s, m->s_temp); if(!m->s) { mov_text_cleanup(m); return AVERROR(ENOMEM); } - tsmb++; - // text-color-rgba - tsmb += 4; } return 0; } @@ -400,6 +404,11 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, if (m->s[entry]->style_fontID == m->ftab[i]->fontID) av_bprintf(buf, "{\\fn%s}", m->ftab[i]->font); } + if (m->d.color != m->s[entry]->color) + av_bprintf(buf, "{\\1c&H%X&}", + RGB_TO_BGR(m->s[entry]->color)); + if (m->d.alpha != m->s[entry]->alpha) + av_bprintf(buf, "{\\1a&H%02X&}", 255 - m->s[entry]->alpha); } if (text_pos == m->s[entry]->style_end) { if (style_active) { From patchwork Mon Apr 6 17:52:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18722 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 EB09F44BD3F for ; Mon, 6 Apr 2020 20:55:53 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D451468B47A; Mon, 6 Apr 2020 20:55:53 +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 8FB9568B49C for ; Mon, 6 Apr 2020 20:55:52 +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:55:51 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:04 -0600 Message-ID: <20200406175218.1299994-10-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 09/23] lavc/movtextdec: restore active style color after hilite 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/movtextdec.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 4d5dcdf5e7..f3a504b47b 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -376,6 +376,7 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, int text_pos = 0; int style_active = 0; int entry = 0; + int color = m->d.color; if (text < text_end && m->box_flags & TWRP_BOX) { if (m->w.wrap_flag == 1) { @@ -404,9 +405,10 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, if (m->s[entry]->style_fontID == m->ftab[i]->fontID) av_bprintf(buf, "{\\fn%s}", m->ftab[i]->font); } - if (m->d.color != m->s[entry]->color) - av_bprintf(buf, "{\\1c&H%X&}", - RGB_TO_BGR(m->s[entry]->color)); + if (m->d.color != m->s[entry]->color) { + color = m->s[entry]->color; + av_bprintf(buf, "{\\1c&H%X&}", RGB_TO_BGR(color)); + } if (m->d.alpha != m->s[entry]->alpha) av_bprintf(buf, "{\\1a&H%02X&}", 255 - m->s[entry]->alpha); } @@ -414,6 +416,7 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, if (style_active) { av_bprintf(buf, "{\\r}"); style_active = 0; + color = m->d.color; } entry++; } @@ -435,9 +438,10 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, } if (text_pos == m->h.hlit_end) { if (m->box_flags & HCLR_BOX) { - av_bprintf(buf, "{\\2c&H000000&}"); + av_bprintf(buf, "{\\2c&H%X&}", RGB_TO_BGR(m->d.color)); } else { - av_bprintf(buf, "{\\1c&HFFFFFF&}{\\2c&H000000&}"); + av_bprintf(buf, "{\\1c&H%X&}{\\2c&H%X&}", + RGB_TO_BGR(color), RGB_TO_BGR(m->d.color)); } } } From patchwork Mon Apr 6 17:52:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18723 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 EB7E5448CAC for ; Mon, 6 Apr 2020 20:56:31 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CDAFF68B4A2; Mon, 6 Apr 2020 20:56:31 +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 82E4568B3A2 for ; Mon, 6 Apr 2020 20:56:25 +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:56:24 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:05 -0600 Message-ID: <20200406175218.1299994-11-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 10/23] lavc/movtextdec: allow setting subtitle frame dimensions 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" Font sizes are relative to the subtitle frame dimensions. If the expected frame dimensions are not known, the font sizes will most likely be incorrect. --- libavcodec/ass.c | 30 +++++++++++++++++++++++------- libavcodec/ass.h | 28 ++++++++++++++++++++++++++++ libavcodec/movtextdec.c | 30 +++++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/libavcodec/ass.c b/libavcodec/ass.c index a51673fb4e..7c26e3fd6d 100644 --- a/libavcodec/ass.c +++ b/libavcodec/ass.c @@ -26,11 +26,13 @@ #include "libavutil/bprint.h" #include "libavutil/common.h" -int ff_ass_subtitle_header(AVCodecContext *avctx, - const char *font, int font_size, - int color, int back_color, - int bold, int italic, int underline, - int border_style, int alignment) +int ff_ass_subtitle_header_full(AVCodecContext *avctx, + int play_res_x, int play_res_y, + const char *font, int font_size, + int primary_color, int secondary_color, + int outline_color, int back_color, + int bold, int italic, int underline, + int border_style, int alignment) { avctx->subtitle_header = av_asprintf( "[Script Info]\r\n" @@ -67,8 +69,8 @@ int ff_ass_subtitle_header(AVCodecContext *avctx, "[Events]\r\n" "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n", !(avctx->flags & AV_CODEC_FLAG_BITEXACT) ? AV_STRINGIFY(LIBAVCODEC_VERSION) : "", - ASS_DEFAULT_PLAYRESX, ASS_DEFAULT_PLAYRESY, - font, font_size, color, color, back_color, back_color, + play_res_x, play_res_y, font, font_size, + primary_color, secondary_color, outline_color, back_color, -bold, -italic, -underline, border_style, alignment); if (!avctx->subtitle_header) @@ -77,6 +79,20 @@ int ff_ass_subtitle_header(AVCodecContext *avctx, return 0; } +int ff_ass_subtitle_header(AVCodecContext *avctx, + const char *font, int font_size, + int color, int back_color, + int bold, int italic, int underline, + int border_style, int alignment) +{ + return ff_ass_subtitle_header_full(avctx, + ASS_DEFAULT_PLAYRESX, ASS_DEFAULT_PLAYRESY, + font, font_size, color, color, + back_color, back_color, + bold, italic, underline, + border_style, alignment); +} + int ff_ass_subtitle_header_default(AVCodecContext *avctx) { return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT, diff --git a/libavcodec/ass.h b/libavcodec/ass.h index 314b43b686..2c260e4e78 100644 --- a/libavcodec/ass.h +++ b/libavcodec/ass.h @@ -47,6 +47,34 @@ typedef struct FFASSDecoderContext { int readorder; } FFASSDecoderContext; +/** + * Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS. + * Can specify all fields explicitly + * + * @param avctx pointer to the AVCodecContext + * @param play_res_x subtitle frame width + * @param play_res_y subtitle frame height + * @param font name of the default font face to use + * @param font_size default font size to use + * @param primary_color default text color to use (ABGR) + * @param secondary_color default secondary text color to use (ABGR) + * @param outline_color default outline color to use (ABGR) + * @param back_color default background color to use (ABGR) + * @param bold 1 for bold text, 0 for normal text + * @param italic 1 for italic text, 0 for normal text + * @param underline 1 for underline text, 0 for normal text + * @param border_style 1 for outline, 3 for opaque box + * @param alignment position of the text (left, center, top...), defined after + * the layout of the numpad (1-3 sub, 4-6 mid, 7-9 top) + * @return >= 0 on success otherwise an error code <0 + */ +int ff_ass_subtitle_header_full(AVCodecContext *avctx, + int play_res_x, int play_res_y, + const char *font, int font_size, + int primary_color, int secondary_color, + int outline_color, int back_color, + int bold, int italic, int underline, + int border_style, int alignment); /** * Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS. * diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index f3a504b47b..4b4da5e0d9 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -21,6 +21,7 @@ #include "avcodec.h" #include "ass.h" +#include "libavutil/opt.h" #include "libavutil/avstring.h" #include "libavutil/common.h" #include "libavutil/bprint.h" @@ -96,6 +97,7 @@ typedef struct { } TextWrapBox; typedef struct { + AVClass *class; StyleBox **s; StyleBox *s_temp; HighlightBox h; @@ -110,6 +112,8 @@ typedef struct { int size_var; int count_s, count_f; int readorder; + int frame_width; + int frame_height; } MovTextContext; typedef struct { @@ -481,9 +485,17 @@ static int mov_text_init(AVCodecContext *avctx) { MovTextContext *m = avctx->priv_data; ret = mov_text_tx3g(avctx, m); if (ret == 0) { - return ff_ass_subtitle_header(avctx, m->d.font, m->d.fontsize, + if (!m->frame_width || !m->frame_height) { + m->frame_width = ASS_DEFAULT_PLAYRESX; + m->frame_height = ASS_DEFAULT_PLAYRESY; + } + return ff_ass_subtitle_header_full(avctx, + m->frame_width, m->frame_height, + m->d.font, m->d.fontsize, + (255 - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color), (255 - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color), (255 - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color), + (255 - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color), m->d.bold, m->d.italic, m->d.underline, ASS_DEFAULT_BORDERSTYLE, m->d.alignment); } else @@ -601,12 +613,28 @@ static void mov_text_flush(AVCodecContext *avctx) m->readorder = 0; } +#define OFFSET(x) offsetof(MovTextContext, x) +#define FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM +static const AVOption options[] = { + { "width", "Frame width, usually video width", OFFSET(frame_width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS }, + { "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_decoder_class = { + .class_name = "MOV text decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + AVCodec ff_movtext_decoder = { .name = "mov_text", .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"), .type = AVMEDIA_TYPE_SUBTITLE, .id = AV_CODEC_ID_MOV_TEXT, .priv_data_size = sizeof(MovTextContext), + .priv_class = &mov_text_decoder_class, .init = mov_text_init, .decode = mov_text_decode_frame, .close = mov_text_decode_close, From patchwork Mon Apr 6 17:52:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18724 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 C1C80448CAC for ; Mon, 6 Apr 2020 20:56:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A67C568B4AD; Mon, 6 Apr 2020 20:56:40 +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 4A1F568B307 for ; Mon, 6 Apr 2020 20:56:33 +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:56:31 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:06 -0600 Message-ID: <20200406175218.1299994-12-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 11/23] lavc/ass_split: fix parsing utf8 scripts 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" The [Script Info] section was skipped if starts with UTF8 BOM --- libavcodec/ass_split.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libavcodec/ass_split.c b/libavcodec/ass_split.c index 67da7c6d84..94c32667af 100644 --- a/libavcodec/ass_split.c +++ b/libavcodec/ass_split.c @@ -354,6 +354,9 @@ static int ass_split(ASSSplitContext *ctx, const char *buf) if (ctx->current_section >= 0) buf = ass_split_section(ctx, buf); + if(!memcmp(buf, "\xef\xbb\xbf", 3)) { // Skip UTF-8 BOM header + buf += 3; + } while (buf && *buf) { if (sscanf(buf, "[%15[0-9A-Za-z+ ]]%c", section, &c) == 2) { buf += strcspn(buf, "\n"); From patchwork Mon Apr 6 17:52:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18725 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 E5D994493D8 for ; Mon, 6 Apr 2020 20:57:12 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C869E68B4CE; Mon, 6 Apr 2020 20:57:12 +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 4B2A468B4AB for ; Mon, 6 Apr 2020 20:57:11 +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:57:09 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:07 -0600 Message-ID: <20200406175218.1299994-13-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 12/23] lavc/movtextenc: use correct color component order 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 | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index c19ef384bc..8638e303fe 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -39,6 +39,7 @@ #define HLIT_BOX (1<<1) #define HCLR_BOX (1<<2) +#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) typedef struct { @@ -134,13 +135,14 @@ static void encode_hlit(MovTextContext *s, uint32_t tsmb_type) static void encode_hclr(MovTextContext *s, uint32_t tsmb_type) { - uint32_t tsmb_size; + uint32_t tsmb_size, color; if (s->box_flags & HCLR_BOX) { tsmb_size = 12; tsmb_size = AV_RB32(&tsmb_size); + color = AV_RB32(&s->hclr.color); av_bprint_append_any(&s->buffer, &tsmb_size, 4); av_bprint_append_any(&s->buffer, &tsmb_type, 4); - av_bprint_append_any(&s->buffer, &s->hclr.color, 4); + av_bprint_append_any(&s->buffer, &color, 4); } } @@ -289,6 +291,8 @@ static void mov_text_style_cb(void *priv, const char style, int close) static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id) { MovTextContext *s = priv; + + color = BGR_TO_RGB(color) << 8; if (color_id == 2) { //secondary color changes if (s->box_flags & HLIT_BOX) { //close tag s->hlit.end = AV_RB16(&s->text_pos); @@ -296,7 +300,7 @@ static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color s->box_flags |= HCLR_BOX; s->box_flags |= HLIT_BOX; s->hlit.start = AV_RB16(&s->text_pos); - s->hclr.color = color | (0xFF << 24); //set alpha value to FF + s->hclr.color = color | 0xFF; //set alpha value to FF } } /* If there are more than one secondary color changes in ASS, take start of From patchwork Mon Apr 6 17:52:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18726 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 EADA94493D8 for ; Mon, 6 Apr 2020 20:57:15 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D64AD68B4F3; Mon, 6 Apr 2020 20:57:15 +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 EB78668B4C0 for ; Mon, 6 Apr 2020 20:57:13 +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:57:12 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:08 -0600 Message-ID: <20200406175218.1299994-14-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 13/23] lavc/movtextenc: keep values in native byte order till written 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 | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index 8638e303fe..5e5b786f44 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -68,7 +68,6 @@ typedef struct { HilightcolorBox hclr; int count; uint8_t box_flags; - uint16_t style_entries; uint16_t style_fontID; uint8_t style_fontsize; uint32_t style_color; @@ -96,10 +95,11 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type) { int j; uint32_t tsmb_size; + uint16_t style_entries; if (s->box_flags & STYL_BOX) { tsmb_size = s->count * STYLE_RECORD_SIZE + SIZE_ADD; tsmb_size = AV_RB32(&tsmb_size); - s->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); @@ -107,10 +107,14 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type) 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, &s->style_entries, 2); + av_bprint_append_any(&s->buffer, &style_entries, 2); for (j = 0; j < s->count; j++) { - av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_start, 2); - av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_end, 2); + uint16_t style_start, style_end; + + style_start = AV_RB16(&s->style_attributes[j]->style_start); + style_end = AV_RB16(&s->style_attributes[j]->style_end); + 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, &s->style_attributes[j]->style_flag, 1); av_bprint_append_any(&s->buffer, &s->style_fontsize, 1); @@ -123,13 +127,16 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type) static void encode_hlit(MovTextContext *s, uint32_t tsmb_type) { uint32_t tsmb_size; + uint16_t start, end; if (s->box_flags & HLIT_BOX) { tsmb_size = 12; tsmb_size = AV_RB32(&tsmb_size); + start = AV_RB16(&s->hlit.start); + end = AV_RB16(&s->hlit.end); av_bprint_append_any(&s->buffer, &tsmb_size, 4); av_bprint_append_any(&s->buffer, &tsmb_type, 4); - av_bprint_append_any(&s->buffer, &s->hlit.start, 2); - av_bprint_append_any(&s->buffer, &s->hlit.end, 2); + av_bprint_append_any(&s->buffer, &start, 2); + av_bprint_append_any(&s->buffer, &end, 2); } } @@ -222,10 +229,10 @@ static void mov_text_style_cb(void *priv, const char style, int close) } s->style_attributes_temp->style_flag = 0; - s->style_attributes_temp->style_start = AV_RB16(&s->text_pos); + s->style_attributes_temp->style_start = s->text_pos; } else { if (s->style_attributes_temp->style_flag) { //break the style record here and start a new one - s->style_attributes_temp->style_end = AV_RB16(&s->text_pos); + s->style_attributes_temp->style_end = s->text_pos; av_dynarray_add(&s->style_attributes, &s->count, s->style_attributes_temp); s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp)); if (!s->style_attributes_temp) { @@ -236,10 +243,10 @@ static void mov_text_style_cb(void *priv, const char style, int close) } s->style_attributes_temp->style_flag = s->style_attributes[s->count - 1]->style_flag; - s->style_attributes_temp->style_start = AV_RB16(&s->text_pos); + s->style_attributes_temp->style_start = s->text_pos; } else { s->style_attributes_temp->style_flag = 0; - s->style_attributes_temp->style_start = AV_RB16(&s->text_pos); + s->style_attributes_temp->style_start = s->text_pos; } } switch (style){ @@ -257,7 +264,7 @@ static void mov_text_style_cb(void *priv, const char style, int close) av_log(s->avctx, AV_LOG_WARNING, "Ignoring unmatched close tag\n"); return; } else { - s->style_attributes_temp->style_end = AV_RB16(&s->text_pos); + s->style_attributes_temp->style_end = s->text_pos; av_dynarray_add(&s->style_attributes, &s->count, s->style_attributes_temp); s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp)); @@ -282,7 +289,7 @@ static void mov_text_style_cb(void *priv, const char style, int close) break; } if (s->style_attributes_temp->style_flag) { //start of new style record - s->style_attributes_temp->style_start = AV_RB16(&s->text_pos); + s->style_attributes_temp->style_start = s->text_pos; } } s->box_flags |= STYL_BOX; @@ -295,11 +302,11 @@ static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color color = BGR_TO_RGB(color) << 8; if (color_id == 2) { //secondary color changes if (s->box_flags & HLIT_BOX) { //close tag - s->hlit.end = AV_RB16(&s->text_pos); + s->hlit.end = s->text_pos; } else { s->box_flags |= HCLR_BOX; s->box_flags |= HLIT_BOX; - s->hlit.start = AV_RB16(&s->text_pos); + s->hlit.start = s->text_pos; s->hclr.color = color | 0xFF; //set alpha value to FF } } @@ -366,7 +373,6 @@ static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, s->text_pos = 0; s->count = 0; s->box_flags = 0; - s->style_entries = 0; for (i = 0; i < sub->num_rects; i++) { const char *ass = sub->rects[i]->ass; From patchwork Mon Apr 6 17:52:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18727 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 DD8724493D8 for ; Mon, 6 Apr 2020 20:57:52 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C433A68B4C0; Mon, 6 Apr 2020 20:57:52 +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 3D114689C2E for ; Mon, 6 Apr 2020 20:57:46 +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:57:44 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:09 -0600 Message-ID: <20200406175218.1299994-15-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 14/23] lavc/movtextenc: simplify style record updates 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" Makes style update code easier to extend for style types not yet handled --- libavcodec/movtextenc.c | 131 +++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 69 deletions(-) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index 5e5b786f44..05532cd544 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -89,6 +89,10 @@ static void mov_text_cleanup(MovTextContext *s) } av_freep(&s->style_attributes); } + if (s->style_attributes_temp) { + s->style_attributes_temp->style_flag = 0; + s->style_attributes_temp->style_start = 0; + } } static void encode_styl(MovTextContext *s, uint32_t tsmb_type) @@ -96,7 +100,7 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type) int j; uint32_t tsmb_size; uint16_t style_entries; - if (s->box_flags & STYL_BOX) { + if ((s->box_flags & STYL_BOX) && s->count) { tsmb_size = s->count * STYLE_RECORD_SIZE + SIZE_ADD; tsmb_size = AV_RB32(&tsmb_size); style_entries = AV_RB16(&s->count); @@ -120,8 +124,8 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type) av_bprint_append_any(&s->buffer, &s->style_fontsize, 1); av_bprint_append_any(&s->buffer, &s->style_color, 4); } - mov_text_cleanup(s); } + mov_text_cleanup(s); } static void encode_hlit(MovTextContext *s, uint32_t tsmb_type) @@ -201,6 +205,11 @@ static av_cold int mov_text_encode_init(AVCodecContext *avctx) MovTextContext *s = avctx->priv_data; s->avctx = avctx; + s->style_attributes_temp = av_mallocz(sizeof(*s->style_attributes_temp)); + if (!s->style_attributes_temp) { + return AVERROR(ENOMEM); + } + avctx->extradata_size = sizeof text_sample_entry; avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); if (!avctx->extradata) @@ -214,85 +223,69 @@ static av_cold int mov_text_encode_init(AVCodecContext *avctx) return s->ass_ctx ? 0 : AVERROR_INVALIDDATA; } -static void mov_text_style_cb(void *priv, const char style, int close) +// Start a new style box if needed +static int mov_text_style_start(MovTextContext *s) { - MovTextContext *s = priv; - if (!close) { - if (!(s->box_flags & STYL_BOX)) { //first style entry - - s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp)); - - if (!s->style_attributes_temp) { - av_bprint_clear(&s->buffer); - s->box_flags &= ~STYL_BOX; - return; - } - - s->style_attributes_temp->style_flag = 0; - s->style_attributes_temp->style_start = s->text_pos; - } else { - if (s->style_attributes_temp->style_flag) { //break the style record here and start a new one - s->style_attributes_temp->style_end = s->text_pos; - av_dynarray_add(&s->style_attributes, &s->count, s->style_attributes_temp); - s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp)); - if (!s->style_attributes_temp) { - mov_text_cleanup(s); - av_bprint_clear(&s->buffer); - s->box_flags &= ~STYL_BOX; - return; - } - - s->style_attributes_temp->style_flag = s->style_attributes[s->count - 1]->style_flag; - s->style_attributes_temp->style_start = s->text_pos; - } else { - s->style_attributes_temp->style_flag = 0; - s->style_attributes_temp->style_start = s->text_pos; - } - } - switch (style){ - case 'b': - s->style_attributes_temp->style_flag |= STYLE_FLAG_BOLD; - break; - case 'i': - s->style_attributes_temp->style_flag |= STYLE_FLAG_ITALIC; - break; - case 'u': - s->style_attributes_temp->style_flag |= STYLE_FLAG_UNDERLINE; - break; - } - } else if (!s->style_attributes_temp) { - av_log(s->avctx, AV_LOG_WARNING, "Ignoring unmatched close tag\n"); - return; - } else { + // there's an existing style entry + if (s->style_attributes_temp->style_start == s->text_pos) + // Still at same text pos, use same entry + return 1; + if (s->style_attributes_temp->style_flag) { + // last style != defaults, end the style entry and start a new one + s->box_flags |= STYL_BOX; s->style_attributes_temp->style_end = s->text_pos; av_dynarray_add(&s->style_attributes, &s->count, s->style_attributes_temp); - s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp)); - if (!s->style_attributes_temp) { mov_text_cleanup(s); av_bprint_clear(&s->buffer); s->box_flags &= ~STYL_BOX; - return; + return 0; } s->style_attributes_temp->style_flag = s->style_attributes[s->count - 1]->style_flag; - switch (style){ - case 'b': - s->style_attributes_temp->style_flag &= ~STYLE_FLAG_BOLD; - break; - case 'i': - s->style_attributes_temp->style_flag &= ~STYLE_FLAG_ITALIC; - break; - case 'u': - s->style_attributes_temp->style_flag &= ~STYLE_FLAG_UNDERLINE; - break; - } - if (s->style_attributes_temp->style_flag) { //start of new style record - s->style_attributes_temp->style_start = s->text_pos; - } + s->style_attributes_temp->style_start = s->text_pos; + } else { // style entry matches defaults, drop entry + s->style_attributes_temp->style_flag = 0; + s->style_attributes_temp->style_start = s->text_pos; + } + 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; +} + +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); + + if (!s->style_attributes_temp || + !!(s->style_attributes_temp->style_flag & style_flag) != close) { + // setting flag that is already set + return; + } + if (mov_text_style_start(s)) { + if (!close) + s->style_attributes_temp->style_flag |= style_flag; + else + s->style_attributes_temp->style_flag &= ~style_flag; } - s->box_flags |= STYL_BOX; } static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id) From patchwork Mon Apr 6 17:52:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18728 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 A3EFF4493D8 for ; Mon, 6 Apr 2020 20:58:00 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 84FEF68B4C7; Mon, 6 Apr 2020 20:58:00 +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 E152A68B303 for ; Mon, 6 Apr 2020 20:57:53 +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:57:52 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:10 -0600 Message-ID: <20200406175218.1299994-16-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 15/23] lavc/movtextenc: fix unclosed style records 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" The last record at the end of each dialog was never closed --- libavcodec/movtextenc.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index 05532cd544..d389111419 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -309,6 +309,12 @@ static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color */ } +static void mov_text_end_cb(void *priv) +{ + // End of text, close any open style record + mov_text_style_start((MovTextContext*)priv); +} + static uint16_t utf8_strlen(const char *text, int len) { uint16_t i = 0, ret = 0; @@ -352,6 +358,7 @@ static const ASSCodesCallbacks mov_text_callbacks = { .new_line = mov_text_new_line_cb, .style = mov_text_style_cb, .color = mov_text_color_cb, + .end = mov_text_end_cb, }; static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, 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 From patchwork Mon Apr 6 17:52:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18730 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 921864495FB for ; Mon, 6 Apr 2020 20:58:46 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 739B768B50E; Mon, 6 Apr 2020 20:58:46 +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 4338D68B4E6 for ; Mon, 6 Apr 2020 20:58:39 +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:37 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:12 -0600 Message-ID: <20200406175218.1299994-18-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 17/23] lavc/movtextenc: add color tag handling 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 | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index 4e7d55efcb..090536b887 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -121,7 +121,7 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type) 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_color = AV_RB32(&s->style_attributes[j]->style_color); style_fontID = AV_RB16(&s->d.style_fontID); av_bprint_append_any(&s->buffer, &style_start, 2); @@ -244,7 +244,8 @@ static int mov_text_style_start(MovTextContext *s) if (s->style_attributes_temp->style_start == s->text_pos) // Still at same text pos, use same entry return 1; - if (s->style_attributes_temp->style_flag) { + if (s->style_attributes_temp->style_flag != s->d.style_flag || + s->style_attributes_temp->style_color != s->d.style_color) { // last style != defaults, end the style entry and start a new one s->box_flags |= STYL_BOX; s->style_attributes_temp->style_end = s->text_pos; @@ -258,9 +259,11 @@ static int mov_text_style_start(MovTextContext *s) } s->style_attributes_temp->style_flag = s->style_attributes[s->count - 1]->style_flag; + s->style_attributes_temp->style_color = s->style_attributes[s->count - 1]->style_color; s->style_attributes_temp->style_start = s->text_pos; } else { // style entry matches defaults, drop entry - s->style_attributes_temp->style_flag = 0; + 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_start = s->text_pos; } return 1; @@ -313,12 +316,26 @@ static void mov_text_style_cb(void *priv, const char style, int close) } } +static void mov_text_color_set(MovTextContext *s, uint32_t color) +{ + if (!s->style_attributes_temp || + (s->style_attributes_temp->style_color & 0xffffff00) == color) { + // color hasn't changed + return; + } + if (mov_text_style_start(s)) + s->style_attributes_temp->style_color = (color & 0xffffff00) | + (s->style_attributes_temp->style_color & 0xff); +} + static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id) { MovTextContext *s = priv; color = BGR_TO_RGB(color) << 8; - if (color_id == 2) { //secondary color changes + if (color_id == 1) { //primary color changes + mov_text_color_set(s, color); + } else if (color_id == 2) { //secondary color changes if (s->box_flags & HLIT_BOX) { //close tag s->hlit.end = s->text_pos; } else { @@ -344,12 +361,15 @@ static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog) { ASSStyle * style = ff_ass_style_get(s->ass_ctx, dialog->style); uint8_t style_flags; + uint32_t color; 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); + color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8; + mov_text_color_set(s, color); } } From patchwork Mon Apr 6 17:52:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18731 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 C5852449AC5 for ; Mon, 6 Apr 2020 20:59:22 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AF3BC68B4F5; Mon, 6 Apr 2020 20:59:22 +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 9344568A466 for ; Mon, 6 Apr 2020 20:59:16 +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:59:15 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:13 -0600 Message-ID: <20200406175218.1299994-19-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 18/23] lavc/movtextenc: add alpha tag handling 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 | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index 090536b887..e82393dde7 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -351,6 +351,26 @@ static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color */ } +static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha) +{ + if (!s->style_attributes_temp || + (s->style_attributes_temp->style_color & 0xff) == alpha) { + // color hasn't changed + return; + } + if (mov_text_style_start(s)) + s->style_attributes_temp->style_color = + (s->style_attributes_temp->style_color & 0xffffff00) | alpha; +} + +static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id) +{ + MovTextContext *s = priv; + + if (alpha_id == 1) // primary alpha changes + mov_text_alpha_set(s, 255 - alpha); +} + static void mov_text_end_cb(void *priv) { // End of text, close any open style record @@ -360,7 +380,7 @@ static void mov_text_end_cb(void *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; + uint8_t style_flags, alpha; uint32_t color; if (style) { @@ -370,6 +390,8 @@ static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog) mov_text_style_set(s, style_flags); color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8; mov_text_color_set(s, color); + alpha = 255 - ((uint32_t)style->primary_color >> 24); + mov_text_alpha_set(s, alpha); } } @@ -416,6 +438,7 @@ static const ASSCodesCallbacks mov_text_callbacks = { .new_line = mov_text_new_line_cb, .style = mov_text_style_cb, .color = mov_text_color_cb, + .alpha = mov_text_alpha_cb, .end = mov_text_end_cb, }; From patchwork Mon Apr 6 17:52:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18732 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 D44E0449AC5 for ; Mon, 6 Apr 2020 20:59:30 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B6B2168B50E; Mon, 6 Apr 2020 20:59:30 +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 20B8968B503 for ; Mon, 6 Apr 2020 20:59:24 +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:59:22 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:14 -0600 Message-ID: <20200406175218.1299994-20-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 19/23] lavc/movtextenc: add font size tag handling 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 | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index e82393dde7..9e657c9635 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -128,7 +128,7 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type) av_bprint_append_any(&s->buffer, &style_end, 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->d.style_fontsize, 1); + av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_fontsize, 1); av_bprint_append_any(&s->buffer, &style_color, 4); } } @@ -244,8 +244,9 @@ static int mov_text_style_start(MovTextContext *s) if (s->style_attributes_temp->style_start == s->text_pos) // Still at same text pos, use same entry return 1; - if (s->style_attributes_temp->style_flag != s->d.style_flag || - s->style_attributes_temp->style_color != s->d.style_color) { + 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_fontsize != s->d.style_fontsize) { // last style != defaults, end the style entry and start a new one s->box_flags |= STYL_BOX; s->style_attributes_temp->style_end = s->text_pos; @@ -260,10 +261,12 @@ static int mov_text_style_start(MovTextContext *s) s->style_attributes_temp->style_flag = s->style_attributes[s->count - 1]->style_flag; s->style_attributes_temp->style_color = s->style_attributes[s->count - 1]->style_color; + s->style_attributes_temp->style_fontsize = s->style_attributes[s->count - 1]->style_fontsize; s->style_attributes_temp->style_start = s->text_pos; } else { // style entry matches defaults, drop entry 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_fontsize = s->d.style_fontsize; s->style_attributes_temp->style_start = s->text_pos; } return 1; @@ -371,6 +374,22 @@ static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id) mov_text_alpha_set(s, 255 - alpha); } +static void mov_text_font_size_set(MovTextContext *s, int size) +{ + if (!s->style_attributes_temp || + s->style_attributes_temp->style_fontsize == size) { + // color hasn't changed + return; + } + if (mov_text_style_start(s)) + s->style_attributes_temp->style_fontsize = size; +} + +static void mov_text_font_size_cb(void *priv, int size) +{ + mov_text_font_size_set((MovTextContext*)priv, size); +} + static void mov_text_end_cb(void *priv) { // End of text, close any open style record @@ -392,6 +411,7 @@ static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog) mov_text_color_set(s, color); alpha = 255 - ((uint32_t)style->primary_color >> 24); mov_text_alpha_set(s, alpha); + mov_text_font_size_set(s, style->font_size); } } @@ -434,12 +454,13 @@ static void mov_text_new_line_cb(void *priv, int forced) } static const ASSCodesCallbacks mov_text_callbacks = { - .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, - .end = mov_text_end_cb, + .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, + .font_size = mov_text_font_size_cb, + .end = mov_text_end_cb, }; static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, From patchwork Mon Apr 6 17:52:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18733 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 0AF3544A7AA for ; Mon, 6 Apr 2020 21:00:09 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E313F68B503; Mon, 6 Apr 2020 21:00:08 +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 7FBF468B4D7 for ; Mon, 6 Apr 2020 21:00:02 +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 11:00:01 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:15 -0600 Message-ID: <20200406175218.1299994-21-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 20/23] lavc/movtextenc: handle cancel overrides callback 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 | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index 9e657c9635..2e65489c4d 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -396,9 +396,8 @@ static void mov_text_end_cb(void *priv) mov_text_style_start((MovTextContext*)priv); } -static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog) +static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style) { - ASSStyle * style = ff_ass_style_get(s->ass_ctx, dialog->style); uint8_t style_flags, alpha; uint32_t color; @@ -412,9 +411,27 @@ static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog) alpha = 255 - ((uint32_t)style->primary_color >> 24); mov_text_alpha_set(s, alpha); mov_text_font_size_set(s, style->font_size); + } else { + // End current style record, go back to defaults + mov_text_style_start(s); } } +static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog) +{ + ASSStyle * style = ff_ass_style_get(s->ass_ctx, dialog->style); + + mov_text_ass_style_set(s, style); +} + +static void mov_text_cancel_overrides_cb(void *priv, const char * style_name) +{ + MovTextContext *s = priv; + ASSStyle * style = ff_ass_style_get(s->ass_ctx, style_name); + + mov_text_ass_style_set(s, style); +} + static uint16_t utf8_strlen(const char *text, int len) { uint16_t i = 0, ret = 0; @@ -454,13 +471,14 @@ static void mov_text_new_line_cb(void *priv, int forced) } static const ASSCodesCallbacks mov_text_callbacks = { - .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, - .font_size = mov_text_font_size_cb, - .end = mov_text_end_cb, + .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, + .font_size = mov_text_font_size_cb, + .cancel_overrides = mov_text_cancel_overrides_cb, + .end = mov_text_end_cb, }; static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, From patchwork Mon Apr 6 17:52:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18734 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 1BBB644A7AA for ; Mon, 6 Apr 2020 21:00:16 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 03EE268B516; Mon, 6 Apr 2020 21:00:16 +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 1519468B4E6 for ; Mon, 6 Apr 2020 21:00:10 +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 11:00:08 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:16 -0600 Message-ID: <20200406175218.1299994-22-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 21/23] lavc/movtextenc: simplify initialization of new style record 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 | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index 2e65489c4d..167dffee6a 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -96,8 +96,7 @@ static void mov_text_cleanup(MovTextContext *s) av_freep(&s->style_attributes); } if (s->style_attributes_temp) { - s->style_attributes_temp->style_flag = 0; - s->style_attributes_temp->style_start = 0; + *s->style_attributes_temp = s->d; } } @@ -122,7 +121,7 @@ static void encode_styl(MovTextContext *s, uint32_t tsmb_type) 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->style_attributes[j]->style_color); - style_fontID = AV_RB16(&s->d.style_fontID); + style_fontID = AV_RB16(&s->style_attributes[j]->style_fontID); av_bprint_append_any(&s->buffer, &style_start, 2); av_bprint_append_any(&s->buffer, &style_end, 2); @@ -259,14 +258,10 @@ static int mov_text_style_start(MovTextContext *s) return 0; } - s->style_attributes_temp->style_flag = s->style_attributes[s->count - 1]->style_flag; - s->style_attributes_temp->style_color = s->style_attributes[s->count - 1]->style_color; - s->style_attributes_temp->style_fontsize = s->style_attributes[s->count - 1]->style_fontsize; + *s->style_attributes_temp = s->d; s->style_attributes_temp->style_start = s->text_pos; } else { // style entry matches defaults, drop entry - 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_fontsize = s->d.style_fontsize; + *s->style_attributes_temp = s->d; s->style_attributes_temp->style_start = s->text_pos; } return 1; From patchwork Mon Apr 6 17:52:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stebbins X-Patchwork-Id: 18735 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 23E8D44A7AA for ; Mon, 6 Apr 2020 21:00:56 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0AD7768B510; Mon, 6 Apr 2020 21:00:56 +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 1CEEA68B4E8 for ; Mon, 6 Apr 2020 21:00:48 +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 11:00:46 -0700 From: John Stebbins To: Date: Mon, 6 Apr 2020 11:52:17 -0600 Message-ID: <20200406175218.1299994-23-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 22/23] lavc/movtextenc: add font name handling 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" Initializes the mov text sample description from the ASS header and creates an mov font table from the fonts available in the ASS Styles. --- libavcodec/ass_split.c | 5 + libavcodec/ass_split.h | 8 ++ libavcodec/movtextenc.c | 253 ++++++++++++++++++++++++++++++++-------- 3 files changed, 216 insertions(+), 50 deletions(-) diff --git a/libavcodec/ass_split.c b/libavcodec/ass_split.c index 94c32667af..9d5a66f931 100644 --- a/libavcodec/ass_split.c +++ b/libavcodec/ass_split.c @@ -599,3 +599,8 @@ ASSStyle *ff_ass_style_get(ASSSplitContext *ctx, const char *style) return ass->styles + i; return NULL; } + +ASS *ff_ass_get(ASSSplitContext *ctx) +{ + return &ctx->ass; +} diff --git a/libavcodec/ass_split.h b/libavcodec/ass_split.h index 30ce77250c..31b8e53242 100644 --- a/libavcodec/ass_split.h +++ b/libavcodec/ass_split.h @@ -204,4 +204,12 @@ int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv, */ ASSStyle *ff_ass_style_get(ASSSplitContext *ctx, const char *style); +/** + * Get ASS structure + * + * @param ctx Context previously initialized by ff_ass_split(). + * @return the ASS + */ +ASS *ff_ass_get(ASSSplitContext *ctx); + #endif /* AVCODEC_ASS_SPLIT_H */ diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c index 167dffee6a..a62bdb7eb0 100644 --- a/libavcodec/movtextenc.c +++ b/libavcodec/movtextenc.c @@ -79,6 +79,8 @@ typedef struct { StyleBox d; uint16_t text_pos; uint16_t byte_count; + char ** fonts; + int font_count; } MovTextContext; typedef struct { @@ -171,69 +173,198 @@ static const Box box_types[] = { const static size_t box_count = FF_ARRAY_ELEMS(box_types); -static av_cold int mov_text_encode_init(AVCodecContext *avctx) +static int mov_text_encode_close(AVCodecContext *avctx) { - /* - * For now, we'll use a fixed default style. When we add styling - * support, this will be generated from the ASS style. - */ - static const uint8_t text_sample_entry[] = { + MovTextContext *s = avctx->priv_data; + int i; + + ff_ass_split_free(s->ass_ctx); + if (s->style_attributes) { + for (i = 0; i < s->count; i++) { + av_freep(&s->style_attributes[i]); + } + av_freep(&s->style_attributes); + } + av_freep(&s->fonts); + av_freep(&s->style_attributes_temp); + av_bprint_finalize(&s->buffer, NULL); + return 0; +} + +static int encode_sample_description(AVCodecContext *avctx) +{ + ASS * ass; + ASSStyle * style; + int i, j; + uint32_t tsmb_size, tsmb_type, back_color, style_color; + uint16_t style_start, style_end, fontID, count; + int font_names_total_len = 0; + MovTextContext *s = avctx->priv_data; + + static const uint8_t display_and_justification[] = { 0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags 0x01, // int8_t horizontal-justification 0xFF, // int8_t vertical-justification - 0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4] - // BoxRecord { + }; + // 0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4] + static const uint8_t box_record[] = { + // BoxRecord { 0x00, 0x00, // int16_t top 0x00, 0x00, // int16_t left 0x00, 0x00, // int16_t bottom 0x00, 0x00, // int16_t right - // }; - // 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] - // }; - // }; + // }; }; + // 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 = ff_ass_get(s->ass_ctx); + 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) { + s->d.style_fontsize = style->font_size; + 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)); + } - MovTextContext *s = avctx->priv_data; - s->avctx = avctx; + av_bprint_append_any(&s->buffer, display_and_justification, + sizeof(display_and_justification)); + back_color = AV_RB32(&back_color); + av_bprint_append_any(&s->buffer, &back_color, 4); + // BoxRecord { + av_bprint_append_any(&s->buffer, box_record, sizeof(box_record)); + // }; + // StyleRecord { + style_start = AV_RB16(&s->d.style_start); + style_end = AV_RB16(&s->d.style_end); + fontID = AV_RB16(&s->d.style_fontID); + style_color = AV_RB32(&s->d.style_color); + av_bprint_append_any(&s->buffer, &style_start, 2); + av_bprint_append_any(&s->buffer, &style_end, 2); + av_bprint_append_any(&s->buffer, &fontID, 2); + av_bprint_append_any(&s->buffer, &s->d.style_flag, 1); + av_bprint_append_any(&s->buffer, &s->d.style_fontsize, 1); + av_bprint_append_any(&s->buffer, &style_color, 4); + // }; + + // 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 + av_dynarray_add(&s->fonts, &s->font_count, style->font_name); + font_names_total_len += strlen(style->font_name); + for (i = 0; i < ass->styles_count; i++) { + int found = 0; + 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 { + tsmb_size = SIZE_ADD + 3 * s->font_count + font_names_total_len; + tsmb_size = AV_RB32(&tsmb_size); + tsmb_type = MKTAG('f','t','a','b'); + count = AV_RB16(&s->font_count); + av_bprint_append_any(&s->buffer, &tsmb_size, 4); + av_bprint_append_any(&s->buffer, &tsmb_type, 4); + av_bprint_append_any(&s->buffer, &count, 2); + // FontRecord { + for (i = 0; i < s->font_count; i++) { + int len; + fontID = i + 1; + fontID = AV_RB16(&fontID); + av_bprint_append_any(&s->buffer, &fontID, 2); + len = strlen(s->fonts[i]); + av_bprint_append_any(&s->buffer, &len, 1); + av_bprint_append_any(&s->buffer, s->fonts[i], len); + } + // }; + // }; - s->style_attributes_temp = av_mallocz(sizeof(*s->style_attributes_temp)); - if (!s->style_attributes_temp) { + if (!av_bprint_is_complete(&s->buffer)) { return AVERROR(ENOMEM); } - avctx->extradata_size = sizeof text_sample_entry; + avctx->extradata_size = s->buffer.len; avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); - if (!avctx->extradata) + if (!avctx->extradata) { return AVERROR(ENOMEM); + } + + memcpy(avctx->extradata, s->buffer.str, avctx->extradata_size); + av_bprint_clear(&s->buffer); + + return 0; +} + +static av_cold int mov_text_encode_init(AVCodecContext *avctx) +{ + int ret; + MovTextContext *s = avctx->priv_data; + s->avctx = avctx; av_bprint_init(&s->buffer, 0, AV_BPRINT_SIZE_UNLIMITED); - memcpy(avctx->extradata, text_sample_entry, avctx->extradata_size); + s->style_attributes_temp = av_mallocz(sizeof(*s->style_attributes_temp)); + if (!s->style_attributes_temp) { + ret = AVERROR(ENOMEM); + goto fail; + } s->ass_ctx = ff_ass_split(avctx->subtitle_header); + if (!s->ass_ctx) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + ret = encode_sample_description(avctx); + if (ret < 0) + goto fail; - // 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 0; - return s->ass_ctx ? 0 : AVERROR_INVALIDDATA; +fail: + mov_text_encode_close(avctx); + return ret; } // Start a new style box if needed @@ -243,8 +374,9 @@ static int mov_text_style_start(MovTextContext *s) if (s->style_attributes_temp->style_start == s->text_pos) // Still at same text pos, use same entry return 1; - if (s->style_attributes_temp->style_flag != s->d.style_flag || - s->style_attributes_temp->style_color != s->d.style_color || + 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) { // last style != defaults, end the style entry and start a new one s->box_flags |= STYL_BOX; @@ -369,6 +501,33 @@ static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id) mov_text_alpha_set(s, 255 - alpha); } +static uint16_t find_font_id(MovTextContext * s, const char * name) +{ + int i; + for (i = 0; i < s->font_count; i++) { + 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); + if (!s->style_attributes_temp || + s->style_attributes_temp->style_fontID == fontID) { + // color hasn't changed + return; + } + if (mov_text_style_start(s)) + s->style_attributes_temp->style_fontID = fontID; +} + +static void mov_text_font_name_cb(void *priv, const char *name) +{ + mov_text_font_name_set((MovTextContext*)priv, name); +} + static void mov_text_font_size_set(MovTextContext *s, int size) { if (!s->style_attributes_temp || @@ -406,6 +565,7 @@ static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style) alpha = 255 - ((uint32_t)style->primary_color >> 24); mov_text_alpha_set(s, alpha); mov_text_font_size_set(s, style->font_size); + mov_text_font_name_set(s, style->font_name); } else { // End current style record, go back to defaults mov_text_style_start(s); @@ -471,6 +631,7 @@ static const ASSCodesCallbacks mov_text_callbacks = { .style = mov_text_style_cb, .color = mov_text_color_cb, .alpha = mov_text_alpha_cb, + .font_name = mov_text_font_name_cb, .font_size = mov_text_font_size_cb, .cancel_overrides = mov_text_cancel_overrides_cb, .end = mov_text_end_cb, @@ -548,14 +709,6 @@ exit: return length; } -static int mov_text_encode_close(AVCodecContext *avctx) -{ - MovTextContext *s = avctx->priv_data; - ff_ass_split_free(s->ass_ctx); - av_bprint_finalize(&s->buffer, NULL); - return 0; -} - AVCodec ff_movtext_encoder = { .name = "mov_text", .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),