From patchwork Sat Jan 4 04:13:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 17189 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 D91B8448358 for ; Sat, 4 Jan 2020 06:14:20 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B0D4368ACEF; Sat, 4 Jan 2020 06:14:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpbgeu1.qq.com (smtpbgeu1.qq.com [52.59.177.22]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 915EE688101 for ; Sat, 4 Jan 2020 06:14:14 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1578111228; bh=/SbOgU5FcKp9UnMoqSBMLg62nhLRPNgC+hs31z5vLmU=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=rVnRMMzzJHSamwfsNC9uhatyGUZYy5vNSp59CsBublU+rB4d1mSf6mHBnEwbFr28y GAkCnEj/Rvb/1ETdiR22oV4PiNDo02msDlWFl529456ptgPUN6iKKdK3maXd/amQhU qeINWTtAXaCtGKyKU7vYRWn6QR0cUNDX9aUAuHdE= X-QQ-mid: esmtp7t1578111227tnjd2fxef Received: from ZHILIZHAO-MB1.tencent.com (unknown [14.17.22.54]) by esmtp4.qq.com (ESMTP) with id ; Sat, 04 Jan 2020 12:13:46 +0800 (CST) X-QQ-SSF: 01000000000000N0WH4000000000002 X-QQ-FEAT: K7kRbNvOnUAOz37gdKuJ2Af6tz5MUvHWJNH6Zks2gDefUvKKZiXZIgaTPUPe0 mYC57bkxTNFvkUmg5jk0RRDD3AY1XN7EVxz0MVb1zXwtKimLOXTHMd3btZdLZtErZqQ2Dr8 BiUMf+f7HuobVBIFdVvFvyHdEvjyeY0RaBBPRiXZl7KrWT8f2tLUUxcWFcwinivJg7xms2N tQ1vmguu8h22SkVyR4g0Wr1H9fqnDnX8BCSL4v870Xk4U1o8mBb1NjuNABsyBjjY4+rVaJs lrctvGa9XbRdNlaIO+Ira40rFUnSp6X9WV7Q== X-QQ-GoodBg: 0 From: quinkblack@foxmail.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 4 Jan 2020 12:13:41 +0800 Message-Id: <20200104041341.22651-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.22.0 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: esmtp:foxmail.com:bgforeign:bgforeign11 X-QQ-Bgrelay: 1 Subject: [FFmpeg-devel] [PATCH v3] avfilter/formats: optimize ff_all_formats 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Zhao Zhili This is a micro-optimization. Saving almost 200 reallocations makes it worth a try. --- v3: fix an obvious infinite loop and pass fate-filter test libavfilter/formats.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/libavfilter/formats.c b/libavfilter/formats.c index 33c64668a0..f29cddc250 100644 --- a/libavfilter/formats.c +++ b/libavfilter/formats.c @@ -348,21 +348,48 @@ int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout) AVFilterFormats *ff_all_formats(enum AVMediaType type) { - AVFilterFormats *ret = NULL; + int count, i; + AVFilterFormats *ret = av_mallocz(sizeof(*ret)); + + if (!ret) + return NULL; if (type == AVMEDIA_TYPE_VIDEO) { const AVPixFmtDescriptor *desc = NULL; - while ((desc = av_pix_fmt_desc_next(desc))) { - if (ff_add_format(&ret, av_pix_fmt_desc_get_id(desc)) < 0) - return NULL; + + count = 0; + while ((desc = av_pix_fmt_desc_next(desc))) + count++; + + ret->formats = av_malloc_array(count, sizeof(*ret->formats)); + if (!ret->formats) { + av_free(ret); + return NULL; + } + ret->nb_formats = count; + + for (i = 0, desc = NULL; i < count; i++) { + desc = av_pix_fmt_desc_next(desc); + ret->formats[i] = av_pix_fmt_desc_get_id(desc); } } else if (type == AVMEDIA_TYPE_AUDIO) { enum AVSampleFormat fmt = 0; - while (av_get_sample_fmt_name(fmt)) { - if (ff_add_format(&ret, fmt) < 0) - return NULL; + + while (av_get_sample_fmt_name(fmt)) fmt++; + + count = fmt; + ret->formats = av_malloc_array(count, sizeof(*ret->formats)); + if (!ret->formats) { + av_free(ret); + return NULL; } + ret->nb_formats = count; + + for (fmt = 0; fmt < count; fmt++) + ret->formats[fmt] = fmt; + } else { + av_freep(&ret); } return ret;