From patchwork Thu Dec 26 10:53:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16971 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 236B8449EFF for ; Thu, 26 Dec 2019 12:54:23 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0D5DB68ABF7; Thu, 26 Dec 2019 12:54:23 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 99E7768ABF3 for ; Thu, 26 Dec 2019 12:54:19 +0200 (EET) Received: by mail-wr1-f68.google.com with SMTP id c9so23362768wrw.8 for ; Thu, 26 Dec 2019 02:54:19 -0800 (PST) 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=1zKhpQpXwqlMoJxYRxVgLSnyf0x2jou7Ez0e6v3eRQw=; b=H10HDAwIjd5NuCPnxNMva9QExl6O+xUayKhH3QnSvOF2z6t+MNl691SCcKKG2dvH/w +8XbBnvH4ugbNa5HaaNWJcwcIhMNRNPjfxooKIiOGy0s3npRfODQRFnjeUQEb2QJCFBm xZiBaISnKvORy10FBVGCF7dcU/ysxtPhHNrFOlQrPx4JmjK1IZ9g9WQ1JXSdk/NyeSg3 SOgE+DuocaPbOsNh8WfjSFvEjWvr5GltlXNpudVg3v/3Entg0ZANx86Ls/onI+D/FkHd ITE5uhP9Hy2awc8SDdecDhPp/ckAp6qCC9vzWDnXk5zyyv+d0fkgIXMFmmjp+985zGp8 besA== 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=1zKhpQpXwqlMoJxYRxVgLSnyf0x2jou7Ez0e6v3eRQw=; b=G4VkiKVsIMCLHkevsaooKhc/dLfIHHc1n49EGHBsy+3PMdSvgJ+6gSNP/Gm28NbQx6 K1NwDQjVV/FuWsgpxWxIY6+P2ImG9HB/FIPn1MG26v8/OjhkvG/TeZ6RFydsUoYBFS5m XK6xjYJZpd+X/CiEogUAAzHt5gRwVw08tiqXJnbQ68AvEhY3iquCg9+NziuLS7JsiRBd pN6Knw846z5CDBEhNZuNAxAWzQk48nEY73H9ttKhNEerejI2eYuVn+EHM1SbD904NWqb 40XPT2Men8VHippTm3GC84GVxaIGGg9eh8mABkJanx8S+MWFw2HTZ+sEHDFYCOsttRJ6 b1ag== X-Gm-Message-State: APjAAAXp6bGgo11soXlbd5rxWQvaRbjkrgTHs75jPt2qxhBysnNVkfzY E/adeRJWSPDVdTuvKJNjNa5jxjCf X-Google-Smtp-Source: APXvYqxt6UISBtb7E0c9eO9mrnpQqcRCA6OsvFThQreBsKT+WxUcEReNKyfyb5fxmrobIAh9GX7MFA== X-Received: by 2002:a5d:6ac3:: with SMTP id u3mr46097886wrw.25.1577357658538; Thu, 26 Dec 2019 02:54:18 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc10203.dynamic.kabel-deutschland.de. [188.193.2.3]) by smtp.gmail.com with ESMTPSA id c68sm7997618wme.13.2019.12.26.02.54.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Dec 2019 02:54:17 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 26 Dec 2019 11:53:37 +0100 Message-Id: <20191226105342.11175-12-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191226105342.11175-1-andreas.rheinhardt@gmail.com> References: <20191226105342.11175-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 12/17] avformat/flvenc: Use array instead of linked list for index 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" Using a linked list had very much overhead (the pointer to the next entry increased the size of the index entry struct from 16 to 24 bytes, not to mention the overhead of having separate allocations), so it is better to (re)allocate a continuous array for the index. av_fast_realloc_array() is used for this purpose, in order not to reallocate the array for each entry. Signed-off-by: Andreas Rheinhardt --- Now no longer increasing the size of the array by a factor of two. libavformat/flvenc.c | 58 +++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 39 deletions(-) diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c index 1aaf0333ca..106be3333b 100644 --- a/libavformat/flvenc.c +++ b/libavformat/flvenc.c @@ -74,7 +74,6 @@ typedef enum { typedef struct FLVFileposition { int64_t keyframe_position; double keyframe_timestamp; - struct FLVFileposition *next; } FLVFileposition; typedef struct FLVContext { @@ -108,9 +107,9 @@ typedef struct FLVContext { int acurframeindex; int64_t keyframes_info_offset; - int64_t filepositions_count; FLVFileposition *filepositions; - FLVFileposition *head_filepositions; + unsigned filepositions_count; + unsigned filepositions_allocated; AVCodecParameters *audio_par; AVCodecParameters *video_par; @@ -549,27 +548,19 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i static int flv_append_keyframe_info(AVFormatContext *s, FLVContext *flv, double ts, int64_t pos) { - FLVFileposition *position = av_malloc(sizeof(FLVFileposition)); - - if (!position) { - av_log(s, AV_LOG_WARNING, "no mem for add keyframe index!\n"); + FLVFileposition *filepos; + + filepos = av_fast_realloc_array(flv->filepositions, + &flv->filepositions_allocated, + flv->filepositions_count + 1, + sizeof(*flv->filepositions)); + if (!filepos) { + av_log(s, AV_LOG_WARNING, "No memory for keyframe index\n"); return AVERROR(ENOMEM); } + flv->filepositions = filepos; - position->keyframe_timestamp = ts; - position->keyframe_position = pos; - - if (!flv->filepositions_count) { - flv->filepositions = position; - flv->head_filepositions = flv->filepositions; - position->next = NULL; - } else { - flv->filepositions->next = position; - position->next = NULL; - flv->filepositions = flv->filepositions->next; - } - - flv->filepositions_count++; + filepos[flv->filepositions_count++] = (FLVFileposition){ pos, ts }; return 0; } @@ -586,7 +577,7 @@ static int shift_data(AVFormatContext *s) int read_size[2]; AVIOContext *read_pb; - metadata_size = flv->filepositions_count * 9 * 2 + 10; /* filepositions and times value */ + metadata_size = flv->filepositions_count * 9LL * 2 + 10; /* filepositions and times value */ metadata_size += 2 + 13; /* filepositions String */ metadata_size += 2 + 5; /* times String */ metadata_size += 3; /* Object end */ @@ -784,7 +775,7 @@ static int flv_write_trailer(AVFormatContext *s) int64_t cur_pos = avio_tell(s->pb); if (build_keyframes_idx) { - FLVFileposition *newflv_posinfo, *p; + FLVFileposition *flv_posinfo = flv->filepositions; avio_seek(pb, flv->videosize_offset, SEEK_SET); put_amf_double(pb, flv->videosize); @@ -809,28 +800,17 @@ static int flv_write_trailer(AVFormatContext *s) avio_seek(pb, flv->keyframes_info_offset, SEEK_SET); put_amf_string(pb, "filepositions"); put_amf_dword_array(pb, flv->filepositions_count); - for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) { - put_amf_double(pb, newflv_posinfo->keyframe_position + flv->keyframe_index_size); + for (unsigned i = 0; i < flv->filepositions_count; i++) { + put_amf_double(pb, flv_posinfo[i].keyframe_position + flv->keyframe_index_size); } put_amf_string(pb, "times"); put_amf_dword_array(pb, flv->filepositions_count); - for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) { - put_amf_double(pb, newflv_posinfo->keyframe_timestamp); + for (unsigned i = 0; i < flv->filepositions_count; i++) { + put_amf_double(pb, flv_posinfo[i].keyframe_timestamp); } - newflv_posinfo = flv->head_filepositions; - while (newflv_posinfo) { - p = newflv_posinfo->next; - if (p) { - newflv_posinfo->next = p->next; - av_free(p); - p = NULL; - } else { - av_free(newflv_posinfo); - newflv_posinfo = NULL; - } - } + av_freep(&flv->filepositions); put_amf_string(pb, ""); avio_w8(pb, AMF_END_OF_OBJECT);