From patchwork Mon Jul 13 19:42:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 21001 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 B211044AEFD for ; Mon, 13 Jul 2020 23:07:27 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 993E468A6D0; Mon, 13 Jul 2020 23:07:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f66.google.com (mail-ed1-f66.google.com [209.85.208.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 25E2E68A3AF for ; Mon, 13 Jul 2020 23:07:21 +0300 (EEST) Received: by mail-ed1-f66.google.com with SMTP id b15so14829674edy.7 for ; Mon, 13 Jul 2020 13:07: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:mime-version :content-transfer-encoding; bh=qbZcVCEArKKXRVvohby2bRH1AXYO51gYXSy3q2AnW6c=; b=OomX/Ydjp8rZwCHxrfDXF2fzWJpTyCmdwSSI0ettjRVWND/z8VPY+qhU90MA+PanX3 g/PvLoQNQLcYXpmMSoCktuabVUttMhV7gIdF7Jmz5Y1yXriybKXFYJTVYftjFvWdW846 0Pr8DqMV9CiBnaetJUxe7Mfi1bZlBzK3o+K0d2SvPtmy9gUBpGbrWE7sEU1dFInzyYTV vV3tRi+k92xXG9eVf/qPXuoLz8IMetxqSSHrEREb9D9BMcezDza1LXGi0UothNJRUJ7S pV/XWRSWSG05Onk7IEp0yjQFMwOvAClB5bVWa9FQtZ3zzPLwqZJGDqzA3jBrXqlzwoY6 8ttg== 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=qbZcVCEArKKXRVvohby2bRH1AXYO51gYXSy3q2AnW6c=; b=CIzidfW65UgvVk3hlrUJYLBucGaR57z8qCJcoz9hsh7PPEfgPDovEAb6Z7dVUDCchs 81MHXlsKcVFU40Wrtst6fPVT+ItsOS/AbOVbpm5OVVpiHZ4gYBHx7pFTFjsLA8h9AwIQ aj6jMfLgphfJCZgiyp3hJ6UeXE7rx9pSESb3TzSEkFyfRpQO5lxPiF/duO956m4sj+CP cSTarj/2TgQv0yYEqcBv972z+rW7VIkD1rcyelLaDjLcJyv9QHKFanqmC7x2nHGX4ct0 OGdE2ZBUSMr0XRDK+EzaMIiol5Sk0DfUwk6rk90RjNJH4abQnJLz7dtZdlGpSFs0PNdw KLuA== X-Gm-Message-State: AOAM533azDZO9ABD6lvjetiq96D42AkXqtYKcNJZ9vBz5wHH6HR5X1YK F9TYtSJqKwn9P66X0j4kcnUQfsSI X-Google-Smtp-Source: ABdhPJybSEUmOknKSzXdDELWSI8Wh3I4Pg0GNxC7uJNbp5y5YwHz995jkgJNQujiM18vechbxlxm5g== X-Received: by 2002:a5d:424f:: with SMTP id s15mr1168251wrr.342.1594669341514; Mon, 13 Jul 2020 12:42:21 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc10296.dynamic.kabel-deutschland.de. [188.193.2.150]) by smtp.gmail.com with ESMTPSA id h5sm27532278wrc.97.2020.07.13.12.42.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2020 12:42:20 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Jul 2020 21:42:08 +0200 Message-Id: <20200713194211.30244-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/4] avformat/au: Store strings instead of pointers to strings in array 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 --- libavformat/au.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/libavformat/au.c b/libavformat/au.c index f92863e400..b419c9ed95 100644 --- a/libavformat/au.c +++ b/libavformat/au.c @@ -68,13 +68,13 @@ static int au_probe(const AVProbeData *p) static int au_read_annotation(AVFormatContext *s, int size) { - static const char * keys[] = { + static const char keys[][7] = { "title", "artist", "album", "track", "genre", - NULL }; + }; AVIOContext *pb = s->pb; enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY; char c; @@ -107,7 +107,7 @@ static int au_read_annotation(AVFormatContext *s, int size) av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n"); } else { av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); - for (i = 0; keys[i] != NULL && key != NULL; i++) { + for (i = 0; i < FF_ARRAY_ELEMS(keys) && key != NULL; i++) { if (av_strcasecmp(keys[i], key) == 0) { av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL); av_freep(&key); @@ -243,14 +243,13 @@ typedef struct AUContext { static int au_get_annotations(AVFormatContext *s, char **buffer) { - static const char * keys[] = { + static const char keys[][7] = { "Title", "Artist", "Album", "Track", "Genre", - NULL }; - int i; + }; int cnt = 0; AVDictionary *m = s->metadata; AVDictionaryEntry *t = NULL; @@ -258,7 +257,7 @@ static int au_get_annotations(AVFormatContext *s, char **buffer) av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); - for (i = 0; keys[i] != NULL; i++) { + for (int i = 0; i < FF_ARRAY_ELEMS(keys); i++) { t = av_dict_get(m, keys[i], NULL, 0); if (t != NULL) { if (cnt++) From patchwork Mon Jul 13 19:42:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 20998 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 A016F448AFC for ; Mon, 13 Jul 2020 22:48:37 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 71D1D68A561; Mon, 13 Jul 2020 22:48:37 +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 4C79468A2D7 for ; Mon, 13 Jul 2020 22:48:31 +0300 (EEST) Received: by mail-wr1-f66.google.com with SMTP id f2so18011244wrp.7 for ; Mon, 13 Jul 2020 12:48:31 -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=J/3bE9BomYS5QDM4FIxnLdVMXiRCroz0olJdWXdvtO8=; b=iGwmOqMmQorAQ+Xv1zA75IvSzT7II27s0HoR/K1Lds59//jk1WpVe/3zm35pKXdB8y uaKRXBZEyl/KadsCe7hSsJeeLqyrIinkBBNnHsoyg/++GztOjsojlvyzfY5xg/GzgaUM gIz/UoqafcaaOKnKJXlKbDrwsTo7QXtFA0HIZz0EY0P9tirgg7z7dtgw6vcCjBfN3aml ri1qZPE1Lf5bD/XxqsonMuT7agA7ildddDQWDlEnhkcjsKu7Yqb4zLVAA5/6SvoFiQL8 NPQJnGL5uJGmHJi0bd07RjwwzX23oQvkBR5+k7pBhPJ/vN87DdETHyVn2+8JM3dc0wcL CGaA== 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=J/3bE9BomYS5QDM4FIxnLdVMXiRCroz0olJdWXdvtO8=; b=LeAatSglydhV5SJFIZ/jq0t31U+hY64GeRkR0wjwoxIAvu+wTwRqQqfxp2r1ZT3E/l EmLy4qY2ei0JDdlNm+EHrwZ+cqFtnsVRcuAQtmKKAETfFA81GLsns3/4bYA1pdoSAA0X zdSaQRgP+ksNp3BbicUA86xcRXjfKsUFxOasy5dzp8fMJ6MFH1NDRnBUZFpWZAdAgIOG uhjauIoXU4B8X5wGLCfvp4OJsQW1PU+8F4aObrye8nhZ99gcnb4PfOLWopwmqR5Oei5B lYUXYLT5saX8X2Pg3JTRsQ0aIPEAzeYEnra8qDLRIH3yAkfPHJCp/p6uZ/REc0JBMak4 FvpQ== X-Gm-Message-State: AOAM531sqmv7ru6c9b+F1HWwXx6PLfgQi4EDM84cu/yyGxgYO1Uc0aaO Wlht2iP5vJJtFOMVi9XMbgOCjowt X-Google-Smtp-Source: ABdhPJzqg9aCe1hxR+aDJ+nqKikESCBQp/PYtzkWt78yUp/e593zCNJiZHB4Hzr+3Ap+UTwagr0edQ== X-Received: by 2002:adf:de0a:: with SMTP id b10mr1145193wrm.72.1594669710405; Mon, 13 Jul 2020 12:48:30 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc10296.dynamic.kabel-deutschland.de. [188.193.2.150]) by smtp.gmail.com with ESMTPSA id h5sm27532278wrc.97.2020.07.13.12.48.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2020 12:48:29 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Jul 2020 21:42:09 +0200 Message-Id: <20200713194211.30244-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200713194211.30244-1-andreas.rheinhardt@gmail.com> References: <20200713194211.30244-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] avformat/au: Remove redundant av_freep() 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" This av_freep(&key) in conjunction with the fact that the loop condition checks for key != NULL was equivalent to a av_freep(&key) + a break immediately thereafter. But given that there is an av_freep(&key) directly after the loop, the av_freep(&key) is unnecessary and the break can also be added explicitly. Signed-off-by: Andreas Rheinhardt --- libavformat/au.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavformat/au.c b/libavformat/au.c index b419c9ed95..c4a32ff76c 100644 --- a/libavformat/au.c +++ b/libavformat/au.c @@ -107,11 +107,11 @@ static int au_read_annotation(AVFormatContext *s, int size) av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n"); } else { av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); - for (i = 0; i < FF_ARRAY_ELEMS(keys) && key != NULL; i++) { + for (i = 0; i < FF_ARRAY_ELEMS(keys); i++) { if (av_strcasecmp(keys[i], key) == 0) { av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL); - av_freep(&key); value = NULL; + break; } } } From patchwork Mon Jul 13 19:42:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 20999 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 F21D4448AFC for ; Mon, 13 Jul 2020 22:48:38 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DDF4768A79B; Mon, 13 Jul 2020 22:48:38 +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 4D88768813F for ; Mon, 13 Jul 2020 22:48:32 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id w3so1005944wmi.4 for ; Mon, 13 Jul 2020 12:48:32 -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=klXtW7PPIp8I6iQpTrVWHnjogyIjX4l4aweE/ClJF/U=; b=lifl2ckVw4A2F0XKwwgR1lbtFE2yFJECbPVDvX5hBiGLHBCn/ph86S4ent+wg0treP cnyX9bkCLIrKd5iocbe74mRSTtNIbYT8YZss5X4a2EcBAOHfOYsTzP9BV2nioUF3n7sa 28unAgwnXfncNL5PZTzI+KxHOs3/VMqBnZbPCJQ4QuXVrPf7Cl9YXpiJedN1nunAk/oX RyYW/zfytGPfYj+lbDpCDl4bUkMXOu4Oc+/M0NFUVTE5Kp10JZRktirTcrjBwXB8sws7 AJcYdHKgleJeF7oc0ITXdRbXQgzRGw0Q/1rV+ybouSlg4vIYayIh4Z/gTGxH3kjDmv8Q b1YA== 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=klXtW7PPIp8I6iQpTrVWHnjogyIjX4l4aweE/ClJF/U=; b=iHLBRErk69/+tqMGLtU8XVF957QpDoLz6fusW/7WrMxP70YKl5874O8J4ppeF4yXzh HWeK/HHWdsZOdiBVoTwvwPaDH3NMBRsSEXIB60CUsX1ailmDqu8torEei6zsJz+lt17l rKViBsFKZRF/jsJVQXZbi7dav4xPVhBM5hC9MSvMHDDbub5KqgWUoUJdxCzqtBYoC8UL tVq6PtQsdJO2nQUR/1f33OAJMq/pkkOhXo8+J8iqT3RTn67TBWNlX7JrT/oAG9iwUbFB RjrTVeGoeTnbnn59Lmk4eRbXg//NOiVSBkDKC3+XwHCtoEWOrJlPBKFbT/UbKiEalSzf VpPA== X-Gm-Message-State: AOAM530bUwmMIFaY9di9I7GOSbvjPgUQwUb+l7YMoUw7fC7k80Im8Np2 JmPQciqS/kF/8a1l4Xb2gRcCkxAe X-Google-Smtp-Source: ABdhPJzGiynv0oLJc0BacLfs8gCGCJ5RPol9oeEsq06UD9q5wMkCLyi9aywmM78Fumh10Q7BakMM/g== X-Received: by 2002:a7b:c099:: with SMTP id r25mr1010553wmh.159.1594669711436; Mon, 13 Jul 2020 12:48:31 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc10296.dynamic.kabel-deutschland.de. [188.193.2.150]) by smtp.gmail.com with ESMTPSA id h5sm27532278wrc.97.2020.07.13.12.48.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2020 12:48:30 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Jul 2020 21:42:10 +0200 Message-Id: <20200713194211.30244-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200713194211.30244-1-andreas.rheinhardt@gmail.com> References: <20200713194211.30244-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/4] avformat/au: Simplify writing string into 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" by using av_bprintf() instead of several av_bprint_append(). Signed-off-by: Andreas Rheinhardt --- libavformat/au.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libavformat/au.c b/libavformat/au.c index c4a32ff76c..a8906a9db7 100644 --- a/libavformat/au.c +++ b/libavformat/au.c @@ -262,13 +262,11 @@ static int au_get_annotations(AVFormatContext *s, char **buffer) if (t != NULL) { if (cnt++) av_bprint_chars(&bprint, '\n', 1); - av_bprint_append_data(&bprint, keys[i], strlen(keys[i])); - av_bprint_chars(&bprint, '=', 1); - av_bprint_append_data(&bprint, t->value, strlen(t->value)); + av_bprintf(&bprint, "%s=%s", keys[i], t->value); } } /* pad with 0's */ - av_bprint_append_data(&bprint, "\0\0\0\0\0\0\0\0", 8); + av_bprint_chars(&bprint, '\0', 8); return av_bprint_finalize(&bprint, buffer); } From patchwork Mon Jul 13 19:42:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 21000 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 0E0B2448AFC for ; Mon, 13 Jul 2020 22:48:42 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DDEA468A8E9; Mon, 13 Jul 2020 22:48:41 +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 9A99F68A615 for ; Mon, 13 Jul 2020 22:48:33 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id q5so18080554wru.6 for ; Mon, 13 Jul 2020 12:48:33 -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=qgF2/JcxRvGyf2Usd0N0wkdj0wI0GYZPjM2ljV4wrSU=; b=cMIlhzsg7ahXcXMJ54/G6qClN+6DpvmlgbRfT8CCdSBop4MbztBWN2QLY/TI0nObnx zfhtoylGfH+xwC45pIPejmski1OzTPhbFDGGRxBjtHr704XyaQiTI/r/JUGHQZVG6pQ/ V0+apT7f/DX0DR4O1LlQuF93BRcODhgKdYJWe5y+43YKgbfJdQmo/pcQHv54j0gUrLk1 BCUS9e2ndD5sEnUBTmFIJ1ndP+SXtj20FPkPyggIzmrxwRExFyFrqTe+EWeF81qent4Q q9+3E+4M028jIL7IdRJnlZFQHnS5GwTe0VRQFuuDflh84FAehD4oTJK1Vf5JBcGhkB3T y0mA== 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=qgF2/JcxRvGyf2Usd0N0wkdj0wI0GYZPjM2ljV4wrSU=; b=lEZKzFZzKBljuqnw3R+hdq8EISbuffwZUPAG82ndaiY4y7O0AcNyIuByUHdfYgkJDN ZfX5+uOkBu9d/FhGO/5WE6/tKmpPIODM4TnLia+MZd77VPHhBzOgB3ftgX6800PHTkQl GWG9IvNQDpI5pSLdRas7lrdyyhRBX9Pb1nzt0Y8gyALTb6+uolSBHbl1mwtW/QVMq2IF 4X2DJO0JPPRIzhS/UDFkK2RRxGRqktXyibXFvQTqf/jyi+bi3x2Ton8PY78O22kpxG3+ BCM04IuWUFVH/grsr9B3ucQj8q0B1zoO+R68EfOmUn3ArBRiRwzeKBiBpYECZSBIz91X qv4Q== X-Gm-Message-State: AOAM531gzUspacK322lK4Ah/rXAZb6uridUDWYAg3hh1VKNIve+nYZOh QqLp1+q5jm3eRa0yEokIVJ4nljI2 X-Google-Smtp-Source: ABdhPJwo4Wlkh/me73q/RgvtwU+c+Ewlct5lh8/iorOopxdmWTpLg1RtsvTGx08fcgbefnOVNtjjlQ== X-Received: by 2002:a05:6000:1cf:: with SMTP id t15mr1218358wrx.180.1594669712692; Mon, 13 Jul 2020 12:48:32 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc10296.dynamic.kabel-deutschland.de. [188.193.2.150]) by smtp.gmail.com with ESMTPSA id h5sm27532278wrc.97.2020.07.13.12.48.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2020 12:48:32 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Jul 2020 21:42:11 +0200 Message-Id: <20200713194211.30244-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200713194211.30244-1-andreas.rheinhardt@gmail.com> References: <20200713194211.30244-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4] avformat/au: Avoid allocation for metadata string 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" When there are potentially annotation (i.e. metadata) fields to write, au_get_annotations() is called to produce a string with them. To do so, it uses an AVBPrint which is finalized to create the string. This is wasteful, because it always leads to an allocation even if the string actually fits into the internal buffer of the AVBPrint. This commit changes this by making au_get_annotations() modify an AVBPrint that resides on the stack of the caller (i.e. of au_write_header()). Furthermore, the AVBPrint is now checked for truncation; limiting the allocations implicit in the AVBPrint allowed to offload the overflow checks. Notice that these were not correct before: The size parameter of avio_write() is an int, yet the string in the AVBPrint was allowed to grow bigger than INT_MAX. And if the length of the string was so near UINT_MAX that the length + 32 overflowed, the old code would write the first eight bytes of the string and nothing more, leading to an invalid file. Finally, the special case in which the metadata dictionary of the AVFormatContext is empty (in which case one still has to write eight binary zeroes) is now no longer treated specially, because this case no longer incurs any allocation. Signed-off-by: Andreas Rheinhardt --- libavformat/au.c | 50 ++++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/libavformat/au.c b/libavformat/au.c index a8906a9db7..c09f4da4c9 100644 --- a/libavformat/au.c +++ b/libavformat/au.c @@ -35,8 +35,6 @@ /* if we don't know the size in advance */ #define AU_UNKNOWN_SIZE ((uint32_t)(~0)) -/* the specification requires an annotation field of at least eight bytes */ -#define AU_DEFAULT_HEADER_SIZE (24+8) static const AVCodecTag codec_au_tags[] = { { AV_CODEC_ID_PCM_MULAW, 1 }, @@ -241,7 +239,7 @@ typedef struct AUContext { #include "rawenc.h" -static int au_get_annotations(AVFormatContext *s, char **buffer) +static int au_get_annotations(AVFormatContext *s, AVBPrint *annotations) { static const char keys[][7] = { "Title", @@ -253,21 +251,19 @@ static int au_get_annotations(AVFormatContext *s, char **buffer) int cnt = 0; AVDictionary *m = s->metadata; AVDictionaryEntry *t = NULL; - AVBPrint bprint; - - av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); for (int i = 0; i < FF_ARRAY_ELEMS(keys); i++) { t = av_dict_get(m, keys[i], NULL, 0); if (t != NULL) { if (cnt++) - av_bprint_chars(&bprint, '\n', 1); - av_bprintf(&bprint, "%s=%s", keys[i], t->value); + av_bprint_chars(annotations, '\n', 1); + av_bprintf(annotations, "%s=%s", keys[i], t->value); } } - /* pad with 0's */ - av_bprint_chars(&bprint, '\0', 8); - return av_bprint_finalize(&bprint, buffer); + /* The specification requires the annotation field to be zero-terminated + * and its length to be a multiple of eight, so pad with 0's */ + av_bprint_chars(annotations, '\0', 8); + return av_bprint_is_complete(annotations) ? 0 : AVERROR(ENOMEM); } static int au_write_header(AVFormatContext *s) @@ -276,9 +272,7 @@ static int au_write_header(AVFormatContext *s) AUContext *au = s->priv_data; AVIOContext *pb = s->pb; AVCodecParameters *par = s->streams[0]->codecpar; - char *annotations = NULL; - - au->header_size = AU_DEFAULT_HEADER_SIZE; + AVBPrint annotations; if (s->nb_streams != 1) { av_log(s, AV_LOG_ERROR, "only one stream is supported\n"); @@ -291,30 +285,24 @@ static int au_write_header(AVFormatContext *s) return AVERROR(EINVAL); } - if (av_dict_count(s->metadata) > 0) { - ret = au_get_annotations(s, &annotations); - if (ret < 0) - return ret; - if (annotations != NULL) { - au->header_size = (24 + strlen(annotations) + 8) & ~7; - if (au->header_size < AU_DEFAULT_HEADER_SIZE) - au->header_size = AU_DEFAULT_HEADER_SIZE; - } - } + av_bprint_init(&annotations, 0, INT_MAX - 24); + ret = au_get_annotations(s, &annotations); + if (ret < 0) + goto fail; + au->header_size = 24 + annotations.len & ~7; + ffio_wfourcc(pb, ".snd"); /* magic number */ avio_wb32(pb, au->header_size); /* header size */ avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */ avio_wb32(pb, par->codec_tag); /* codec ID */ avio_wb32(pb, par->sample_rate); avio_wb32(pb, par->channels); - if (annotations != NULL) { - avio_write(pb, annotations, au->header_size - 24); - av_freep(&annotations); - } else { - avio_wb64(pb, 0); /* annotation field */ - } + avio_write(pb, annotations.str, annotations.len & ~7); - return 0; +fail: + av_bprint_finalize(&annotations, NULL); + + return ret; } static int au_write_trailer(AVFormatContext *s)