From patchwork Sat Oct 17 18:22:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23039 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 6C9F444B29E for ; Sat, 17 Oct 2020 21:23:03 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4F5C968B9AB; Sat, 17 Oct 2020 21:23:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1ED1468B63A for ; Sat, 17 Oct 2020 21:22:57 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id z22so3233711wmi.0 for ; Sat, 17 Oct 2020 11:22:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Y2VLxe2UP7StkgF0px4I2bA6RdyALevkd9roCrwUuYk=; b=eXidy3E3dRWh5msbCQeLX1twHcZSaQIqVaLtRVyZPct8cZicrCflBS2aIObfkP+X4q cho5T6rroV7NYxbnMw66fZJ656p7xoSN2Tr5ila0mZJiyan3pc1yQAHX7cKRJ/5nht7F yK1WlsQ9p0PB/vWo9yYK2+dXahg/1vDIt2hlBqfPjBPyfzCPWkRmwM9l5g03sFQF9hNs SE5Yh+Cw5MlM0kXcvsK82rYsAvBr8VtqnlmEVSD3pVB7VbyjTjkFMSMci1OVqT+Oj7L2 mCAW2BtA889Jan6QfplyZJiKLCdO/n77wlKirVqHBVBuPmqPARnCpyDi31dmJlgql3Ek R1PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Y2VLxe2UP7StkgF0px4I2bA6RdyALevkd9roCrwUuYk=; b=WsIHF3VPsn1wx8ehpuIpkKSuLaBxqBQE+hPiCR3FHDMptInSnTNHfBIKD1Yv3V6EDw 4VRZBZV7YnhvMMrFk+GXY43qhzjWV4I/0OGRznQE3G+XyUjio2DpgNi6KscQZbG50Bk2 w1nLmV359dLXfTfaI5D5uWJQhDjNxTpvOjfnd9LwU7+OeU/y2p+Tg43Jp9pPT+i5UBxz vL76WOTBG4PmRv/Ad3XgrjgqoIw4hhXw+5nycqKOylM/AQrQuwY4ZSZQIT2cgTIHapFj iRcvPKldY5Y2739dScjOAUGgF8/BxTHm5qAu2QzCd2ZIkAcfuUmIVXihgd7oAgQimWmH 1BCA== X-Gm-Message-State: AOAM532cvrFUpJlCS1qSNEve33O47lwOlrMDfCh/I9ATPfsFtIqNQc8F z1EkwpvW9xslkMhgxWI77EFH620QJhE= X-Google-Smtp-Source: ABdhPJxETo+XpSCS9kqLJdlsYUrf+wVvk6JT0mSh3aFMsUrFpt7Fxi6I2Ic+kf6MYLHAQeCS7VvOow== X-Received: by 2002:a1c:3:: with SMTP id 3mr9340014wma.14.1602958976250; Sat, 17 Oct 2020 11:22:56 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.22.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:22:55 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:38 +0200 Message-Id: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/11] avcodec/movtextdec: Reset counter of fonts when freeing them 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" If allocating fonts fails when reading the header, all fonts are freed, yet the counter of fonts is not reset and no error is returned; when subtitles are decoded lateron, the inexistent list of fonts is searched for the matching font for this particular entry which of course leads to a segfault. Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 4a21dbf36d..068bdb7802 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -148,6 +148,7 @@ static void mov_text_cleanup_ftab(MovTextContext *m) } } av_freep(&m->ftab); + m->ftab_entries = 0; } static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) @@ -230,7 +231,6 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) box_size += 3; if (avctx->extradata_size < box_size) { mov_text_cleanup_ftab(m); - m->ftab_entries = 0; return -1; } m->ftab_temp = av_mallocz(sizeof(*m->ftab_temp)); @@ -245,7 +245,6 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) box_size = box_size + font_length; if (avctx->extradata_size < box_size) { mov_text_cleanup_ftab(m); - m->ftab_entries = 0; return -1; } m->ftab_temp->font = av_malloc(font_length + 1); From patchwork Sat Oct 17 18:22:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23040 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 3CD0844B29E for ; Sat, 17 Oct 2020 21:23:21 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2B28268B9EB; Sat, 17 Oct 2020 21:23:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 33D7768B981 for ; Sat, 17 Oct 2020 21:23:15 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id h5so7000421wrv.7 for ; Sat, 17 Oct 2020 11:23:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S4KpIWzMqOzmGlqzXYffBEll+/Fi0VA9jvD9CTIF6JU=; b=siAWQUZcICjkqd+hHJFKt36M3DSfmLxS5d5v5WIPeIr36m03Ttt1naVE/HwtqPIxKS 8J0OTAIo3sJbMoojsIhPFfZ9G6pdv8TKCLwr5C7UjN4AvMkrVwfiOIXFcO5TQrGi2AXT P65m34bOoarcoe/3mOFRoWxX3tOXUbaHro9cY4LnNjp7Joofj+ahw/s4jnb5h+uzWdXl nzDsAWH0MkhyYCZtFsIzaS41rHVqiftZzSg2xnIzUjOSBr4fmZ+HhXftIIQ4IhuH/6sN +UMoJ6vYglLCbIhCA2VV5CwXzzqMMF68MqnKjHs+3aK5fUooxVrSPCUj11XL+S5ffKp4 a2mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S4KpIWzMqOzmGlqzXYffBEll+/Fi0VA9jvD9CTIF6JU=; b=gtNHqaUjuGmjTQyQNb8BVrprDu9BLJOPWnGRyOis6Y88xu0xXHUVUKneUKBBE3s5ot FE48dKwzG/zQrBMCK/RlzMTsOdBDUsCCMQ2ZbdYfhbjkhmDCZl3IhGS8EC8uysjOyE4V YW0NIwMXD8S49jRQgssdoXnb3wonLXMx9I4WWL4UGy3eSLXe/atySY3Ud5gAwF3o69jR RzDxzJqxOdodICxFGbYUXd/ZIRI0Y2XZrrXXRO3A5JyEq0nXT3eMFyDyM/soMxs5qvTN h5qlXgMEkhI9ZoSrTZtEGIiU3BrgXyiPTaMbCcpoAob+uNfz0J4TipYr1ifLnfdrbJRs Fhuw== X-Gm-Message-State: AOAM530ga+0k4LyTOptgSzbWc1fQAzxsnV0AnM+SKptMTKx54GPtXs/4 IEA1zX5cQu+MfvQyhM4FYUDaUNPpR9Q= X-Google-Smtp-Source: ABdhPJwXwCX9DeAIvAli7N4E2jcyGO8X9+7RB3Vmnn6Er2pI0iEKQz3vde1xwFjvVtaptm5+DR4oog== X-Received: by 2002:a5d:6642:: with SMTP id f2mr11535912wrw.374.1602958994277; Sat, 17 Oct 2020 11:23:14 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:13 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:39 +0200 Message-Id: <20201017182248.577108-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/11] avcodec/movtextdec: Fix leaks of strings upon reallocation failure 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Up until now, the 3GPP Timed Text decoder used av_dynarray_add() for a list of font entries, a structure which contains an allocated string. The font entries are owned by the pointers in the dynamic array and are therefore unsuitable for av_dynarray_add() which simply frees the array, but not the font entries and of course not the strings. The latter all leak if reallocating the dynamic array fails. This commit fixes this. It stops reallocating the array altogether: After all, the final number of elements (pending errors) is already known in advance. Furthermore, the font entries are now the entries of the new array, i.e. the font entries are no longer allocated separately. This also removes one level of indirection. Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 61 ++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 068bdb7802..ad60c77519 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -102,15 +102,14 @@ typedef struct { StyleBox *s_temp; HighlightBox h; HilightcolorBox c; - FontRecord **ftab; - FontRecord *ftab_temp; + FontRecord *ftab; TextWrapBox w; MovTextDefault d; uint8_t box_flags; uint16_t style_entries, ftab_entries; uint64_t tracksize; int size_var; - int count_s, count_f; + int count_s; int readorder; int frame_width; int frame_height; @@ -137,16 +136,8 @@ static void mov_text_cleanup(MovTextContext *m) static void mov_text_cleanup_ftab(MovTextContext *m) { - int i; - if (m->ftab_temp) - av_freep(&m->ftab_temp->font); - av_freep(&m->ftab_temp); - if (m->ftab) { - for(i = 0; i < m->count_f; i++) { - av_freep(&m->ftab[i]->font); - av_freep(&m->ftab[i]); - } - } + for (unsigned i = 0; i < m->ftab_entries; i++) + av_freep(&m->ftab[i].font); av_freep(&m->ftab); m->ftab_entries = 0; } @@ -156,9 +147,9 @@ 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; + unsigned ftab_entries; StyleBox s_default; - m->count_f = 0; m->ftab_entries = 0; box_size = BOX_SIZE_INITIAL; /* Size till ftab_entries */ if (avctx->extradata_size < box_size) @@ -223,7 +214,16 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) // ftab tx3g_ptr += 4; - m->ftab_entries = AV_RB16(tx3g_ptr); + // In case of broken header, init default font + m->d.font = ASS_DEFAULT_FONT; + + ftab_entries = AV_RB16(tx3g_ptr); + if (!ftab_entries) + return 0; + m->ftab = av_calloc(ftab_entries, sizeof(*m->ftab)); + if (!m->ftab) + return AVERROR(ENOMEM); + m->ftab_entries = ftab_entries; tx3g_ptr += 2; for (i = 0; i < m->ftab_entries; i++) { @@ -233,12 +233,7 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) mov_text_cleanup_ftab(m); return -1; } - m->ftab_temp = av_mallocz(sizeof(*m->ftab_temp)); - if (!m->ftab_temp) { - mov_text_cleanup_ftab(m); - return AVERROR(ENOMEM); - } - m->ftab_temp->fontID = AV_RB16(tx3g_ptr); + m->ftab[i].fontID = AV_RB16(tx3g_ptr); tx3g_ptr += 2; font_length = *tx3g_ptr++; @@ -247,26 +242,18 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) mov_text_cleanup_ftab(m); return -1; } - m->ftab_temp->font = av_malloc(font_length + 1); - if (!m->ftab_temp->font) { + m->ftab[i].font = av_malloc(font_length + 1); + if (!m->ftab[i].font) { mov_text_cleanup_ftab(m); return AVERROR(ENOMEM); } - memcpy(m->ftab_temp->font, tx3g_ptr, font_length); - m->ftab_temp->font[font_length] = '\0'; - av_dynarray_add(&m->ftab, &m->count_f, m->ftab_temp); - if (!m->ftab) { - mov_text_cleanup_ftab(m); - return AVERROR(ENOMEM); - } - m->ftab_temp = NULL; + memcpy(m->ftab[i].font, tx3g_ptr, font_length); + m->ftab[i].font[font_length] = '\0'; 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; + if (m->d.fontID == m->ftab[i].fontID) + m->d.font = m->ftab[i].font; } return 0; } @@ -405,8 +392,8 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, 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 (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) { color = m->s[entry]->color; From patchwork Sat Oct 17 18:22:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23041 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 321D644B29E for ; Sat, 17 Oct 2020 21:23:23 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1779268BA8B; Sat, 17 Oct 2020 21:23:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 315C268BA31 for ; Sat, 17 Oct 2020 21:23:16 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id l15so3229349wmh.1 for ; Sat, 17 Oct 2020 11:23:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SDZhIdMvcJPrHdrI5oFyI7OGDuGUjirwii74OdQXtPc=; b=JYyEC+1Ud4qgZQ0yolBZmY6PS2z4+ENkcHE1qveUIknPqPVrKYJb8PgGsak5gAvUWb OH42r/BvW55eZPihCjte8D7aIIoJ8uhYSsDxGRaRo5LhdqcTpxwSWHY06keIKRPGn7NW iF4P9PEKhKesizMvuiTTsfORXmuxKFD13BDc9qYf5mgry29iqgGv7LTzsAEdv4fjJREc gliRQ47l0NqzY9nLSM9hznB8dgvvXvNcS8jiBUEAGXd35lKSiVTEJ0RLVJRQQv4tDD86 Wq3DK9c4ivrECmyp/38DnmYycYPnv7zAZtRT3T/amFcuC+H2e6gPMldyEPgjUGymO8Ep Na6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SDZhIdMvcJPrHdrI5oFyI7OGDuGUjirwii74OdQXtPc=; b=JQHTEMJRSrZ78FEm3/40IBGxup3Aezr+8Us5cIKEpK07oniBD/QZ0a6/f+JIo7zkA+ OYGcO80ABYan0+g6q949S8JhELCo2rSx+g4hEmzmBWqFl/XFE5XWnt9PyYWyoSSxs45U g6fIQyTBR7YvK9FnKnlsAaHIahtoeNkfXd+rIwXxE+GtjClsN+AwrQ1xmzkZcQLiZyZp cDOHc/sXX7DbIDrHnBPbKfwEw+UjBBaZ3Klxbglt8WjsQ0PQAnLdAHbB5gvwdCzTAF/V 9aGc+hQuQF7FilDbaumcR8zbWK48WRA+Uiuty3Rpr9l77xl7EmycZVqkrlI1nuYaxa1E L7ow== X-Gm-Message-State: AOAM5326EWDMUGTIf//Jjm2UbnDj8s6olYkyUDVqeqZ8S+37yVAmGsrI ALTCLPu39sPyR2yGAB/VLCo3Q9/IXj8= X-Google-Smtp-Source: ABdhPJwzMQEwQ0qxM2kZfR6pAFC5AsjPoM7BPALQf7h75vZF/KYuS2blJvrC6TiS3U7V44eK3k5b0Q== X-Received: by 2002:a1c:4d05:: with SMTP id o5mr9334819wmh.94.1602958995237; Sat, 17 Oct 2020 11:23:15 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:14 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:40 +0200 Message-Id: <20201017182248.577108-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 03/11] avcodec/movtextdec: Simplify checking for invalid extradata 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Every font entry occupies at least three bytes, so checking early whether there is that much data available is a low-effort way to exclude invalid extradata. Doing so leads to an overall simplification. Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index ad60c77519..e46c932c20 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -145,14 +145,13 @@ static void mov_text_cleanup_ftab(MovTextContext *m) static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) { uint8_t *tx3g_ptr = avctx->extradata; - int i, box_size, font_length; + int i, font_length, remaining = avctx->extradata_size - BOX_SIZE_INITIAL; int8_t v_align, h_align; unsigned ftab_entries; StyleBox s_default; m->ftab_entries = 0; - box_size = BOX_SIZE_INITIAL; /* Size till ftab_entries */ - if (avctx->extradata_size < box_size) + if (remaining < 0) return -1; // Display Flags @@ -220,6 +219,9 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) ftab_entries = AV_RB16(tx3g_ptr); if (!ftab_entries) return 0; + remaining -= 3 * ftab_entries; + if (remaining < 0) + return AVERROR_INVALIDDATA; m->ftab = av_calloc(ftab_entries, sizeof(*m->ftab)); if (!m->ftab) return AVERROR(ENOMEM); @@ -227,18 +229,12 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) tx3g_ptr += 2; for (i = 0; i < m->ftab_entries; i++) { - - box_size += 3; - if (avctx->extradata_size < box_size) { - mov_text_cleanup_ftab(m); - return -1; - } m->ftab[i].fontID = AV_RB16(tx3g_ptr); tx3g_ptr += 2; font_length = *tx3g_ptr++; - box_size = box_size + font_length; - if (avctx->extradata_size < box_size) { + remaining -= font_length; + if (remaining < 0) { mov_text_cleanup_ftab(m); return -1; } From patchwork Sat Oct 17 18:22:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23042 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 0170C44B29E for ; Sat, 17 Oct 2020 21:23:24 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E3B6868BAA2; Sat, 17 Oct 2020 21:23:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3E9A168BA02 for ; Sat, 17 Oct 2020 21:23:17 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id h7so7028071wre.4 for ; Sat, 17 Oct 2020 11:23:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vGgzA8wSlaSRjxQnnKr+R/Ek1pvjFKLvcK2UHCcXn8g=; b=WvbH7eCk2LdyrfSiFhrTZ+jg0BnvwOQ02l7gfU/zHajENCaRDFwAtQlwIdbLX4x5Rl +kSmo5dUcyWZXHZYNqHwXNDw3FSALNmbf5LJIpZyvTZ2uhKQ62nRp/i9wlmMPYO4xpNj /fQAsvL1p+AJeNq1kViPHozZ9P9ly5cYkeqyG8vIFcAzRUjy8/qwm+N9o4GpBX3eniI4 jwJh58lYJo6Lz0cy16HQppt7UXroH+/bMaUfF5buksDyq4THmYCFSXLUv/7VAu3Z5xJI QbkegYM7c13Z2U3cVeqr0GCmSqDvKL5tiAS29/heauj1oRw1llpWHMjNFBibmq43jee4 ggIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vGgzA8wSlaSRjxQnnKr+R/Ek1pvjFKLvcK2UHCcXn8g=; b=KWgDs4HBosBMeppzOweRyR5DuZlpcFdc3J0yI1JBEp4d/X9o+/96xIDvoFUNtJ6CdP RQLzPDEb6N0LOvjf625cpNkizY/gz3rR9Z4i7aI92OG/qNY+90wxs2xF/9/OMZo3mjRL QTPtVEkynZdPqKvjqXM2nJ0SirxJK2U0uh0TUDbjzDIYI4L1Qg0J/kim3tvOgk+CybuS Lw+UFIwTV7M/ypqVO2SAsUfnUBHpUZB+yaYfkMfHJrGRv6+muiEiiU0IZo/+HoP38OS7 v8AIRNzJq+CfGHd56ViCdShIJwPQfVt1XTGvHmMZRSZdMcgH5dA0IfQmGD+ERKqwF4z6 +nnA== X-Gm-Message-State: AOAM530bJ1J7AgQoY3NdMDDxl1Z9exPYecy7wUgwuOy+3wyKibbWAH9S BAhb9v0zjhkTEWu/erpL5pDR4Ph1hec= X-Google-Smtp-Source: ABdhPJzPrp3vhEN8nId2mBzhPzypAHNv0xSVhnd8WqSLBKE0cVCzB9kNM6loqt41BmbqMg+qTjPIPw== X-Received: by 2002:a5d:4f0b:: with SMTP id c11mr11018600wru.316.1602958996388; Sat, 17 Oct 2020 11:23:16 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:15 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:41 +0200 Message-Id: <20201017182248.577108-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/11] avcodec/movtextdec: Simplify finding default font 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" There is no need to walk through the list of fonts twice. Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index e46c932c20..974118c4c1 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -145,7 +145,7 @@ static void mov_text_cleanup_ftab(MovTextContext *m) static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) { uint8_t *tx3g_ptr = avctx->extradata; - int i, font_length, remaining = avctx->extradata_size - BOX_SIZE_INITIAL; + int i, j = -1, font_length, remaining = avctx->extradata_size - BOX_SIZE_INITIAL; int8_t v_align, h_align; unsigned ftab_entries; StyleBox s_default; @@ -230,6 +230,8 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) for (i = 0; i < m->ftab_entries; i++) { m->ftab[i].fontID = AV_RB16(tx3g_ptr); + if (m->ftab[i].fontID == m->d.fontID) + j = i; tx3g_ptr += 2; font_length = *tx3g_ptr++; @@ -247,10 +249,8 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) m->ftab[i].font[font_length] = '\0'; tx3g_ptr = tx3g_ptr + font_length; } - for (i = 0; i < m->ftab_entries; i++) { - if (m->d.fontID == m->ftab[i].fontID) - m->d.font = m->ftab[i].font; - } + if (j >= 0) + m->d.font = m->ftab[j].font; return 0; } From patchwork Sat Oct 17 18:22:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23043 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 D8AF444B29E for ; Sat, 17 Oct 2020 21:23:24 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C21B768BADC; Sat, 17 Oct 2020 21:23:24 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8D17068BA02 for ; Sat, 17 Oct 2020 21:23:18 +0300 (EEST) Received: by mail-wr1-f66.google.com with SMTP id x7so7035252wrl.3 for ; Sat, 17 Oct 2020 11:23:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fTRYcH+8zZcKjDA1AnNYs5e67dFrbNPxZEY6HGZtWMA=; b=ue5A5Gww61r+CxdOmgr72OdQ7G99HfNpXj4Cg8EIbDGP/WeE85cNw9r5J1ot8K1reV eXvIwKeTP9QuU3iUA/INzWzCxMeo49ZlRHuDecKOvPZdXi46J0OQPywFGdQ652W+HSRy Nf8gufR1v8h/j1N51+r8dD7l+xr1NfxAazkK/pViDxGp0rF0Es5iJfevluePpQKgOTZj D+mbTwxXKNSoEEJRi8bFR/fhUdtOXVEuiCBjOZkGKJZLoCr63PZDFds7OMKUcfGZ6BVi 4iOlPUm4+CGFC2NN2IPg4UOTqQmg4fjF9ePQy65UrU1wBUN1tBUZ0qE/MXTPMxzJIa/s 3Xsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fTRYcH+8zZcKjDA1AnNYs5e67dFrbNPxZEY6HGZtWMA=; b=UJY5IB5w+wQozK32aRmb/dsVQsy0sf2A1T0vgLmCEr9uwptt4M9ZkSJGm7+dK6KPST TGsxtqM/5XDH+UNfg/bzbLBM7w6KGqri2MFFZrULE2xyCWK7/qpCM3kQD3IF47LzdX3P AhFC0+5lqyh38WMvaC1Xl6idLxnTqSV6kKBqsmWBOPSqgAL9bbhk1hhStnRSFw9mUs+1 HVt5NJWt7vho3tCY3cJE5+6nSQO/ogETu6IQYbEDr4Lb90mAKg9h/NO2tj+QHD47PfpQ g46uc/odPosREJagYFYaOXpCIaH83AqhLc79mC22XJbjo5Vbu57s3g9pOEupZRxJYaLu y0pw== X-Gm-Message-State: AOAM530yBtX40ER1hTOcZan2UP+wgkm4lvQoJ+FXiLqrWMIX0epaP5OE A14NWIDpZL8uBBUsUfitkRAMnwGqwdk= X-Google-Smtp-Source: ABdhPJzWPPJ6y+zXBaLxLAiqmH3u2M9kgp7uDF8mvuLvdV547HVpQg6kzn5OF/PaQ99NlPcIFsJf4g== X-Received: by 2002:adf:f2c9:: with SMTP id d9mr10986777wrp.319.1602958997615; Sat, 17 Oct 2020 11:23:17 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:17 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:42 +0200 Message-Id: <20201017182248.577108-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/11] avcodec/movtextdec: Fix leaks on (re)allocation failure 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Up until now, the 3GPP Timed Text decoder used av_dynarray_add() for a list of style entries. Said entries are inidiviually allocated and owned by the pointers in the dynamic array and are therefore unsuitable for av_dynarray_add() which simply frees the array, but not the entries on error. In this case the intended new entry also leaks because it has been forgotten to free it. This commit fixes this. It is now allocated in one go and not reallocated multiple times (and it won't be overallocated any more). After all, the final number of elements (pending errors) is already known in advance. Furthermore, the style entries are now the entries of the new array, i.e. they are no longer allocated separately. This also removes one level of indirection. Signed-off-by: Andreas Rheinhardt --- If there are several styl boxes in the same packet, the earlier code would add them to the list (and increment count_s), but lateron use style_entries (which only contains the number of entries in the last styl box) as count. So it used the number of the last successfully parsed styl box, but it used the entries from the earliest styl boxes. Weird. Anyway, given that one styl box can contain an entry for each character I only made sure that there are no memleaks when multiple styl boxes are present. libavcodec/movtextdec.c | 84 ++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 48 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 974118c4c1..e9df979e92 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -98,8 +98,7 @@ typedef struct { typedef struct { AVClass *class; - StyleBox **s; - StyleBox *s_temp; + StyleBox *s; HighlightBox h; HilightcolorBox c; FontRecord *ftab; @@ -109,7 +108,6 @@ typedef struct { uint16_t style_entries, ftab_entries; uint64_t tracksize; int size_var; - int count_s; int readorder; int frame_width; int frame_height; @@ -123,13 +121,8 @@ typedef struct { static void mov_text_cleanup(MovTextContext *m) { - int i; if (m->box_flags & STYL_BOX) { - for(i = 0; i < m->count_s; i++) { - av_freep(&m->s[i]); - } av_freep(&m->s); - m->count_s = 0; m->style_entries = 0; } } @@ -283,50 +276,45 @@ static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) { int i; int style_entries = AV_RB16(tsmb); + StyleBox *tmp; tsmb += 2; // A single style record is of length 12 bytes. if (m->tracksize + m->size_var + 2 + style_entries * 12 > avpkt->size) return -1; + tmp = av_realloc_array(m->s, style_entries, sizeof(*m->s)); + if (!tmp) + return AVERROR(ENOMEM); + m->s = tmp; m->style_entries = style_entries; m->box_flags |= STYL_BOX; for(i = 0; i < m->style_entries; i++) { - m->s_temp = av_malloc(sizeof(*m->s_temp)); - if (!m->s_temp) { - mov_text_cleanup(m); - return AVERROR(ENOMEM); - } - m->s_temp->style_start = AV_RB16(tsmb); + StyleBox *style = &m->s[i]; + style->style_start = AV_RB16(tsmb); tsmb += 2; - m->s_temp->style_end = AV_RB16(tsmb); + style->style_end = AV_RB16(tsmb); - if ( m->s_temp->style_end < m->s_temp->style_start - || (m->count_s && m->s_temp->style_start < m->s[m->count_s - 1]->style_end)) { - av_freep(&m->s_temp); + if ( style->style_end < style->style_start + || (i && style->style_start < m->s[i - 1].style_end)) { mov_text_cleanup(m); return AVERROR(ENOMEM); } tsmb += 2; - m->s_temp->style_fontID = AV_RB16(tsmb); + style->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); + style->style_flag = AV_RB8(tsmb); + style->bold = !!(style->style_flag & STYLE_FLAG_BOLD); + style->italic = !!(style->style_flag & STYLE_FLAG_ITALIC); + style->underline = !!(style->style_flag & STYLE_FLAG_UNDERLINE); tsmb++; - m->s_temp->fontsize = AV_RB8(tsmb); + style->fontsize = AV_RB8(tsmb); tsmb++; - m->s_temp->color = AV_RB24(tsmb); + style->color = AV_RB24(tsmb); tsmb += 3; - m->s_temp->alpha = AV_RB8(tsmb); + style->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); - } } return 0; } @@ -376,29 +364,30 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, int len; if ((m->box_flags & STYL_BOX) && entry < m->style_entries) { - if (text_pos == m->s[entry]->style_start) { + const StyleBox *style = &m->s[entry]; + if (text_pos == style->style_start) { style_active = 1; - 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); - 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) + if (style->bold ^ m->d.bold) + av_bprintf(buf, "{\\b%d}", style->bold); + if (style->italic ^ m->d.italic) + av_bprintf(buf, "{\\i%d}", style->italic); + if (style->underline ^ m->d.underline) + av_bprintf(buf, "{\\u%d}", style->underline); + if (style->fontsize != m->d.fontsize) + av_bprintf(buf, "{\\fs%d}", style->fontsize); + if (style->style_fontID != m->d.fontID) for (i = 0; i < m->ftab_entries; i++) { - if (m->s[entry]->style_fontID == m->ftab[i].fontID) + if (style->style_fontID == m->ftab[i].fontID) av_bprintf(buf, "{\\fn%s}", m->ftab[i].font); } - if (m->d.color != m->s[entry]->color) { - color = m->s[entry]->color; + if (m->d.color != style->color) { + color = style->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); + if (m->d.alpha != style->alpha) + av_bprintf(buf, "{\\1a&H%02X&}", 255 - style->alpha); } - if (text_pos == m->s[entry]->style_end) { + if (text_pos == style->style_end) { if (style_active) { av_bprintf(buf, "{\\r}"); style_active = 0; @@ -526,7 +515,6 @@ static int mov_text_decode_frame(AVCodecContext *avctx, m->tracksize = 2 + text_length; m->style_entries = 0; m->box_flags = 0; - m->count_s = 0; // Note that the spec recommends lines be no longer than 2048 characters. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); if (text_length + 2 != avpkt->size) { From patchwork Sat Oct 17 18:22:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23045 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 C5A3044B29E for ; Sat, 17 Oct 2020 21:23:27 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AEDF368BB2E; Sat, 17 Oct 2020 21:23:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B279168BAAD for ; Sat, 17 Oct 2020 21:23:19 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id k18so8756122wmj.5 for ; Sat, 17 Oct 2020 11:23:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5THTPmO/KU2WyJV72rlLIi0BPIx/eRJx6HoJ6lClaqs=; b=N7DX8CntQyBh7m/JB/lsSk0YOt4OQ/SXDUSgk61x9Ap83TWpbrLqbFY67sq4mLUN3x u5/s1GxjtG0R8b6I6OvqbgpZKx8OfoEDc6cWYXOFY9WhoO6vqRrIkM/0J8FLKGaVj3rX 7cby6aUJizUmv7FI/RPjNxcoKr2ol4C7lkCIRxUhQZ7ld+KiTBq6oWO7q5+c04GKcTS4 KcbbRtUViG7TdmMFT3U0PuuTAvvw2pek/4Z3mPSuWeHpR00SizP+iVRq6T+lGOLmYsx8 VdxS+/CTE4S7oEFuK5uCF4tdK/DIELULW2+DXsn8DEypRLMoF4mWjo0dQqYWI9puuXNV E1AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5THTPmO/KU2WyJV72rlLIi0BPIx/eRJx6HoJ6lClaqs=; b=tnyzFguK9noKAHxBBnBnVB6zQOAyWINdCuHlTS89rEtp8kNDilsYMEJIGdw8ncoHl3 38xyz8FG+9vMABJt8FQS2RO617z7hGmlpaSd3mt3a/KVPjgP2vMvxe2LVisl69TYxayD cMDUovyMEzMKLHzn8PaVtWVfb3ICj/gx+kNzdetD4ghGPXJIsqUBfe1LNWaPsn7P9EYu QzT04X5RgraGV2+QMro+l56H9N+hlxyriUNgO1yxFbofSb8/m4sMGu0PNakNaKfUCfh2 CGfy8Bm0IOnqaRvLl74QsPliWT1lYDv6+ie2XFoY95dvtZlfuHkBQtJYbLb0I9pxGpFn k1FQ== X-Gm-Message-State: AOAM5336VGUre0ScF48Jc/5tH5fr5J0nIzHQNNyLyOBFudp6jJpvTvDX apNZswQmoQMH4bNFQ3rQZwDZvh1qcxw= X-Google-Smtp-Source: ABdhPJxx5i6hDJhz111m2kG3N0TMv28Vyk+wP3fkiLyuUEhkvUw7RKFpbFcMwoIfUuu8YjuCITX/3Q== X-Received: by 2002:a1c:1f87:: with SMTP id f129mr9551057wmf.182.1602958998848; Sat, 17 Oct 2020 11:23:18 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:18 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:43 +0200 Message-Id: <20201017182248.577108-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/11] avcodec/movtextdec: Skip empty styles 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" They would either lead to unnecessary ASS tags being emitted (namely tags that are reset immediately thereafter) or would lead to problems when parsing: e.g. if a zero-length style immediately follows another style, the current code will end the preceding style and set the zero-length style as the next potentially active style, but it is only tested for activation when the next character is parsed at which point the current offset is already greater than both the starting as well as the end offset of the empty style. It will therefore neither be opened nor closed and all subsequent styles will be ignored. Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index e9df979e92..d167eddea5 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -302,6 +302,14 @@ static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) } tsmb += 2; + if (style->style_start == style->style_end) { + /* Skip this style as it applies to no character */ + tsmb += 8; + m->style_entries--; + i--; + continue; + } + style->style_fontID = AV_RB16(tsmb); tsmb += 2; style->style_flag = AV_RB8(tsmb); From patchwork Sat Oct 17 18:22:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23047 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 9E92344B29E for ; Sat, 17 Oct 2020 21:23:29 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8B5E368BB52; Sat, 17 Oct 2020 21:23:29 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DF14868BB23 for ; Sat, 17 Oct 2020 21:23:20 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id 13so6518225wmf.0 for ; Sat, 17 Oct 2020 11:23:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JtjB52rY7QOXK8IO4eSI/EwTLBjeDiiIF41zA4RDWGg=; b=K7nTY6CpBYBcDu90wUJAYHilQ8qBqpGDTEyeHv9itcFHE45DwmLQ0tRrqCvh920E39 obI61c+O7DBRuymCfSBCxX9Nxyy5e/AgvXSu/BFgAJu1Yz2gTOdBMLxAxCcclLTSQNnd GlIM/w9wccCHz5yrMs3DFntNdAxpH0U20hvC5iVkiHGP2vmR1dVYzTy1uvH3NA0K3Ypc Zq/yvGWZ2wj+sVpBISTKgf3aDKCh1eRkLg1kUNgF88MmogkPLR0qiDvcSBmpCmGg4JLz fNhZCa2r2qorZbF66gxBIagCWnCyif6dzk3VxDqDXqvcJ/SQVEkZxFz7++YfY3DnK8jF Cmhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JtjB52rY7QOXK8IO4eSI/EwTLBjeDiiIF41zA4RDWGg=; b=aGWLsz8Ghr0zbrGrBKBTdmQGKGBeID1IQxbVuQolF6mZURPg3NHvCuddENZV9QUyyl Aka5XwojxkFzH4iwKR5CwvF2CT0BeKAbIyy7gI+4jpXZSVqNx7/Qxe1HQ1PqPNhav5kJ pOBx0+Eb6sl6UPkTDdGPuWKyEjbBUqG7fC+tp2pSjnG5ZDpNi9JWVPChwDtb2QZYTGpQ Hi8bbNfKvporrA82Kk2uOI8vw0eAewu9TChYdNeTdGalkHboi537GQFYyOtQvU5LifzG OEghdoMVCGLmk33T2cfOElQ2XfCett/99zozQPUgFrBhLp5Z9+z801vwivp/cdzzzpwb 9uYw== X-Gm-Message-State: AOAM533q90ve26pm9aCls3fWxWsPn8tS4Rfy0abQi6g0CRy+C6o34DNE mMz/kgfSDHQge2u7DxHHfCN/LNomylE= X-Google-Smtp-Source: ABdhPJyU+fOnAn09f7jsLkM3WfE85B6yytEZ/yHrUoHtxRibPo7itEvZMZSl4i2N8pNy7BNi9y2HIg== X-Received: by 2002:a1c:7707:: with SMTP id t7mr10077558wmi.54.1602959000106; Sat, 17 Oct 2020 11:23:20 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:19 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:44 +0200 Message-Id: <20201017182248.577108-7-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 07/11] avcodec/movtextdec: Fix immediately adjacent styles 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The checks for whether a style should be opened/closed at the current character position are as follows: A variable entry contained the index of the currently active or potentially next active style. If the current character position coincided with the start of style[entry], the style was activated; this was followed by a check whether the current character position coincided with the end of style[entry]; if so, the style was deactivated and entry incremented. Afterwards the char was processed. The order of the checks leads to problems in case the endChar of style A coincides with the startChar of the next style (say B): Style B was never opened. When we are at said common position, the currently active style is A and so the start pos check does not succeed; but the end pos check does and it closes the currently active style A and increments entry. At the next iteration of the loop, the current character position is bigger than the start position of style B (which is style[entry]) and therefore the style is not activated. The solution is of course to first check for whether a style needs to be closed (and increment entry if it does) before checking whether the next style needs to be opened. Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index d167eddea5..d46d64b6f2 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -373,7 +373,16 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, if ((m->box_flags & STYL_BOX) && entry < m->style_entries) { const StyleBox *style = &m->s[entry]; - if (text_pos == style->style_start) { + if (text_pos == style->style_end) { + if (style_active) { + av_bprintf(buf, "{\\r}"); + style_active = 0; + color = m->d.color; + } + entry++; + style++; + } + if (entry < m->style_entries && text_pos == style->style_start) { style_active = 1; if (style->bold ^ m->d.bold) av_bprintf(buf, "{\\b%d}", style->bold); @@ -395,14 +404,6 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, if (m->d.alpha != style->alpha) av_bprintf(buf, "{\\1a&H%02X&}", 255 - style->alpha); } - if (text_pos == style->style_end) { - if (style_active) { - av_bprintf(buf, "{\\r}"); - style_active = 0; - color = m->d.color; - } - entry++; - } } if (m->box_flags & HLIT_BOX) { if (text_pos == m->h.hlit_start) { From patchwork Sat Oct 17 18:22:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23048 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 779AD44B29E for ; Sat, 17 Oct 2020 21:23:30 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 62A5B68BB5B; Sat, 17 Oct 2020 21:23:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DC53D68BAA0 for ; Sat, 17 Oct 2020 21:23:21 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id f21so6500093wml.3 for ; Sat, 17 Oct 2020 11:23:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qWjGB3K4tZtPsFaQdo0C3hATnY2naBh4AbAHSQObzzA=; b=S0E6lek96jqLI/sitNUQnIWT4sH/E1Ho8l67Ks5LJYTf1Gc6v7oDfIVFq2DJcBda3x 4yQRxvXmm7y26ifO2Z/NAW9Q5wkfQ3uLzhIf6TKOmmtJ6k79krIMZ+6dAU9XndNSzuhW Qfdaa5N3y5Q0zbTydgmQht6kQHqG2PjYQmWHegyjhTX5xRhwr025qIJjhZ1PL4+DV034 J6h0fTr48mRYsZmtw+OJK6hLv+NdeTIus2GM5vjbzg06Zy1pXLSl4dSS0iM42rkUJVDC ol+AuzXGn5Lpnv2epCic3bcigrAkzCfqT1ZnqOub9BzgBzg2aR1RosivOBBS3rSA9G+s WBbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qWjGB3K4tZtPsFaQdo0C3hATnY2naBh4AbAHSQObzzA=; b=lkzyDIMK9Ddg4mTRTLsxGjM3ooycSJ8jKsRmma53C0vP6OsotGRyLlwTvTSyeatjxj dlqbrG3DsLZSfUaKPclJQW16RUtvsNrxsuo8Sy3zcF0xgvyY2/2s8AkKYb5gMVB17g3g eLrn7/BsIUt7mkIxo+O2repSpJyxxOA7aJEl6d2hDOcCSa6daMzEyN6eo7meVT5iDp2p virj3KSue5jXrJ4h0K3B4dmizvlpB5YlA5U4oY0xEfD5lBE4+GvOoF0PIcuXVlwCsf+6 QzpKFW939n8VoKqlEUO8v+oSAojCDTjHjdcfU81xPZvG1FXSsUlrQV++4c2qjv01FPMH 800A== X-Gm-Message-State: AOAM533hvmk9CHtTLpCRQKjzDpxOBUsFrGee2OCo2tqlRyZvs34wPYrQ Ehn95Ic69t5cm9bMgyjcWsMmgSHB/zE= X-Google-Smtp-Source: ABdhPJzzl0fezxqulRaP0szOTpUKDO8jwbIwg2qTEYeBh7T6C8OXI5hwkylhExOOGKbbrWVwE+SF2w== X-Received: by 2002:a1c:dc43:: with SMTP id t64mr9802222wmg.6.1602959001225; Sat, 17 Oct 2020 11:23:21 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:20 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:45 +0200 Message-Id: <20201017182248.577108-8-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/11] avcodec/movtextdec: Remove unnecessary variable 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" style_active doesn't do anything any more: It is already assured that style_active is one when one reaches the end of a style. Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index d46d64b6f2..1bfca8b79d 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -356,7 +356,6 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, MovTextContext *m = avctx->priv_data; int i = 0; int text_pos = 0; - int style_active = 0; int entry = 0; int color = m->d.color; @@ -374,16 +373,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) { const StyleBox *style = &m->s[entry]; if (text_pos == style->style_end) { - if (style_active) { - av_bprintf(buf, "{\\r}"); - style_active = 0; - color = m->d.color; - } + av_bprintf(buf, "{\\r}"); + color = m->d.color; entry++; style++; } if (entry < m->style_entries && text_pos == style->style_start) { - style_active = 1; if (style->bold ^ m->d.bold) av_bprintf(buf, "{\\b%d}", style->bold); if (style->italic ^ m->d.italic) From patchwork Sat Oct 17 18:22:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23049 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 9CBAB44B29E for ; Sat, 17 Oct 2020 21:23:31 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8511168BB6A; Sat, 17 Oct 2020 21:23:31 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3756668BB53 for ; Sat, 17 Oct 2020 21:23:23 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id j136so8779170wmj.2 for ; Sat, 17 Oct 2020 11:23:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Dm/+dCftEHISWKL9CjU5Dh5EY5E9n4hO+bIZzRnKMO4=; b=svxNltdoteoKqWdrvP4nkw39qfQ7koUqoh3DN2tljYJcre5/d7ItZ5oVSdXeNRv5Yc AnD18x4YrnVpBnBrF9GvQh3J/Vgq0+0Z6K4Q5RXt5vqYKidUq1WZFM91dm+yDbVtjy36 IxocRu5IFH0JYViCyOWGm6RFFVY+JIYSf1RbuoT2JjntIB5K0VlSOg+LwdEoTiHrFgXH itVfFlEDxjN5ute/HxnOGIjVcOARHhgjifxJgrzGGn9KPHgI8tkEKsVFeHj1Jq/rYYAd y2od0K53f62JhURqd8J6QESKE9mXrLNL2QAcVcS5yD0jUVaLNrELbPEH1vzVQpsh5IfB sYFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Dm/+dCftEHISWKL9CjU5Dh5EY5E9n4hO+bIZzRnKMO4=; b=KmIjNaSGtHv/C47B+NChagNwZiIaBcZMOt7PIitKamJGDcfkppo+YsjGpXa8SQUlSQ 2VZdYvQ+XhYWAF9mjj+Lcmhl0dQ2rPTdGoIRfGGBmskfgytEwbQGmhsgZUAEOQuJ3/CZ 8wMts1fF2gsBdjoh7L2RQmnzWEHZxgyJ8GnCgHhcpPBPZse9TxnQr1Kdf38zyiBxAPsP 8nr8+BbO+VSBzxCVJb3yixN/EDbS4+0nv4NDYLrSP4kJ9PzgKFpuI95q8TvfTXxm5bbJ ngM6kc9Dv/5+gAuyEyb/dzhAdwz4W3Lg3dkz2Gy1ruDqSHjwtkjNUXUq6cVBAUcRj2ym 73kw== X-Gm-Message-State: AOAM532cOgrqUepKi2nFbyh5J5XSpbEbkun+01tdB9cPo/h2QQVPfEbI WHXGmCbF6//1x2M9IBhaxrKiSCAM9fo= X-Google-Smtp-Source: ABdhPJwIrnZWdqjPfgcXFSUY17UVxb114VT69RySzQ8qI5mZDO4bZGS4oYdCATe7WOxQqmQ2WYvlfA== X-Received: by 2002:a1c:5641:: with SMTP id k62mr9146386wmb.108.1602959002284; Sat, 17 Oct 2020 11:23:22 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:21 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:46 +0200 Message-Id: <20201017182248.577108-9-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/11] avcodec/movtextdec: Avoid loop when writing UTF-8 character to AVBPrint 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 1bfca8b79d..e5ce58a184 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -430,7 +430,6 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, av_log(avctx, AV_LOG_ERROR, "invalid UTF-8 byte in subtitle\n"); len = 1; } - for (i = 0; i < len; i++) { switch (*text) { case '\r': break; @@ -438,11 +437,10 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, av_bprintf(buf, "\\N"); break; default: - av_bprint_chars(buf, *text, 1); + av_bprint_append_data(buf, text, len); break; } - text++; - } + text += len; text_pos++; } From patchwork Sat Oct 17 18:22:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23044 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 CACC144B29E for ; Sat, 17 Oct 2020 21:23:25 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id ACCAD68BB24; Sat, 17 Oct 2020 21:23:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 548AC68BA31 for ; Sat, 17 Oct 2020 21:23:24 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id z22so3234135wmi.0 for ; Sat, 17 Oct 2020 11:23:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2depkfVSyyhSc2TiTyn02sFcX2Ua6VCYXWcmnZFbpTM=; b=OYdFUegbgf3cfjd1TaX5M5hWlCICchIkjQrYyGSXCDlzvYVXIXJMa0RIEmnMMGdRdr kVtzky4xLdH7bi8w5IC1wv6Qkvk9LZaeDbvlixe1CRx3IjvkQeKAKtlJQQjzSbOyVCTv VgxZrebLbZt95HVwXMHof8qEC/b4e5/2cYgCGFw9R1qeHiDYNOkRktyCxN0mANz6EGiV cgHp2753pN9BqNWCpzfyisOmd5T3dO+lt2P3r6X20OmjN4tTvcywVXA5piXddsgtmJRm wllFZ2ALtF2gxOcSdK99yTD8WHcGpiYmhGhcUqzc0G1JBVuN3jc8AIOHcjZ53+vdcZIY Y4IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2depkfVSyyhSc2TiTyn02sFcX2Ua6VCYXWcmnZFbpTM=; b=OVqF17S+w4I03lw+f7iz327laqUDl27RgeUOf3Fy3j/37zIVO5cIJ7Pv6tpw1lFF2N waoac/rruD/a9OErMFnlN5a5s1WJjpMdkypOkPjrTp+YHq78Mr6rzX9eyeasCRs1etDo KPLV1WDI2yxThgsNq+isROeYKBKcFctqwXX7bDCAcn3UjtHwMH/slzjdlqmf30FsUT8/ /a8RHJmdwqT43frpfwvygMdkecZUfrASPuIPgdsbxUBPJ58Kep2pMFekEZOPrfuwXY31 BNhfF1yWunU0ez17GwO9DQzZfH+J740GKsw6CFBcoglsWIOcaUD2XgnR6wvVdKjW0rOA 9b9A== X-Gm-Message-State: AOAM532ukJmxDBkw8/VFvtaAc+9cI69YWq85RD487x/PuWlGFj3SOc1e yXwzTqMydNkVuktMCJukVuaFrXQLx9Q= X-Google-Smtp-Source: ABdhPJw3/1lsMAfLITiDpmlsHfBaNqw+4Lh8NgVfB6CMgforMSO0gqfffeE9ur+pK8NU41XpBkVuCQ== X-Received: by 2002:a1c:63c3:: with SMTP id x186mr9915615wmb.66.1602959003424; Sat, 17 Oct 2020 11:23:23 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:22 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:47 +0200 Message-Id: <20201017182248.577108-10-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/11] avcodec/movtextdec: Use bytestream API 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Improves readability. Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 74 ++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index e5ce58a184..95dfb2a0af 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -27,6 +27,7 @@ #include "libavutil/bprint.h" #include "libavutil/intreadwrite.h" #include "libavutil/mem.h" +#include "bytestream.h" #define STYLE_FLAG_BOLD (1<<0) #define STYLE_FLAG_ITALIC (1<<1) @@ -137,7 +138,7 @@ static void mov_text_cleanup_ftab(MovTextContext *m) static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) { - uint8_t *tx3g_ptr = avctx->extradata; + const uint8_t *tx3g_ptr = avctx->extradata; int i, j = -1, font_length, remaining = avctx->extradata_size - BOX_SIZE_INITIAL; int8_t v_align, h_align; unsigned ftab_entries; @@ -150,8 +151,8 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) // Display Flags tx3g_ptr += 4; // Alignment - h_align = *tx3g_ptr++; - v_align = *tx3g_ptr++; + h_align = bytestream_get_byte(&tx3g_ptr); + v_align = bytestream_get_byte(&tx3g_ptr); if (h_align == 0) { if (v_align == 0) m->d.alignment = TOP_LEFT; @@ -177,29 +178,24 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) m->d.alignment = BOTTOM_RIGHT; } // Background Color - m->d.back_color = AV_RB24(tx3g_ptr); - tx3g_ptr += 3; - m->d.back_alpha = AV_RB8(tx3g_ptr); - tx3g_ptr += 1; + m->d.back_color = bytestream_get_be24(&tx3g_ptr); + m->d.back_alpha = bytestream_get_byte(&tx3g_ptr); // BoxRecord tx3g_ptr += 8; // StyleRecord tx3g_ptr += 4; // fontID - m->d.fontID = AV_RB16(tx3g_ptr); - tx3g_ptr += 2; + m->d.fontID = bytestream_get_be16(&tx3g_ptr); // face-style-flags - s_default.style_flag = *tx3g_ptr++; + s_default.style_flag = bytestream_get_byte(&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); // fontsize - m->d.fontsize = *tx3g_ptr++; + m->d.fontsize = bytestream_get_byte(&tx3g_ptr); // Primary color - m->d.color = AV_RB24(tx3g_ptr); - tx3g_ptr += 3; - m->d.alpha = AV_RB8(tx3g_ptr); - tx3g_ptr += 1; + m->d.color = bytestream_get_be24(&tx3g_ptr); + m->d.alpha = bytestream_get_byte(&tx3g_ptr); // FontRecord // FontRecord Size tx3g_ptr += 4; @@ -209,7 +205,7 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) // In case of broken header, init default font m->d.font = ASS_DEFAULT_FONT; - ftab_entries = AV_RB16(tx3g_ptr); + ftab_entries = bytestream_get_be16(&tx3g_ptr); if (!ftab_entries) return 0; remaining -= 3 * ftab_entries; @@ -219,14 +215,12 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) if (!m->ftab) return AVERROR(ENOMEM); m->ftab_entries = ftab_entries; - tx3g_ptr += 2; for (i = 0; i < m->ftab_entries; i++) { - m->ftab[i].fontID = AV_RB16(tx3g_ptr); + m->ftab[i].fontID = bytestream_get_be16(&tx3g_ptr); if (m->ftab[i].fontID == m->d.fontID) j = i; - tx3g_ptr += 2; - font_length = *tx3g_ptr++; + font_length = bytestream_get_byte(&tx3g_ptr); remaining -= font_length; if (remaining < 0) { @@ -238,9 +232,8 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) mov_text_cleanup_ftab(m); return AVERROR(ENOMEM); } - memcpy(m->ftab[i].font, tx3g_ptr, font_length); + bytestream_get_buffer(&tx3g_ptr, m->ftab[i].font, font_length); m->ftab[i].font[font_length] = '\0'; - tx3g_ptr = tx3g_ptr + font_length; } if (j >= 0) m->d.font = m->ftab[j].font; @@ -250,34 +243,31 @@ static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m) static int decode_twrp(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) { m->box_flags |= TWRP_BOX; - m->w.wrap_flag = *tsmb++; + m->w.wrap_flag = bytestream_get_byte(&tsmb); return 0; } static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) { m->box_flags |= HLIT_BOX; - m->h.hlit_start = AV_RB16(tsmb); - tsmb += 2; - m->h.hlit_end = AV_RB16(tsmb); - tsmb += 2; + m->h.hlit_start = bytestream_get_be16(&tsmb); + m->h.hlit_end = bytestream_get_be16(&tsmb); return 0; } static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) { m->box_flags |= HCLR_BOX; - memcpy(m->c.hlit_color, tsmb, 4); - tsmb += 4; + bytestream_get_buffer(&tsmb, m->c.hlit_color, 4); return 0; } static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) { int i; - int style_entries = AV_RB16(tsmb); + int style_entries = bytestream_get_be16(&tsmb); StyleBox *tmp; - tsmb += 2; + // A single style record is of length 12 bytes. if (m->tracksize + m->size_var + 2 + style_entries * 12 > avpkt->size) return -1; @@ -291,17 +281,14 @@ static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) m->box_flags |= STYL_BOX; for(i = 0; i < m->style_entries; i++) { StyleBox *style = &m->s[i]; - style->style_start = AV_RB16(tsmb); - tsmb += 2; - style->style_end = AV_RB16(tsmb); + style->style_start = bytestream_get_be16(&tsmb); + style->style_end = bytestream_get_be16(&tsmb); if ( style->style_end < style->style_start || (i && style->style_start < m->s[i - 1].style_end)) { mov_text_cleanup(m); return AVERROR(ENOMEM); } - - tsmb += 2; if (style->style_start == style->style_end) { /* Skip this style as it applies to no character */ tsmb += 8; @@ -310,19 +297,14 @@ static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt) continue; } - style->style_fontID = AV_RB16(tsmb); - tsmb += 2; - style->style_flag = AV_RB8(tsmb); + style->style_fontID = bytestream_get_be16(&tsmb); + style->style_flag = bytestream_get_byte(&tsmb); style->bold = !!(style->style_flag & STYLE_FLAG_BOLD); style->italic = !!(style->style_flag & STYLE_FLAG_ITALIC); style->underline = !!(style->style_flag & STYLE_FLAG_UNDERLINE); - tsmb++; - style->fontsize = AV_RB8(tsmb); - tsmb++; - style->color = AV_RB24(tsmb); - tsmb += 3; - style->alpha = AV_RB8(tsmb); - tsmb++; + style->fontsize = bytestream_get_byte(&tsmb); + style->color = bytestream_get_be24(&tsmb); + style->alpha = bytestream_get_byte(&tsmb); } return 0; } From patchwork Sat Oct 17 18:22:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23046 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 AE43944B29E for ; Sat, 17 Oct 2020 21:23:28 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9C4D768BB56; Sat, 17 Oct 2020 21:23:28 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9769A68BB23 for ; Sat, 17 Oct 2020 21:23:25 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id a72so6490813wme.5 for ; Sat, 17 Oct 2020 11:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NzvW24V7MQV3SJWBDwkOeY4sgOcXtJAqBHZeJ2IVDRQ=; b=HxvhFxc2pziA8wUoDqulerakUj+m1DSlNwx2LKppfgDk1mF2OgJBjPv7HVcmZmxrEN eboimdtoyESY5HR2I8JAHLOulUDvHjHRGraysks5mGXH6hrcbYxHJhiatOjNlKvg64Zc G0d5Vpc+0KgkFnJrnUIvPXJWgLIBNrg7WjnDA2zKNk6F2aFPJt9lO1Ytqxrxd3Rbk28t CK9n+KauD/A5lVJg0ZgY4ZcNEjkuou7tgxl7mdOzd7NhZCCVDA5sJpIMLznlEbvP1pcS GexA5dQLY4v46AyclM2f6PG+6JlWQICsZzEY05yciJK5w+9ZUkpjzM97dCNyxt8XFMVo xLdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NzvW24V7MQV3SJWBDwkOeY4sgOcXtJAqBHZeJ2IVDRQ=; b=PpwWTbSNOXlSXGMUa8ChS0mign0ze6z/rgxsbWzE8DMBBgpA+7Jco93cPOkaSsrnls qrbuQ6AVpsE4rYNUsRP96bTctX+OXVwmDKv/qVfzOq4U1E6ScV3nztEbzM3Srl2b1H0z wDyDMlV8koI6/9R3OxCgvExBh4wN0ADZdm/k5ddsz0vHBjzRTBISRjdIgpd2JjIPl+9P ro4PIvm+qx8iikHkFkSiQxGulxM1mMdNCc4rAeo5hutMF2vr24RhP0pEHhG3j6OMM6nS SYG/HKXag1fS1AgvdV3Usj1VzHRKpjIkLNbt1xc5wHuEE/p7MapN7j75waYALP24hO8K NT1w== X-Gm-Message-State: AOAM532LyIBY837r9eIr47WuDLws4UKzj5hNnyVJQqAwAAbTsESJj2Dy rETv9NXygycpzDF3i6Zjw1sbG8TZf/U= X-Google-Smtp-Source: ABdhPJyYcH30s2qiVy4K6E29c8L7y0S+bUwz2CZXLBX9tywpl6ql4thIE4spuIpqWE/BQYpNnLn3TA== X-Received: by 2002:a1c:3:: with SMTP id 3mr9341414wma.14.1602959004500; Sat, 17 Oct 2020 11:23:24 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d20sm10440688wra.38.2020.10.17.11.23.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Oct 2020 11:23:23 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Oct 2020 20:22:48 +0200 Message-Id: <20201017182248.577108-11-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> References: <20201017182248.577108-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 11/11] avcodec/movtextdec: Reindentation 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Andreas Rheinhardt --- libavcodec/movtextdec.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c index 95dfb2a0af..ad790bf44c 100644 --- a/libavcodec/movtextdec.c +++ b/libavcodec/movtextdec.c @@ -412,16 +412,16 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end, av_log(avctx, AV_LOG_ERROR, "invalid UTF-8 byte in subtitle\n"); len = 1; } - switch (*text) { - case '\r': - break; - case '\n': - av_bprintf(buf, "\\N"); - break; - default: - av_bprint_append_data(buf, text, len); - break; - } + switch (*text) { + case '\r': + break; + case '\n': + av_bprintf(buf, "\\N"); + break; + default: + av_bprint_append_data(buf, text, len); + break; + } text += len; text_pos++; }